]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java
Merge "Testing SonarQube with Simantics Platform SDK"
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / GraphExplorerFactory.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import java.lang.reflect.Method;\r
15 \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
40 \r
41 /**\r
42  * @author Tuukka Lehtonen\r
43  */\r
44 public class GraphExplorerFactory {\r
45 \r
46     private int                     maxChildrenShown      = GraphExplorerImpl.DEFAULT_MAX_CHILDREN;\r
47 \r
48     private SelectionDataResolver  selectionDataResolver;\r
49     private SelectionFilter        selectionFilter;\r
50 \r
51     private IServiceLocator        serviceLocator;\r
52 \r
53     private BinaryFunction<Object[], GraphExplorer, Object[]>  selectionTransformation = new BinaryFunction<Object[], GraphExplorer, Object[]>() {\r
54 \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
61                         try {\r
62                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {\r
63 \r
64                                         @Override\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
68                                         }\r
69 \r
70                                 });\r
71                         } catch (DatabaseException e) {\r
72                                 Logger.defaultLogError(e);\r
73                         }\r
74                         } else if (input instanceof Variable) {\r
75                                 final Variable inputVariable = (Variable)input;\r
76                         try {\r
77                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {\r
78 \r
79                                         @Override\r
80                                         public Resource perform(ReadGraph graph) throws DatabaseException {\r
81                                                 return Variables.getModel(graph, inputVariable);\r
82                                         }\r
83 \r
84                                 });\r
85                         } catch (DatabaseException e) {\r
86                                 Logger.defaultLogError(e);\r
87                         }\r
88                         }\r
89                 }\r
90                 return null;\r
91         }\r
92         \r
93         \r
94         @Override\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
101                 if(model != null)\r
102                         context.setHint(SelectionHints.KEY_MODEL, model);\r
103                 result[i] = context;\r
104             }\r
105             return result;\r
106         }\r
107 \r
108     };\r
109 \r
110     public static GraphExplorerFactory getInstance() {\r
111         return new GraphExplorerFactory();\r
112     }\r
113 \r
114     /**\r
115      * @param n\r
116      * @return this instance\r
117      */\r
118     public GraphExplorerFactory maxChildrenShown(int n) {\r
119         this.maxChildrenShown = n;\r
120         return this;\r
121     }\r
122 \r
123     public GraphExplorerFactory selectionTransformation(BinaryFunction<Object[], GraphExplorer, Object[]> transformation) {\r
124         this.selectionTransformation = transformation;\r
125         return this;\r
126     }\r
127 \r
128     /**\r
129      * @param r\r
130      * @return this instance\r
131      */\r
132     public GraphExplorerFactory selectionDataResolver(SelectionDataResolver r) {\r
133         this.selectionDataResolver = r;\r
134         return this;\r
135     }\r
136 \r
137     public GraphExplorerFactory setSelectionFilter(SelectionFilter filter) {\r
138         this.selectionFilter = filter;\r
139         return this;\r
140     }\r
141 \r
142     public GraphExplorerFactory setServiceLocator(IServiceLocator serviceLocator) {\r
143         this.serviceLocator = serviceLocator;\r
144         return this;\r
145     }\r
146 \r
147     public GraphExplorer create(Composite parent) {\r
148         return create(parent, 0);\r
149     }\r
150 \r
151     /**\r
152      * @param site\r
153      * @param parent\r
154      * @param style SWT style hints for the explorer tree control to create\r
155      * @return\r
156      */\r
157     public GraphExplorer create(Composite parent, int style) {\r
158         return createWithStyle(parent, style | SWT.VIRTUAL);\r
159         //return createWithStyle(parent, style);\r
160     }\r
161 \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
168         }\r
169 //      @Override\r
170 //      public int getMaxChildren() {\r
171 //              return maxChildrenShown;\r
172 //      }\r
173     }\r
174     \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
181         return explorer;\r
182     }\r
183     \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
190         return explorer;\r
191     }\r
192     \r
193     public GraphExplorer create3(Composite parent, int style) {\r
194         //GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);\r
195         try {\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
206                 return explorer;\r
207         } catch (Throwable t) {\r
208                 ExceptionUtils.logAndShowError(t);\r
209                 return null;\r
210         }\r
211     }\r
212 \r
213 //    void hookActions(IWorkbenchSite site) {\r
214 //        IActionBars actionBars = null;\r
215 //\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
222 //\r
223 //        if (actionBars != null) {\r
224 //            actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), new Action() {\r
225 //                @Override\r
226 //                public void run() {\r
227 //                    System.out.println("SELECT_ALL");\r
228 //                }\r
229 //            });\r
230 //            actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), new Action() {\r
231 //                @Override\r
232 //                public void run() {\r
233 //                    System.out.println("FIND");\r
234 //                }\r
235 //            });\r
236 //            actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), new Action() {\r
237 //                @Override\r
238 //                public void run() {\r
239 //                    System.out.println("UNDO");\r
240 //                    // SimanticsUI.undo();\r
241 //                }\r
242 //            });\r
243 //            actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), new Action() {\r
244 //                @Override\r
245 //                public void run() {\r
246 //                    System.out.println("REDO");\r
247 //                    // SimanticsUI.redo();\r
248 //                }\r
249 //            });\r
250 //            actionBars.updateActionBars();\r
251 //        }\r
252 //    }\r
253 \r
254 }\r