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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.property;
\r
14 import java.util.Collection;
\r
16 import org.eclipse.core.expressions.PropertyTester;
\r
17 import org.simantics.DatabaseJob;
\r
18 import org.simantics.Simantics;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.Session;
\r
22 import org.simantics.db.common.request.UniqueRead;
\r
23 import org.simantics.db.common.utils.RequestUtil;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.request.ActiveModels;
\r
26 import org.simantics.scl.reflection.OntologyVersions;
\r
27 import org.simantics.ui.SimanticsUI;
\r
28 import org.simantics.utils.ui.ErrorLogger;
\r
31 * @author Tuukka Lehtonen
\r
33 public class ModelPropertyTester extends PropertyTester {
\r
36 * Tests if the received object within the model browser tree node is an
\r
37 * instance of the class specified as the argument.
\r
39 private static final String HAS_ACTIVE_MODELS_OF_TYPE = "hasActiveModelsOfType";
\r
42 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
\r
44 if (HAS_ACTIVE_MODELS_OF_TYPE.equals(property)) {
\r
45 if (args.length == 0)
\r
48 Session session = Simantics.peekSession();
\r
49 final Resource project = Simantics.peekProjectResource();
\r
50 if (session != null && project != null) {
\r
51 if (DatabaseJob.inProgress())
\r
54 return RequestUtil.trySyncRequest(
\r
56 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
57 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
59 new UniqueRead<Boolean>() {
\r
61 public Boolean perform(ReadGraph graph)
\r
62 throws DatabaseException {
\r
63 Resource type = graph.getPossibleResource( OntologyVersions.getInstance().currentVersion((String) args[0]) );
\r
67 Collection<Resource> activeModels = graph.syncRequest(new ActiveModels(project));
\r
68 for (Resource activeModel : activeModels)
\r
69 if (graph.isInstanceOf(activeModel, type))
\r
76 } catch (DatabaseException | InterruptedException e) {
\r
77 ErrorLogger.defaultLogError(e);
\r