]> gerrit.simantics Code Review - simantics/platform.git/blob
0f7cec48de62500cab7e84868569aa040d0b053c
[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.ReadRequest;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.layer0.request.ActiveModels;
40 import org.simantics.db.layer0.request.PossibleModel;
41 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
42 import org.simantics.diagram.stubs.DiagramResource;
43 import org.simantics.issues.common.BatchIssueSource;
44 import org.simantics.issues.common.BatchIssueValidationContext;
45 import org.simantics.issues.common.SelectedModelBatchIssueSources;
46 import org.simantics.modeling.ModelingResources;
47 import org.simantics.ui.workbench.IResourceEditorInput;
48 import org.simantics.utils.ui.ExceptionUtils;
49
50 /**
51  * @author Tuukka Lehtonen
52  */
53 public class UpdateIssuesForOpenDiagrams extends AbstractHandler {
54
55     @Override
56     public Object execute(ExecutionEvent event) throws ExecutionException {
57         Runnable postValidation = null;
58         try {
59             update(postValidation);
60         } catch (DatabaseException e) {
61             ErrorLogger.defaultLogError(e);
62         } catch (PartInitException e) {
63             ErrorLogger.defaultLogError(e);
64         }
65         return null;
66     }
67
68     public static void update(Runnable postValidation) throws DatabaseException, PartInitException {
69         final List<IResourceEditorInput> inputs = new ArrayList<>();
70         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
71         for (IWorkbenchWindow wb : windows) {
72             IWorkbenchPage pages[] = wb.getPages();
73             for (IWorkbenchPage page : pages) {
74                 for (IEditorReference ref : page.getEditorReferences()) {
75                     IEditorInput input = ref.getEditorInput();
76                     if (input instanceof IResourceEditorInput) {
77                         IResourceEditorInput in = (IResourceEditorInput) input;
78                         inputs.add(in);
79                     }
80                 }
81             }
82         }
83
84         final Set<Resource> compositesToValidate = new TreeSet<>();
85         final Collection<BatchIssueSource> validations = new ArrayList<>();
86         final Collection<Resource> activeModels = new ArrayList<>();
87         final BatchIssueValidationContext context = new BatchIssueValidationContext();
88
89         try {
90             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
91                 @Override
92                 public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
93                     try {
94                         Resource project = Simantics.getProjectResource();
95                         Session session = Simantics.getSession();
96
97                         activeModels.addAll(session.syncRequest(new ActiveModels(project)));
98                         if(activeModels.size() != 1) return;
99
100                         RunActiveValidations.toBatchIssueSources(session,
101                                 session.syncRequest(new SelectedModelBatchIssueSources(activeModels.iterator().next())),
102                                 validations);
103
104                         session.syncRequest(new ReadRequest() {
105                             @Override
106                             public void run(ReadGraph graph) throws DatabaseException {
107                                 DiagramResource DIA = DiagramResource.getInstance(graph);
108                                 ModelingResources MOD = ModelingResources.getInstance(graph);
109                                 for (IResourceEditorInput input : inputs) {
110                                     Resource in = input.getResource();
111                                     if (graph.isInstanceOf(in, DIA.Diagram)) {
112                                         Resource composite = graph.getPossibleObject(in, MOD.DiagramToComposite);
113                                         if (composite != null) {
114                                             Resource model = graph.sync(new PossibleModel(composite));
115                                             if (activeModels.contains(model)) {
116                                                 compositesToValidate.add(composite);
117                                                 //System.out.println("TODO: re-validate composite " + NameUtils.getSafeLabel(graph, composite));
118                                             }
119                                         }
120                                     }
121                                 }
122                                 
123                                 context.contexts = compositesToValidate;
124                                 if(!compositesToValidate.isEmpty()) {
125                                         ModelTransferableGraphSourceRequest.getDomainOnly(graph, monitor, compositesToValidate);
126                                 }
127                                 
128                             }
129                         });
130                     } catch (DatabaseException e) {
131                         throw new InvocationTargetException(e);
132                     } finally {
133                         monitor.done();
134                     }
135                 }
136             });
137         } catch (InvocationTargetException e) {
138             ExceptionUtils.logAndShowError(e.getTargetException());
139             return;
140         } catch (InterruptedException e) {
141             // Operation cancelled, ignore.
142             return;
143         }
144
145         if (!context.contexts.isEmpty() && activeModels.size() == 1) {
146             RunActiveValidations.run(postValidation, validations, context);
147         }
148         
149     }
150
151 }