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