--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 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.browsing.ui.swt;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.services.IServiceLocator;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.SelectionDataResolver;\r
+import org.simantics.browsing.ui.SelectionFilter;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.PossibleTypedParent;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.datastructures.BinaryFunction;\r
+import org.simantics.utils.datastructures.hints.IHintContext;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class GraphExplorerFactory {\r
+\r
+ private int maxChildrenShown = GraphExplorerImpl.DEFAULT_MAX_CHILDREN;\r
+\r
+ private SelectionDataResolver selectionDataResolver;\r
+ private SelectionFilter selectionFilter;\r
+\r
+ private IServiceLocator serviceLocator;\r
+\r
+ private BinaryFunction<Object[], GraphExplorer, Object[]> selectionTransformation = new BinaryFunction<Object[], GraphExplorer, Object[]>() {\r
+\r
+ private Resource getModel(final Object object) {\r
+ if(object instanceof NodeContext) {\r
+ NodeContext context = (NodeContext)object;\r
+ Object input = context.getConstant(BuiltinKeys.INPUT);\r
+ if(input instanceof Resource) {\r
+ final Resource inputResource = (Resource)input;\r
+ try {\r
+ return Simantics.getSession().sync(new UniqueRead<Resource>() {\r
+\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ return graph.sync(new PossibleTypedParent(inputResource, SIMU.Model));\r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+ } else if (input instanceof Variable) {\r
+ final Variable inputVariable = (Variable)input;\r
+ try {\r
+ return Simantics.getSession().sync(new UniqueRead<Resource>() {\r
+\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ return Variables.getModel(graph, inputVariable);\r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ \r
+ @Override\r
+ public Object[] call(GraphExplorer explorer, Object[] objects) {\r
+ Object[] result = new Object[objects.length];\r
+ for (int i = 0; i < objects.length; i++) {\r
+ IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);\r
+ context.setHint(SelectionHints.KEY_MAIN, objects[i]);\r
+ Resource model = getModel(objects[i]);\r
+ if(model != null)\r
+ context.setHint(SelectionHints.KEY_MODEL, model);\r
+ result[i] = context;\r
+ }\r
+ return result;\r
+ }\r
+\r
+ };\r
+\r
+ public static GraphExplorerFactory getInstance() {\r
+ return new GraphExplorerFactory();\r
+ }\r
+\r
+ /**\r
+ * @param n\r
+ * @return this instance\r
+ */\r
+ public GraphExplorerFactory maxChildrenShown(int n) {\r
+ this.maxChildrenShown = n;\r
+ return this;\r
+ }\r
+\r
+ public GraphExplorerFactory selectionTransformation(BinaryFunction<Object[], GraphExplorer, Object[]> transformation) {\r
+ this.selectionTransformation = transformation;\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * @param r\r
+ * @return this instance\r
+ */\r
+ public GraphExplorerFactory selectionDataResolver(SelectionDataResolver r) {\r
+ this.selectionDataResolver = r;\r
+ return this;\r
+ }\r
+\r
+ public GraphExplorerFactory setSelectionFilter(SelectionFilter filter) {\r
+ this.selectionFilter = filter;\r
+ return this;\r
+ }\r
+\r
+ public GraphExplorerFactory setServiceLocator(IServiceLocator serviceLocator) {\r
+ this.serviceLocator = serviceLocator;\r
+ return this;\r
+ }\r
+\r
+ public GraphExplorer create(Composite parent) {\r
+ return create(parent, 0);\r
+ }\r
+\r
+ /**\r
+ * @param site\r
+ * @param parent\r
+ * @param style SWT style hints for the explorer tree control to create\r
+ * @return\r
+ */\r
+ public GraphExplorer create(Composite parent, int style) {\r
+ return createWithStyle(parent, style | SWT.VIRTUAL);\r
+ //return createWithStyle(parent, style);\r
+ }\r
+\r
+ private static class GraphExplorerWithMaxChildren extends GraphExplorerImpl {\r
+// private int maxChildrenShown;\r
+ GraphExplorerWithMaxChildren(Composite parent, int style, int maxChildrenShown) {\r
+ super(parent, style);\r
+ setMaxChildren(maxChildrenShown);\r
+// this.maxChildrenShown = maxChildrenShown;\r
+ }\r
+// @Override\r
+// public int getMaxChildren() {\r
+// return maxChildrenShown;\r
+// }\r
+ }\r
+ \r
+ public GraphExplorer createWithStyle(Composite parent, int style) {\r
+ GraphExplorerImpl explorer = new GraphExplorerWithMaxChildren(parent, style, maxChildrenShown);\r
+ explorer.setSelectionDataResolver(selectionDataResolver);\r
+ explorer.setSelectionFilter(selectionFilter);\r
+ explorer.setSelectionTransformation(selectionTransformation);\r
+ explorer.setServiceLocator(serviceLocator);\r
+ return explorer;\r
+ }\r
+ \r
+ public GraphExplorer create2(Composite parent, int style) {\r
+ GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);\r
+ explorer.setSelectionDataResolver(selectionDataResolver);\r
+ explorer.setSelectionFilter(selectionFilter);\r
+ explorer.setSelectionTransformation(selectionTransformation);\r
+ explorer.setServiceLocator(serviceLocator);\r
+ return explorer;\r
+ }\r
+\r
+// void hookActions(IWorkbenchSite site) {\r
+// IActionBars actionBars = null;\r
+//\r
+// if (site instanceof IEditorSite)\r
+// actionBars = ((IEditorSite) site).getActionBars();\r
+// if (site instanceof IViewSite)\r
+// actionBars = ((IViewSite) site).getActionBars();\r
+// if (site instanceof IPageSite)\r
+// actionBars = ((IPageSite) site).getActionBars();\r
+//\r
+// if (actionBars != null) {\r
+// actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), new Action() {\r
+// @Override\r
+// public void run() {\r
+// System.out.println("SELECT_ALL");\r
+// }\r
+// });\r
+// actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), new Action() {\r
+// @Override\r
+// public void run() {\r
+// System.out.println("FIND");\r
+// }\r
+// });\r
+// actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), new Action() {\r
+// @Override\r
+// public void run() {\r
+// System.out.println("UNDO");\r
+// // SimanticsUI.undo();\r
+// }\r
+// });\r
+// actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), new Action() {\r
+// @Override\r
+// public void run() {\r
+// System.out.println("REDO");\r
+// // SimanticsUI.redo();\r
+// }\r
+// });\r
+// actionBars.updateActionBars();\r
+// }\r
+// }\r
+\r
+}\r