]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/NodePropertyTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / NodePropertyTester.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/NodePropertyTester.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/NodePropertyTester.java
new file mode 100644 (file)
index 0000000..200e210
--- /dev/null
@@ -0,0 +1,218 @@
+/*******************************************************************************\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.browsing.ui.swt;\r
+\r
+import java.util.List;\r
+\r
+import org.eclipse.core.expressions.PropertyTester;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.simantics.DatabaseJob;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.node.IDeletable;\r
+import org.simantics.browsing.ui.common.node.IModifiable;\r
+import org.simantics.browsing.ui.common.node.IRefreshable;\r
+import org.simantics.browsing.ui.model.queries.IsNodeContextModifiable;\r
+import org.simantics.browsing.ui.model.queries.IsNodeContextRemovable;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.utils.RequestUtil;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * A JFace property tester extension for the Eclipse command framework that is\r
+ * meant for working with {@link NodeContext} instances of the Simantics\r
+ * browsing framework.\r
+ * \r
+ * <p>\r
+ * This tester expects to receive IStructuredSelections that contain NodeContext\r
+ * instances.\r
+ * \r
+ * <p>\r
+ * It supports testing of the following properties:\r
+ * <ul>\r
+ * <li>nodeClass - tests if the {@link NodeContext} input (see\r
+ * {@link BuiltinKeys#INPUT}) is an instance of the class specified as an\r
+ * argument. The class must be given as a qualified class name.</li>\r
+ * <li>deletable - for testing whether a NodeContext can be considered\r
+ * generically deletable, see <code>org.eclipse.ui.edit.delete</code> command.\r
+ * No arguments required.</li>\r
+ * <li>modifiable - for testing whether a NodeContext can be considered\r
+ * generically modifiable (either in-line or not), see\r
+ * <code>org.eclipse.ui.edit.rename</code> command. No arguments required.</li>\r
+ * <li>refreshable - for testing whether a NodeContext can be considered\r
+ * generically refreshable, see <code>org.eclipse.ui.file.refresh</code>\r
+ * command. No arguments required.</li>\r
+ * </ul>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class NodePropertyTester 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 NODE_CLASS = "nodeClass";\r
+\r
+    /**\r
+     * Tests if the received object is considered deletable.\r
+     */\r
+    private static final String DELETABLE = "deletable";\r
+\r
+    /**\r
+     * Tests if the received object is considered modifiable.\r
+     */\r
+    private static final String MODIFIABLE = "modifiable";\r
+\r
+    /**\r
+     * Tests if the received object is considered refreshable.\r
+     */\r
+    private static final String REFRESHABLE = "refreshable";\r
+\r
+    ContextTester deletableTester = new DeletableTester();\r
+    ContextTester modifiableTester = new ModifiableTester();\r
+    ContextTester refreshableTester = new RefreshableTester();\r
+\r
+    @Override\r
+    public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {\r
+//        System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);\r
+        if (!(receiver instanceof ISelection))\r
+            return false;\r
+\r
+        List<NodeContext> ncs = ISelectionUtils.getPossibleKeys((ISelection) receiver, SelectionHints.KEY_MAIN, NodeContext.class);\r
+        \r
+        if (ncs.isEmpty())\r
+            return false;\r
+\r
+        if (NODE_CLASS.equals(property)) {\r
+            return testCollection(ncs, new NodeClassTester(args));\r
+        } else if (DELETABLE.equals(property)) {\r
+            return testCollection(ncs, deletableTester);\r
+        } else if (MODIFIABLE.equals(property)) {\r
+            if (ncs.size() == 1)\r
+                return testCollection(ncs, modifiableTester);\r
+            return false;\r
+        } else if (REFRESHABLE.equals(property)) {\r
+            return testCollection(ncs, refreshableTester);\r
+        }\r
+\r
+        return false;\r
+    }\r
+\r
+    static interface ContextTester {\r
+        boolean test(NodeContext c);\r
+    }\r
+\r
+    public static class NodeClassTester implements ContextTester {\r
+        Object[] args;\r
+        NodeClassTester(Object[] args) {\r
+            this.args = args;\r
+        }\r
+        @Override\r
+        public boolean test(NodeContext nc) {\r
+            try {\r
+                Object input = nc.getConstant(BuiltinKeys.INPUT);\r
+                Class<?> inputClass = input.getClass();\r
+//                System.out.println("input: " + input);\r
+                boolean assignable = false;\r
+                for (Object o : args) {\r
+                    Class<?> clazz = Class.forName((String) o, true, input.getClass().getClassLoader());\r
+//                    System.out.println("  ARG " + o + " CLAZZ: " + clazz);\r
+                    if (clazz.isAssignableFrom(inputClass)) {\r
+//                        System.out.println("  assignable!");\r
+                        assignable = true;\r
+                        break;\r
+                    }\r
+                }\r
+                return assignable;\r
+            } catch (ClassNotFoundException e) {\r
+            }\r
+            return false;\r
+        }\r
+    }\r
+\r
+    public static class DeletableTester implements ContextTester {\r
+        @Override\r
+        public boolean test(NodeContext nc) {\r
+            Object input = nc.getConstant(BuiltinKeys.INPUT);\r
+            if (input instanceof Resource) {\r
+                Session session = Simantics.peekSession();\r
+                try {\r
+                    if (session != null && !DatabaseJob.inProgress())\r
+                        return RequestUtil.trySyncRequest(\r
+                                session,\r
+                                SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
+                                SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
+                                false,\r
+                                new IsNodeContextRemovable( nc ) );\r
+                } catch (DatabaseException | InterruptedException e) {\r
+                }\r
+                return false;\r
+            } else if (input instanceof IDeletable) {\r
+                // OK, this is something that should be considered\r
+                // deletable, as long as we can find\r
+                // a method for performing the deletion.\r
+                return true;\r
+            }\r
+            return false;\r
+        }\r
+    }\r
+\r
+    public static class ModifiableTester implements ContextTester {\r
+        @Override\r
+        public boolean test(NodeContext nc) {\r
+            Object input = nc.getConstant(BuiltinKeys.INPUT);\r
+            if (input instanceof Resource) {\r
+                Session session = Simantics.peekSession();\r
+                try {\r
+                    if (session != null && !DatabaseJob.inProgress())\r
+                        return RequestUtil.trySyncRequest(\r
+                                session,\r
+                                SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
+                                SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
+                                false,\r
+                                new IsNodeContextModifiable( nc ) );\r
+                } catch (DatabaseException | InterruptedException e) {\r
+                }\r
+                return false;\r
+            } else if (input instanceof IModifiable) {\r
+                return true;\r
+            }\r
+            return false;\r
+        }\r
+    }\r
+\r
+    public static class RefreshableTester implements ContextTester {\r
+        @Override\r
+        public boolean test(NodeContext nc) {\r
+            return nc.getConstant(BuiltinKeys.INPUT) instanceof IRefreshable;\r
+        }\r
+    }\r
+\r
+    private boolean testCollection(List<NodeContext> receiver, ContextTester tester) {\r
+        if (receiver.isEmpty())\r
+            return false;\r
+\r
+        int fails = 0;\r
+        for (NodeContext nc : receiver) {\r
+            if (!tester.test(nc))\r
+                ++fails;\r
+        }\r
+        return fails > 0 ? false : true;\r
+    }\r
+\r
+}\r