1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import java.util.List;
16 import org.eclipse.core.expressions.PropertyTester;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.simantics.DatabaseJob;
19 import org.simantics.Simantics;
20 import org.simantics.browsing.ui.BuiltinKeys;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.browsing.ui.common.node.IDeletable;
23 import org.simantics.browsing.ui.common.node.IModifiable;
24 import org.simantics.browsing.ui.common.node.IRefreshable;
25 import org.simantics.browsing.ui.model.queries.IsNodeContextModifiable;
26 import org.simantics.browsing.ui.model.queries.IsNodeContextRemovable;
27 import org.simantics.db.Resource;
28 import org.simantics.db.Session;
29 import org.simantics.db.common.utils.RequestUtil;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.SelectionHints;
32 import org.simantics.ui.SimanticsUI;
33 import org.simantics.utils.ui.ISelectionUtils;
36 * A JFace property tester extension for the Eclipse command framework that is
37 * meant for working with {@link NodeContext} instances of the Simantics
41 * This tester expects to receive IStructuredSelections that contain NodeContext
45 * It supports testing of the following properties:
47 * <li>nodeClass - tests if the {@link NodeContext} input (see
48 * {@link BuiltinKeys#INPUT}) is an instance of the class specified as an
49 * argument. The class must be given as a qualified class name.</li>
50 * <li>deletable - for testing whether a NodeContext can be considered
51 * generically deletable, see <code>org.eclipse.ui.edit.delete</code> command.
52 * No arguments required.</li>
53 * <li>modifiable - for testing whether a NodeContext can be considered
54 * generically modifiable (either in-line or not), see
55 * <code>org.eclipse.ui.edit.rename</code> command. No arguments required.</li>
56 * <li>refreshable - for testing whether a NodeContext can be considered
57 * generically refreshable, see <code>org.eclipse.ui.file.refresh</code>
58 * command. No arguments required.</li>
61 * @author Tuukka Lehtonen
63 public class NodePropertyTester extends PropertyTester {
66 * Tests if the received object within the model browser tree node is an
67 * instance of the class specified as the argument.
69 private static final String NODE_CLASS = "nodeClass";
72 * Tests if the received object is considered deletable.
74 private static final String DELETABLE = "deletable";
77 * Tests if the received object is considered modifiable.
79 private static final String MODIFIABLE = "modifiable";
82 * Tests if the received object is considered refreshable.
84 private static final String REFRESHABLE = "refreshable";
86 ContextTester deletableTester = new DeletableTester();
87 ContextTester modifiableTester = new ModifiableTester();
88 ContextTester refreshableTester = new RefreshableTester();
91 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
92 // System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
93 if (!(receiver instanceof ISelection))
96 List<NodeContext> ncs = ISelectionUtils.getPossibleKeys((ISelection) receiver, SelectionHints.KEY_MAIN, NodeContext.class);
101 if (NODE_CLASS.equals(property)) {
102 return testCollection(ncs, new NodeClassTester(args));
103 } else if (DELETABLE.equals(property)) {
104 return testCollection(ncs, deletableTester);
105 } else if (MODIFIABLE.equals(property)) {
107 return testCollection(ncs, modifiableTester);
109 } else if (REFRESHABLE.equals(property)) {
110 return testCollection(ncs, refreshableTester);
116 static interface ContextTester {
117 boolean test(NodeContext c);
120 public static class NodeClassTester implements ContextTester {
122 NodeClassTester(Object[] args) {
126 public boolean test(NodeContext nc) {
128 Object input = nc.getConstant(BuiltinKeys.INPUT);
129 Class<?> inputClass = input.getClass();
130 // System.out.println("input: " + input);
131 boolean assignable = false;
132 for (Object o : args) {
133 Class<?> clazz = Class.forName((String) o, true, input.getClass().getClassLoader());
134 // System.out.println(" ARG " + o + " CLAZZ: " + clazz);
135 if (clazz.isAssignableFrom(inputClass)) {
136 // System.out.println(" assignable!");
142 } catch (ClassNotFoundException e) {
148 public static class DeletableTester implements ContextTester {
150 public boolean test(NodeContext nc) {
151 Object input = nc.getConstant(BuiltinKeys.INPUT);
152 if (input instanceof Resource) {
153 Session session = Simantics.peekSession();
155 if (session != null && !DatabaseJob.inProgress())
156 return RequestUtil.trySyncRequest(
158 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
159 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
161 new IsNodeContextRemovable( nc ) );
162 } catch (DatabaseException | InterruptedException e) {
165 } else if (input instanceof IDeletable) {
166 // OK, this is something that should be considered
167 // deletable, as long as we can find
168 // a method for performing the deletion.
175 public static class ModifiableTester implements ContextTester {
177 public boolean test(NodeContext nc) {
178 Object input = nc.getConstant(BuiltinKeys.INPUT);
179 if (input instanceof Resource) {
180 Session session = Simantics.peekSession();
182 if (session != null && !DatabaseJob.inProgress())
183 return RequestUtil.trySyncRequest(
185 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
186 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
188 new IsNodeContextModifiable( nc ) );
189 } catch (DatabaseException | InterruptedException e) {
192 } else if (input instanceof IModifiable) {
199 public static class RefreshableTester implements ContextTester {
201 public boolean test(NodeContext nc) {
202 return nc.getConstant(BuiltinKeys.INPUT) instanceof IRefreshable;
206 private boolean testCollection(List<NodeContext> receiver, ContextTester tester) {
207 if (receiver.isEmpty())
211 for (NodeContext nc : receiver) {
212 if (!tester.test(nc))
215 return fails > 0 ? false : true;