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