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