]> gerrit.simantics Code Review - simantics/3d.git/blob
c778c0e8c4260d8c3a3f080494e1fbebc7864253
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.processeditor.dialogs;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.Collection;\r
15 import java.util.HashSet;\r
16 import java.util.Set;\r
17 import java.util.TreeMap;\r
18 import java.util.Map.Entry;\r
19 \r
20 import org.eclipse.jface.dialogs.Dialog;\r
21 import org.eclipse.jface.dialogs.IDialogConstants;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.events.SelectionAdapter;\r
24 import org.eclipse.swt.events.SelectionEvent;\r
25 import org.eclipse.swt.events.SelectionListener;\r
26 import org.eclipse.swt.layout.FillLayout;\r
27 import org.eclipse.swt.layout.GridData;\r
28 import org.eclipse.swt.layout.GridLayout;\r
29 import org.eclipse.swt.widgets.Button;\r
30 import org.eclipse.swt.widgets.Composite;\r
31 import org.eclipse.swt.widgets.Control;\r
32 import org.eclipse.swt.widgets.Label;\r
33 import org.eclipse.swt.widgets.List;\r
34 import org.eclipse.swt.widgets.Shell;\r
35 import org.simantics.db.Graph;\r
36 import org.simantics.db.GraphRequestAdapter;\r
37 import org.simantics.db.GraphRequestStatus;\r
38 import org.simantics.db.Resource;\r
39 import org.simantics.db.Session;\r
40 import org.simantics.layer0.utils.EntityFactory;\r
41 import org.simantics.layer0.utils.IEntity;\r
42 import org.simantics.layer0.utils.viewpoints.TraversalPath;\r
43 import org.simantics.processeditor.ProcessResource;\r
44 import org.simantics.processeditor.common.PathUtils;\r
45 import org.simantics.processeditor.monitors.PathContainer;\r
46 import org.simantics.proconf.browsing.GraphExplorer;\r
47 import org.simantics.proconf.g3d.stubs.G3DModel;\r
48 import org.simantics.proconf.g3d.tools.OEPathSelectionListener;\r
49 \r
50 \r
51 public class ConfigureMonitorDialog extends Dialog {\r
52         Session session;\r
53         List typeList;\r
54         java.util.List<Resource> nodes;\r
55 \r
56         \r
57         private Composite oeLabelComposite;\r
58         //private Control viewpointControl;\r
59         private Composite oeComposite;\r
60         private GraphExplorer oe;\r
61         \r
62         private Button applyAnimationButton;\r
63         \r
64         Resource selectedType;\r
65         Resource sampleInstance;\r
66         java.util.List<Resource> sampleSource;\r
67         java.util.List<java.util.List<Resource>> samplePath;\r
68 \r
69         \r
70         public ConfigureMonitorDialog(Shell parentShell, Session session, java.util.List<Resource> nodes) {\r
71                 super(parentShell);\r
72                 this.nodes = nodes;\r
73                 this.session = session;\r
74                 int shellStyle = getShellStyle();\r
75                 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);\r
76         }\r
77         \r
78         @Override\r
79     protected void configureShell(Shell newShell) {  \r
80         super.configureShell(newShell);\r
81         newShell.setText("Configure monitors");\r
82         \r
83     }\r
84         \r
85         protected Control createDialogArea(Composite parent) {\r
86         Composite composite = (Composite) super.createDialogArea(parent);\r
87         GridLayout layout = new GridLayout(2,true);\r
88                 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);\r
89                 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);\r
90                 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);\r
91                 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);\r
92                 composite.setLayout(layout);\r
93                 \r
94                 Label label = new Label(composite, SWT.WRAP);\r
95         label.setText("Types");\r
96         GridData data = new GridData(GridData.CENTER,GridData.FILL,true,false,1,1);\r
97         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
98         label.setLayoutData(data);\r
99         label.setFont(parent.getFont());\r
100         \r
101         oeLabelComposite = new Composite(composite,SWT.NONE);\r
102         oeLabelComposite.setLayoutData(data);\r
103         oeLabelComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
104         //label = new Label(composite, SWT.WRAP);\r
105         label = new Label(oeLabelComposite, SWT.WRAP);\r
106         label.setText("Monitor source");\r
107 //        label.setLayoutData(data);\r
108         label.setFont(parent.getFont());\r
109         \r
110         typeList = new List(composite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY | SWT.V_SCROLL);\r
111         typeList.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
112         \r
113         oeComposite = new Composite(composite,SWT.BORDER);\r
114         oeComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
115         layout = new GridLayout();\r
116         layout.marginWidth = 0;\r
117         layout.marginHeight = 0;\r
118         oeComposite.setLayout(layout);\r
119         session.asyncRead(new GraphRequestAdapter() {\r
120                 TreeMap<String, Resource> sorter;\r
121                 @Override\r
122                 public GraphRequestStatus perform(Graph g) throws Exception {\r
123                         Collection<Resource> types = getTypes(g,nodes);\r
124                 \r
125                 sorter = new TreeMap<String, Resource>();\r
126                 for (Resource type : types) {\r
127                         IEntity t = EntityFactory.create(g, type);\r
128                     String key = t.getName();\r
129                     if (key.equals("")) key = "ERROR (" + type.getResourceId(g.getSession()) + ")";\r
130                     sorter.put(key, type);\r
131                 }\r
132                 \r
133                \r
134                         return GraphRequestStatus.transactionComplete();\r
135                 }\r
136                 \r
137                 @Override\r
138                 public void requestCompleted(GraphRequestStatus status) {\r
139                         getShell().getDisplay().asyncExec(new Runnable() {\r
140                                 @Override\r
141                                 public void run() {\r
142                                          for (Entry<String, Resource> e : sorter.entrySet()) {    \r
143                                 typeList.add(e.getKey());\r
144                                 typeList.setData(e.getKey(), e.getValue());\r
145                                          }\r
146                                 }\r
147                         });\r
148                 }\r
149         });\r
150         \r
151         typeList.addSelectionListener(new SelectionListener() {\r
152             public void widgetSelected(SelectionEvent e) {\r
153                 session.asyncRead(new GraphRequestAdapter() {\r
154                         @Override\r
155                         public GraphRequestStatus perform(Graph g) throws Exception {\r
156                                  String key = typeList.getItem(typeList.getSelectionIndex());\r
157                          selectType(EntityFactory.create(g,(Resource) typeList.getData(key)));\r
158                                  return GraphRequestStatus.transactionComplete();\r
159                         }\r
160                 });\r
161                \r
162             }\r
163             public void widgetDefaultSelected(SelectionEvent e) {\r
164             }\r
165         });\r
166         \r
167      \r
168         \r
169         Composite buttonComposite = new Composite(composite,SWT.NONE);\r
170         buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,4,1));\r
171         buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
172         \r
173         \r
174         applyAnimationButton = new Button(buttonComposite,SWT.PUSH);\r
175         applyAnimationButton.setText("Apply monitor configuration");\r
176         applyAnimationButton.addSelectionListener(new SelectionAdapter() {\r
177                 @Override\r
178                 public void widgetSelected(SelectionEvent e) {\r
179                         applyAnimation();\r
180                 }\r
181         });\r
182         return composite;\r
183         }\r
184         \r
185         /**\r
186          * Updates type selection:\r
187          * Finds all GraphicsModels that are connected to instances of selected type with HasGraphics-relation\r
188          * and lists them in modelList.\r
189          *\r
190          * @param resource\r
191          */\r
192         private void selectType(IEntity resource) {\r
193                 selectedType = resource.getResource();\r
194                 \r
195                 TreeMap<String, G3DModel> sorter = new TreeMap<String, G3DModel>();\r
196                 \r
197                 Collection<IEntity> models = resource.getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics);\r
198         for (IEntity model : models) {\r
199                 \r
200                         String key = model.getName();\r
201                 if (key.equals("")) key = "ERROR (" + model.getResource().getResourceId(resource.getGraph().getSession()) + ")";\r
202                 sorter.put(key, new G3DModel(model));\r
203         }\r
204         \r
205         if (oe != null) {\r
206                 oe.dispose();   \r
207         }\r
208         sampleInstance = null;\r
209         sampleSource = null;\r
210         for (Resource n : nodes) {\r
211                 IEntity t = EntityFactory.create(resource.getGraph(),n);\r
212                 if (t.isInstanceOf(resource.getResource())) {\r
213                         sampleInstance = n;\r
214                         break;\r
215                 }\r
216         }\r
217         if (sampleInstance != null) {\r
218 //              oe = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId()));\r
219 //              if(viewpointControl != null && !viewpointControl.isDisposed()) {\r
220 //                      viewpointControl.dispose();\r
221 //              }\r
222 //              viewpointControl = oe.getControl(oeLabelComposite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE);\r
223 //              Control c = oe.getControl(oeComposite, 1, OntologyExplorer.OntologyTree, SWT.MULTI);\r
224 //              c.setLayoutData(new GridData(GridData.FILL_BOTH));\r
225 //              oe.init(null, ViewpointUtils.getModelledHandler(oe.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false);\r
226 //              oe.setSelectionScheme(new SourceSelectionScheme(oe));\r
227 //              oeComposite.getParent().layout(true,true);\r
228                 oe = new GraphExplorer(oeComposite,SWT.MULTI);\r
229                 oe.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));\r
230                 oe.getViewer().addPostSelectionChangedListener(new OEPathSelectionListener(){\r
231                         @Override\r
232                         protected void pathSelectionUpdated(java.util.List<TraversalPath> paths) {\r
233                                 if (paths.size() == 0) {\r
234                         //selectSource(null);\r
235                     } else {\r
236                         final java.util.List<TraversalPath> ps = paths;\r
237                         session.asyncRead(new GraphRequestAdapter() {\r
238                                 @Override\r
239                                 public GraphRequestStatus perform(Graph g) throws Exception {\r
240                                         selectSource(g,ps);\r
241                                         return GraphRequestStatus.transactionComplete();\r
242                                 }\r
243                         });\r
244                         \r
245                     }\r
246                                 \r
247                         }\r
248                 });\r
249         }\r
250                 \r
251         }\r
252         \r
253         \r
254         \r
255         private void selectSource(Graph graph, java.util.List<TraversalPath> resources) {\r
256                 if (resources == null) {\r
257                         sampleSource = null;\r
258                 }\r
259                 for (TraversalPath p : resources) {\r
260                         IEntity t = EntityFactory.create(graph, p.getResource());\r
261                         if (!t.isInstanceOf(ProcessResource.builtins.Double)) {\r
262                                 sampleSource = null;\r
263                                 return;\r
264                         }\r
265                 }\r
266                 sampleSource = new ArrayList<Resource>();\r
267                 for (TraversalPath p : resources)\r
268                         sampleSource.add(p.getResource());\r
269                 \r
270                 samplePath = new ArrayList<java.util.List<Resource>>();\r
271                 for (int i = 0; i < sampleSource.size(); i++) {\r
272                         TraversalPath tpath = resources.get(i);\r
273                         java.util.List<Resource> path = new ArrayList<Resource>();\r
274                         PathUtils.createPath(path, tpath,sampleInstance,sampleSource.get(i),oe);\r
275                         samplePath.add(path);\r
276                 }       \r
277                 \r
278         }\r
279         \r
280         private void applyAnimation() {\r
281                 \r
282                 if (sampleInstance == null || sampleSource == null || samplePath == null ) {\r
283                         throw new RuntimeException("Missing required selections");\r
284                         //return;\r
285                 }\r
286 \r
287                 PathContainer.getInstance().clearPaths(selectedType);\r
288                 for (java.util.List<Resource> s : samplePath) {\r
289                         PathContainer.getInstance().addPath(selectedType, s);\r
290                 }\r
291         }\r
292         \r
293 \r
294         \r
295 \r
296 \r
297         private Collection<Resource> getTypes(Graph g, java.util.List<Resource> instances) {\r
298                 Set<Resource> types = new HashSet<Resource>();\r
299                 for (Resource r : instances) {\r
300                         IEntity t = EntityFactory.create(g,r);\r
301                         Collection<IEntity> tTypes = t.getTypes();\r
302                         for (IEntity type : tTypes)\r
303                                 types.add(type.getResource());\r
304                 }\r
305                 return types;\r
306         }\r
307         \r
308         \r
309 }\r