1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.modeling.ui.actions;
14 import java.util.ArrayDeque;
15 import java.util.Deque;
16 import java.util.HashSet;
17 import java.util.List;
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;
38 public class ValidateMappingHandler extends AbstractHandler {
41 public Object execute(ExecutionEvent event) throws ExecutionException {
42 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
44 if (!selection.isEmpty() && selection instanceof StructuredSelection) {
45 final Set<Resource> compositesToTest = new HashSet<Resource>();
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);
52 compositesToTest.add(node);
57 Simantics.getSession().asyncRequest(new ReadRequest() {
59 public void run(ReadGraph graph) throws DatabaseException {
60 Layer0 L0 = Layer0.getInstance(graph);
61 StructuralResource2 STR = StructuralResource2.getInstance(graph);
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();
68 for (Resource child : graph.syncRequest(new ObjectsWithType(check, L0.ConsistsOf, STR.Composite))) {
69 if (compositesToTest.add(child))
70 toBeChecked.offer(child);
74 for (Resource composite : compositesToTest)
75 graph.syncRequest(new ValidateMapping(composite));