1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\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.tools;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Collection;
\r
15 import java.util.List;
\r
16 import java.util.Stack;
\r
18 import org.eclipse.jface.action.Action;
\r
19 import org.eclipse.jface.action.IMenuManager;
\r
20 import org.eclipse.jface.action.IToolBarManager;
\r
21 import org.eclipse.swt.layout.FillLayout;
\r
22 import org.eclipse.swt.widgets.Composite;
\r
23 import org.simantics.db.Graph;
\r
24 import org.simantics.db.GraphRequestAdapter;
\r
25 import org.simantics.db.GraphRequestStatus;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.processeditor.Activator;
\r
28 import org.simantics.processeditor.ProcessResource;
\r
29 import org.simantics.processeditor.animations.PipeAnimationController;
\r
30 import org.simantics.processeditor.dialogs.ConfigureAnimationDialog;
\r
31 import org.simantics.processeditor.dialogs.ConfigureMonitorDialog;
\r
32 import org.simantics.processeditor.dialogs.ConfigurePipelineAnimationDialog;
\r
33 import org.simantics.processeditor.monitors.Monitor;
\r
34 import org.simantics.processeditor.monitors.ResourcePathPropertyProvider;
\r
35 import org.simantics.processeditor.monitors.TextMonitor;
\r
36 import org.simantics.processeditor.stubs.PipeRun;
\r
37 import org.simantics.proconf.g3d.actions.ContextAction;
\r
38 import org.simantics.proconf.g3d.actions.FocusAction;
\r
39 import org.simantics.proconf.g3d.animation.Animatable;
\r
40 import org.simantics.proconf.g3d.animation.AnimationController;
\r
41 import org.simantics.proconf.g3d.animation.AnimationSystem;
\r
42 import org.simantics.proconf.g3d.animation.TestAnimationController;
\r
43 import org.simantics.proconf.g3d.animation.ui.AnimationControlCreator;
\r
44 import org.simantics.proconf.g3d.base.EditorContribution;
\r
45 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;
\r
46 import org.simantics.proconf.g3d.common.StructuredResourceSelection;
\r
47 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;
\r
48 import org.simantics.proconf.g3d.stubs.G3DNode;
\r
49 import org.simantics.utils.ui.jface.MenuTools;
\r
52 * Plant Visualization Contribution
\r
54 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
57 public class PlantVisualizationContribution implements EditorContribution {
\r
58 private List<ContextAction> actions = new ArrayList<ContextAction>();
\r
59 private ThreeDimensionalEditorBase parent;
\r
61 private AnimationSystem animationSystem = null;
\r
62 private Action animatePipesAction = null;
\r
63 private Action animateAction = null;
\r
64 private Action showMonitorsAction = null;
\r
65 private Action configureAnimationAction = null;
\r
66 private Action configurePipelineAnimationAction = null;
\r
67 private Action configureMonitorAction = null;
\r
69 private Monitor monitor;
\r
71 public PlantVisualizationContribution(ThreeDimensionalEditorBase parent) {
\r
72 this.parent = parent;
\r
76 public void createControl(Composite parent) {
\r
77 parent.setLayout(new FillLayout());
\r
78 this.parent.getRenderingComposite().setLayoutData(null);
\r
82 public void disposeControl() {
\r
87 public void dispose() {
\r
92 public void fillContextMenu(Graph graph, IMenuManager manager,
\r
93 StructuredResourceSelection selection) {
\r
98 public void fillLocalPullDown(IMenuManager menuManager) {
\r
99 MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(showMonitorsAction);
\r
100 MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(configureMonitorAction);
\r
101 IMenuManager animationMenu = MenuTools.getOrCreate(parent.getMenuID(),"Animations",menuManager);
\r
102 animationMenu.add(configureAnimationAction);
\r
103 animationMenu.add(configurePipelineAnimationAction);
\r
104 MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animateAction);
\r
105 MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animatePipesAction);
\r
110 // public void fillMainMenu(List<IContributionItem> list) {
\r
111 // MenuTools.getOrCreate("Monitors", list).add(showMonitorsAction);
\r
112 // MenuTools.getOrCreate("Monitors", list).add(configureMonitorAction);
\r
113 // IMenuManager animationMenu = MenuTools.getOrCreate("Animations",list);
\r
114 // animationMenu.add(configureAnimationAction);
\r
115 // animationMenu.add(configurePipelineAnimationAction);
\r
116 // MenuTools.getOrCreate("Test", animationMenu).add(animateAction);
\r
117 // MenuTools.getOrCreate("Test", animationMenu).add(animatePipesAction);
\r
122 public void fillLocalToolBar(IToolBarManager manager) {
\r
123 AnimationControlCreator c = new AnimationControlCreator(getAnimationSystem());
\r
124 manager.add(c.createStopAction());
\r
125 manager.add(c.createPauseAction());
\r
126 manager.add(c.createPlayAction());
\r
127 manager.add(showMonitorsAction);
\r
131 public Collection<ContextAction> getActions() {
\r
136 public String getName() {
\r
137 return "Plant Visualization";
\r
141 public void run() {
\r
142 parent.getSession().syncRead(new GraphRequestAdapter() {
\r
144 public GraphRequestStatus perform(Graph g) throws Exception {
\r
145 getAnimationSystem().run(g,0.01);
\r
147 return GraphRequestStatus.transactionComplete();
\r
154 private G3DNode getRoot(Graph g) {
\r
155 return new G3DNode(g,parent.getScenegraphAdapter().getRootResource());
\r
159 public void initialize(Graph graph) {
\r
160 animateAction = new Action("", Action.AS_CHECK_BOX) {
\r
161 public void run() {
\r
162 if (this.isChecked()) {
\r
163 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
165 public GraphRequestStatus perform(Graph g)
\r
167 AnimationController c = new TestAnimationController();
\r
168 Collection<IGraphicsNode> nodes = parent.getScenegraphAdapter().getNodes();
\r
169 for (IGraphicsNode node : nodes) {
\r
170 if (node instanceof Animatable) {
\r
171 Animatable a = (Animatable) node;
\r
172 if (a.setRandomAnimation(g))
\r
173 c.addAnimatable(a);
\r
176 animationSystem.add(c);
\r
177 return GraphRequestStatus.transactionComplete();
\r
181 // showMessage("Activated " + animatables.size()
\r
182 // + " animations");
\r
184 animationSystem.stop();
\r
189 animateAction.setText("Random animations");
\r
191 animatePipesAction = new AnimatePipesAction("Pipe animations");
\r
193 showMonitorsAction = new Action("Interactive Monitor", Action.AS_CHECK_BOX) {
\r
194 public void run() {
\r
195 showMonitors(this.isChecked());
\r
198 showMonitorsAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor.png"));
\r
202 configureAnimationAction = new Action() {
\r
204 public void run() {
\r
205 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
206 List <Resource> list = new ArrayList<Resource>();
\r
208 public GraphRequestStatus perform(Graph g) throws Exception {
\r
209 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
210 stack.add(getRoot(g));
\r
211 while (!stack.isEmpty()) {
\r
212 G3DNode n = stack.pop();
\r
213 list.add(n.getResource());
\r
214 for (G3DNode no : n.getChild())
\r
218 if (list.size() == 0)
\r
219 return GraphRequestStatus.transactionComplete();
\r
221 ConfigureAnimationDialog dialog = new ConfigureAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getScenegraphAdapter(),getAnimationSystem());
\r
223 return GraphRequestStatus.transactionComplete();
\r
232 configureAnimationAction.setText("Configure animations");
\r
233 configureAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));
\r
235 configurePipelineAnimationAction = new Action() {
\r
237 public void run() {
\r
238 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
239 List <Resource> list = new ArrayList<Resource>();
\r
241 public GraphRequestStatus perform(Graph g) throws Exception {
\r
242 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
243 stack.add(getRoot(g));
\r
244 while (!stack.isEmpty()) {
\r
245 G3DNode n = stack.pop();
\r
246 if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))
\r
247 list.add(n.getResource());
\r
248 else //piperun will not contain other piperuns
\r
249 for (G3DNode no : n.getChild())
\r
253 if (list.size() == 0)
\r
254 return GraphRequestStatus.transactionComplete();
\r
256 ConfigurePipelineAnimationDialog dialog = new ConfigurePipelineAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getRenderingComponent(),getAnimationSystem());
\r
258 return GraphRequestStatus.transactionComplete();
\r
267 configurePipelineAnimationAction.setText("Configure pipeline animations");
\r
268 configurePipelineAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));
\r
269 configureMonitorAction = new Action() {
\r
271 public void run() {
\r
272 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
273 List <Resource> list = new ArrayList<Resource>();
\r
275 public GraphRequestStatus perform(Graph g) throws Exception {
\r
276 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
277 stack.add(getRoot(g));
\r
278 while (!stack.isEmpty()) {
\r
279 G3DNode n = stack.pop();
\r
280 list.add(n.getResource());
\r
281 for (G3DNode no : n.getChild())
\r
285 if (list.size() == 0)
\r
286 return GraphRequestStatus.transactionComplete();
\r
288 ConfigureMonitorDialog dialog = new ConfigureMonitorDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list);
\r
290 return GraphRequestStatus.transactionComplete();
\r
297 configureMonitorAction.setText("Configure monitor");
\r
298 configureMonitorAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor_edit.png"));
\r
300 actions.add(new FocusAction(parent));
\r
303 private AnimationSystem getAnimationSystem() {
\r
304 if (animationSystem == null) {
\r
305 animationSystem = new AnimationSystem(parent.getScenegraphAdapter());
\r
307 return animationSystem;
\r
310 protected void updateMonitor(Graph graph) {
\r
311 if (showMonitorsAction.isChecked()) {
\r
313 List<IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();
\r
314 if (nodes.size() > 0) {
\r
315 IGraphicsNode selected = nodes.get(0);
\r
316 if (monitor.acceptNode(graph,selected))
\r
317 monitor.setNode(graph,selected);
\r
323 protected Monitor createMonitor() {
\r
324 Monitor m = new TextMonitor(parent);
\r
325 m.setTextProvider(new ResourcePathPropertyProvider());
\r
329 private void showMonitors(boolean show) {
\r
331 if (monitor == null) {
\r
332 monitor = createMonitor();
\r
334 final List <IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();
\r
335 if (nodes.size() > 0) {
\r
336 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
338 public GraphRequestStatus perform(Graph g) throws Exception {
\r
339 monitor.setNode(g,nodes.get(0));
\r
340 return GraphRequestStatus.transactionComplete();
\r
346 } else if (!show && monitor != null) {
\r
348 parent.setViewChanged(true);
\r
352 private class AnimatePipesAction extends Action {
\r
354 public AnimatePipesAction(String text) {
\r
355 super(text, Action.AS_CHECK_BOX);
\r
359 public void run() {
\r
360 if (this.isChecked()) {
\r
361 parent.getSession().syncRead(new GraphRequestAdapter() {
\r
362 List<PipeRun> list = new ArrayList<PipeRun>();
\r
365 public GraphRequestStatus perform(Graph g) throws Exception {
\r
366 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
367 stack.add(getRoot(g));
\r
368 while (!stack.isEmpty()) {
\r
369 G3DNode n = stack.pop();
\r
370 if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))
\r
371 list.add(new PipeRun(n));
\r
373 //piperun will not contain other piperuns
\r
374 for (G3DNode no : n.getChild())
\r
378 for (PipeRun n : list) {
\r
379 PipeAnimationController c = new PipeAnimationController(parent.getRenderingComponent(), n);
\r
380 //animationSystem.add(c);
\r
381 getAnimationSystem().add(c);
\r
383 return GraphRequestStatus.transactionComplete();
\r
389 //stopAnimations();
\r
390 getAnimationSystem().stop();
\r