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