-/*******************************************************************************\r
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.property;\r
-\r
-import org.eclipse.core.expressions.PropertyTester;\r
-import org.simantics.DatabaseJob;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.utils.RequestUtil;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-\r
-/**\r
- * A JFace property tester extension for the Eclipse command framework that is\r
- * meant for working with Simantics database {@link Resource} instances.\r
- * \r
- * <p>\r
- * This tester expects to receive IStructuredSelections that contain Resource\r
- * instances.\r
- * \r
- * <p>\r
- * It supports testing of the following properties:\r
- * <ul>\r
- * <li>partOf - </li>\r
- * </ul>\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class ComponentTypePropertyTester extends PropertyTester {\r
-\r
- /**\r
- * Tests to see if the input Resource is part of a structural component\r
- * type.\r
- */\r
- private static final String PART_OF = "partOf";\r
-\r
- @Override\r
- public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
- final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);\r
- if (resource == null)\r
- return false;\r
-\r
- Session session = SimanticsUI.peekSession();\r
- if (session == null)\r
- return false;\r
-\r
- if (DatabaseJob.inProgress())\r
- return false;\r
-\r
- try {\r
- return RequestUtil.trySyncRequest(\r
- session,\r
- SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
- SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
- false,\r
- new UniqueRead<Boolean>() {\r
- @Override\r
- public Boolean perform(ReadGraph g) throws DatabaseException {\r
- return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));\r
- }\r
- });\r
- } catch (DatabaseException | InterruptedException e) {\r
- // Purposefully not logging these exceptions, there might be way too\r
- // many even under normal circumstances.\r
- // TODO: add debug tracing options controlling the printing of these exceptions\r
- return false;\r
- }\r
- }\r
-\r
- private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {\r
- if (PART_OF.equals(property)) {\r
- boolean expected = parseBoolean(expectedValue, true);\r
- boolean is = partOfComponentType(graph, resource) != null;\r
- return is == expected;\r
- }\r
- return false;\r
- }\r
-\r
- private Resource partOfComponentType(ReadGraph graph, Resource resource) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
- Resource componentType = null;\r
- for (Resource curComponent = resource; true;) {\r
- componentType = graph.getPossibleObject(curComponent, STR.Defines);\r
- if (componentType != null) {\r
- return componentType;\r
- }\r
- curComponent = graph.getPossibleObject(curComponent, L0.PartOf);\r
- if (curComponent == null)\r
- break; \r
- }\r
- return null;\r
- }\r
-\r
- boolean parseBoolean(Object value, boolean defaultValue) {\r
- if (value instanceof Boolean)\r
- return (Boolean) value;\r
- if (value instanceof String)\r
- return Boolean.parseBoolean((String) value);\r
- return defaultValue;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.property;
+
+import org.eclipse.core.expressions.PropertyTester;
+import org.simantics.DatabaseJob;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.utils.RequestUtil;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.layer0.Layer0;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+
+/**
+ * A JFace property tester extension for the Eclipse command framework that is
+ * meant for working with Simantics database {@link Resource} instances.
+ *
+ * <p>
+ * This tester expects to receive IStructuredSelections that contain Resource
+ * instances.
+ *
+ * <p>
+ * It supports testing of the following properties:
+ * <ul>
+ * <li>partOf - </li>
+ * </ul>
+ *
+ * @author Tuukka Lehtonen
+ */
+public class ComponentTypePropertyTester extends PropertyTester {
+
+ /**
+ * Tests to see if the input Resource is part of a structural component
+ * type.
+ */
+ private static final String PART_OF = "partOf";
+
+ @Override
+ public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
+ final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
+ if (resource == null)
+ return false;
+
+ Session session = SimanticsUI.peekSession();
+ if (session == null)
+ return false;
+
+ if (DatabaseJob.inProgress())
+ return false;
+
+ try {
+ return RequestUtil.trySyncRequest(
+ session,
+ SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
+ SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
+ false,
+ new UniqueRead<Boolean>() {
+ @Override
+ public Boolean perform(ReadGraph g) throws DatabaseException {
+ return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
+ }
+ });
+ } catch (DatabaseException | InterruptedException e) {
+ // Purposefully not logging these exceptions, there might be way too
+ // many even under normal circumstances.
+ // TODO: add debug tracing options controlling the printing of these exceptions
+ return false;
+ }
+ }
+
+ private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
+ if (PART_OF.equals(property)) {
+ boolean expected = parseBoolean(expectedValue, true);
+ boolean is = partOfComponentType(graph, resource) != null;
+ return is == expected;
+ }
+ return false;
+ }
+
+ private Resource partOfComponentType(ReadGraph graph, Resource resource) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ Resource componentType = null;
+ for (Resource curComponent = resource; true;) {
+ componentType = graph.getPossibleObject(curComponent, STR.Defines);
+ if (componentType != null) {
+ return componentType;
+ }
+ curComponent = graph.getPossibleObject(curComponent, L0.PartOf);
+ if (curComponent == null)
+ break;
+ }
+ return null;
+ }
+
+ boolean parseBoolean(Object value, boolean defaultValue) {
+ if (value instanceof Boolean)
+ return (Boolean) value;
+ if (value instanceof String)
+ return Boolean.parseBoolean((String) value);
+ return defaultValue;
+ }
+
+}