]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/ModelEvaluators.java
25e57286fd10c79efe9ff1575fb0a0f57ec325b8
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / ModelEvaluators.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.modeling.ui.modelBrowser;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.function.Supplier;
21
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.simantics.browsing.ui.BuiltinKeys;
24 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
25 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
26 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;
27 import org.simantics.browsing.ui.DataSource;
28 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
29 import org.simantics.browsing.ui.NodeContext;
30 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
31 import org.simantics.browsing.ui.common.ColumnKeys;
32 import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;
33 import org.simantics.browsing.ui.common.EvaluatorImpl;
34 import org.simantics.browsing.ui.common.comparators.AlphanumericComparatorFactory;
35 import org.simantics.browsing.ui.common.imagers.ContainerImager;
36 import org.simantics.browsing.ui.common.labelers.LabelerContent;
37 import org.simantics.browsing.ui.common.labelers.LabelerStub;
38 import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;
39 import org.simantics.browsing.ui.content.Imager;
40 import org.simantics.browsing.ui.content.ImagerFactory;
41 import org.simantics.browsing.ui.content.Labeler;
42 import org.simantics.browsing.ui.content.LabelerFactory;
43 import org.simantics.browsing.ui.content.Viewpoint;
44 import org.simantics.browsing.ui.content.ViewpointFactory;
45 import org.simantics.browsing.ui.graph.impl.LazyGraphLabeler;
46 import org.simantics.browsing.ui.graph.impl.LazyViewpoint;
47 import org.simantics.browsing.ui.graph.impl.MissingImageDescriptor;
48 import org.simantics.browsing.ui.graph.impl.StringRepresentationLabelerFactory;
49 import org.simantics.browsing.ui.swt.OldAdapterImagerFactory;
50 import org.simantics.db.ReadGraph;
51 import org.simantics.db.Resource;
52 import org.simantics.db.exception.DatabaseException;
53 import org.simantics.layer0.Layer0;
54 import org.simantics.modeling.ui.modelBrowser.model.IChildrenCallback;
55 import org.simantics.modeling.ui.modelBrowser.model.IDisposable;
56 import org.simantics.modeling.ui.modelBrowser.model.INode;
57 import org.simantics.modeling.ui.modelBrowser.model.INode2;
58 import org.simantics.modeling.ui.modelBrowser.model.IUpdateable;
59 import org.simantics.ui.SimanticsUI;
60 import org.simantics.utils.datastructures.UnaryFunction;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 /**
65  * @author Hannu Niemistö
66  * @author Tuukka Lehtonen
67  */
68 @Deprecated
69 public final class ModelEvaluators {
70
71     public static UnaryFunction<Boolean, Object> passTester() {
72         return new UnaryFunction<Boolean, Object>() {
73             @Override
74             public Boolean call(Object arg) {
75                 return Boolean.TRUE;
76             }
77         };
78     }
79
80     public static UnaryFunction<Boolean, Object> exactClassTester(final Class<?> ... classes) {
81         final Set<Class<?>> classSet = new HashSet<Class<?>>();
82         for(Class<?> clazz : classes) classSet.add(clazz);
83         return new UnaryFunction<Boolean, Object>() {
84             @Override
85             public Boolean call(Object arg) {
86                 if(classSet.contains(arg.getClass())) return Boolean.TRUE;
87                 return Boolean.FALSE;
88             }
89         };
90     }
91
92     public static UnaryFunction<Boolean, Object> classTester(final Class<?> ... classes) {
93         return new UnaryFunction<Boolean, Object>() {
94             @Override
95             public Boolean call(Object arg) {
96                 for(Class<?> clazz : classes) {
97                     if(clazz.isInstance(arg)) return Boolean.TRUE;
98                 }
99                 return Boolean.FALSE;
100             }
101         };
102     }
103
104     /**
105      * @param session
106      * @param resourceManager
107      * @param factoryHints
108      * @return
109      */
110     public static Evaluator createResourceEvaluator() {
111         Evaluator resourceEvaluator = new EvaluatorImpl();
112
113         resourceEvaluator.addViewpoint(new ResourceViewpointFactory(), 1.0);
114         resourceEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
115         resourceEvaluator.addLabeler(new StringRepresentationLabelerFactory(), 1.0);
116         resourceEvaluator.addImager(new OldAdapterImagerFactory(), 1.0);
117
118         return resourceEvaluator;
119     }
120
121     /**
122      * @param session
123      * @param resourceManager
124      * @param factoryHints
125      * @param tester
126      * @return
127      */
128     public static Evaluator createResourceEvaluator(final UnaryFunction<Boolean, Object> tester) {
129         Evaluator resourceEvaluator = new EvaluatorImpl();
130
131         resourceEvaluator.addViewpoint(new ResourceViewpointFactoryWithTester(tester), 1.0);
132         resourceEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
133         resourceEvaluator.addLabeler(new StringRepresentationLabelerFactory(), 1.0);
134         resourceEvaluator.addImager(new OldAdapterImagerFactory(), 1.0);
135
136         return resourceEvaluator;
137     }
138
139     /**
140      * @param session
141      * @param resourceManager
142      * @return
143      */
144     public static Evaluator createNodeEvaluator() {
145         Evaluator nodeEvaluator = new EvaluatorImpl();
146
147         nodeEvaluator.addViewpoint(new NodeViewpointFactory(), 1.0);
148         nodeEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
149         nodeEvaluator.addLabeler(new NodeLabelerFactory(), 1.0);
150         nodeEvaluator.addImager(new NodeImagerFactory(), 1.0);
151
152         return nodeEvaluator;
153     }
154
155     /**
156      * @param session
157      * @param resourceManager
158      * @param tester
159      * @return
160      */
161     public static Evaluator createNodeEvaluator(final UnaryFunction<Boolean, Object> tester) {
162         Evaluator nodeEvaluator = new EvaluatorImpl();
163
164         nodeEvaluator.addViewpoint(new NodeViewpointFactoryWithTester(tester), 1.0);
165         nodeEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
166         nodeEvaluator.addLabeler(new NodeLabelerFactory(), 1.0);
167         nodeEvaluator.addImager(new NodeImagerFactory(), 1.0);
168
169         return nodeEvaluator;
170     }
171
172     /**
173      * @param session
174      * @param resourceManager
175      * @return
176      */
177     public static Evaluator createNode2Evaluator() {
178         Evaluator nodeEvaluator = new EvaluatorImpl();
179
180         nodeEvaluator.addViewpoint(new Node2ViewpointFactory(), 1.0);
181         nodeEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
182         nodeEvaluator.addLabeler(new Node2LabelerFactory(), 1.0);
183         nodeEvaluator.addImager(new Node2ImagerFactory(), 1.0);
184
185         return nodeEvaluator;
186     }
187
188 }
189
190 abstract class BaseViewpointFactory implements ViewpointFactory {
191     protected abstract class VPB extends LazyViewpoint implements Supplier<Boolean>, IChildrenCallback {
192         public VPB(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
193             super(updater, context, key);
194         }
195
196         @Override
197         public String toString() {
198             return BaseViewpointFactory.this.toString();
199         }
200
201         @Override
202         public Object getIdentity() {
203             // This is necessary to give graph requests related to this
204             // LazyViewpoint a unique-enough identity so that they don't collide
205             // unexpectedly with other users of ModelEvaluators.
206             // This makes requests created with different concrete classes of
207             // BaseViewpointFactory unique.
208             return BaseViewpointFactory.this.getClass();
209         }
210
211         @Override
212         public Boolean get() {
213             return Boolean.valueOf(updater.isDisposed());
214         }
215
216         @Override
217         public void refreshChildren(Collection<?> newChildren) {
218             NodeContext[] ncs = toContextsWithInput(newChildren);
219             setHasChildren(ncs.length > 0);
220             setChildren(updater, ncs);
221             updater.scheduleReplace(context, key, this);
222         }
223
224         @Override
225         public Boolean hasChildren(ReadGraph graph) throws DatabaseException {
226             // hasChildren must do the same graph operations as children
227             // since they both share the same PrimitiveQueryUpdater.
228             return children(graph).length > 0;
229         }
230     };
231 }
232
233 abstract class BaseViewpointFactoryWithTester extends BaseViewpointFactory {
234     protected final UnaryFunction<Boolean, Object> tester;
235
236     BaseViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
237         this.tester = tester;
238     }
239 }
240
241 class ResourceViewpointFactory extends BaseViewpointFactory {
242     @Override
243     public String toString() {
244         return "Consists Of";
245     }
246
247     class VP extends VPB {
248         public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
249             super(updater, context, key);
250         }
251
252         @Override
253         public NodeContext[] children(ReadGraph graph) throws DatabaseException {
254             return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
255         }
256
257         protected Collection<?> getChildren(ReadGraph g, Resource r) throws DatabaseException {
258                 Layer0 b = Layer0.getInstance(g);
259             Collection<Resource> resources = g.getObjects(r, b.ConsistsOf);
260             ArrayList<Object> ret = new ArrayList<Object>(resources.size());
261             for (Resource res : resources) {
262                 Object node = null;
263 //              try {
264 //                  node = g.adapt2(res, INode2.class));
265 //              } catch (AdaptionException e) {
266                 node = g.getPossibleAdapter(res, INode.class);
267                 if (node != null) {
268                     if (node instanceof IDisposable)
269                         ((IDisposable) node).setDisposedCallable(this);
270 //                    if (node instanceof IUpdateable)
271 //                        ((IUpdateable) node).setChildrenCallback(this);
272                     ret.add(node);
273 //                  }
274                 }
275             }
276             return ret;
277         }
278     };
279
280     @Override
281     public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
282         return new VP(updater, context, key);
283     }
284 }
285
286 class ResourceViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
287
288     ResourceViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
289         super(tester);
290     }
291
292     @Override
293     public String toString() {
294         return "Consists Of";
295     }
296
297     class VP extends VPB {
298         public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
299             super(updater, context, key);
300         }
301
302         @Override
303         public NodeContext[] children(ReadGraph graph) throws DatabaseException {
304             return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
305         }
306
307         protected Collection<?> getChildren(ReadGraph g, Resource r) throws DatabaseException {
308                 Layer0 b = Layer0.getInstance(g);
309             Collection<Resource> resources = g.getObjects(r, b.ConsistsOf);
310             ArrayList<Object> ret = new ArrayList<Object>(resources.size());
311             for (Resource res : resources) {
312                 Object node = null;
313 //              try {
314 //                  node = g.adapt2(res, INode2.class));
315 //              } catch (AdaptionException e) {
316                 node = g.getPossibleAdapter(res, INode.class);
317                 if (node != null) {
318                     if (tester.call(node)) {
319                         if (node instanceof IDisposable)
320                             ((IDisposable) node).setDisposedCallable(this);
321 //                        if (node instanceof IUpdateable)
322 //                            ((IUpdateable) node).setChildrenCallback(this);
323                         ret.add(node);
324                     }
325                 }
326 //              }
327             }
328             return ret;
329         }
330     };
331
332     @Override
333     public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
334         return new VP(updater, context, key);
335     }
336 }
337
338 class NodeViewpointFactory extends BaseViewpointFactory {
339     @Override
340     public String toString() {
341         return "Standard";
342     }
343
344     class VP extends VPB {
345         public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
346             super(updater, context, key);
347         }
348
349         @Override
350         public NodeContext[] children(ReadGraph graph) throws DatabaseException {
351             INode node = (INode) context.getConstant(BuiltinKeys.INPUT);
352             if (node instanceof IUpdateable)
353                 ((IUpdateable) node).setChildrenCallback(this);
354
355             Collection<?> children = node.getChildren(graph);
356             for (Object child : children) {
357                 if (child instanceof IDisposable)
358                     ((IDisposable) child).setDisposedCallable(this);
359             }
360             return toContextsWithInput(children);
361         }
362     };
363
364     @Override
365     public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
366         return new VP(updater, context, key);
367     }
368 }
369
370 class NodeViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
371
372     NodeViewpointFactoryWithTester(final UnaryFunction<Boolean, Object> tester) {
373         super(tester);
374     }
375
376     @Override
377     public String toString() {
378         return "Standard";
379     }
380
381     class VP extends VPB {
382         public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
383             super(updater, context, key);
384         }
385
386         @Override
387         public NodeContext[] children(ReadGraph graph) throws DatabaseException {
388             INode node = (INode) context.getConstant(BuiltinKeys.INPUT);
389             if (node instanceof IUpdateable)
390                 ((IUpdateable) node).setChildrenCallback(this);
391
392             ArrayList<Object> result = new ArrayList<Object>();
393             for (Object child : node.getChildren(graph)) {
394                 if (tester.call(child)) {
395                     result.add(child);
396                     if (child instanceof IDisposable)
397                         ((IDisposable) child).setDisposedCallable(this);
398                 }
399             }
400             return toContextsWithInput(result);
401         }
402     }
403
404     @Override
405     public Viewpoint create(PrimitiveQueryUpdater updater,NodeContext context, ViewpointKey key) {
406         return new VP(updater, context, key);
407     }
408
409 }
410
411 class NodeLabelerFactory implements LabelerFactory {
412     @Override
413     public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {
414         return new LazyGraphLabeler(updater, context, key) {
415             @Override
416             public Object getIdentity(LabelerKey key) {
417                 return NodeLabelerFactory.this.getClass();
418             }
419
420             @Override
421             public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
422                 return Collections.singletonMap(ColumnKeys.SINGLE,
423                         ((INode) context.getConstant(BuiltinKeys.INPUT)).getLabel(graph));
424             }
425
426             @Override
427             public Modifier getModifier(ModificationContext sourcePart, String key) {
428                 return ((INode) context.getConstant(BuiltinKeys.INPUT)).getModifier(SimanticsUI.getSession(), key);
429             }
430
431             @Override
432             public int category(ReadGraph graph) throws DatabaseException {
433                 return ((INode) context.getConstant(BuiltinKeys.INPUT)).getCategory(graph);
434             }
435
436             @Override
437             public Logger getLogger() {
438                 return LoggerFactory.getLogger(NodeLabelerFactory.class);
439             }
440         };
441     }
442 }
443
444 class NodeImagerFactory implements ImagerFactory {
445
446     @Override
447     public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
448         final ContainerImager<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();
449         result.setImage(MissingImageDescriptor.getInstance());
450
451         DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
452
453         source.schedule(g -> {
454             try {
455                 ImageDescriptor descriptor = ((INode)context.getConstant(BuiltinKeys.INPUT)).getImage(g);
456                 result.setImage(descriptor);
457                 updater.scheduleReplace(context, key, result);
458             } catch (DatabaseException e) {
459                 e.printStackTrace();
460             }
461         });
462
463         return result;
464     }
465
466 }
467
468 class Node2ViewpointFactory implements ViewpointFactory {
469
470     @Override
471     public String toString() {
472         return "Standard";
473     }
474
475     @Override
476     public Viewpoint create(final PrimitiveQueryUpdater updater, final NodeContext context, final ViewpointKey key) {
477         class V extends ViewpointStub implements Runnable, Supplier<Boolean> {
478             @Override
479             public void run() {
480                 updater.scheduleReplace(context, key, V.this);
481             }
482
483             @Override
484             public Boolean get() {
485                 return Boolean.valueOf(updater.isDisposed());
486             }
487
488             @Override
489             public NodeContext[] getChildren() {
490                 Collection<?> children = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getChildren(this, context);
491                 for (Object child : children) {
492                     if (child instanceof IDisposable)
493                         ((IDisposable) child).setDisposedCallable(this);
494                 }
495                 return toContextsWithInput(children);
496             }
497
498             @Override
499             public Boolean getHasChildren() {
500                 return ((INode2) context.getConstant(BuiltinKeys.INPUT)).hasChildren(this, context);
501             }
502         }
503
504         return new V();
505     }
506 }
507
508 class Node2LabelerFactory implements LabelerFactory {
509
510     @Override
511     public Labeler create(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
512
513         class L extends LabelerStub implements Runnable {
514             @Override
515             public Modifier getModifier(ModificationContext sourcePart, String key) {
516                 return ((INode2)context.getConstant(BuiltinKeys.INPUT)).getModifier(key);
517             }
518             @Override
519             public void run() {
520                 String label = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getLabel(this, context);
521                 int category = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getCategory(this, context);
522                 setContent(new LabelerContent(category, Collections.singletonMap(ColumnKeys.SINGLE, label)));
523                 updater.scheduleReplace(context, key, this);
524             }
525         };
526
527         L result = new L();
528
529         String label = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getLabel(result, context);
530         int category = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getCategory(result, context);
531         result.setContent(new LabelerContent(category, Collections.singletonMap(ColumnKeys.SINGLE, label)));
532
533         return result;
534     }
535
536 }
537
538 class Node2ImagerFactory implements ImagerFactory {
539
540     @Override
541     public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
542         assert(updater != null);
543         assert(context != null);
544
545         final ContainerImager<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();
546
547         Runnable callback = new Runnable() {
548             @Override
549             public void run() {
550                 ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(this, context);
551                 result.setImage(desc);
552                 updater.scheduleReplace(context, key, result);
553             }
554         };
555
556         ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(callback, context);
557         result.setImage(desc);
558         return result;
559
560     }
561
562 }