1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.modeling.ui.modelBrowser;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
20 import java.util.function.Supplier;
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;
63 * @author Hannu Niemistö
64 * @author Tuukka Lehtonen
67 public final class ModelEvaluators {
69 public static UnaryFunction<Boolean, Object> passTester() {
70 return new UnaryFunction<Boolean, Object>() {
72 public Boolean call(Object arg) {
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>() {
83 public Boolean call(Object arg) {
84 if(classSet.contains(arg.getClass())) return Boolean.TRUE;
90 public static UnaryFunction<Boolean, Object> classTester(final Class<?> ... classes) {
91 return new UnaryFunction<Boolean, Object>() {
93 public Boolean call(Object arg) {
94 for(Class<?> clazz : classes) {
95 if(clazz.isInstance(arg)) return Boolean.TRUE;
104 * @param resourceManager
105 * @param factoryHints
108 public static Evaluator createResourceEvaluator() {
109 Evaluator resourceEvaluator = new EvaluatorImpl();
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);
116 return resourceEvaluator;
121 * @param resourceManager
122 * @param factoryHints
126 public static Evaluator createResourceEvaluator(final UnaryFunction<Boolean, Object> tester) {
127 Evaluator resourceEvaluator = new EvaluatorImpl();
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);
134 return resourceEvaluator;
139 * @param resourceManager
142 public static Evaluator createNodeEvaluator() {
143 Evaluator nodeEvaluator = new EvaluatorImpl();
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);
150 return nodeEvaluator;
155 * @param resourceManager
159 public static Evaluator createNodeEvaluator(final UnaryFunction<Boolean, Object> tester) {
160 Evaluator nodeEvaluator = new EvaluatorImpl();
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);
167 return nodeEvaluator;
172 * @param resourceManager
175 public static Evaluator createNode2Evaluator() {
176 Evaluator nodeEvaluator = new EvaluatorImpl();
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);
183 return nodeEvaluator;
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);
195 public String toString() {
196 return BaseViewpointFactory.this.toString();
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();
210 public Boolean get() {
211 return Boolean.valueOf(updater.isDisposed());
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);
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;
231 abstract class BaseViewpointFactoryWithTester extends BaseViewpointFactory {
232 protected final UnaryFunction<Boolean, Object> tester;
234 BaseViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
235 this.tester = tester;
239 class ResourceViewpointFactory extends BaseViewpointFactory {
241 public String toString() {
242 return "Consists Of";
245 class VP extends VPB {
246 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
247 super(updater, context, key);
251 public NodeContext[] children(ReadGraph graph) throws DatabaseException {
252 return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
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) {
262 // node = g.adapt2(res, INode2.class));
263 // } catch (AdaptionException e) {
264 node = g.getPossibleAdapter(res, INode.class);
266 if (node instanceof IDisposable)
267 ((IDisposable) node).setDisposedCallable(this);
268 // if (node instanceof IUpdateable)
269 // ((IUpdateable) node).setChildrenCallback(this);
279 public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
280 return new VP(updater, context, key);
284 class ResourceViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
286 ResourceViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
291 public String toString() {
292 return "Consists Of";
295 class VP extends VPB {
296 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
297 super(updater, context, key);
301 public NodeContext[] children(ReadGraph graph) throws DatabaseException {
302 return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
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) {
312 // node = g.adapt2(res, INode2.class));
313 // } catch (AdaptionException e) {
314 node = g.getPossibleAdapter(res, INode.class);
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);
331 public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
332 return new VP(updater, context, key);
336 class NodeViewpointFactory extends BaseViewpointFactory {
338 public String toString() {
342 class VP extends VPB {
343 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
344 super(updater, context, key);
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);
353 Collection<?> children = node.getChildren(graph);
354 for (Object child : children) {
355 if (child instanceof IDisposable)
356 ((IDisposable) child).setDisposedCallable(this);
358 return toContextsWithInput(children);
363 public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
364 return new VP(updater, context, key);
368 class NodeViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
370 NodeViewpointFactoryWithTester(final UnaryFunction<Boolean, Object> tester) {
375 public String toString() {
379 class VP extends VPB {
380 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
381 super(updater, context, key);
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);
390 ArrayList<Object> result = new ArrayList<Object>();
391 for (Object child : node.getChildren(graph)) {
392 if (tester.call(child)) {
394 if (child instanceof IDisposable)
395 ((IDisposable) child).setDisposedCallable(this);
398 return toContextsWithInput(result);
403 public Viewpoint create(PrimitiveQueryUpdater updater,NodeContext context, ViewpointKey key) {
404 return new VP(updater, context, key);
409 class NodeLabelerFactory implements LabelerFactory {
411 public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {
412 return new LazyGraphLabeler(updater, context, key) {
414 public Object getIdentity(LabelerKey key) {
415 return NodeLabelerFactory.this.getClass();
419 public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
420 return Collections.singletonMap(ColumnKeys.SINGLE,
421 ((INode) context.getConstant(BuiltinKeys.INPUT)).getLabel(graph));
425 public Modifier getModifier(ModificationContext sourcePart, String key) {
426 return ((INode) context.getConstant(BuiltinKeys.INPUT)).getModifier(SimanticsUI.getSession(), key);
430 public int category(ReadGraph graph) throws DatabaseException {
431 return ((INode) context.getConstant(BuiltinKeys.INPUT)).getCategory(graph);
437 class NodeImagerFactory implements ImagerFactory {
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());
444 DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
446 source.schedule(g -> {
448 ImageDescriptor descriptor = ((INode)context.getConstant(BuiltinKeys.INPUT)).getImage(g);
449 result.setImage(descriptor);
450 updater.scheduleReplace(context, key, result);
451 } catch (DatabaseException e) {
461 class Node2ViewpointFactory implements ViewpointFactory {
464 public String toString() {
469 public Viewpoint create(final PrimitiveQueryUpdater updater, final NodeContext context, final ViewpointKey key) {
470 class V extends ViewpointStub implements Runnable, Supplier<Boolean> {
473 updater.scheduleReplace(context, key, V.this);
477 public Boolean get() {
478 return Boolean.valueOf(updater.isDisposed());
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);
488 return toContextsWithInput(children);
492 public Boolean getHasChildren() {
493 return ((INode2) context.getConstant(BuiltinKeys.INPUT)).hasChildren(this, context);
501 class Node2LabelerFactory implements LabelerFactory {
504 public Labeler create(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
506 class L extends LabelerStub implements Runnable {
508 public Modifier getModifier(ModificationContext sourcePart, String key) {
509 return ((INode2)context.getConstant(BuiltinKeys.INPUT)).getModifier(key);
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);
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)));
531 class Node2ImagerFactory implements ImagerFactory {
534 public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
535 assert(updater != null);
536 assert(context != null);
538 final ContainerImager<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();
540 Runnable callback = new Runnable() {
543 ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(this, context);
544 result.setImage(desc);
545 updater.scheduleReplace(context, key, result);
549 ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(callback, context);
550 result.setImage(desc);