]> gerrit.simantics Code Review - simantics/3d.git/blob
c66b266319befffc69a8f44fb75ac3e2503d4621
[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.List;\r
15 \r
16 import org.eclipse.jface.dialogs.Dialog;\r
17 import org.eclipse.jface.dialogs.IDialogConstants;\r
18 import org.eclipse.swt.SWT;\r
19 import org.eclipse.swt.events.SelectionAdapter;\r
20 import org.eclipse.swt.events.SelectionEvent;\r
21 import org.eclipse.swt.layout.FillLayout;\r
22 import org.eclipse.swt.layout.GridData;\r
23 import org.eclipse.swt.layout.GridLayout;\r
24 import org.eclipse.swt.widgets.Button;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Control;\r
27 import org.eclipse.swt.widgets.Label;\r
28 import org.eclipse.swt.widgets.Shell;\r
29 import org.eclipse.swt.widgets.Text;\r
30 import org.simantics.db.Graph;\r
31 import org.simantics.db.GraphRequestAdapter;\r
32 import org.simantics.db.GraphRequestStatus;\r
33 import org.simantics.db.Resource;\r
34 import org.simantics.db.Session;\r
35 import org.simantics.layer0.utils.EntityFactory;\r
36 import org.simantics.layer0.utils.IEntity;\r
37 import org.simantics.layer0.utils.viewpoints.TraversalPath;\r
38 import org.simantics.processeditor.ProcessResource;\r
39 import org.simantics.processeditor.animations.ResourcePipeAnimationController;\r
40 import org.simantics.processeditor.common.PathUtils;\r
41 import org.simantics.processeditor.stubs.PipeRun;\r
42 import org.simantics.proconf.browsing.GraphExplorer;\r
43 import org.simantics.proconf.g3d.animation.AnimationSystem;\r
44 import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
45 import org.simantics.proconf.g3d.tools.OEPathSelectionListener;\r
46 import org.simantics.utils.ui.ErrorLogger;\r
47 \r
48 \r
49 \r
50 public class ConfigurePipelineAnimationDialog extends Dialog {\r
51         Session session;\r
52         java.util.List<Resource> nodes;\r
53         \r
54         //private Control viewpointControl1;\r
55         private GraphExplorer oe1;\r
56         \r
57         \r
58         //private Control viewpointControl2;\r
59         private GraphExplorer oe2;\r
60         \r
61         JmeRenderingComponent component;\r
62         \r
63         private Button applyAnimationButton;\r
64         \r
65         Resource sampleInstance;\r
66         Resource sampleSource1;\r
67         Resource sampleSource2;\r
68         java.util.List<Resource> samplePath1;\r
69         java.util.List<Resource> samplePath2;\r
70         AnimationSystem animationSystem;\r
71         \r
72         Button scaleButton1;\r
73         Text minText1;\r
74         Text maxText1;\r
75         \r
76         Button scaleButton2;\r
77         Text minText2;\r
78         Text maxText2;\r
79         \r
80         public ConfigurePipelineAnimationDialog(Shell parentShell, Session session,java.util.List<Resource> nodes, JmeRenderingComponent component, AnimationSystem animationSystem) {\r
81                 super(parentShell);\r
82                 this.session = session;\r
83                 this.nodes = nodes;\r
84                 this.component = component;\r
85                 this.animationSystem = animationSystem;\r
86                 int shellStyle = getShellStyle();\r
87                 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);\r
88         }\r
89         \r
90         @Override\r
91     protected void configureShell(Shell newShell) {  \r
92         super.configureShell(newShell);\r
93         newShell.setText("Configure pipeline animations");\r
94         \r
95     }\r
96         \r
97         protected Control createDialogArea(Composite parent) {\r
98         Composite composite = (Composite) super.createDialogArea(parent);\r
99         GridLayout layout = new GridLayout(2,true);\r
100                 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);\r
101                 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);\r
102                 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);\r
103                 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);\r
104                 composite.setLayout(layout);\r
105                 \r
106                 Label label = new Label(composite, SWT.WRAP);\r
107         label.setText("Color Change");\r
108         GridData data = new GridData(GridData.CENTER,GridData.FILL,true,false,1,1);\r
109         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
110         label.setLayoutData(data);\r
111         label.setFont(parent.getFont());\r
112         \r
113         label = new Label(composite, SWT.WRAP);\r
114         label.setText("Particle velocity");\r
115         label.setLayoutData(data);\r
116         label.setFont(parent.getFont());\r
117         \r
118         sampleInstance = nodes.get(0).getResource();\r
119         \r
120 //      oe1 = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId()));\r
121 //      viewpointControl1 = oe1.getControl(composite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE);\r
122 //      \r
123 //      oe2 = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId()));\r
124 //      viewpointControl2 = oe2.getControl(composite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE);\r
125 //      \r
126 //      Control c = oe1.getControl(composite, 1, OntologyExplorer.OntologyTree, SWT.SINGLE|SWT.BORDER);\r
127 //      c.setLayoutData(new GridData(GridData.FILL_BOTH));\r
128 //      \r
129 //      c = oe2.getControl(composite, 1, OntologyExplorer.OntologyTree, SWT.SINGLE|SWT.BORDER);\r
130 //      c.setLayoutData(new GridData(GridData.FILL_BOTH));\r
131 //      \r
132 //      oe1.init(null, ViewpointUtils.getModelledHandler(oe1.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false);\r
133 //      oe1.setSelectionScheme(new SourceSelectionScheme(oe1));\r
134 //      \r
135 //      oe2.init(null, ViewpointUtils.getModelledHandler(oe1.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false);\r
136 //      oe2.setSelectionScheme(new SourceSelectionScheme(oe2));\r
137         \r
138         oe1 = new GraphExplorer(composite,SWT.SINGLE);\r
139         oe1.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));\r
140         \r
141         oe2 = new GraphExplorer(composite,SWT.SINGLE);\r
142         oe2.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));\r
143         \r
144         oe1.getViewer().addPostSelectionChangedListener(new OEPathListener(oe1));\r
145         oe2.getViewer().addPostSelectionChangedListener(new OEPathListener(oe2));\r
146         \r
147         \r
148         Composite buttonComposite = new Composite(composite,SWT.NONE);\r
149         buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,1,1));\r
150         buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
151         \r
152         scaleButton1 = new Button(buttonComposite,SWT.CHECK);\r
153         scaleButton1.setText("Scaling");\r
154         scaleButton1.addSelectionListener(new SelectionAdapter() {\r
155                 @Override\r
156                 public void widgetSelected(SelectionEvent e) {\r
157                         if (scaleButton1.getSelection()) {\r
158                                 minText1.setEnabled(true);\r
159                                 maxText1.setEnabled(true);\r
160                         } else {\r
161                                 minText1.setEnabled(false);\r
162                                 maxText1.setEnabled(false);\r
163                         }\r
164                 }\r
165         });\r
166         minText1 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);\r
167         minText1.setText("0.0");\r
168         \r
169         maxText1 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);\r
170         maxText1.setText("1.0");\r
171         \r
172         minText1.setEnabled(false);\r
173                 maxText1.setEnabled(false);\r
174                 \r
175                 buttonComposite = new Composite(composite,SWT.NONE);\r
176         buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,1,1));\r
177         buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
178         \r
179                 scaleButton2 = new Button(buttonComposite,SWT.CHECK);\r
180         scaleButton2.setText("Scaling");\r
181         scaleButton2.addSelectionListener(new SelectionAdapter() {\r
182                 @Override\r
183                 public void widgetSelected(SelectionEvent e) {\r
184                         if (scaleButton2.getSelection()) {\r
185                                 minText2.setEnabled(true);\r
186                                 maxText2.setEnabled(true);\r
187                         } else {\r
188                                 minText2.setEnabled(false);\r
189                                 maxText2.setEnabled(false);\r
190                         }\r
191                 }\r
192         });\r
193         minText2 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);\r
194         minText2.setText("0.0");\r
195         \r
196         maxText2 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);\r
197         maxText2.setText("1.0");\r
198         \r
199         minText2.setEnabled(false);\r
200                 maxText2.setEnabled(false);\r
201         \r
202                 \r
203                 applyAnimationButton = new Button(composite,SWT.PUSH);\r
204         applyAnimationButton.setText("Apply animation");\r
205         applyAnimationButton.addSelectionListener(new SelectionAdapter() {\r
206                 @Override\r
207                 public void widgetSelected(SelectionEvent e) {\r
208                         session.syncRead(new GraphRequestAdapter() {\r
209                                 @Override\r
210                                 public GraphRequestStatus perform(Graph g) throws Exception {\r
211                                         applyAnimation(g);\r
212                                         return GraphRequestStatus.transactionComplete();\r
213                                 }\r
214                         });\r
215                         \r
216                 }\r
217         });\r
218         applyAnimationButton.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,2,1));\r
219         return composite;\r
220         }\r
221         \r
222         private class OEPathListener extends OEPathSelectionListener {\r
223                 GraphExplorer oe;\r
224                 \r
225                 public OEPathListener(GraphExplorer oe) {\r
226                         this.oe = oe;\r
227                 }\r
228 \r
229         protected void pathSelectionUpdated(List<TraversalPath> paths) {\r
230             if (paths.size() == 0) {\r
231                 //selectSource(null);\r
232             } else {\r
233                 selectSource(oe,paths.get(0));\r
234             }\r
235         }\r
236         }\r
237         \r
238         private void selectSource(GraphExplorer oe,TraversalPath path) {\r
239                 Graph graph = null;\r
240                 IEntity t = EntityFactory.create(graph,path.getResource());\r
241                 if (!t.isInstanceOf(ProcessResource.builtins.Double)) \r
242                         return;\r
243                 if (oe == oe1) {\r
244                         sampleSource1 = path.getResource();\r
245                         samplePath1 = new ArrayList<Resource>();\r
246                         PathUtils.createPath(samplePath1, path,sampleInstance,sampleSource1,oe);\r
247                 } else if (oe == oe2) {\r
248                         sampleSource2 = path.getResource();\r
249                         samplePath2 = new ArrayList<Resource>();\r
250                         PathUtils.createPath(samplePath2, path,sampleInstance,sampleSource2,oe);\r
251                 }\r
252         }\r
253         \r
254         \r
255         \r
256         \r
257         private void applyAnimation(Graph graph) {\r
258                 if (sampleInstance == null || sampleSource1 == null || sampleSource2 == null || samplePath1 == null|| samplePath2 == null) {\r
259                         throw new RuntimeException("Missing required selections");\r
260                         //return;\r
261                 }\r
262                 java.util.List<Resource> instances1 = new ArrayList<Resource>();\r
263                 java.util.List<Resource> instances2 = new ArrayList<Resource>();\r
264                 java.util.List<Resource> sources1 = new ArrayList<Resource>();\r
265                 java.util.List<Resource> sources2 = new ArrayList<Resource>();\r
266                 browseOthers(instances1, sources1,samplePath1);\r
267                 browseOthers(instances2, sources2,samplePath2);\r
268                 instances1.add(sampleInstance);\r
269                 instances2.add(sampleInstance); // instances1 == instances2\r
270                 sources1.add(sampleSource1);\r
271                 sources2.add(sampleSource2);\r
272                 \r
273                 for (int i = 0; i < instances1.size(); i++) {\r
274                         \r
275                         double cMin = 0.0;\r
276                         double cMax = 1.0;\r
277                         double vMin = 0.0;\r
278                         double vMax = 0.0;\r
279                         \r
280                         if (scaleButton1.getSelection()) {\r
281                                 cMin = Double.parseDouble(minText1.getText());\r
282                                 cMax = Double.parseDouble(maxText1.getText());\r
283                         }\r
284                         if (scaleButton2.getSelection()) {\r
285                                 vMin = Double.parseDouble(minText2.getText());\r
286                                 vMax = Double.parseDouble(maxText2.getText());\r
287                         }\r
288                         ResourcePipeAnimationController c = new ResourcePipeAnimationController(component, new PipeRun(graph,instances1.get(i)),sources1.get(i),cMin,cMax,sources2.get(i),vMin,vMax);\r
289                         \r
290                         animationSystem.add(c);\r
291                 }\r
292                 \r
293         }\r
294         \r
295         private void browseOthers(java.util.List<Resource> instances,java.util.List<Resource> sources,java.util.List<Resource> samplePath) {\r
296                 Graph graph = null;\r
297                 java.util.List<Resource> possibleInstances = new ArrayList<Resource>();\r
298                 for (Resource n : nodes) {\r
299                         possibleInstances.add(n);\r
300                 }\r
301                 \r
302                 for (Resource instance : possibleInstances) {\r
303                         IEntity source = PathUtils.findSimilar(samplePath,EntityFactory.create(graph,instance));\r
304                         if (source != null) {\r
305                                 instances.add(instance);\r
306                                 sources.add(source.getResource());\r
307                         } else {\r
308                                 ErrorLogger.getDefault().logWarning("Cannot find animation source for " + instance, null);\r
309                         }\r
310                 }\r
311         }\r
312         \r
313         \r
314         \r
315 }\r