1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\r
14 import org.eclipse.swt.SWT;
\r
15 import org.eclipse.swt.widgets.Composite;
\r
16 import org.eclipse.ui.services.IServiceLocator;
\r
17 import org.simantics.Simantics;
\r
18 import org.simantics.browsing.ui.BuiltinKeys;
\r
19 import org.simantics.browsing.ui.GraphExplorer;
\r
20 import org.simantics.browsing.ui.NodeContext;
\r
21 import org.simantics.browsing.ui.SelectionDataResolver;
\r
22 import org.simantics.browsing.ui.SelectionFilter;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.common.request.PossibleTypedParent;
\r
26 import org.simantics.db.common.request.UniqueRead;
\r
27 import org.simantics.db.common.utils.Logger;
\r
28 import org.simantics.db.exception.DatabaseException;
\r
29 import org.simantics.db.layer0.SelectionHints;
\r
30 import org.simantics.db.layer0.variable.Variable;
\r
31 import org.simantics.db.layer0.variable.Variables;
\r
32 import org.simantics.simulation.ontology.SimulationResource;
\r
33 import org.simantics.utils.datastructures.BinaryFunction;
\r
34 import org.simantics.utils.datastructures.hints.IHintContext;
\r
37 * @author Tuukka Lehtonen
\r
39 public class GraphExplorerFactory {
\r
41 private int maxChildrenShown = GraphExplorerImpl.DEFAULT_MAX_CHILDREN;
\r
43 private SelectionDataResolver selectionDataResolver;
\r
44 private SelectionFilter selectionFilter;
\r
46 private IServiceLocator serviceLocator;
\r
48 private BinaryFunction<Object[], GraphExplorer, Object[]> selectionTransformation = new BinaryFunction<Object[], GraphExplorer, Object[]>() {
\r
50 private Resource getModel(final Object object) {
\r
51 if(object instanceof NodeContext) {
\r
52 NodeContext context = (NodeContext)object;
\r
53 Object input = context.getConstant(BuiltinKeys.INPUT);
\r
54 if(input instanceof Resource) {
\r
55 final Resource inputResource = (Resource)input;
\r
57 return Simantics.getSession().sync(new UniqueRead<Resource>() {
\r
60 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
61 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
62 return graph.sync(new PossibleTypedParent(inputResource, SIMU.Model));
\r
66 } catch (DatabaseException e) {
\r
67 Logger.defaultLogError(e);
\r
69 } else if (input instanceof Variable) {
\r
70 final Variable inputVariable = (Variable)input;
\r
72 return Simantics.getSession().sync(new UniqueRead<Resource>() {
\r
75 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
76 return Variables.getModel(graph, inputVariable);
\r
80 } catch (DatabaseException e) {
\r
81 Logger.defaultLogError(e);
\r
90 public Object[] call(GraphExplorer explorer, Object[] objects) {
\r
91 Object[] result = new Object[objects.length];
\r
92 for (int i = 0; i < objects.length; i++) {
\r
93 IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);
\r
94 context.setHint(SelectionHints.KEY_MAIN, objects[i]);
\r
95 Resource model = getModel(objects[i]);
\r
97 context.setHint(SelectionHints.KEY_MODEL, model);
\r
98 result[i] = context;
\r
105 public static GraphExplorerFactory getInstance() {
\r
106 return new GraphExplorerFactory();
\r
111 * @return this instance
\r
113 public GraphExplorerFactory maxChildrenShown(int n) {
\r
114 this.maxChildrenShown = n;
\r
118 public GraphExplorerFactory selectionTransformation(BinaryFunction<Object[], GraphExplorer, Object[]> transformation) {
\r
119 this.selectionTransformation = transformation;
\r
125 * @return this instance
\r
127 public GraphExplorerFactory selectionDataResolver(SelectionDataResolver r) {
\r
128 this.selectionDataResolver = r;
\r
132 public GraphExplorerFactory setSelectionFilter(SelectionFilter filter) {
\r
133 this.selectionFilter = filter;
\r
137 public GraphExplorerFactory setServiceLocator(IServiceLocator serviceLocator) {
\r
138 this.serviceLocator = serviceLocator;
\r
142 public GraphExplorer create(Composite parent) {
\r
143 return create(parent, 0);
\r
149 * @param style SWT style hints for the explorer tree control to create
\r
152 public GraphExplorer create(Composite parent, int style) {
\r
153 return createWithStyle(parent, style | SWT.VIRTUAL);
\r
154 //return createWithStyle(parent, style);
\r
157 private static class GraphExplorerWithMaxChildren extends GraphExplorerImpl {
\r
158 // private int maxChildrenShown;
\r
159 GraphExplorerWithMaxChildren(Composite parent, int style, int maxChildrenShown) {
\r
160 super(parent, style);
\r
161 setMaxChildren(maxChildrenShown);
\r
162 // this.maxChildrenShown = maxChildrenShown;
\r
165 // public int getMaxChildren() {
\r
166 // return maxChildrenShown;
\r
170 public GraphExplorer createWithStyle(Composite parent, int style) {
\r
171 GraphExplorerImpl explorer = new GraphExplorerWithMaxChildren(parent, style, maxChildrenShown);
\r
172 explorer.setSelectionDataResolver(selectionDataResolver);
\r
173 explorer.setSelectionFilter(selectionFilter);
\r
174 explorer.setSelectionTransformation(selectionTransformation);
\r
175 explorer.setServiceLocator(serviceLocator);
\r
179 public GraphExplorer create2(Composite parent, int style) {
\r
180 GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);
\r
181 explorer.setSelectionDataResolver(selectionDataResolver);
\r
182 explorer.setSelectionFilter(selectionFilter);
\r
183 explorer.setSelectionTransformation(selectionTransformation);
\r
184 explorer.setServiceLocator(serviceLocator);
\r
188 // void hookActions(IWorkbenchSite site) {
\r
189 // IActionBars actionBars = null;
\r
191 // if (site instanceof IEditorSite)
\r
192 // actionBars = ((IEditorSite) site).getActionBars();
\r
193 // if (site instanceof IViewSite)
\r
194 // actionBars = ((IViewSite) site).getActionBars();
\r
195 // if (site instanceof IPageSite)
\r
196 // actionBars = ((IPageSite) site).getActionBars();
\r
198 // if (actionBars != null) {
\r
199 // actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), new Action() {
\r
201 // public void run() {
\r
202 // System.out.println("SELECT_ALL");
\r
205 // actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), new Action() {
\r
207 // public void run() {
\r
208 // System.out.println("FIND");
\r
211 // actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), new Action() {
\r
213 // public void run() {
\r
214 // System.out.println("UNDO");
\r
215 // // SimanticsUI.undo();
\r
218 // actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), new Action() {
\r
220 // public void run() {
\r
221 // System.out.println("REDO");
\r
222 // // SimanticsUI.redo();
\r
225 // actionBars.updateActionBars();
\r