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