]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/ModelPropertyTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / ModelPropertyTester.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.property;\r
13 \r
14 import java.util.Collection;\r
15 \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
29 \r
30 /**\r
31  * @author Tuukka Lehtonen\r
32  */\r
33 public class ModelPropertyTester extends PropertyTester {\r
34 \r
35     /**\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
38      */\r
39     private static final String HAS_ACTIVE_MODELS_OF_TYPE = "hasActiveModelsOfType";\r
40 \r
41     @Override\r
42     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
43         try {\r
44             if (HAS_ACTIVE_MODELS_OF_TYPE.equals(property)) {\r
45                 if (args.length == 0)\r
46                     return false;\r
47 \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
52                         return false;\r
53 \r
54                     return RequestUtil.trySyncRequest(\r
55                             session,\r
56                             SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
57                             SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
58                             false,\r
59                             new UniqueRead<Boolean>() {\r
60                         @Override\r
61                         public Boolean perform(ReadGraph graph)\r
62                                 throws DatabaseException {\r
63                             Resource type = graph.getPossibleResource( OntologyVersions.getInstance().currentVersion((String) args[0]) );\r
64                             if (type == null)\r
65                                 return false;\r
66 \r
67                             Collection<Resource> activeModels = graph.syncRequest(new ActiveModels(project));\r
68                             for (Resource activeModel : activeModels)\r
69                                 if (graph.isInstanceOf(activeModel, type))\r
70                                     return true;\r
71                             return false;\r
72                         }\r
73                     });\r
74                 }\r
75             }\r
76         } catch (DatabaseException | InterruptedException e) {\r
77             ErrorLogger.defaultLogError(e);\r
78         }\r
79         return false;\r
80     }\r
81 \r
82 }\r