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