--- /dev/null
+package org.simantics.interop.browsing;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Map;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.DataSource;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;\r
+import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;\r
+import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;\r
+import org.simantics.browsing.ui.GraphExplorer.ModificationContext;\r
+import org.simantics.browsing.ui.common.ColumnKeys;\r
+import org.simantics.browsing.ui.common.EvaluatorImpl;\r
+import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;\r
+import org.simantics.browsing.ui.common.comparators.AlphanumericComparatorFactory;\r
+import org.simantics.browsing.ui.common.imagers.ContainerImager;\r
+import org.simantics.browsing.ui.content.Imager;\r
+import org.simantics.browsing.ui.content.ImagerFactory;\r
+import org.simantics.browsing.ui.content.Labeler;\r
+import org.simantics.browsing.ui.content.LabelerFactory;\r
+import org.simantics.browsing.ui.content.Viewpoint;\r
+import org.simantics.browsing.ui.content.ViewpointFactory;\r
+import org.simantics.browsing.ui.graph.impl.LazyGraphLabeler;\r
+import org.simantics.browsing.ui.graph.impl.LazyViewpoint;\r
+import org.simantics.browsing.ui.graph.impl.MissingImageDescriptor;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.modeling.ui.modelBrowser.model.IChildrenCallback;\r
+import org.simantics.modeling.ui.modelBrowser.model.IDisposable;\r
+import org.simantics.modeling.ui.modelBrowser.model.IUpdateable;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.Callable;\r
+import org.simantics.utils.datastructures.Callback;\r
+\r
+\r
+public class INodeEvaluators {\r
+ public static Evaluator createEvaluator() {\r
+ Evaluator nodeEvaluator = new EvaluatorImpl();\r
+\r
+ nodeEvaluator.addViewpoint(new NodeViewpointFactory(), 1.0);\r
+ nodeEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);\r
+ nodeEvaluator.addLabeler(new NodeLabelerFactory(), 1.0);\r
+ nodeEvaluator.addImager(new NodeImagerFactory(), 1.0);\r
+ \r
+ return nodeEvaluator;\r
+ }\r
+}\r
+\r
+abstract class BaseViewpointFactory implements ViewpointFactory {\r
+ protected abstract class VPB extends LazyViewpoint implements Callable<Boolean>, IChildrenCallback {\r
+ public VPB(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {\r
+ super(updater, context, key);\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return BaseViewpointFactory.this.toString();\r
+ }\r
+\r
+ @Override\r
+ public Object getIdentity() {\r
+ // This is necessary to give graph requests related to this\r
+ // LazyViewpoint a unique-enough identity so that they don't collide\r
+ // unexpectedly with other users of ModelEvaluators.\r
+ // This makes requests created with different concrete classes of\r
+ // BaseViewpointFactory unique.\r
+ return BaseViewpointFactory.this.getClass();\r
+ }\r
+\r
+ @Override\r
+ public Boolean call() {\r
+ return Boolean.valueOf(updater.isDisposed());\r
+ }\r
+\r
+ @Override\r
+ public void refreshChildren(Collection<?> newChildren) {\r
+ NodeContext[] ncs = toContextsWithInput(newChildren);\r
+ setHasChildren(ncs.length > 0);\r
+ setChildren(ncs);\r
+ updater.scheduleReplace(context, key, this);\r
+ }\r
+\r
+ @Override\r
+ public Boolean hasChildren(ReadGraph graph) throws DatabaseException {\r
+ // hasChildren must do the same graph operations as children\r
+ // since they both share the same PrimitiveQueryUpdater.\r
+ return children(graph).length > 0;\r
+ }\r
+ };\r
+}\r
+\r
+class NodeViewpointFactory extends BaseViewpointFactory {\r
+ @Override\r
+ public String toString() {\r
+ return "Standard";\r
+ }\r
+\r
+ class VP extends VPB {\r
+ public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {\r
+ super(updater, context, key);\r
+ }\r
+\r
+ @Override\r
+ public NodeContext[] children(ReadGraph graph) throws DatabaseException {\r
+ INode node = (INode) context.getConstant(BuiltinKeys.INPUT);\r
+ if (node instanceof IUpdateable)\r
+ ((IUpdateable) node).setChildrenCallback(this);\r
+\r
+ Collection<?> children = node.getChildren(graph);\r
+ for (Object child : children) {\r
+ if (child instanceof IDisposable)\r
+ ((IDisposable) child).setDisposedCallable(this);\r
+ }\r
+ return toContextsWithInput(children);\r
+ }\r
+ };\r
+\r
+ @Override\r
+ public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {\r
+ return new VP(updater, context, key);\r
+ }\r
+}\r
+\r
+class NodeLabelerFactory implements LabelerFactory {\r
+ @Override\r
+ public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {\r
+ return new LazyGraphLabeler(updater, context, key) {\r
+ @Override\r
+ public Object getIdentity(LabelerKey key) {\r
+ return NodeLabelerFactory.this.getClass();\r
+ }\r
+\r
+ @Override\r
+ public Map<String, String> labels(ReadGraph graph) throws DatabaseException {\r
+ return Collections.singletonMap(ColumnKeys.SINGLE,\r
+ ((INode) context.getConstant(BuiltinKeys.INPUT)).getLabel(graph));\r
+ }\r
+\r
+ @Override\r
+ public Modifier getModifier(ModificationContext sourcePart, String key) {\r
+ return ((INode) context.getConstant(BuiltinKeys.INPUT)).getModifier(SimanticsUI.getSession(), key);\r
+ }\r
+\r
+ @Override\r
+ public int category(ReadGraph graph) throws DatabaseException {\r
+ return ((INode) context.getConstant(BuiltinKeys.INPUT)).getCategory(graph);\r
+ }\r
+ };\r
+ }\r
+}\r
+\r
+class NodeImagerFactory implements ImagerFactory {\r
+\r
+ @Override\r
+ public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {\r
+ final ContainerImager<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();\r
+ result.setImage(MissingImageDescriptor.getInstance());\r
+\r
+ DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
+\r
+ source.schedule(new Callback<ReadGraph>() {\r
+ @Override\r
+ public void run(ReadGraph g) {\r
+ try {\r
+ ImageDescriptor descriptor = ((INode)context.getConstant(BuiltinKeys.INPUT)).getImage(g);\r
+ result.setImage(descriptor);\r
+ updater.scheduleReplace(context, key, result);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ });\r
+\r
+ return result;\r
+ }\r
+\r
+}
\ No newline at end of file