]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ValidateMappingHandler.java
Improved usability of shared library export wizard
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / ValidateMappingHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.modeling.ui.actions;
13
14 import java.util.ArrayDeque;
15 import java.util.Deque;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.eclipse.core.commands.AbstractHandler;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.ui.PlatformUI;
27 import org.simantics.Simantics;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.Resource;
30 import org.simantics.db.common.request.ObjectsWithType;
31 import org.simantics.db.common.request.ReadRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.layer0.Layer0;
34 import org.simantics.modeling.validation.ValidateMapping;
35 import org.simantics.structural.stubs.StructuralResource2;
36 import org.simantics.structural.ui.modelBrowser.nodes.CompositeNode;
37
38 public class ValidateMappingHandler extends AbstractHandler {
39
40     @Override
41     public Object execute(ExecutionEvent event) throws ExecutionException {
42         ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
43
44         if (!selection.isEmpty() && selection instanceof StructuredSelection) {
45             final Set<Resource> compositesToTest = new HashSet<Resource>(); 
46
47             final List<?> elements = ((StructuredSelection) selection).toList();
48             for (Object element : elements) {
49                 if (element instanceof IAdaptable) {
50                     Resource node = (Resource) ((IAdaptable) element).getAdapter(Resource.class);
51                     if (node != null) {
52                         compositesToTest.add(node);
53                     }
54                 }
55             }
56
57             Simantics.getSession().asyncRequest(new ReadRequest() {
58                 @Override
59                 public void run(ReadGraph graph) throws DatabaseException {
60                     Layer0 L0 = Layer0.getInstance(graph); 
61                     StructuralResource2 STR = StructuralResource2.getInstance(graph);
62
63                     // Try to test all child composites also.
64                     Deque<Resource> toBeChecked = new ArrayDeque<Resource>(compositesToTest);
65                     while (!toBeChecked.isEmpty()) {
66                         Resource check = toBeChecked.poll();
67
68                         for (Resource child : graph.syncRequest(new ObjectsWithType(check, L0.ConsistsOf, STR.Composite))) {
69                             if (compositesToTest.add(child))
70                                 toBeChecked.offer(child);
71                         }
72                     }
73
74                     for (Resource composite : compositesToTest)
75                         graph.syncRequest(new ValidateMapping(composite));
76                 }
77             });
78         }
79         return null;
80     }
81
82 }