]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java
Migrated source code from Simantics SVN
[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 org.eclipse.swt.SWT;\r
15 import org.eclipse.swt.widgets.Composite;\r
16 import org.eclipse.ui.services.IServiceLocator;\r
17 import org.simantics.Simantics;\r
18 import org.simantics.browsing.ui.BuiltinKeys;\r
19 import org.simantics.browsing.ui.GraphExplorer;\r
20 import org.simantics.browsing.ui.NodeContext;\r
21 import org.simantics.browsing.ui.SelectionDataResolver;\r
22 import org.simantics.browsing.ui.SelectionFilter;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.common.request.PossibleTypedParent;\r
26 import org.simantics.db.common.request.UniqueRead;\r
27 import org.simantics.db.common.utils.Logger;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.db.layer0.SelectionHints;\r
30 import org.simantics.db.layer0.variable.Variable;\r
31 import org.simantics.db.layer0.variable.Variables;\r
32 import org.simantics.simulation.ontology.SimulationResource;\r
33 import org.simantics.utils.datastructures.BinaryFunction;\r
34 import org.simantics.utils.datastructures.hints.IHintContext;\r
35 \r
36 /**\r
37  * @author Tuukka Lehtonen\r
38  */\r
39 public class GraphExplorerFactory {\r
40 \r
41     private int                     maxChildrenShown      = GraphExplorerImpl.DEFAULT_MAX_CHILDREN;\r
42 \r
43     private SelectionDataResolver  selectionDataResolver;\r
44     private SelectionFilter        selectionFilter;\r
45 \r
46     private IServiceLocator        serviceLocator;\r
47 \r
48     private BinaryFunction<Object[], GraphExplorer, Object[]>  selectionTransformation = new BinaryFunction<Object[], GraphExplorer, Object[]>() {\r
49 \r
50         private Resource getModel(final Object object) {\r
51                 if(object instanceof NodeContext) {\r
52                         NodeContext context = (NodeContext)object;\r
53                         Object input = context.getConstant(BuiltinKeys.INPUT);\r
54                         if(input instanceof Resource) {\r
55                                 final Resource inputResource = (Resource)input;\r
56                         try {\r
57                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {\r
58 \r
59                                         @Override\r
60                                         public Resource perform(ReadGraph graph) throws DatabaseException {\r
61                                                 SimulationResource SIMU = SimulationResource.getInstance(graph);\r
62                                                 return graph.sync(new PossibleTypedParent(inputResource, SIMU.Model));\r
63                                         }\r
64 \r
65                                 });\r
66                         } catch (DatabaseException e) {\r
67                                 Logger.defaultLogError(e);\r
68                         }\r
69                         } else if (input instanceof Variable) {\r
70                                 final Variable inputVariable = (Variable)input;\r
71                         try {\r
72                                 return Simantics.getSession().sync(new UniqueRead<Resource>() {\r
73 \r
74                                         @Override\r
75                                         public Resource perform(ReadGraph graph) throws DatabaseException {\r
76                                                 return Variables.getModel(graph, inputVariable);\r
77                                         }\r
78 \r
79                                 });\r
80                         } catch (DatabaseException e) {\r
81                                 Logger.defaultLogError(e);\r
82                         }\r
83                         }\r
84                 }\r
85                 return null;\r
86         }\r
87         \r
88         \r
89         @Override\r
90         public Object[] call(GraphExplorer explorer, Object[] objects) {\r
91             Object[] result = new Object[objects.length];\r
92             for (int i = 0; i < objects.length; i++) {\r
93                 IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);\r
94                 context.setHint(SelectionHints.KEY_MAIN, objects[i]);\r
95                 Resource model = getModel(objects[i]);\r
96                 if(model != null)\r
97                         context.setHint(SelectionHints.KEY_MODEL, model);\r
98                 result[i] = context;\r
99             }\r
100             return result;\r
101         }\r
102 \r
103     };\r
104 \r
105     public static GraphExplorerFactory getInstance() {\r
106         return new GraphExplorerFactory();\r
107     }\r
108 \r
109     /**\r
110      * @param n\r
111      * @return this instance\r
112      */\r
113     public GraphExplorerFactory maxChildrenShown(int n) {\r
114         this.maxChildrenShown = n;\r
115         return this;\r
116     }\r
117 \r
118     public GraphExplorerFactory selectionTransformation(BinaryFunction<Object[], GraphExplorer, Object[]> transformation) {\r
119         this.selectionTransformation = transformation;\r
120         return this;\r
121     }\r
122 \r
123     /**\r
124      * @param r\r
125      * @return this instance\r
126      */\r
127     public GraphExplorerFactory selectionDataResolver(SelectionDataResolver r) {\r
128         this.selectionDataResolver = r;\r
129         return this;\r
130     }\r
131 \r
132     public GraphExplorerFactory setSelectionFilter(SelectionFilter filter) {\r
133         this.selectionFilter = filter;\r
134         return this;\r
135     }\r
136 \r
137     public GraphExplorerFactory setServiceLocator(IServiceLocator serviceLocator) {\r
138         this.serviceLocator = serviceLocator;\r
139         return this;\r
140     }\r
141 \r
142     public GraphExplorer create(Composite parent) {\r
143         return create(parent, 0);\r
144     }\r
145 \r
146     /**\r
147      * @param site\r
148      * @param parent\r
149      * @param style SWT style hints for the explorer tree control to create\r
150      * @return\r
151      */\r
152     public GraphExplorer create(Composite parent, int style) {\r
153         return createWithStyle(parent, style | SWT.VIRTUAL);\r
154         //return createWithStyle(parent, style);\r
155     }\r
156 \r
157     private static class GraphExplorerWithMaxChildren extends GraphExplorerImpl {\r
158 //      private int maxChildrenShown;\r
159         GraphExplorerWithMaxChildren(Composite parent, int style, int maxChildrenShown) {\r
160                 super(parent, style);\r
161                 setMaxChildren(maxChildrenShown);\r
162 //              this.maxChildrenShown = maxChildrenShown;\r
163         }\r
164 //      @Override\r
165 //      public int getMaxChildren() {\r
166 //              return maxChildrenShown;\r
167 //      }\r
168     }\r
169     \r
170     public GraphExplorer createWithStyle(Composite parent, int style) {\r
171         GraphExplorerImpl explorer = new GraphExplorerWithMaxChildren(parent, style, maxChildrenShown);\r
172         explorer.setSelectionDataResolver(selectionDataResolver);\r
173         explorer.setSelectionFilter(selectionFilter);\r
174         explorer.setSelectionTransformation(selectionTransformation);\r
175         explorer.setServiceLocator(serviceLocator);\r
176         return explorer;\r
177     }\r
178     \r
179     public GraphExplorer create2(Composite parent, int style) {\r
180         GraphExplorerImpl2 explorer = new GraphExplorerImpl2(parent, style);\r
181         explorer.setSelectionDataResolver(selectionDataResolver);\r
182         explorer.setSelectionFilter(selectionFilter);\r
183         explorer.setSelectionTransformation(selectionTransformation);\r
184         explorer.setServiceLocator(serviceLocator);\r
185         return explorer;\r
186     }\r
187 \r
188 //    void hookActions(IWorkbenchSite site) {\r
189 //        IActionBars actionBars = null;\r
190 //\r
191 //        if (site instanceof IEditorSite)\r
192 //            actionBars = ((IEditorSite) site).getActionBars();\r
193 //        if (site instanceof IViewSite)\r
194 //            actionBars = ((IViewSite) site).getActionBars();\r
195 //        if (site instanceof IPageSite)\r
196 //            actionBars = ((IPageSite) site).getActionBars();\r
197 //\r
198 //        if (actionBars != null) {\r
199 //            actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), new Action() {\r
200 //                @Override\r
201 //                public void run() {\r
202 //                    System.out.println("SELECT_ALL");\r
203 //                }\r
204 //            });\r
205 //            actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), new Action() {\r
206 //                @Override\r
207 //                public void run() {\r
208 //                    System.out.println("FIND");\r
209 //                }\r
210 //            });\r
211 //            actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), new Action() {\r
212 //                @Override\r
213 //                public void run() {\r
214 //                    System.out.println("UNDO");\r
215 //                    // SimanticsUI.undo();\r
216 //                }\r
217 //            });\r
218 //            actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), new Action() {\r
219 //                @Override\r
220 //                public void run() {\r
221 //                    System.out.println("REDO");\r
222 //                    // SimanticsUI.redo();\r
223 //                }\r
224 //            });\r
225 //            actionBars.updateActionBars();\r
226 //        }\r
227 //    }\r
228 \r
229 }\r