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