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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.elements;
\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
27 * An Eclipse property tester for normal elements.
\r
29 * @author Tuukka Lehtonen
\r
31 public class ElementPropertyTester extends PropertyTester {
\r
34 * Tests if the received element resource is mapped to a counterpart.
\r
35 * Default expected value is true.
\r
37 private static final String MAPPED = "mapped";
\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
45 Session session = SimanticsUI.peekSession();
\r
46 if (session == null)
\r
49 if (DatabaseJob.inProgress())
\r
53 return RequestUtil.trySyncRequest(
\r
55 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
56 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
58 new UniqueRead<Boolean>() {
\r
60 public Boolean perform(ReadGraph g) throws DatabaseException {
\r
61 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
\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
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