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.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;
52 * @author Tuukka Lehtonen
54 public class UpdateIssuesForOpenDiagrams extends AbstractHandler {
57 public Object execute(ExecutionEvent event) throws ExecutionException {
58 Runnable postValidation = null;
60 update(postValidation);
61 } catch (DatabaseException e) {
62 ErrorLogger.defaultLogError(e);
63 } catch (PartInitException e) {
64 ErrorLogger.defaultLogError(e);
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;
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();
91 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
93 public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
95 Resource project = Simantics.getProjectResource();
96 Session session = Simantics.getSession();
98 activeModels.addAll(session.syncRequest(new ActiveModels(project)));
99 if(activeModels.size() != 1) return;
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);
107 session.syncRequest(new ReadRequest() {
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));
126 context.contexts = compositesToValidate;
127 if(!compositesToValidate.isEmpty()) {
128 ModelTransferableGraphSourceRequest.getDomainOnly(graph, monitor, compositesToValidate);
133 } catch (DatabaseException e) {
134 throw new InvocationTargetException(e);
140 } catch (InvocationTargetException e) {
141 ExceptionUtils.logAndShowError(e.getTargetException());
143 } catch (InterruptedException e) {
144 // Operation cancelled, ignore.
148 if (!context.contexts.isEmpty() && activeModels.size() == 1) {
149 RunActiveValidations.run(postValidation, validations, context);