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.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;
26 * An Eclipse property tester for flag elements.
28 * @author Tuukka Lehtonen
30 public class FlagPropertyTester extends PropertyTester {
33 * Tests if the received flag resource is connected to a counterpart.
34 * Default expected value is true.
36 private static final String CONNECTED = "connected";
39 * Tests if the received flag resource is connected to a counterpart.
40 * Default expected value is true.
42 private static final String LOCALLY_CONNECTED = "locallyConnected";
45 * Tests if the received flag resource is marked as an external flag.
47 private static final String EXTERNAL = "external";
50 * Tests if the received flag resource is a merged flag with more than 1
53 private static final String MERGED = "merged";
56 public boolean test(Object receiver, final String property, final Object[] args, final Object expectedValue) {
57 final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
61 Session session = SimanticsUI.peekSession();
65 if (DatabaseJob.inProgress())
69 return RequestUtil.trySyncRequest(
71 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
72 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
74 new UniqueRead<Boolean>() {
76 public Boolean perform(ReadGraph g) throws DatabaseException {
77 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
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
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);
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;
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);