--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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.diagram.flag;\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.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+/**\r
+ * An Eclipse property tester for flag elements.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class FlagPropertyTester extends PropertyTester {\r
+\r
+ /**\r
+ * Tests if the received flag resource is connected to a counterpart.\r
+ * Default expected value is true.\r
+ */\r
+ private static final String CONNECTED = "connected";\r
+\r
+ /**\r
+ * Tests if the received flag resource is connected to a counterpart.\r
+ * Default expected value is true.\r
+ */\r
+ private static final String LOCALLY_CONNECTED = "locallyConnected";\r
+\r
+ /**\r
+ * Tests if the received flag resource is marked as an external flag.\r
+ */\r
+ private static final String EXTERNAL = "external";\r
+\r
+ /**\r
+ * Tests if the received flag resource is a merged flag with more than 1\r
+ * counterpart.\r
+ */\r
+ private static final String MERGED = "merged";\r
+\r
+ @Override\r
+ public boolean test(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 (EXTERNAL.equals(property)) {\r
+ boolean expected = parseBoolean(expectedValue, true);\r
+ boolean ext = FlagUtil.isExternal(graph, resource);\r
+ return ext ? expected : expected;\r
+ } else if (CONNECTED.equals(property)) {\r
+ boolean expected = parseBoolean(expectedValue, true);\r
+ boolean isConnected = \r
+ FlagUtil.countCounterparts(graph, resource) > 0 ||\r
+ FlagUtil.isLifted(graph, resource);\r
+ return isConnected == expected;\r
+ } else if (LOCALLY_CONNECTED.equals(property)) {\r
+ boolean expected = parseBoolean(expectedValue, true);\r
+ boolean isLocallyConnected = \r
+ FlagUtil.isJoinedInSingleDiagram(graph, resource);\r
+ return isLocallyConnected == expected;\r
+ } else if (MERGED.equals(property)) {\r
+ boolean expected = parseBoolean(expectedValue, true);\r
+ boolean isMerged = FlagUtil.countCounterparts(graph, resource) > 1;\r
+ return isMerged == expected;\r
+ }\r
+ return false;\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