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;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 * @author Hannu Niemistö
66 * @author Tuukka Lehtonen
69 public final class ModelEvaluators {
71 public static UnaryFunction<Boolean, Object> passTester() {
72 return new UnaryFunction<Boolean, Object>() {
74 public Boolean call(Object arg) {
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>() {
85 public Boolean call(Object arg) {
86 if(classSet.contains(arg.getClass())) return Boolean.TRUE;
92 public static UnaryFunction<Boolean, Object> classTester(final Class<?> ... classes) {
93 return new UnaryFunction<Boolean, Object>() {
95 public Boolean call(Object arg) {
96 for(Class<?> clazz : classes) {
97 if(clazz.isInstance(arg)) return Boolean.TRUE;
106 * @param resourceManager
107 * @param factoryHints
110 public static Evaluator createResourceEvaluator() {
111 Evaluator resourceEvaluator = new EvaluatorImpl();
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);
118 return resourceEvaluator;
123 * @param resourceManager
124 * @param factoryHints
128 public static Evaluator createResourceEvaluator(final UnaryFunction<Boolean, Object> tester) {
129 Evaluator resourceEvaluator = new EvaluatorImpl();
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);
136 return resourceEvaluator;
141 * @param resourceManager
144 public static Evaluator createNodeEvaluator() {
145 Evaluator nodeEvaluator = new EvaluatorImpl();
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);
152 return nodeEvaluator;
157 * @param resourceManager
161 public static Evaluator createNodeEvaluator(final UnaryFunction<Boolean, Object> tester) {
162 Evaluator nodeEvaluator = new EvaluatorImpl();
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);
169 return nodeEvaluator;
174 * @param resourceManager
177 public static Evaluator createNode2Evaluator() {
178 Evaluator nodeEvaluator = new EvaluatorImpl();
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);
185 return nodeEvaluator;
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);
197 public String toString() {
198 return BaseViewpointFactory.this.toString();
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();
212 public Boolean get() {
213 return Boolean.valueOf(updater.isDisposed());
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);
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;
233 abstract class BaseViewpointFactoryWithTester extends BaseViewpointFactory {
234 protected final UnaryFunction<Boolean, Object> tester;
236 BaseViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
237 this.tester = tester;
241 class ResourceViewpointFactory extends BaseViewpointFactory {
243 public String toString() {
244 return "Consists Of";
247 class VP extends VPB {
248 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
249 super(updater, context, key);
253 public NodeContext[] children(ReadGraph graph) throws DatabaseException {
254 return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
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) {
264 // node = g.adapt2(res, INode2.class));
265 // } catch (AdaptionException e) {
266 node = g.getPossibleAdapter(res, INode.class);
268 if (node instanceof IDisposable)
269 ((IDisposable) node).setDisposedCallable(this);
270 // if (node instanceof IUpdateable)
271 // ((IUpdateable) node).setChildrenCallback(this);
281 public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
282 return new VP(updater, context, key);
286 class ResourceViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
288 ResourceViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
293 public String toString() {
294 return "Consists Of";
297 class VP extends VPB {
298 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
299 super(updater, context, key);
303 public NodeContext[] children(ReadGraph graph) throws DatabaseException {
304 return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
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) {
314 // node = g.adapt2(res, INode2.class));
315 // } catch (AdaptionException e) {
316 node = g.getPossibleAdapter(res, INode.class);
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);
333 public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
334 return new VP(updater, context, key);
338 class NodeViewpointFactory extends BaseViewpointFactory {
340 public String toString() {
344 class VP extends VPB {
345 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
346 super(updater, context, key);
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);
355 Collection<?> children = node.getChildren(graph);
356 for (Object child : children) {
357 if (child instanceof IDisposable)
358 ((IDisposable) child).setDisposedCallable(this);
360 return toContextsWithInput(children);
365 public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
366 return new VP(updater, context, key);
370 class NodeViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
372 NodeViewpointFactoryWithTester(final UnaryFunction<Boolean, Object> tester) {
377 public String toString() {
381 class VP extends VPB {
382 public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
383 super(updater, context, key);
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);
392 ArrayList<Object> result = new ArrayList<Object>();
393 for (Object child : node.getChildren(graph)) {
394 if (tester.call(child)) {
396 if (child instanceof IDisposable)
397 ((IDisposable) child).setDisposedCallable(this);
400 return toContextsWithInput(result);
405 public Viewpoint create(PrimitiveQueryUpdater updater,NodeContext context, ViewpointKey key) {
406 return new VP(updater, context, key);
411 class NodeLabelerFactory implements LabelerFactory {
413 public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {
414 return new LazyGraphLabeler(updater, context, key) {
416 public Object getIdentity(LabelerKey key) {
417 return NodeLabelerFactory.this.getClass();
421 public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
422 return Collections.singletonMap(ColumnKeys.SINGLE,
423 ((INode) context.getConstant(BuiltinKeys.INPUT)).getLabel(graph));
427 public Modifier getModifier(ModificationContext sourcePart, String key) {
428 return ((INode) context.getConstant(BuiltinKeys.INPUT)).getModifier(SimanticsUI.getSession(), key);
432 public int category(ReadGraph graph) throws DatabaseException {
433 return ((INode) context.getConstant(BuiltinKeys.INPUT)).getCategory(graph);
437 public Logger getLogger() {
438 return LoggerFactory.getLogger(NodeLabelerFactory.class);
444 class NodeImagerFactory implements ImagerFactory {
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());
451 DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
453 source.schedule(g -> {
455 ImageDescriptor descriptor = ((INode)context.getConstant(BuiltinKeys.INPUT)).getImage(g);
456 result.setImage(descriptor);
457 updater.scheduleReplace(context, key, result);
458 } catch (DatabaseException e) {
468 class Node2ViewpointFactory implements ViewpointFactory {
471 public String toString() {
476 public Viewpoint create(final PrimitiveQueryUpdater updater, final NodeContext context, final ViewpointKey key) {
477 class V extends ViewpointStub implements Runnable, Supplier<Boolean> {
480 updater.scheduleReplace(context, key, V.this);
484 public Boolean get() {
485 return Boolean.valueOf(updater.isDisposed());
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);
495 return toContextsWithInput(children);
499 public Boolean getHasChildren() {
500 return ((INode2) context.getConstant(BuiltinKeys.INPUT)).hasChildren(this, context);
508 class Node2LabelerFactory implements LabelerFactory {
511 public Labeler create(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
513 class L extends LabelerStub implements Runnable {
515 public Modifier getModifier(ModificationContext sourcePart, String key) {
516 return ((INode2)context.getConstant(BuiltinKeys.INPUT)).getModifier(key);
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);
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)));
538 class Node2ImagerFactory implements ImagerFactory {
541 public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
542 assert(updater != null);
543 assert(context != null);
545 final ContainerImager<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();
547 Runnable callback = new Runnable() {
550 ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(this, context);
551 result.setImage(desc);
552 updater.scheduleReplace(context, key, result);
556 ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(callback, context);
557 result.setImage(desc);