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.procedure.adapter.TransientCacheListener;
30 import org.simantics.db.common.utils.RequestUtil;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.SelectionHints;
33 import org.simantics.ui.SimanticsUI;
34 import org.simantics.utils.ui.ISelectionUtils;
37 * A JFace property tester extension for the Eclipse command framework that is
38 * meant for working with {@link NodeContext} instances of the Simantics
42 * This tester expects to receive IStructuredSelections that contain NodeContext
46 * It supports testing of the following properties:
48 * <li>nodeClass - tests if the {@link NodeContext} input (see
49 * {@link BuiltinKeys#INPUT}) is an instance of the class specified as an
50 * argument. The class must be given as a qualified class name.</li>
51 * <li>deletable - for testing whether a NodeContext can be considered
52 * generically deletable, see <code>org.eclipse.ui.edit.delete</code> command.
53 * No arguments required.</li>
54 * <li>modifiable - for testing whether a NodeContext can be considered
55 * generically modifiable (either in-line or not), see
56 * <code>org.eclipse.ui.edit.rename</code> command. No arguments required.</li>
57 * <li>refreshable - for testing whether a NodeContext can be considered
58 * generically refreshable, see <code>org.eclipse.ui.file.refresh</code>
59 * command. No arguments required.</li>
62 * @author Tuukka Lehtonen
64 public class NodePropertyTester extends PropertyTester {
67 * Tests if the received object within the model browser tree node is an
68 * instance of the class specified as the argument.
70 private static final String NODE_CLASS = "nodeClass";
73 * Tests if the received object is considered deletable.
75 private static final String DELETABLE = "deletable";
78 * Tests if the received object is considered modifiable.
80 private static final String MODIFIABLE = "modifiable";
83 * Tests if the received object is considered refreshable.
85 private static final String REFRESHABLE = "refreshable";
87 ContextTester deletableTester = new DeletableTester();
88 ContextTester modifiableTester = new ModifiableTester();
89 ContextTester refreshableTester = new RefreshableTester();
92 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
93 // System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
94 if (!(receiver instanceof ISelection))
97 List<NodeContext> ncs = ISelectionUtils.getPossibleKeys((ISelection) receiver, SelectionHints.KEY_MAIN, NodeContext.class);
102 if (NODE_CLASS.equals(property)) {
103 return testCollection(ncs, new NodeClassTester(args));
104 } else if (DELETABLE.equals(property)) {
105 return testCollection(ncs, deletableTester);
106 } else if (MODIFIABLE.equals(property)) {
108 return testCollection(ncs, modifiableTester);
110 } else if (REFRESHABLE.equals(property)) {
111 return testCollection(ncs, refreshableTester);
117 static interface ContextTester {
118 boolean test(NodeContext c);
121 public static class NodeClassTester implements ContextTester {
123 NodeClassTester(Object[] args) {
127 public boolean test(NodeContext nc) {
129 Object input = nc.getConstant(BuiltinKeys.INPUT);
130 Class<?> inputClass = input.getClass();
131 // System.out.println("input: " + input);
132 boolean assignable = false;
133 for (Object o : args) {
134 Class<?> clazz = Class.forName((String) o, true, input.getClass().getClassLoader());
135 // System.out.println(" ARG " + o + " CLAZZ: " + clazz);
136 if (clazz.isAssignableFrom(inputClass)) {
137 // System.out.println(" assignable!");
143 } catch (ClassNotFoundException e) {
149 public static class DeletableTester implements ContextTester {
151 public boolean test(NodeContext nc) {
152 Object input = nc.getConstant(BuiltinKeys.INPUT);
153 if (input instanceof Resource) {
154 Session session = Simantics.peekSession();
156 if (session != null && !DatabaseJob.inProgress())
157 return RequestUtil.trySyncRequest(
159 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
160 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
162 new IsNodeContextRemovable( nc ),
163 TransientCacheListener.instance());
164 } catch (DatabaseException | InterruptedException e) {
167 } else if (input instanceof IDeletable) {
168 // OK, this is something that should be considered
169 // deletable, as long as we can find
170 // a method for performing the deletion.
177 public static class ModifiableTester implements ContextTester {
179 public boolean test(NodeContext nc) {
180 Object input = nc.getConstant(BuiltinKeys.INPUT);
181 if (input instanceof Resource) {
182 Session session = Simantics.peekSession();
184 if (session != null && !DatabaseJob.inProgress())
185 return RequestUtil.trySyncRequest(
187 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
188 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
190 new IsNodeContextModifiable( nc ),
191 TransientCacheListener.instance());
192 } catch (DatabaseException | InterruptedException e) {
195 } else if (input instanceof IModifiable) {
202 public static class RefreshableTester implements ContextTester {
204 public boolean test(NodeContext nc) {
205 return nc.getConstant(BuiltinKeys.INPUT) instanceof IRefreshable;
209 private boolean testCollection(List<NodeContext> receiver, ContextTester tester) {
210 if (receiver.isEmpty())
214 for (NodeContext nc : receiver) {
215 if (!tester.test(nc))
218 return fails > 0 ? false : true;