]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / GraphExplorerFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.lang.reflect.Method;
15 import java.util.function.BiFunction;
16
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.ui.services.IServiceLocator;
21 import org.osgi.framework.Bundle;
22 import org.simantics.Simantics;
23 import org.simantics.browsing.ui.BuiltinKeys;
24 import org.simantics.browsing.ui.GraphExplorer;
25 import org.simantics.browsing.ui.NodeContext;
26 import org.simantics.browsing.ui.SelectionDataResolver;
27 import org.simantics.browsing.ui.SelectionFilter;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.Resource;
30 import org.simantics.db.common.request.PossibleTypedParent;
31 import org.simantics.db.common.request.UniqueRead;
32 import org.simantics.db.common.utils.Logger;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.SelectionHints;
35 import org.simantics.db.layer0.variable.Variable;
36 import org.simantics.db.layer0.variable.Variables;
37 import org.simantics.simulation.ontology.SimulationResource;
38 import org.simantics.utils.datastructures.hints.IHintContext;
39 import org.simantics.utils.ui.ExceptionUtils;
40
41 /**
42  * @author Tuukka Lehtonen
43  */
44 public class GraphExplorerFactory {
45
46     private int                     maxChildrenShown      = GraphExplorerImpl.DEFAULT_MAX_CHILDREN;
47
48     private SelectionDataResolver  selectionDataResolver;
49     private SelectionFilter        selectionFilter;
50
51     private IServiceLocator        serviceLocator;
52
53     private BiFunction<GraphExplorer, Object[], Object[]> selectionTransformation = new BiFunction<GraphExplorer, Object[], Object[]>() {
54
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;
61                         try {
62                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {
63
64                                         @Override
65                                         public Resource perform(ReadGraph graph) throws DatabaseException {
66                                                 SimulationResource SIMU = SimulationResource.getInstance(graph);
67                                                 return graph.sync(new PossibleTypedParent(inputResource, SIMU.Model));
68                                         }
69
70                                 });
71                         } catch (DatabaseException e) {
72                                 Logger.defaultLogError(e);
73                         }
74                         } else if (input instanceof Variable) {
75                                 final Variable inputVariable = (Variable)input;
76                         try {
77                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {
78
79                                         @Override
80                                         public Resource perform(ReadGraph graph) throws DatabaseException {
81                                                 return Variables.getModel(graph, inputVariable);
82                                         }
83
84                                 });
85                         } catch (DatabaseException e) {
86                                 Logger.defaultLogError(e);
87                         }
88                         }
89                 }
90                 return null;
91         }
92         
93         @Override
94         public Object[] apply(GraphExplorer explorer, Object[] objects) {
95             Object[] result = new Object[objects.length];
96             for (int i = 0; i < objects.length; i++) {
97                 IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);
98                 context.setHint(SelectionHints.KEY_MAIN, objects[i]);
99                 Resource model = getModel(objects[i]);
100                 if(model != null)
101                         context.setHint(SelectionHints.KEY_MODEL, model);
102                 result[i] = context;
103             }
104             return result;
105         }
106
107     };
108
109     public static GraphExplorerFactory getInstance() {
110         return new GraphExplorerFactory();
111     }
112
113     /**
114      * @param n
115      * @return this instance
116      */
117     public GraphExplorerFactory maxChildrenShown(int n) {
118         this.maxChildrenShown = n;
119         return this;
120     }
121
122     public GraphExplorerFactory selectionTransformation(BiFunction<GraphExplorer, Object[], Object[]> transformation) {
123         this.selectionTransformation = transformation;
124         return this;
125     }
126
127     /**
128      * @param r
129      * @return this instance
130      */
131     public GraphExplorerFactory selectionDataResolver(SelectionDataResolver r) {
132         this.selectionDataResolver = r;
133         return this;
134     }
135
136     public GraphExplorerFactory setSelectionFilter(SelectionFilter filter) {
137         this.selectionFilter = filter;
138         return this;
139     }
140
141     public GraphExplorerFactory setServiceLocator(IServiceLocator serviceLocator) {
142         this.serviceLocator = serviceLocator;
143         return this;
144     }
145
146     public GraphExplorer create(Composite parent) {
147         return create(parent, 0);
148     }
149
150     /**
151      * @param site
152      * @param parent
153      * @param style SWT style hints for the explorer tree control to create
154      * @return
155      */
156     public GraphExplorer create(Composite parent, int style) {
157         return createWithStyle(parent, style | SWT.VIRTUAL);
158         //return createWithStyle(parent, style);
159     }
160
161     private static class GraphExplorerWithMaxChildren extends GraphExplorerImpl {
162 //      private int maxChildrenShown;
163         GraphExplorerWithMaxChildren(Composite parent, int style, int maxChildrenShown) {
164                 super(parent, style);
165                 setMaxChildren(maxChildrenShown);
166 //              this.maxChildrenShown = maxChildrenShown;
167         }
168 //      @Override
169 //      public int getMaxChildren() {
170 //              return maxChildrenShown;
171 //      }
172     }
173     
174     public GraphExplorer createWithStyle(Composite parent, int style) {
175         GraphExplorerImpl explorer = new GraphExplorerWithMaxChildren(parent, style, maxChildrenShown);
176         explorer.setSelectionDataResolver(selectionDataResolver);
177         explorer.setSelectionFilter(selectionFilter);
178         explorer.setSelectionTransformation(selectionTransformation);
179         explorer.setServiceLocator(serviceLocator);
180         return explorer;
181     }
182     
183     public GraphExplorer create2(Composite parent, int style) {
184         GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);
185         explorer.setSelectionDataResolver(selectionDataResolver);
186         explorer.setSelectionFilter(selectionFilter);
187         explorer.setSelectionTransformation(selectionTransformation);
188         explorer.setServiceLocator(serviceLocator);
189         return explorer;
190     }
191     
192     public GraphExplorer create3(Composite parent, int style) {
193         //GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);
194         try {
195                 Bundle bundle = Platform.getBundle("org.simantics.browsing.ui.nattable");
196                 @SuppressWarnings("unchecked")
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);
206                 return explorer;
207         } catch (Throwable t) {
208                 ExceptionUtils.logAndShowError(t);
209                 return null;
210         }
211     }
212
213 //    void hookActions(IWorkbenchSite site) {
214 //        IActionBars actionBars = null;
215 //
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();
222 //
223 //        if (actionBars != null) {
224 //            actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), new Action() {
225 //                @Override
226 //                public void run() {
227 //                    System.out.println("SELECT_ALL");
228 //                }
229 //            });
230 //            actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), new Action() {
231 //                @Override
232 //                public void run() {
233 //                    System.out.println("FIND");
234 //                }
235 //            });
236 //            actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), new Action() {
237 //                @Override
238 //                public void run() {
239 //                    System.out.println("UNDO");
240 //                    // SimanticsUI.undo();
241 //                }
242 //            });
243 //            actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), new Action() {
244 //                @Override
245 //                public void run() {
246 //                    System.out.println("REDO");
247 //                    // SimanticsUI.redo();
248 //                }
249 //            });
250 //            actionBars.updateActionBars();
251 //        }
252 //    }
253
254 }