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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.dialogs;
\r
13 import java.util.ArrayList;
\r
14 import java.util.List;
\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.ErrorLogger;
\r
50 public class ConfigurePipelineAnimationDialog extends Dialog {
\r
52 java.util.List<Resource> nodes;
\r
54 //private Control viewpointControl1;
\r
55 private GraphExplorer oe1;
\r
58 //private Control viewpointControl2;
\r
59 private GraphExplorer oe2;
\r
61 JmeRenderingComponent component;
\r
63 private Button applyAnimationButton;
\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
72 Button scaleButton1;
\r
76 Button scaleButton2;
\r
80 public ConfigurePipelineAnimationDialog(Shell parentShell, Session session,java.util.List<Resource> nodes, JmeRenderingComponent component, AnimationSystem animationSystem) {
\r
82 this.session = session;
\r
84 this.component = component;
\r
85 this.animationSystem = animationSystem;
\r
86 int shellStyle = getShellStyle();
\r
87 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
\r
91 protected void configureShell(Shell newShell) {
\r
92 super.configureShell(newShell);
\r
93 newShell.setText("Configure pipeline animations");
\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
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
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
118 sampleInstance = nodes.get(0).getResource();
\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
123 // oe2 = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId()));
\r
124 // viewpointControl2 = oe2.getControl(composite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE);
\r
126 // Control c = oe1.getControl(composite, 1, OntologyExplorer.OntologyTree, SWT.SINGLE|SWT.BORDER);
\r
127 // c.setLayoutData(new GridData(GridData.FILL_BOTH));
\r
129 // c = oe2.getControl(composite, 1, OntologyExplorer.OntologyTree, SWT.SINGLE|SWT.BORDER);
\r
130 // c.setLayoutData(new GridData(GridData.FILL_BOTH));
\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
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
138 oe1 = new GraphExplorer(composite,SWT.SINGLE);
\r
139 oe1.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
\r
141 oe2 = new GraphExplorer(composite,SWT.SINGLE);
\r
142 oe2.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
\r
144 oe1.getViewer().addPostSelectionChangedListener(new OEPathListener(oe1));
\r
145 oe2.getViewer().addPostSelectionChangedListener(new OEPathListener(oe2));
\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
152 scaleButton1 = new Button(buttonComposite,SWT.CHECK);
\r
153 scaleButton1.setText("Scaling");
\r
154 scaleButton1.addSelectionListener(new SelectionAdapter() {
\r
156 public void widgetSelected(SelectionEvent e) {
\r
157 if (scaleButton1.getSelection()) {
\r
158 minText1.setEnabled(true);
\r
159 maxText1.setEnabled(true);
\r
161 minText1.setEnabled(false);
\r
162 maxText1.setEnabled(false);
\r
166 minText1 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);
\r
167 minText1.setText("0.0");
\r
169 maxText1 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);
\r
170 maxText1.setText("1.0");
\r
172 minText1.setEnabled(false);
\r
173 maxText1.setEnabled(false);
\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
179 scaleButton2 = new Button(buttonComposite,SWT.CHECK);
\r
180 scaleButton2.setText("Scaling");
\r
181 scaleButton2.addSelectionListener(new SelectionAdapter() {
\r
183 public void widgetSelected(SelectionEvent e) {
\r
184 if (scaleButton2.getSelection()) {
\r
185 minText2.setEnabled(true);
\r
186 maxText2.setEnabled(true);
\r
188 minText2.setEnabled(false);
\r
189 maxText2.setEnabled(false);
\r
193 minText2 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);
\r
194 minText2.setText("0.0");
\r
196 maxText2 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER);
\r
197 maxText2.setText("1.0");
\r
199 minText2.setEnabled(false);
\r
200 maxText2.setEnabled(false);
\r
203 applyAnimationButton = new Button(composite,SWT.PUSH);
\r
204 applyAnimationButton.setText("Apply animation");
\r
205 applyAnimationButton.addSelectionListener(new SelectionAdapter() {
\r
207 public void widgetSelected(SelectionEvent e) {
\r
208 session.syncRead(new GraphRequestAdapter() {
\r
210 public GraphRequestStatus perform(Graph g) throws Exception {
\r
212 return GraphRequestStatus.transactionComplete();
\r
218 applyAnimationButton.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,2,1));
\r
222 private class OEPathListener extends OEPathSelectionListener {
\r
225 public OEPathListener(GraphExplorer oe) {
\r
229 protected void pathSelectionUpdated(List<TraversalPath> paths) {
\r
230 if (paths.size() == 0) {
\r
231 //selectSource(null);
\r
233 selectSource(oe,paths.get(0));
\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
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
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
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
273 for (int i = 0; i < instances1.size(); i++) {
\r
280 if (scaleButton1.getSelection()) {
\r
281 cMin = Double.parseDouble(minText1.getText());
\r
282 cMax = Double.parseDouble(maxText1.getText());
\r
284 if (scaleButton2.getSelection()) {
\r
285 vMin = Double.parseDouble(minText2.getText());
\r
286 vMax = Double.parseDouble(maxText2.getText());
\r
288 ResourcePipeAnimationController c = new ResourcePipeAnimationController(component, new PipeRun(graph,instances1.get(i)),sources1.get(i),cMin,cMax,sources2.get(i),vMin,vMax);
\r
290 animationSystem.add(c);
\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
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
308 ErrorLogger.getDefault().logWarning("Cannot find animation source for " + instance, null);
\r