]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java
Use RuntimeEnvironmentRequest2 where applicable
[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.browsing.ui.common.AdaptableHintContext;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.common.request.PossibleTypedParent;
32 import org.simantics.db.common.request.UniqueRead;
33 import org.simantics.db.common.utils.Logger;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.layer0.SelectionHints;
36 import org.simantics.db.layer0.variable.Variable;
37 import org.simantics.db.layer0.variable.Variables;
38 import org.simantics.simulation.ontology.SimulationResource;
39 import org.simantics.utils.datastructures.hints.IHintContext;
40 import org.simantics.utils.ui.ExceptionUtils;
41
42 /**
43  * @author Tuukka Lehtonen
44  */
45 public class GraphExplorerFactory {
46
47     private int                     maxChildrenShown      = GraphExplorerImpl.DEFAULT_MAX_CHILDREN;
48
49     private SelectionDataResolver  selectionDataResolver;
50     private SelectionFilter        selectionFilter;
51
52     private IServiceLocator        serviceLocator;
53
54     private BiFunction<GraphExplorer, Object[], Object[]> selectionTransformation = new BiFunction<GraphExplorer, Object[], Object[]>() {
55
56         private Resource getModel(final Object object) {
57                 if(object instanceof NodeContext) {
58                         NodeContext context = (NodeContext)object;
59                         Object input = context.getConstant(BuiltinKeys.INPUT);
60                         if(input instanceof Resource) {
61                                 final Resource inputResource = (Resource)input;
62                         try {
63                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {
64
65                                         @Override
66                                         public Resource perform(ReadGraph graph) throws DatabaseException {
67                                                 SimulationResource SIMU = SimulationResource.getInstance(graph);
68                                                 return graph.sync(new PossibleTypedParent(inputResource, SIMU.Model));
69                                         }
70
71                                 });
72                         } catch (DatabaseException e) {
73                                 Logger.defaultLogError(e);
74                         }
75                         } else if (input instanceof Variable) {
76                                 final Variable inputVariable = (Variable)input;
77                         try {
78                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {
79
80                                         @Override
81                                         public Resource perform(ReadGraph graph) throws DatabaseException {
82                                                 return Variables.getModel(graph, inputVariable);
83                                         }
84
85                                 });
86                         } catch (DatabaseException e) {
87                                 Logger.defaultLogError(e);
88                         }
89                         }
90                 }
91                 return null;
92         }
93         
94         @Override
95         public Object[] apply(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]);
101                 if(model != null)
102                         context.setHint(SelectionHints.KEY_MODEL, model);
103                 result[i] = context;
104             }
105             return result;
106         }
107
108     };
109
110     public static GraphExplorerFactory getInstance() {
111         return new GraphExplorerFactory();
112     }
113
114     /**
115      * @param n
116      * @return this instance
117      */
118     public GraphExplorerFactory maxChildrenShown(int n) {
119         this.maxChildrenShown = n;
120         return this;
121     }
122
123     public GraphExplorerFactory selectionTransformation(BiFunction<GraphExplorer, Object[], Object[]> transformation) {
124         this.selectionTransformation = transformation;
125         return this;
126     }
127
128     /**
129      * @param r
130      * @return this instance
131      */
132     public GraphExplorerFactory selectionDataResolver(SelectionDataResolver r) {
133         this.selectionDataResolver = r;
134         return this;
135     }
136
137     public GraphExplorerFactory setSelectionFilter(SelectionFilter filter) {
138         this.selectionFilter = filter;
139         return this;
140     }
141
142     public GraphExplorerFactory setServiceLocator(IServiceLocator serviceLocator) {
143         this.serviceLocator = serviceLocator;
144         return this;
145     }
146
147     public GraphExplorer create(Composite parent) {
148         return create(parent, 0);
149     }
150
151     /**
152      * @param site
153      * @param parent
154      * @param style SWT style hints for the explorer tree control to create
155      * @return
156      */
157     public GraphExplorer create(Composite parent, int style) {
158         return createWithStyle(parent, style | SWT.VIRTUAL);
159         //return createWithStyle(parent, style);
160     }
161
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;
168         }
169 //      @Override
170 //      public int getMaxChildren() {
171 //              return maxChildrenShown;
172 //      }
173     }
174     
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);
181         return explorer;
182     }
183     
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);
190         return explorer;
191     }
192     
193     public GraphExplorer create3(Composite parent, int style) {
194         //GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);
195         try {
196                 Bundle bundle = Platform.getBundle("org.simantics.browsing.ui.nattable");
197                 @SuppressWarnings("unchecked")
198                         Class<GraphExplorer> clazz = (Class<GraphExplorer>)bundle.loadClass("org.simantics.browsing.ui.nattable.NatTableGraphExplorer");
199                 //Class<GraphExplorer> clazz = (Class<GraphExplorer>)bundle.getClass().getClassLoader().loadClass("org.simantics.browsing.ui.nattable.NatTableGraphExplorer");
200                 GraphExplorer explorer = clazz.getConstructor(Composite.class, int.class).newInstance(parent,style);
201                 explorer.setSelectionDataResolver(selectionDataResolver);
202                 explorer.setSelectionFilter(selectionFilter);
203                 explorer.setSelectionTransformation(selectionTransformation);
204                 Method m = clazz.getMethod("setServiceLocator", IServiceLocator.class);
205                 m.invoke(explorer, serviceLocator);
206                 //explorer.setServiceLocator(serviceLocator);
207                 return explorer;
208         } catch (Throwable t) {
209                 ExceptionUtils.logAndShowError(t);
210                 return null;
211         }
212     }
213
214 //    void hookActions(IWorkbenchSite site) {
215 //        IActionBars actionBars = null;
216 //
217 //        if (site instanceof IEditorSite)
218 //            actionBars = ((IEditorSite) site).getActionBars();
219 //        if (site instanceof IViewSite)
220 //            actionBars = ((IViewSite) site).getActionBars();
221 //        if (site instanceof IPageSite)
222 //            actionBars = ((IPageSite) site).getActionBars();
223 //
224 //        if (actionBars != null) {
225 //            actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), new Action() {
226 //                @Override
227 //                public void run() {
228 //                    System.out.println("SELECT_ALL");
229 //                }
230 //            });
231 //            actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), new Action() {
232 //                @Override
233 //                public void run() {
234 //                    System.out.println("FIND");
235 //                }
236 //            });
237 //            actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), new Action() {
238 //                @Override
239 //                public void run() {
240 //                    System.out.println("UNDO");
241 //                    // SimanticsUI.undo();
242 //                }
243 //            });
244 //            actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), new Action() {
245 //                @Override
246 //                public void run() {
247 //                    System.out.println("REDO");
248 //                    // SimanticsUI.redo();
249 //                }
250 //            });
251 //            actionBars.updateActionBars();
252 //        }
253 //    }
254
255 }