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 java.lang.reflect.Method;
\r
16 import org.eclipse.core.runtime.Platform;
\r
17 import org.eclipse.swt.SWT;
\r
18 import org.eclipse.swt.widgets.Composite;
\r
19 import org.eclipse.ui.services.IServiceLocator;
\r
20 import org.osgi.framework.Bundle;
\r
21 import org.simantics.Simantics;
\r
22 import org.simantics.browsing.ui.BuiltinKeys;
\r
23 import org.simantics.browsing.ui.GraphExplorer;
\r
24 import org.simantics.browsing.ui.NodeContext;
\r
25 import org.simantics.browsing.ui.SelectionDataResolver;
\r
26 import org.simantics.browsing.ui.SelectionFilter;
\r
27 import org.simantics.db.ReadGraph;
\r
28 import org.simantics.db.Resource;
\r
29 import org.simantics.db.common.request.PossibleTypedParent;
\r
30 import org.simantics.db.common.request.UniqueRead;
\r
31 import org.simantics.db.common.utils.Logger;
\r
32 import org.simantics.db.exception.DatabaseException;
\r
33 import org.simantics.db.layer0.SelectionHints;
\r
34 import org.simantics.db.layer0.variable.Variable;
\r
35 import org.simantics.db.layer0.variable.Variables;
\r
36 import org.simantics.simulation.ontology.SimulationResource;
\r
37 import org.simantics.utils.datastructures.BinaryFunction;
\r
38 import org.simantics.utils.datastructures.hints.IHintContext;
\r
39 import org.simantics.utils.ui.ExceptionUtils;
\r
42 * @author Tuukka Lehtonen
\r
44 public class GraphExplorerFactory {
\r
46 private int maxChildrenShown = GraphExplorerImpl.DEFAULT_MAX_CHILDREN;
\r
48 private SelectionDataResolver selectionDataResolver;
\r
49 private SelectionFilter selectionFilter;
\r
51 private IServiceLocator serviceLocator;
\r
53 private BinaryFunction<Object[], GraphExplorer, Object[]> selectionTransformation = new BinaryFunction<Object[], GraphExplorer, Object[]>() {
\r
55 private Resource getModel(final Object object) {
\r
56 if(object instanceof NodeContext) {
\r
57 NodeContext context = (NodeContext)object;
\r
58 Object input = context.getConstant(BuiltinKeys.INPUT);
\r
59 if(input instanceof Resource) {
\r
60 final Resource inputResource = (Resource)input;
\r
62 return Simantics.getSession().sync(new UniqueRead<Resource>() {
\r
65 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
66 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
67 return graph.sync(new PossibleTypedParent(inputResource, SIMU.Model));
\r
71 } catch (DatabaseException e) {
\r
72 Logger.defaultLogError(e);
\r
74 } else if (input instanceof Variable) {
\r
75 final Variable inputVariable = (Variable)input;
\r
77 return Simantics.getSession().sync(new UniqueRead<Resource>() {
\r
80 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
81 return Variables.getModel(graph, inputVariable);
\r
85 } catch (DatabaseException e) {
\r
86 Logger.defaultLogError(e);
\r
95 public Object[] call(GraphExplorer explorer, Object[] objects) {
\r
96 Object[] result = new Object[objects.length];
\r
97 for (int i = 0; i < objects.length; i++) {
\r
98 IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);
\r
99 context.setHint(SelectionHints.KEY_MAIN, objects[i]);
\r
100 Resource model = getModel(objects[i]);
\r
102 context.setHint(SelectionHints.KEY_MODEL, model);
\r
103 result[i] = context;
\r
110 public static GraphExplorerFactory getInstance() {
\r
111 return new GraphExplorerFactory();
\r
116 * @return this instance
\r
118 public GraphExplorerFactory maxChildrenShown(int n) {
\r
119 this.maxChildrenShown = n;
\r
123 public GraphExplorerFactory selectionTransformation(BinaryFunction<Object[], GraphExplorer, Object[]> transformation) {
\r
124 this.selectionTransformation = transformation;
\r
130 * @return this instance
\r
132 public GraphExplorerFactory selectionDataResolver(SelectionDataResolver r) {
\r
133 this.selectionDataResolver = r;
\r
137 public GraphExplorerFactory setSelectionFilter(SelectionFilter filter) {
\r
138 this.selectionFilter = filter;
\r
142 public GraphExplorerFactory setServiceLocator(IServiceLocator serviceLocator) {
\r
143 this.serviceLocator = serviceLocator;
\r
147 public GraphExplorer create(Composite parent) {
\r
148 return create(parent, 0);
\r
154 * @param style SWT style hints for the explorer tree control to create
\r
157 public GraphExplorer create(Composite parent, int style) {
\r
158 return createWithStyle(parent, style | SWT.VIRTUAL);
\r
159 //return createWithStyle(parent, style);
\r
162 private static class GraphExplorerWithMaxChildren extends GraphExplorerImpl {
\r
163 // private int maxChildrenShown;
\r
164 GraphExplorerWithMaxChildren(Composite parent, int style, int maxChildrenShown) {
\r
165 super(parent, style);
\r
166 setMaxChildren(maxChildrenShown);
\r
167 // this.maxChildrenShown = maxChildrenShown;
\r
170 // public int getMaxChildren() {
\r
171 // return maxChildrenShown;
\r
175 public GraphExplorer createWithStyle(Composite parent, int style) {
\r
176 GraphExplorerImpl explorer = new GraphExplorerWithMaxChildren(parent, style, maxChildrenShown);
\r
177 explorer.setSelectionDataResolver(selectionDataResolver);
\r
178 explorer.setSelectionFilter(selectionFilter);
\r
179 explorer.setSelectionTransformation(selectionTransformation);
\r
180 explorer.setServiceLocator(serviceLocator);
\r
184 public GraphExplorer create2(Composite parent, int style) {
\r
185 GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);
\r
186 explorer.setSelectionDataResolver(selectionDataResolver);
\r
187 explorer.setSelectionFilter(selectionFilter);
\r
188 explorer.setSelectionTransformation(selectionTransformation);
\r
189 explorer.setServiceLocator(serviceLocator);
\r
193 public GraphExplorer create3(Composite parent, int style) {
\r
194 //GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);
\r
196 Bundle bundle = Platform.getBundle("org.simantics.browsing.ui.nattable");
\r
197 Class<GraphExplorer> clazz = (Class<GraphExplorer>)bundle.loadClass("org.simantics.browsing.ui.nattable.NatTableGraphExplorer");
\r
198 //Class<GraphExplorer> clazz = (Class<GraphExplorer>)bundle.getClass().getClassLoader().loadClass("org.simantics.browsing.ui.nattable.NatTableGraphExplorer");
\r
199 GraphExplorer explorer = clazz.getConstructor(Composite.class, int.class).newInstance(parent,style);
\r
200 explorer.setSelectionDataResolver(selectionDataResolver);
\r
201 explorer.setSelectionFilter(selectionFilter);
\r
202 explorer.setSelectionTransformation(selectionTransformation);
\r
203 Method m = clazz.getMethod("setServiceLocator", IServiceLocator.class);
\r
204 m.invoke(explorer, serviceLocator);
\r
205 //explorer.setServiceLocator(serviceLocator);
\r
207 } catch (Throwable t) {
\r
208 ExceptionUtils.logAndShowError(t);
\r
213 // void hookActions(IWorkbenchSite site) {
\r
214 // IActionBars actionBars = null;
\r
216 // if (site instanceof IEditorSite)
\r
217 // actionBars = ((IEditorSite) site).getActionBars();
\r
218 // if (site instanceof IViewSite)
\r
219 // actionBars = ((IViewSite) site).getActionBars();
\r
220 // if (site instanceof IPageSite)
\r
221 // actionBars = ((IPageSite) site).getActionBars();
\r
223 // if (actionBars != null) {
\r
224 // actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), new Action() {
\r
226 // public void run() {
\r
227 // System.out.println("SELECT_ALL");
\r
230 // actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), new Action() {
\r
232 // public void run() {
\r
233 // System.out.println("FIND");
\r
236 // actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), new Action() {
\r
238 // public void run() {
\r
239 // System.out.println("UNDO");
\r
240 // // SimanticsUI.undo();
\r
243 // actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), new Action() {
\r
245 // public void run() {
\r
246 // System.out.println("REDO");
\r
247 // // SimanticsUI.redo();
\r
250 // actionBars.updateActionBars();
\r