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