-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.modelBrowser;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.function.Supplier;\r
-\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.simantics.browsing.ui.BuiltinKeys;\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.DataSource;\r
-import org.simantics.browsing.ui.GraphExplorer.ModificationContext;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
-import org.simantics.browsing.ui.common.ColumnKeys;\r
-import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;\r
-import org.simantics.browsing.ui.common.EvaluatorImpl;\r
-import org.simantics.browsing.ui.common.comparators.AlphanumericComparatorFactory;\r
-import org.simantics.browsing.ui.common.imagers.ContainerImager;\r
-import org.simantics.browsing.ui.common.labelers.LabelerContent;\r
-import org.simantics.browsing.ui.common.labelers.LabelerStub;\r
-import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;\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.browsing.ui.graph.impl.StringRepresentationLabelerFactory;\r
-import org.simantics.browsing.ui.swt.OldAdapterImagerFactory;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\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.INode;\r
-import org.simantics.modeling.ui.modelBrowser.model.INode2;\r
-import org.simantics.modeling.ui.modelBrowser.model.IUpdateable;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.datastructures.UnaryFunction;\r
-\r
-/**\r
- * @author Hannu Niemistö\r
- * @author Tuukka Lehtonen\r
- */\r
-@Deprecated\r
-public final class ModelEvaluators {\r
-\r
- public static UnaryFunction<Boolean, Object> passTester() {\r
- return new UnaryFunction<Boolean, Object>() {\r
- @Override\r
- public Boolean call(Object arg) {\r
- return Boolean.TRUE;\r
- }\r
- };\r
- }\r
-\r
- public static UnaryFunction<Boolean, Object> exactClassTester(final Class<?> ... classes) {\r
- final Set<Class<?>> classSet = new HashSet<Class<?>>();\r
- for(Class<?> clazz : classes) classSet.add(clazz);\r
- return new UnaryFunction<Boolean, Object>() {\r
- @Override\r
- public Boolean call(Object arg) {\r
- if(classSet.contains(arg.getClass())) return Boolean.TRUE;\r
- return Boolean.FALSE;\r
- }\r
- };\r
- }\r
-\r
- public static UnaryFunction<Boolean, Object> classTester(final Class<?> ... classes) {\r
- return new UnaryFunction<Boolean, Object>() {\r
- @Override\r
- public Boolean call(Object arg) {\r
- for(Class<?> clazz : classes) {\r
- if(clazz.isInstance(arg)) return Boolean.TRUE;\r
- }\r
- return Boolean.FALSE;\r
- }\r
- };\r
- }\r
-\r
- /**\r
- * @param session\r
- * @param resourceManager\r
- * @param factoryHints\r
- * @return\r
- */\r
- public static Evaluator createResourceEvaluator() {\r
- Evaluator resourceEvaluator = new EvaluatorImpl();\r
-\r
- resourceEvaluator.addViewpoint(new ResourceViewpointFactory(), 1.0);\r
- resourceEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);\r
- resourceEvaluator.addLabeler(new StringRepresentationLabelerFactory(), 1.0);\r
- resourceEvaluator.addImager(new OldAdapterImagerFactory(), 1.0);\r
-\r
- return resourceEvaluator;\r
- }\r
-\r
- /**\r
- * @param session\r
- * @param resourceManager\r
- * @param factoryHints\r
- * @param tester\r
- * @return\r
- */\r
- public static Evaluator createResourceEvaluator(final UnaryFunction<Boolean, Object> tester) {\r
- Evaluator resourceEvaluator = new EvaluatorImpl();\r
-\r
- resourceEvaluator.addViewpoint(new ResourceViewpointFactoryWithTester(tester), 1.0);\r
- resourceEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);\r
- resourceEvaluator.addLabeler(new StringRepresentationLabelerFactory(), 1.0);\r
- resourceEvaluator.addImager(new OldAdapterImagerFactory(), 1.0);\r
-\r
- return resourceEvaluator;\r
- }\r
-\r
- /**\r
- * @param session\r
- * @param resourceManager\r
- * @return\r
- */\r
- public static Evaluator createNodeEvaluator() {\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
- * @param session\r
- * @param resourceManager\r
- * @param tester\r
- * @return\r
- */\r
- public static Evaluator createNodeEvaluator(final UnaryFunction<Boolean, Object> tester) {\r
- Evaluator nodeEvaluator = new EvaluatorImpl();\r
-\r
- nodeEvaluator.addViewpoint(new NodeViewpointFactoryWithTester(tester), 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
- * @param session\r
- * @param resourceManager\r
- * @return\r
- */\r
- public static Evaluator createNode2Evaluator() {\r
- Evaluator nodeEvaluator = new EvaluatorImpl();\r
-\r
- nodeEvaluator.addViewpoint(new Node2ViewpointFactory(), 1.0);\r
- nodeEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);\r
- nodeEvaluator.addLabeler(new Node2LabelerFactory(), 1.0);\r
- nodeEvaluator.addImager(new Node2ImagerFactory(), 1.0);\r
-\r
- return nodeEvaluator;\r
- }\r
-\r
-}\r
-\r
-abstract class BaseViewpointFactory implements ViewpointFactory {\r
- protected abstract class VPB extends LazyViewpoint implements Supplier<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 get() {\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(updater, 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
-abstract class BaseViewpointFactoryWithTester extends BaseViewpointFactory {\r
- protected final UnaryFunction<Boolean, Object> tester;\r
-\r
- BaseViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {\r
- this.tester = tester;\r
- }\r
-}\r
-\r
-class ResourceViewpointFactory extends BaseViewpointFactory {\r
- @Override\r
- public String toString() {\r
- return "Consists Of";\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
- return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );\r
- }\r
-\r
- protected Collection<?> getChildren(ReadGraph g, Resource r) throws DatabaseException {\r
- Layer0 b = Layer0.getInstance(g);\r
- Collection<Resource> resources = g.getObjects(r, b.ConsistsOf);\r
- ArrayList<Object> ret = new ArrayList<Object>(resources.size());\r
- for (Resource res : resources) {\r
- Object node = null;\r
-// try {\r
-// node = g.adapt2(res, INode2.class));\r
-// } catch (AdaptionException e) {\r
- node = g.getPossibleAdapter(res, INode.class);\r
- if (node != null) {\r
- if (node instanceof IDisposable)\r
- ((IDisposable) node).setDisposedCallable(this);\r
-// if (node instanceof IUpdateable)\r
-// ((IUpdateable) node).setChildrenCallback(this);\r
- ret.add(node);\r
-// }\r
- }\r
- }\r
- return ret;\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 ResourceViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {\r
-\r
- ResourceViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {\r
- super(tester);\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return "Consists Of";\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
- return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );\r
- }\r
-\r
- protected Collection<?> getChildren(ReadGraph g, Resource r) throws DatabaseException {\r
- Layer0 b = Layer0.getInstance(g);\r
- Collection<Resource> resources = g.getObjects(r, b.ConsistsOf);\r
- ArrayList<Object> ret = new ArrayList<Object>(resources.size());\r
- for (Resource res : resources) {\r
- Object node = null;\r
-// try {\r
-// node = g.adapt2(res, INode2.class));\r
-// } catch (AdaptionException e) {\r
- node = g.getPossibleAdapter(res, INode.class);\r
- if (node != null) {\r
- if (tester.call(node)) {\r
- if (node instanceof IDisposable)\r
- ((IDisposable) node).setDisposedCallable(this);\r
-// if (node instanceof IUpdateable)\r
-// ((IUpdateable) node).setChildrenCallback(this);\r
- ret.add(node);\r
- }\r
- }\r
-// }\r
- }\r
- return ret;\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 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 NodeViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {\r
-\r
- NodeViewpointFactoryWithTester(final UnaryFunction<Boolean, Object> tester) {\r
- super(tester);\r
- }\r
-\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
- ArrayList<Object> result = new ArrayList<Object>();\r
- for (Object child : node.getChildren(graph)) {\r
- if (tester.call(child)) {\r
- result.add(child);\r
- if (child instanceof IDisposable)\r
- ((IDisposable) child).setDisposedCallable(this);\r
- }\r
- }\r
- return toContextsWithInput(result);\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
-\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(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
- return result;\r
- }\r
-\r
-}\r
-\r
-class Node2ViewpointFactory implements ViewpointFactory {\r
-\r
- @Override\r
- public String toString() {\r
- return "Standard";\r
- }\r
-\r
- @Override\r
- public Viewpoint create(final PrimitiveQueryUpdater updater, final NodeContext context, final ViewpointKey key) {\r
- class V extends ViewpointStub implements Runnable, Supplier<Boolean> {\r
- @Override\r
- public void run() {\r
- updater.scheduleReplace(context, key, V.this);\r
- }\r
-\r
- @Override\r
- public Boolean get() {\r
- return Boolean.valueOf(updater.isDisposed());\r
- }\r
-\r
- @Override\r
- public NodeContext[] getChildren() {\r
- Collection<?> children = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getChildren(this, context);\r
- for (Object child : children) {\r
- if (child instanceof IDisposable)\r
- ((IDisposable) child).setDisposedCallable(this);\r
- }\r
- return toContextsWithInput(children);\r
- }\r
-\r
- @Override\r
- public Boolean getHasChildren() {\r
- return ((INode2) context.getConstant(BuiltinKeys.INPUT)).hasChildren(this, context);\r
- }\r
- }\r
-\r
- return new V();\r
- }\r
-}\r
-\r
-class Node2LabelerFactory implements LabelerFactory {\r
-\r
- @Override\r
- public Labeler create(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {\r
-\r
- class L extends LabelerStub implements Runnable {\r
- @Override\r
- public Modifier getModifier(ModificationContext sourcePart, String key) {\r
- return ((INode2)context.getConstant(BuiltinKeys.INPUT)).getModifier(key);\r
- }\r
- @Override\r
- public void run() {\r
- String label = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getLabel(this, context);\r
- int category = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getCategory(this, context);\r
- setContent(new LabelerContent(category, Collections.singletonMap(ColumnKeys.SINGLE, label)));\r
- updater.scheduleReplace(context, key, this);\r
- }\r
- };\r
-\r
- L result = new L();\r
-\r
- String label = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getLabel(result, context);\r
- int category = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getCategory(result, context);\r
- result.setContent(new LabelerContent(category, Collections.singletonMap(ColumnKeys.SINGLE, label)));\r
-\r
- return result;\r
- }\r
-\r
-}\r
-\r
-class Node2ImagerFactory implements ImagerFactory {\r
-\r
- @Override\r
- public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {\r
- assert(updater != null);\r
- assert(context != null);\r
-\r
- final ContainerImager<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();\r
-\r
- Runnable callback = new Runnable() {\r
- @Override\r
- public void run() {\r
- ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(this, context);\r
- result.setImage(desc);\r
- updater.scheduleReplace(context, key, result);\r
- }\r
- };\r
-\r
- ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(callback, context);\r
- result.setImage(desc);\r
- return result;\r
-\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.modelBrowser;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.simantics.Simantics;
+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.GraphExplorer.ModificationContext;
+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.common.labelers.LabelerContent;
+import org.simantics.browsing.ui.common.labelers.LabelerStub;
+import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;
+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.browsing.ui.graph.impl.StringRepresentationLabelerFactory;
+import org.simantics.browsing.ui.swt.OldAdapterImagerFactory;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ui.modelBrowser.model.IChildrenCallback;
+import org.simantics.modeling.ui.modelBrowser.model.IDisposable;
+import org.simantics.modeling.ui.modelBrowser.model.INode;
+import org.simantics.modeling.ui.modelBrowser.model.INode2;
+import org.simantics.modeling.ui.modelBrowser.model.IUpdateable;
+import org.simantics.utils.datastructures.UnaryFunction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Hannu Niemistö
+ * @author Tuukka Lehtonen
+ */
+@Deprecated
+public final class ModelEvaluators {
+
+ public static UnaryFunction<Boolean, Object> passTester() {
+ return new UnaryFunction<Boolean, Object>() {
+ @Override
+ public Boolean call(Object arg) {
+ return Boolean.TRUE;
+ }
+ };
+ }
+
+ public static UnaryFunction<Boolean, Object> exactClassTester(final Class<?> ... classes) {
+ final Set<Class<?>> classSet = new HashSet<Class<?>>();
+ for(Class<?> clazz : classes) classSet.add(clazz);
+ return new UnaryFunction<Boolean, Object>() {
+ @Override
+ public Boolean call(Object arg) {
+ if(classSet.contains(arg.getClass())) return Boolean.TRUE;
+ return Boolean.FALSE;
+ }
+ };
+ }
+
+ public static UnaryFunction<Boolean, Object> classTester(final Class<?> ... classes) {
+ return new UnaryFunction<Boolean, Object>() {
+ @Override
+ public Boolean call(Object arg) {
+ for(Class<?> clazz : classes) {
+ if(clazz.isInstance(arg)) return Boolean.TRUE;
+ }
+ return Boolean.FALSE;
+ }
+ };
+ }
+
+ /**
+ * @param session
+ * @param resourceManager
+ * @param factoryHints
+ * @return
+ */
+ public static Evaluator createResourceEvaluator() {
+ Evaluator resourceEvaluator = new EvaluatorImpl();
+
+ resourceEvaluator.addViewpoint(new ResourceViewpointFactory(), 1.0);
+ resourceEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
+ resourceEvaluator.addLabeler(new StringRepresentationLabelerFactory(), 1.0);
+ resourceEvaluator.addImager(new OldAdapterImagerFactory(), 1.0);
+
+ return resourceEvaluator;
+ }
+
+ /**
+ * @param session
+ * @param resourceManager
+ * @param factoryHints
+ * @param tester
+ * @return
+ */
+ public static Evaluator createResourceEvaluator(final UnaryFunction<Boolean, Object> tester) {
+ Evaluator resourceEvaluator = new EvaluatorImpl();
+
+ resourceEvaluator.addViewpoint(new ResourceViewpointFactoryWithTester(tester), 1.0);
+ resourceEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
+ resourceEvaluator.addLabeler(new StringRepresentationLabelerFactory(), 1.0);
+ resourceEvaluator.addImager(new OldAdapterImagerFactory(), 1.0);
+
+ return resourceEvaluator;
+ }
+
+ /**
+ * @param session
+ * @param resourceManager
+ * @return
+ */
+ public static Evaluator createNodeEvaluator() {
+ 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;
+ }
+
+ /**
+ * @param session
+ * @param resourceManager
+ * @param tester
+ * @return
+ */
+ public static Evaluator createNodeEvaluator(final UnaryFunction<Boolean, Object> tester) {
+ Evaluator nodeEvaluator = new EvaluatorImpl();
+
+ nodeEvaluator.addViewpoint(new NodeViewpointFactoryWithTester(tester), 1.0);
+ nodeEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
+ nodeEvaluator.addLabeler(new NodeLabelerFactory(), 1.0);
+ nodeEvaluator.addImager(new NodeImagerFactory(), 1.0);
+
+ return nodeEvaluator;
+ }
+
+ /**
+ * @param session
+ * @param resourceManager
+ * @return
+ */
+ public static Evaluator createNode2Evaluator() {
+ Evaluator nodeEvaluator = new EvaluatorImpl();
+
+ nodeEvaluator.addViewpoint(new Node2ViewpointFactory(), 1.0);
+ nodeEvaluator.addComparator(new AlphanumericComparatorFactory(ColumnKeys.SINGLE), 2.0);
+ nodeEvaluator.addLabeler(new Node2LabelerFactory(), 1.0);
+ nodeEvaluator.addImager(new Node2ImagerFactory(), 1.0);
+
+ return nodeEvaluator;
+ }
+
+}
+
+abstract class BaseViewpointFactory implements ViewpointFactory {
+ protected abstract class VPB extends LazyViewpoint implements Supplier<Boolean>, IChildrenCallback {
+ public VPB(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
+ super(updater, context, key);
+ }
+
+ @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;
+ }
+ };
+}
+
+abstract class BaseViewpointFactoryWithTester extends BaseViewpointFactory {
+ protected final UnaryFunction<Boolean, Object> tester;
+
+ BaseViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
+ this.tester = tester;
+ }
+}
+
+class ResourceViewpointFactory extends BaseViewpointFactory {
+ @Override
+ public String toString() {
+ return "Consists Of";
+ }
+
+ class VP extends VPB {
+ public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
+ super(updater, context, key);
+ }
+
+ @Override
+ public NodeContext[] children(ReadGraph graph) throws DatabaseException {
+ return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
+ }
+
+ protected Collection<?> getChildren(ReadGraph g, Resource r) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(g);
+ Collection<Resource> resources = g.getObjects(r, b.ConsistsOf);
+ ArrayList<Object> ret = new ArrayList<Object>(resources.size());
+ for (Resource res : resources) {
+ Object node = null;
+// try {
+// node = g.adapt2(res, INode2.class));
+// } catch (AdaptionException e) {
+ node = g.getPossibleAdapter(res, INode.class);
+ if (node != null) {
+ if (node instanceof IDisposable)
+ ((IDisposable) node).setDisposedCallable(this);
+// if (node instanceof IUpdateable)
+// ((IUpdateable) node).setChildrenCallback(this);
+ ret.add(node);
+// }
+ }
+ }
+ return ret;
+ }
+ };
+
+ @Override
+ public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
+ return new VP(updater, context, key);
+ }
+}
+
+class ResourceViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
+
+ ResourceViewpointFactoryWithTester(UnaryFunction<Boolean, Object> tester) {
+ super(tester);
+ }
+
+ @Override
+ public String toString() {
+ return "Consists Of";
+ }
+
+ class VP extends VPB {
+ public VP(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
+ super(updater, context, key);
+ }
+
+ @Override
+ public NodeContext[] children(ReadGraph graph) throws DatabaseException {
+ return toContextsWithInput( getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)) );
+ }
+
+ protected Collection<?> getChildren(ReadGraph g, Resource r) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(g);
+ Collection<Resource> resources = g.getObjects(r, b.ConsistsOf);
+ ArrayList<Object> ret = new ArrayList<Object>(resources.size());
+ for (Resource res : resources) {
+ Object node = null;
+// try {
+// node = g.adapt2(res, INode2.class));
+// } catch (AdaptionException e) {
+ node = g.getPossibleAdapter(res, INode.class);
+ if (node != null) {
+ if (tester.call(node)) {
+ if (node instanceof IDisposable)
+ ((IDisposable) node).setDisposedCallable(this);
+// if (node instanceof IUpdateable)
+// ((IUpdateable) node).setChildrenCallback(this);
+ ret.add(node);
+ }
+ }
+// }
+ }
+ return ret;
+ }
+ };
+
+ @Override
+ public Viewpoint create(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
+ return new VP(updater, context, key);
+ }
+}
+
+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 NodeViewpointFactoryWithTester extends BaseViewpointFactoryWithTester {
+
+ NodeViewpointFactoryWithTester(final UnaryFunction<Boolean, Object> tester) {
+ super(tester);
+ }
+
+ @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);
+
+ ArrayList<Object> result = new ArrayList<Object>();
+ for (Object child : node.getChildren(graph)) {
+ if (tester.call(child)) {
+ result.add(child);
+ if (child instanceof IDisposable)
+ ((IDisposable) child).setDisposedCallable(this);
+ }
+ }
+ return toContextsWithInput(result);
+ }
+ }
+
+ @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<String, String> labels(ReadGraph graph) throws DatabaseException {
+ return Collections.singletonMap(ColumnKeys.SINGLE,
+ ((INode) context.getConstant(BuiltinKeys.INPUT)).getLabel(graph));
+ }
+
+ @Override
+ public Modifier getModifier(ModificationContext sourcePart, String key) {
+ return ((INode) context.getConstant(BuiltinKeys.INPUT)).getModifier(Simantics.getSession(), key);
+ }
+
+ @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<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();
+ result.setImage(MissingImageDescriptor.getInstance());
+
+ DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+
+ source.schedule(g -> {
+ try {
+ ImageDescriptor descriptor = ((INode)context.getConstant(BuiltinKeys.INPUT)).getImage(g);
+ result.setImage(descriptor);
+ updater.scheduleReplace(context, key, result);
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ }
+ });
+
+ return result;
+ }
+
+}
+
+class Node2ViewpointFactory implements ViewpointFactory {
+
+ @Override
+ public String toString() {
+ return "Standard";
+ }
+
+ @Override
+ public Viewpoint create(final PrimitiveQueryUpdater updater, final NodeContext context, final ViewpointKey key) {
+ class V extends ViewpointStub implements Runnable, Supplier<Boolean> {
+ @Override
+ public void run() {
+ updater.scheduleReplace(context, key, V.this);
+ }
+
+ @Override
+ public Boolean get() {
+ return Boolean.valueOf(updater.isDisposed());
+ }
+
+ @Override
+ public NodeContext[] getChildren() {
+ Collection<?> children = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getChildren(this, context);
+ for (Object child : children) {
+ if (child instanceof IDisposable)
+ ((IDisposable) child).setDisposedCallable(this);
+ }
+ return toContextsWithInput(children);
+ }
+
+ @Override
+ public Boolean getHasChildren() {
+ return ((INode2) context.getConstant(BuiltinKeys.INPUT)).hasChildren(this, context);
+ }
+ }
+
+ return new V();
+ }
+}
+
+class Node2LabelerFactory implements LabelerFactory {
+
+ @Override
+ public Labeler create(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
+
+ class L extends LabelerStub implements Runnable {
+ @Override
+ public Modifier getModifier(ModificationContext sourcePart, String key) {
+ return ((INode2)context.getConstant(BuiltinKeys.INPUT)).getModifier(key);
+ }
+ @Override
+ public void run() {
+ String label = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getLabel(this, context);
+ int category = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getCategory(this, context);
+ setContent(new LabelerContent(category, Collections.singletonMap(ColumnKeys.SINGLE, label)));
+ updater.scheduleReplace(context, key, this);
+ }
+ };
+
+ L result = new L();
+
+ String label = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getLabel(result, context);
+ int category = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getCategory(result, context);
+ result.setContent(new LabelerContent(category, Collections.singletonMap(ColumnKeys.SINGLE, label)));
+
+ return result;
+ }
+
+}
+
+class Node2ImagerFactory implements ImagerFactory {
+
+ @Override
+ public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
+ assert(updater != null);
+ assert(context != null);
+
+ final ContainerImager<ImageDescriptor> result = new ContainerImager<ImageDescriptor>();
+
+ Runnable callback = new Runnable() {
+ @Override
+ public void run() {
+ ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(this, context);
+ result.setImage(desc);
+ updater.scheduleReplace(context, key, result);
+ }
+ };
+
+ ImageDescriptor desc = ((INode2) context.getConstant(BuiltinKeys.INPUT)).getImage(callback, context);
+ result.setImage(desc);
+ return result;
+
+ }
+
+}