]> gerrit.simantics Code Review - simantics/platform.git/blob
9b2040c4b01bf854f753a0f8811414d99d33b936
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.issues.ui.handler;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.TreeSet;
20
21 import org.eclipse.core.commands.AbstractHandler;
22 import org.eclipse.core.commands.ExecutionEvent;
23 import org.eclipse.core.commands.ExecutionException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorReference;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.PartInitException;
31 import org.eclipse.ui.PlatformUI;
32 import org.simantics.Simantics;
33 import org.simantics.browsing.ui.common.ErrorLogger;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.Session;
37 import org.simantics.db.common.request.Queries;
38 import org.simantics.db.common.request.ReadRequest;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.request.ActiveModels;
41 import org.simantics.db.layer0.request.PossibleModel;
42 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
43 import org.simantics.diagram.stubs.DiagramResource;
44 import org.simantics.issues.common.BatchIssueSource;
45 import org.simantics.issues.common.BatchIssueValidationContext;
46 import org.simantics.issues.common.SelectedModelBatchIssueSources;
47 import org.simantics.modeling.ModelingResources;
48 import org.simantics.ui.workbench.IResourceEditorInput;
49 import org.simantics.utils.ui.ExceptionUtils;
50
51 /**
52  * @author Tuukka Lehtonen
53  */
54 public class UpdateIssuesForOpenDiagrams extends AbstractHandler {
55
56     @Override
57     public Object execute(ExecutionEvent event) throws ExecutionException {
58         Runnable postValidation = null;
59         try {
60             update(postValidation);
61         } catch (DatabaseException e) {
62             ErrorLogger.defaultLogError(e);
63         } catch (PartInitException e) {
64             ErrorLogger.defaultLogError(e);
65         }
66         return null;
67     }
68
69     public static void update(Runnable postValidation) throws DatabaseException, PartInitException {
70         final List<IResourceEditorInput> inputs = new ArrayList<IResourceEditorInput>();
71         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
72         for (IWorkbenchWindow wb : windows) {
73             IWorkbenchPage pages[] = wb.getPages();
74             for (IWorkbenchPage page : pages) {
75                 for (IEditorReference ref : page.getEditorReferences()) {
76                     IEditorInput input = ref.getEditorInput();
77                     if (input instanceof IResourceEditorInput) {
78                         IResourceEditorInput in = (IResourceEditorInput) input;
79                         inputs.add(in);
80                     }
81                 }
82             }
83         }
84
85         final Set<Resource> compositesToValidate = new TreeSet<Resource>();
86         final Collection<BatchIssueSource> validations = new ArrayList<BatchIssueSource>();
87         final Collection<Resource> activeModels = new ArrayList<Resource>();
88         final BatchIssueValidationContext context = new BatchIssueValidationContext();
89
90         try {
91             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
92                 @Override
93                 public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
94                     try {
95                         Resource project = Simantics.getProjectResource();
96                         Session session = Simantics.getSession();
97
98                         activeModels.addAll(session.syncRequest(new ActiveModels(project)));
99                         if(activeModels.size() != 1) return;
100
101                         Collection<Resource> activeSources = session.syncRequest(new SelectedModelBatchIssueSources(activeModels.iterator().next()));
102                         for(Resource source : activeSources) {
103                             BatchIssueSource bis = session.syncRequest(Queries.adapt(source, BatchIssueSource.class));
104                             validations.add(bis);
105                         }
106
107                         session.syncRequest(new ReadRequest() {
108                             @Override
109                             public void run(ReadGraph graph) throws DatabaseException {
110                                 DiagramResource DIA = DiagramResource.getInstance(graph);
111                                 ModelingResources MOD = ModelingResources.getInstance(graph);
112                                 for (IResourceEditorInput input : inputs) {
113                                     Resource in = input.getResource();
114                                     if (graph.isInstanceOf(in, DIA.Diagram)) {
115                                         Resource composite = graph.getPossibleObject(in, MOD.DiagramToComposite);
116                                         if (composite != null) {
117                                             Resource model = graph.sync(new PossibleModel(composite));
118                                             if (activeModels.contains(model)) {
119                                                 compositesToValidate.add(composite);
120                                                 //System.out.println("TODO: re-validate composite " + NameUtils.getSafeLabel(graph, composite));
121                                             }
122                                         }
123                                     }
124                                 }
125                                 
126                                 context.contexts = compositesToValidate;
127                                 if(!compositesToValidate.isEmpty()) {
128                                         ModelTransferableGraphSourceRequest.getDomainOnly(graph, monitor, compositesToValidate);
129                                 }
130                                 
131                             }
132                         });
133                     } catch (DatabaseException e) {
134                         throw new InvocationTargetException(e);
135                     } finally {
136                         monitor.done();
137                     }
138                 }
139             });
140         } catch (InvocationTargetException e) {
141             ExceptionUtils.logAndShowError(e.getTargetException());
142             return;
143         } catch (InterruptedException e) {
144             // Operation cancelled, ignore.
145             return;
146         }
147
148         if (!context.contexts.isEmpty() && activeModels.size() == 1) {
149             RunActiveValidations.run(postValidation, validations, context);
150         }
151         
152     }
153
154 }