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.Simantics;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.request.UniqueRead;
21 import org.simantics.db.common.utils.RequestUtil;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.structural.stubs.StructuralResource2;
25 import org.simantics.ui.SimanticsUI;
26 import org.simantics.ui.utils.ResourceAdaptionUtils;
29 * A JFace property tester extension for the Eclipse command framework that is
30 * meant for working with Simantics database {@link Resource} instances.
33 * This tester expects to receive IStructuredSelections that contain Resource
37 * It supports testing of the following properties:
42 * @author Tuukka Lehtonen
44 public class ComponentTypePropertyTester extends PropertyTester {
47 * Tests to see if the input Resource is part of a structural component
50 private static final String PART_OF = "partOf";
53 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
54 final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
58 Session session = Simantics.peekSession();
62 if (DatabaseJob.inProgress())
66 return RequestUtil.trySyncRequest(
68 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
69 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
71 new UniqueRead<Boolean>() {
73 public Boolean perform(ReadGraph g) throws DatabaseException {
74 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
77 } catch (DatabaseException | InterruptedException e) {
78 // Purposefully not logging these exceptions, there might be way too
79 // many even under normal circumstances.
80 // TODO: add debug tracing options controlling the printing of these exceptions
85 private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
86 if (PART_OF.equals(property)) {
87 boolean expected = parseBoolean(expectedValue, true);
88 boolean is = partOfComponentType(graph, resource) != null;
89 return is == expected;
94 private Resource partOfComponentType(ReadGraph graph, Resource resource) throws DatabaseException {
95 Layer0 L0 = Layer0.getInstance(graph);
96 StructuralResource2 STR = StructuralResource2.getInstance(graph);
97 Resource componentType = null;
98 for (Resource curComponent = resource; true;) {
99 componentType = graph.getPossibleObject(curComponent, STR.Defines);
100 if (componentType != null) {
101 return componentType;
103 curComponent = graph.getPossibleObject(curComponent, L0.PartOf);
104 if (curComponent == null)
110 boolean parseBoolean(Object value, boolean defaultValue) {
111 if (value instanceof Boolean)
112 return (Boolean) value;
113 if (value instanceof String)
114 return Boolean.parseBoolean((String) value);