]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/NodePropertyTester.java
Added Variable/Resource type information for DnD'd entities to JSON
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / NodePropertyTester.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.util.List;
15
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;
34
35 /**
36  * A JFace property tester extension for the Eclipse command framework that is
37  * meant for working with {@link NodeContext} instances of the Simantics
38  * browsing framework.
39  * 
40  * <p>
41  * This tester expects to receive IStructuredSelections that contain NodeContext
42  * instances.
43  * 
44  * <p>
45  * It supports testing of the following properties:
46  * <ul>
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>
59  * </ul>
60  * 
61  * @author Tuukka Lehtonen
62  */
63 public class NodePropertyTester extends PropertyTester {
64
65     /**
66      * Tests if the received object within the model browser tree node is an
67      * instance of the class specified as the argument.
68      */
69     private static final String NODE_CLASS = "nodeClass";
70
71     /**
72      * Tests if the received object is considered deletable.
73      */
74     private static final String DELETABLE = "deletable";
75
76     /**
77      * Tests if the received object is considered modifiable.
78      */
79     private static final String MODIFIABLE = "modifiable";
80
81     /**
82      * Tests if the received object is considered refreshable.
83      */
84     private static final String REFRESHABLE = "refreshable";
85
86     ContextTester deletableTester = new DeletableTester();
87     ContextTester modifiableTester = new ModifiableTester();
88     ContextTester refreshableTester = new RefreshableTester();
89
90     @Override
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))
94             return false;
95
96         List<NodeContext> ncs = ISelectionUtils.getPossibleKeys((ISelection) receiver, SelectionHints.KEY_MAIN, NodeContext.class);
97         
98         if (ncs.isEmpty())
99             return false;
100
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)) {
106             if (ncs.size() == 1)
107                 return testCollection(ncs, modifiableTester);
108             return false;
109         } else if (REFRESHABLE.equals(property)) {
110             return testCollection(ncs, refreshableTester);
111         }
112
113         return false;
114     }
115
116     static interface ContextTester {
117         boolean test(NodeContext c);
118     }
119
120     public static class NodeClassTester implements ContextTester {
121         Object[] args;
122         NodeClassTester(Object[] args) {
123             this.args = args;
124         }
125         @Override
126         public boolean test(NodeContext nc) {
127             try {
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!");
137                         assignable = true;
138                         break;
139                     }
140                 }
141                 return assignable;
142             } catch (ClassNotFoundException e) {
143             }
144             return false;
145         }
146     }
147
148     public static class DeletableTester implements ContextTester {
149         @Override
150         public boolean test(NodeContext nc) {
151             Object input = nc.getConstant(BuiltinKeys.INPUT);
152             if (input instanceof Resource) {
153                 Session session = Simantics.peekSession();
154                 try {
155                     if (session != null && !DatabaseJob.inProgress())
156                         return RequestUtil.trySyncRequest(
157                                 session,
158                                 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
159                                 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
160                                 false,
161                                 new IsNodeContextRemovable( nc ) );
162                 } catch (DatabaseException | InterruptedException e) {
163                 }
164                 return false;
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.
169                 return true;
170             }
171             return false;
172         }
173     }
174
175     public static class ModifiableTester implements ContextTester {
176         @Override
177         public boolean test(NodeContext nc) {
178             Object input = nc.getConstant(BuiltinKeys.INPUT);
179             if (input instanceof Resource) {
180                 Session session = Simantics.peekSession();
181                 try {
182                     if (session != null && !DatabaseJob.inProgress())
183                         return RequestUtil.trySyncRequest(
184                                 session,
185                                 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
186                                 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
187                                 false,
188                                 new IsNodeContextModifiable( nc ) );
189                 } catch (DatabaseException | InterruptedException e) {
190                 }
191                 return false;
192             } else if (input instanceof IModifiable) {
193                 return true;
194             }
195             return false;
196         }
197     }
198
199     public static class RefreshableTester implements ContextTester {
200         @Override
201         public boolean test(NodeContext nc) {
202             return nc.getConstant(BuiltinKeys.INPUT) instanceof IRefreshable;
203         }
204     }
205
206     private boolean testCollection(List<NodeContext> receiver, ContextTester tester) {
207         if (receiver.isEmpty())
208             return false;
209
210         int fails = 0;
211         for (NodeContext nc : receiver) {
212             if (!tester.test(nc))
213                 ++fails;
214         }
215         return fails > 0 ? false : true;
216     }
217
218 }