1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 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.property;
14 import org.eclipse.core.expressions.PropertyTester;
15 import org.simantics.DatabaseJob;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.common.request.UniqueRead;
20 import org.simantics.db.common.utils.RequestUtil;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.structural.stubs.StructuralResource2;
24 import org.simantics.ui.SimanticsUI;
25 import org.simantics.ui.utils.ResourceAdaptionUtils;
28 * A JFace property tester extension for the Eclipse command framework that is
29 * meant for working with Simantics database {@link Resource} instances.
32 * This tester expects to receive IStructuredSelections that contain Resource
36 * It supports testing of the following properties:
41 * @author Tuukka Lehtonen
43 public class ComponentTypePropertyTester extends PropertyTester {
46 * Tests to see if the input Resource is part of a structural component
49 private static final String PART_OF = "partOf";
52 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
53 final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
57 Session session = SimanticsUI.peekSession();
61 if (DatabaseJob.inProgress())
65 return RequestUtil.trySyncRequest(
67 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
68 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
70 new UniqueRead<Boolean>() {
72 public Boolean perform(ReadGraph g) throws DatabaseException {
73 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
76 } catch (DatabaseException | InterruptedException e) {
77 // Purposefully not logging these exceptions, there might be way too
78 // many even under normal circumstances.
79 // TODO: add debug tracing options controlling the printing of these exceptions
84 private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
85 if (PART_OF.equals(property)) {
86 boolean expected = parseBoolean(expectedValue, true);
87 boolean is = partOfComponentType(graph, resource) != null;
88 return is == expected;
93 private Resource partOfComponentType(ReadGraph graph, Resource resource) throws DatabaseException {
94 Layer0 L0 = Layer0.getInstance(graph);
95 StructuralResource2 STR = StructuralResource2.getInstance(graph);
96 Resource componentType = null;
97 for (Resource curComponent = resource; true;) {
98 componentType = graph.getPossibleObject(curComponent, STR.Defines);
99 if (componentType != null) {
100 return componentType;
102 curComponent = graph.getPossibleObject(curComponent, L0.PartOf);
103 if (curComponent == null)
109 boolean parseBoolean(Object value, boolean defaultValue) {
110 if (value instanceof Boolean)
111 return (Boolean) value;
112 if (value instanceof String)
113 return Boolean.parseBoolean((String) value);