]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/ModelPropertyTester.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/ModelPropertyTester.java
new file mode 100644 (file)
index 0000000..5a4d485
--- /dev/null
@@ -0,0 +1,82 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.property;\r
+\r
+import java.util.Collection;\r
+\r
+import org.eclipse.core.expressions.PropertyTester;\r
+import org.simantics.DatabaseJob;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.common.utils.RequestUtil;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.ActiveModels;\r
+import org.simantics.scl.reflection.OntologyVersions;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ModelPropertyTester extends PropertyTester {\r
+\r
+    /**\r
+     * Tests if the received object within the model browser tree node is an\r
+     * instance of the class specified as the argument.\r
+     */\r
+    private static final String HAS_ACTIVE_MODELS_OF_TYPE = "hasActiveModelsOfType";\r
+\r
+    @Override\r
+    public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
+        try {\r
+            if (HAS_ACTIVE_MODELS_OF_TYPE.equals(property)) {\r
+                if (args.length == 0)\r
+                    return false;\r
+\r
+                Session session = Simantics.peekSession();\r
+                final Resource project = Simantics.peekProjectResource();\r
+                if (session != null && project != null) {\r
+                    if (DatabaseJob.inProgress())\r
+                        return false;\r
+\r
+                    return RequestUtil.trySyncRequest(\r
+                            session,\r
+                            SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
+                            SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
+                            false,\r
+                            new UniqueRead<Boolean>() {\r
+                        @Override\r
+                        public Boolean perform(ReadGraph graph)\r
+                                throws DatabaseException {\r
+                            Resource type = graph.getPossibleResource( OntologyVersions.getInstance().currentVersion((String) args[0]) );\r
+                            if (type == null)\r
+                                return false;\r
+\r
+                            Collection<Resource> activeModels = graph.syncRequest(new ActiveModels(project));\r
+                            for (Resource activeModel : activeModels)\r
+                                if (graph.isInstanceOf(activeModel, type))\r
+                                    return true;\r
+                            return false;\r
+                        }\r
+                    });\r
+                }\r
+            }\r
+        } catch (DatabaseException | InterruptedException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+        }\r
+        return false;\r
+    }\r
+\r
+}\r