]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/ElementPropertyTester.java
Merge "(refs #7519) Added old constructor back to ConnectionVisuals."
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / ElementPropertyTester.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.elements;
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.modeling.ModelingResources;
23 import org.simantics.ui.SimanticsUI;
24 import org.simantics.ui.utils.ResourceAdaptionUtils;
25
26 /**
27  * An Eclipse property tester for normal elements.
28  * 
29  * @author Tuukka Lehtonen
30  */
31 public class ElementPropertyTester extends PropertyTester {
32
33     /**
34      * Tests if the received element resource is mapped to a counterpart.
35      * Default expected value is true.
36      */
37     private static final String MAPPED = "mapped";
38
39     @Override
40     public boolean test(Object receiver, final String property, final Object[] args, final Object expectedValue) {
41         final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
42         if (resource == null)
43             return false;
44
45         Session session = SimanticsUI.peekSession();
46         if (session == null)
47             return false;
48
49         if (DatabaseJob.inProgress())
50             return false;
51
52         try {
53             return RequestUtil.trySyncRequest(
54                     session,
55                     SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
56                     SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
57                     false,
58                     new UniqueRead<Boolean>() {
59                 @Override
60                 public Boolean perform(ReadGraph g) throws DatabaseException {
61                     return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
62                 }
63             });
64         } catch (DatabaseException | InterruptedException e) {
65             // Purposefully not logging these exceptions, there might be way too
66             // many even under normal circumstances.
67             // TODO: add debug tracing options controlling the printing of these exceptions
68             return false;
69         }
70     }
71
72     private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
73         if (MAPPED.equals(property)) {
74                 return graph.hasStatement(resource, ModelingResources.getInstance(graph).ElementToComponent);
75         } 
76         return false;
77     }
78
79 }