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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.flag;
\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
26 * An Eclipse property tester for flag elements.
\r
28 * @author Tuukka Lehtonen
\r
30 public class FlagPropertyTester extends PropertyTester {
\r
33 * Tests if the received flag resource is connected to a counterpart.
\r
34 * Default expected value is true.
\r
36 private static final String CONNECTED = "connected";
\r
39 * Tests if the received flag resource is connected to a counterpart.
\r
40 * Default expected value is true.
\r
42 private static final String LOCALLY_CONNECTED = "locallyConnected";
\r
45 * Tests if the received flag resource is marked as an external flag.
\r
47 private static final String EXTERNAL = "external";
\r
50 * Tests if the received flag resource is a merged flag with more than 1
\r
53 private static final String MERGED = "merged";
\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
61 Session session = SimanticsUI.peekSession();
\r
62 if (session == null)
\r
65 if (DatabaseJob.inProgress())
\r
69 return RequestUtil.trySyncRequest(
\r
71 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
72 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
74 new UniqueRead<Boolean>() {
\r
76 public Boolean perform(ReadGraph g) throws DatabaseException {
\r
77 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
\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
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
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