1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.issues.ui.handler;
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
19 import java.util.TreeSet;
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;
51 * @author Tuukka Lehtonen
53 public class UpdateIssuesForOpenDiagrams extends AbstractHandler {
56 public Object execute(ExecutionEvent event) throws ExecutionException {
57 Runnable postValidation = null;
59 update(postValidation);
60 } catch (DatabaseException e) {
61 ErrorLogger.defaultLogError(e);
62 } catch (PartInitException e) {
63 ErrorLogger.defaultLogError(e);
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;
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();
90 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
92 public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
94 Resource project = Simantics.getProjectResource();
95 Session session = Simantics.getSession();
97 activeModels.addAll(session.syncRequest(new ActiveModels(project)));
98 if(activeModels.size() != 1) return;
100 RunActiveValidations.toBatchIssueSources(session,
101 session.syncRequest(new SelectedModelBatchIssueSources(activeModels.iterator().next())),
104 session.syncRequest(new ReadRequest() {
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));
123 context.contexts = compositesToValidate;
124 if(!compositesToValidate.isEmpty()) {
125 ModelTransferableGraphSourceRequest.getDomainOnly(graph, monitor, compositesToValidate);
130 } catch (DatabaseException e) {
131 throw new InvocationTargetException(e);
137 } catch (InvocationTargetException e) {
138 ExceptionUtils.logAndShowError(e.getTargetException());
140 } catch (InterruptedException e) {
141 // Operation cancelled, ignore.
145 if (!context.contexts.isEmpty() && activeModels.size() == 1) {
146 RunActiveValidations.run(postValidation, validations, context);