1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.property;
\r
14 import org.eclipse.core.expressions.PropertyTester;
\r
15 import org.simantics.DatabaseJob;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.Session;
\r
19 import org.simantics.db.common.request.UniqueRead;
\r
20 import org.simantics.db.common.utils.RequestUtil;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.layer0.Layer0;
\r
23 import org.simantics.structural.stubs.StructuralResource2;
\r
24 import org.simantics.ui.SimanticsUI;
\r
25 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
28 * A JFace property tester extension for the Eclipse command framework that is
\r
29 * meant for working with Simantics database {@link Resource} instances.
\r
32 * This tester expects to receive IStructuredSelections that contain Resource
\r
36 * It supports testing of the following properties:
\r
38 * <li>partOf - </li>
\r
41 * @author Tuukka Lehtonen
\r
43 public class ComponentTypePropertyTester extends PropertyTester {
\r
46 * Tests to see if the input Resource is part of a structural component
\r
49 private static final String PART_OF = "partOf";
\r
52 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
\r
53 final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
\r
54 if (resource == null)
\r
57 Session session = SimanticsUI.peekSession();
\r
58 if (session == null)
\r
61 if (DatabaseJob.inProgress())
\r
65 return RequestUtil.trySyncRequest(
\r
67 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
68 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
70 new UniqueRead<Boolean>() {
\r
72 public Boolean perform(ReadGraph g) throws DatabaseException {
\r
73 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
\r
76 } catch (DatabaseException | InterruptedException e) {
\r
77 // Purposefully not logging these exceptions, there might be way too
\r
78 // many even under normal circumstances.
\r
79 // TODO: add debug tracing options controlling the printing of these exceptions
\r
84 private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
\r
85 if (PART_OF.equals(property)) {
\r
86 boolean expected = parseBoolean(expectedValue, true);
\r
87 boolean is = partOfComponentType(graph, resource) != null;
\r
88 return is == expected;
\r
93 private Resource partOfComponentType(ReadGraph graph, Resource resource) throws DatabaseException {
\r
94 Layer0 L0 = Layer0.getInstance(graph);
\r
95 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
96 Resource componentType = null;
\r
97 for (Resource curComponent = resource; true;) {
\r
98 componentType = graph.getPossibleObject(curComponent, STR.Defines);
\r
99 if (componentType != null) {
\r
100 return componentType;
\r
102 curComponent = graph.getPossibleObject(curComponent, L0.PartOf);
\r
103 if (curComponent == null)
\r
109 boolean parseBoolean(Object value, boolean defaultValue) {
\r
110 if (value instanceof Boolean)
\r
111 return (Boolean) value;
\r
112 if (value instanceof String)
\r
113 return Boolean.parseBoolean((String) value);
\r
114 return defaultValue;
\r