]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/ComponentTypePropertyTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / ComponentTypePropertyTester.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 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.modeling.ui.property;\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.layer0.Layer0;\r
23 import org.simantics.structural.stubs.StructuralResource2;\r
24 import org.simantics.ui.SimanticsUI;\r
25 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
26 \r
27 /**\r
28  * A JFace property tester extension for the Eclipse command framework that is\r
29  * meant for working with Simantics database {@link Resource} instances.\r
30  * \r
31  * <p>\r
32  * This tester expects to receive IStructuredSelections that contain Resource\r
33  * instances.\r
34  * \r
35  * <p>\r
36  * It supports testing of the following properties:\r
37  * <ul>\r
38  * <li>partOf - </li>\r
39  * </ul>\r
40  * \r
41  * @author Tuukka Lehtonen\r
42  */\r
43 public class ComponentTypePropertyTester extends PropertyTester {\r
44 \r
45     /**\r
46      * Tests to see if the input Resource is part of a structural component\r
47      * type.\r
48      */\r
49     private static final String PART_OF = "partOf";\r
50 \r
51     @Override\r
52     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
53         final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);\r
54         if (resource == null)\r
55             return false;\r
56 \r
57         Session session = SimanticsUI.peekSession();\r
58         if (session == null)\r
59             return false;\r
60 \r
61         if (DatabaseJob.inProgress())\r
62             return false;\r
63 \r
64         try {\r
65             return RequestUtil.trySyncRequest(\r
66                     session,\r
67                     SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
68                     SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
69                     false,\r
70                     new UniqueRead<Boolean>() {\r
71                 @Override\r
72                 public Boolean perform(ReadGraph g) throws DatabaseException {\r
73                     return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));\r
74                 }\r
75             });\r
76         } catch (DatabaseException | InterruptedException e) {\r
77             // Purposefully not logging these exceptions, there might be way too\r
78             // many even under normal circumstances.\r
79             // TODO: add debug tracing options controlling the printing of these exceptions\r
80             return false;\r
81         }\r
82     }\r
83 \r
84     private boolean doTest(ReadGraph graph, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {\r
85         if (PART_OF.equals(property)) {\r
86             boolean expected = parseBoolean(expectedValue, true);\r
87             boolean is = partOfComponentType(graph, resource) != null;\r
88             return is == expected;\r
89         }\r
90         return false;\r
91     }\r
92 \r
93     private Resource partOfComponentType(ReadGraph graph, Resource resource) throws DatabaseException {\r
94         Layer0 L0 = Layer0.getInstance(graph);\r
95         StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
96         Resource componentType = null;\r
97         for (Resource curComponent = resource; true;) {\r
98             componentType = graph.getPossibleObject(curComponent, STR.Defines);\r
99             if (componentType != null) {\r
100                 return componentType;\r
101             }\r
102             curComponent = graph.getPossibleObject(curComponent, L0.PartOf);\r
103             if (curComponent == null)\r
104                 break;  \r
105         }\r
106         return null;\r
107     }\r
108 \r
109     boolean parseBoolean(Object value, boolean defaultValue) {\r
110         if (value instanceof Boolean)\r
111             return (Boolean) value;\r
112         if (value instanceof String)\r
113             return Boolean.parseBoolean((String) value);\r
114         return defaultValue;\r
115     }\r
116 \r
117 }\r