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