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