1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.flag;
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;
27 * An Eclipse property tester for flag elements.
29 * @author Tuukka Lehtonen
31 public class FlagPropertyTester extends PropertyTester {
34 * Tests if the received flag resource is connected to a counterpart.
35 * Default expected value is true.
37 private static final String CONNECTED = "connected";
40 * Tests if the received flag resource is connected to a counterpart.
41 * Default expected value is true.
43 private static final String LOCALLY_CONNECTED = "locallyConnected";
46 * Tests if the received flag resource is marked as an external flag.
48 private static final String EXTERNAL = "external";
51 * Tests if the received flag resource is a merged flag with more than 1
54 private static final String MERGED = "merged";
57 public boolean test(Object receiver, final String property, final Object[] args, final Object expectedValue) {
58 final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
62 Session session = Simantics.peekSession();
66 if (DatabaseJob.inProgress())
70 return RequestUtil.trySyncRequest(
72 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
73 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
75 new UniqueRead<Boolean>() {
77 public Boolean perform(ReadGraph g) throws DatabaseException {
78 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
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
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);
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;
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);