X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FNodePropertyTester.java;fp=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FNodePropertyTester.java;h=200e21066e2f7bea7296e33a875bbe7e5e84251b;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git
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
index 000000000..200e21066
--- /dev/null
+++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/NodePropertyTester.java
@@ -0,0 +1,218 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.swt;
+
+import java.util.List;
+
+import org.eclipse.core.expressions.PropertyTester;
+import org.eclipse.jface.viewers.ISelection;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.common.node.IDeletable;
+import org.simantics.browsing.ui.common.node.IModifiable;
+import org.simantics.browsing.ui.common.node.IRefreshable;
+import org.simantics.browsing.ui.model.queries.IsNodeContextModifiable;
+import org.simantics.browsing.ui.model.queries.IsNodeContextRemovable;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.utils.RequestUtil;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.utils.ui.ISelectionUtils;
+
+/**
+ * A JFace property tester extension for the Eclipse command framework that is
+ * meant for working with {@link NodeContext} instances of the Simantics
+ * browsing framework.
+ *
+ *
+ * This tester expects to receive IStructuredSelections that contain NodeContext
+ * instances.
+ *
+ *
+ * It supports testing of the following properties:
+ *
+ * - nodeClass - tests if the {@link NodeContext} input (see
+ * {@link BuiltinKeys#INPUT}) is an instance of the class specified as an
+ * argument. The class must be given as a qualified class name.
+ * - deletable - for testing whether a NodeContext can be considered
+ * generically deletable, see
org.eclipse.ui.edit.delete
command.
+ * No arguments required.
+ * - modifiable - for testing whether a NodeContext can be considered
+ * generically modifiable (either in-line or not), see
+ *
org.eclipse.ui.edit.rename
command. No arguments required.
+ * - refreshable - for testing whether a NodeContext can be considered
+ * generically refreshable, see
org.eclipse.ui.file.refresh
+ * command. No arguments required.
+ *
+ *
+ * @author Tuukka Lehtonen
+ */
+public class NodePropertyTester extends PropertyTester {
+
+ /**
+ * Tests if the received object within the model browser tree node is an
+ * instance of the class specified as the argument.
+ */
+ private static final String NODE_CLASS = "nodeClass";
+
+ /**
+ * Tests if the received object is considered deletable.
+ */
+ private static final String DELETABLE = "deletable";
+
+ /**
+ * Tests if the received object is considered modifiable.
+ */
+ private static final String MODIFIABLE = "modifiable";
+
+ /**
+ * Tests if the received object is considered refreshable.
+ */
+ private static final String REFRESHABLE = "refreshable";
+
+ ContextTester deletableTester = new DeletableTester();
+ ContextTester modifiableTester = new ModifiableTester();
+ ContextTester refreshableTester = new RefreshableTester();
+
+ @Override
+ public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
+// System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
+ if (!(receiver instanceof ISelection))
+ return false;
+
+ List ncs = ISelectionUtils.getPossibleKeys((ISelection) receiver, SelectionHints.KEY_MAIN, NodeContext.class);
+
+ if (ncs.isEmpty())
+ return false;
+
+ if (NODE_CLASS.equals(property)) {
+ return testCollection(ncs, new NodeClassTester(args));
+ } else if (DELETABLE.equals(property)) {
+ return testCollection(ncs, deletableTester);
+ } else if (MODIFIABLE.equals(property)) {
+ if (ncs.size() == 1)
+ return testCollection(ncs, modifiableTester);
+ return false;
+ } else if (REFRESHABLE.equals(property)) {
+ return testCollection(ncs, refreshableTester);
+ }
+
+ return false;
+ }
+
+ static interface ContextTester {
+ boolean test(NodeContext c);
+ }
+
+ public static class NodeClassTester implements ContextTester {
+ Object[] args;
+ NodeClassTester(Object[] args) {
+ this.args = args;
+ }
+ @Override
+ public boolean test(NodeContext nc) {
+ try {
+ Object input = nc.getConstant(BuiltinKeys.INPUT);
+ Class> inputClass = input.getClass();
+// System.out.println("input: " + input);
+ boolean assignable = false;
+ for (Object o : args) {
+ Class> clazz = Class.forName((String) o, true, input.getClass().getClassLoader());
+// System.out.println(" ARG " + o + " CLAZZ: " + clazz);
+ if (clazz.isAssignableFrom(inputClass)) {
+// System.out.println(" assignable!");
+ assignable = true;
+ break;
+ }
+ }
+ return assignable;
+ } catch (ClassNotFoundException e) {
+ }
+ return false;
+ }
+ }
+
+ public static class DeletableTester implements ContextTester {
+ @Override
+ public boolean test(NodeContext nc) {
+ Object input = nc.getConstant(BuiltinKeys.INPUT);
+ if (input instanceof Resource) {
+ Session session = Simantics.peekSession();
+ try {
+ if (session != null && !DatabaseJob.inProgress())
+ return RequestUtil.trySyncRequest(
+ session,
+ SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
+ SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
+ false,
+ new IsNodeContextRemovable( nc ) );
+ } catch (DatabaseException | InterruptedException e) {
+ }
+ return false;
+ } else if (input instanceof IDeletable) {
+ // OK, this is something that should be considered
+ // deletable, as long as we can find
+ // a method for performing the deletion.
+ return true;
+ }
+ return false;
+ }
+ }
+
+ public static class ModifiableTester implements ContextTester {
+ @Override
+ public boolean test(NodeContext nc) {
+ Object input = nc.getConstant(BuiltinKeys.INPUT);
+ if (input instanceof Resource) {
+ Session session = Simantics.peekSession();
+ try {
+ if (session != null && !DatabaseJob.inProgress())
+ return RequestUtil.trySyncRequest(
+ session,
+ SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
+ SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
+ false,
+ new IsNodeContextModifiable( nc ) );
+ } catch (DatabaseException | InterruptedException e) {
+ }
+ return false;
+ } else if (input instanceof IModifiable) {
+ return true;
+ }
+ return false;
+ }
+ }
+
+ public static class RefreshableTester implements ContextTester {
+ @Override
+ public boolean test(NodeContext nc) {
+ return nc.getConstant(BuiltinKeys.INPUT) instanceof IRefreshable;
+ }
+ }
+
+ private boolean testCollection(List receiver, ContextTester tester) {
+ if (receiver.isEmpty())
+ return false;
+
+ int fails = 0;
+ for (NodeContext nc : receiver) {
+ if (!tester.test(nc))
+ ++fails;
+ }
+ return fails > 0 ? false : true;
+ }
+
+}