]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/ModelPropertyTester.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / ModelPropertyTester.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 java.util.Collection;
15
16 import org.eclipse.core.expressions.PropertyTester;
17 import org.simantics.DatabaseJob;
18 import org.simantics.Simantics;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.common.request.UniqueRead;
23 import org.simantics.db.common.utils.RequestUtil;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.request.ActiveModels;
26 import org.simantics.scl.reflection.OntologyVersions;
27 import org.simantics.ui.SimanticsUI;
28 import org.simantics.utils.ui.ErrorLogger;
29
30 /**
31  * @author Tuukka Lehtonen
32  */
33 public class ModelPropertyTester extends PropertyTester {
34
35     /**
36      * Tests if the received object within the model browser tree node is an
37      * instance of the class specified as the argument.
38      */
39     private static final String HAS_ACTIVE_MODELS_OF_TYPE = "hasActiveModelsOfType";
40
41     @Override
42     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
43         try {
44             if (HAS_ACTIVE_MODELS_OF_TYPE.equals(property)) {
45                 if (args.length == 0)
46                     return false;
47
48                 Session session = Simantics.peekSession();
49                 final Resource project = Simantics.peekProjectResource();
50                 if (session != null && project != null) {
51                     if (DatabaseJob.inProgress())
52                         return false;
53
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 graph)
62                                 throws DatabaseException {
63                             Resource type = graph.getPossibleResource( OntologyVersions.getInstance().currentVersion((String) args[0]) );
64                             if (type == null)
65                                 return false;
66
67                             Collection<Resource> activeModels = graph.syncRequest(new ActiveModels(project));
68                             for (Resource activeModel : activeModels)
69                                 if (graph.isInstanceOf(activeModel, type))
70                                     return true;
71                             return false;
72                         }
73                     });
74                 }
75             }
76         } catch (DatabaseException | InterruptedException e) {
77             ErrorLogger.defaultLogError(e);
78         }
79         return false;
80     }
81
82 }