]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagPropertyTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / FlagPropertyTester.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.flag;\r
13 \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.ui.SimanticsUI;\r
23 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
24 \r
25 /**\r
26  * An Eclipse property tester for flag elements.\r
27  * \r
28  * @author Tuukka Lehtonen\r
29  */\r
30 public class FlagPropertyTester extends PropertyTester {\r
31 \r
32     /**\r
33      * Tests if the received flag resource is connected to a counterpart.\r
34      * Default expected value is true.\r
35      */\r
36     private static final String CONNECTED = "connected";\r
37 \r
38     /**\r
39      * Tests if the received flag resource is connected to a counterpart.\r
40      * Default expected value is true.\r
41      */\r
42     private static final String LOCALLY_CONNECTED = "locallyConnected";\r
43 \r
44     /**\r
45      * Tests if the received flag resource is marked as an external flag.\r
46      */\r
47     private static final String EXTERNAL = "external";\r
48 \r
49     /**\r
50      * Tests if the received flag resource is a merged flag with more than 1\r
51      * counterpart.\r
52      */\r
53     private static final String MERGED = "merged";\r
54 \r
55     @Override\r
56     public boolean test(Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
57         final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);\r
58         if (resource == null)\r
59             return false;\r
60 \r
61         Session session = SimanticsUI.peekSession();\r
62         if (session == null)\r
63             return false;\r
64 \r
65         if (DatabaseJob.inProgress())\r
66             return false;\r
67 \r
68         try {\r
69             return RequestUtil.trySyncRequest(\r
70                     session,\r
71                     SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
72                     SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
73                     false,\r
74                     new UniqueRead<Boolean>() {\r
75                 @Override\r
76                 public Boolean perform(ReadGraph g) throws DatabaseException {\r
77                     return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));\r
78                 }\r
79             });\r
80         } catch (DatabaseException | InterruptedException e) {\r
81             // Purposefully not logging these exceptions, there might be way too\r
82             // many even under normal circumstances.\r
83             // TODO: add debug tracing options controlling the printing of these exceptions\r
84             return false;\r
85         }\r
86     }\r
87 \r
88     private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {\r
89         if (EXTERNAL.equals(property)) {\r
90             boolean expected = parseBoolean(expectedValue, true);\r
91             boolean ext = FlagUtil.isExternal(graph, resource);\r
92             return ext ? expected : expected;\r
93         } else if (CONNECTED.equals(property)) {\r
94             boolean expected = parseBoolean(expectedValue, true);\r
95             boolean isConnected = \r
96                 FlagUtil.countCounterparts(graph, resource) > 0 ||\r
97                 FlagUtil.isLifted(graph, resource);\r
98             return isConnected == expected;\r
99         } else if (LOCALLY_CONNECTED.equals(property)) {\r
100             boolean expected = parseBoolean(expectedValue, true);\r
101             boolean isLocallyConnected = \r
102                 FlagUtil.isJoinedInSingleDiagram(graph, resource);\r
103             return isLocallyConnected == expected;\r
104         } else if (MERGED.equals(property)) {\r
105             boolean expected = parseBoolean(expectedValue, true);\r
106             boolean isMerged = FlagUtil.countCounterparts(graph, resource) > 1;\r
107             return isMerged == expected;\r
108         }\r
109         return false;\r
110     }\r
111 \r
112     boolean parseBoolean(Object value, boolean defaultValue) {\r
113         if (value instanceof Boolean)\r
114             return (Boolean) value;\r
115         if (value instanceof String)\r
116             return Boolean.parseBoolean((String) value);\r
117         return defaultValue;\r
118     }\r
119 \r
120 }\r