/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.processeditor.tools; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Stack; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.simantics.db.Graph; import org.simantics.db.GraphRequestAdapter; import org.simantics.db.GraphRequestStatus; import org.simantics.db.Resource; import org.simantics.processeditor.Activator; import org.simantics.processeditor.ProcessResource; import org.simantics.processeditor.animations.PipeAnimationController; import org.simantics.processeditor.dialogs.ConfigureAnimationDialog; import org.simantics.processeditor.dialogs.ConfigureMonitorDialog; import org.simantics.processeditor.dialogs.ConfigurePipelineAnimationDialog; import org.simantics.processeditor.monitors.Monitor; import org.simantics.processeditor.monitors.ResourcePathPropertyProvider; import org.simantics.processeditor.monitors.TextMonitor; import org.simantics.processeditor.stubs.PipeRun; import org.simantics.proconf.g3d.actions.ContextAction; import org.simantics.proconf.g3d.actions.FocusAction; import org.simantics.proconf.g3d.animation.Animatable; import org.simantics.proconf.g3d.animation.AnimationController; import org.simantics.proconf.g3d.animation.AnimationSystem; import org.simantics.proconf.g3d.animation.TestAnimationController; import org.simantics.proconf.g3d.animation.ui.AnimationControlCreator; import org.simantics.proconf.g3d.base.EditorContribution; import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase; import org.simantics.proconf.g3d.common.StructuredResourceSelection; import org.simantics.proconf.g3d.scenegraph.IGraphicsNode; import org.simantics.proconf.g3d.stubs.G3DNode; import org.simantics.utils.ui.jface.MenuTools; /** * Plant Visualization Contribution * * @author Marko Luukkainen * */ public class PlantVisualizationContribution implements EditorContribution { private List actions = new ArrayList(); private ThreeDimensionalEditorBase parent; private AnimationSystem animationSystem = null; private Action animatePipesAction = null; private Action animateAction = null; private Action showMonitorsAction = null; private Action configureAnimationAction = null; private Action configurePipelineAnimationAction = null; private Action configureMonitorAction = null; private Monitor monitor; public PlantVisualizationContribution(ThreeDimensionalEditorBase parent) { this.parent = parent; } @Override public void createControl(Composite parent) { parent.setLayout(new FillLayout()); this.parent.getRenderingComposite().setLayoutData(null); } @Override public void disposeControl() { } @Override public void dispose() { } @Override public void fillContextMenu(Graph graph, IMenuManager manager, StructuredResourceSelection selection) { } @Override public void fillLocalPullDown(IMenuManager menuManager) { MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(showMonitorsAction); MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(configureMonitorAction); IMenuManager animationMenu = MenuTools.getOrCreate(parent.getMenuID(),"Animations",menuManager); animationMenu.add(configureAnimationAction); animationMenu.add(configurePipelineAnimationAction); MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animateAction); MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animatePipesAction); } // @Override // public void fillMainMenu(List list) { // MenuTools.getOrCreate("Monitors", list).add(showMonitorsAction); // MenuTools.getOrCreate("Monitors", list).add(configureMonitorAction); // IMenuManager animationMenu = MenuTools.getOrCreate("Animations",list); // animationMenu.add(configureAnimationAction); // animationMenu.add(configurePipelineAnimationAction); // MenuTools.getOrCreate("Test", animationMenu).add(animateAction); // MenuTools.getOrCreate("Test", animationMenu).add(animatePipesAction); // // } @Override public void fillLocalToolBar(IToolBarManager manager) { AnimationControlCreator c = new AnimationControlCreator(getAnimationSystem()); manager.add(c.createStopAction()); manager.add(c.createPauseAction()); manager.add(c.createPlayAction()); manager.add(showMonitorsAction); } @Override public Collection getActions() { return actions; } @Override public String getName() { return "Plant Visualization"; } @Override public void run() { parent.getSession().syncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { getAnimationSystem().run(g,0.01); updateMonitor(g); return GraphRequestStatus.transactionComplete(); } }); } private G3DNode getRoot(Graph g) { return new G3DNode(g,parent.getScenegraphAdapter().getRootResource()); } @Override public void initialize(Graph graph) { animateAction = new Action("", Action.AS_CHECK_BOX) { public void run() { if (this.isChecked()) { parent.getSession().asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { AnimationController c = new TestAnimationController(); Collection nodes = parent.getScenegraphAdapter().getNodes(); for (IGraphicsNode node : nodes) { if (node instanceof Animatable) { Animatable a = (Animatable) node; if (a.setRandomAnimation(g)) c.addAnimatable(a); } } animationSystem.add(c); return GraphRequestStatus.transactionComplete(); } }); // showMessage("Activated " + animatables.size() // + " animations"); } else { animationSystem.stop(); } } }; animateAction.setText("Random animations"); animatePipesAction = new AnimatePipesAction("Pipe animations"); showMonitorsAction = new Action("Interactive Monitor", Action.AS_CHECK_BOX) { public void run() { showMonitors(this.isChecked()); } }; showMonitorsAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor.png")); configureAnimationAction = new Action() { public void run() { parent.getSession().syncRead(new GraphRequestAdapter(){ List list = new ArrayList(); @Override public GraphRequestStatus perform(Graph g) throws Exception { Stack stack = new Stack(); stack.add(getRoot(g)); while (!stack.isEmpty()) { G3DNode n = stack.pop(); list.add(n.getResource()); for (G3DNode no : n.getChild()) stack.push(no); } if (list.size() == 0) return GraphRequestStatus.transactionComplete(); ConfigureAnimationDialog dialog = new ConfigureAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getScenegraphAdapter(),getAnimationSystem()); dialog.open(); return GraphRequestStatus.transactionComplete(); } }); } }; configureAnimationAction.setText("Configure animations"); configureAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png")); configurePipelineAnimationAction = new Action() { public void run() { parent.getSession().syncRead(new GraphRequestAdapter(){ List list = new ArrayList(); @Override public GraphRequestStatus perform(Graph g) throws Exception { Stack stack = new Stack(); stack.add(getRoot(g)); while (!stack.isEmpty()) { G3DNode n = stack.pop(); if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun)) list.add(n.getResource()); else //piperun will not contain other piperuns for (G3DNode no : n.getChild()) stack.push(no); } if (list.size() == 0) return GraphRequestStatus.transactionComplete(); ConfigurePipelineAnimationDialog dialog = new ConfigurePipelineAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getRenderingComponent(),getAnimationSystem()); dialog.open(); return GraphRequestStatus.transactionComplete(); } }); } }; configurePipelineAnimationAction.setText("Configure pipeline animations"); configurePipelineAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png")); configureMonitorAction = new Action() { public void run() { parent.getSession().syncRead(new GraphRequestAdapter(){ List list = new ArrayList(); @Override public GraphRequestStatus perform(Graph g) throws Exception { Stack stack = new Stack(); stack.add(getRoot(g)); while (!stack.isEmpty()) { G3DNode n = stack.pop(); list.add(n.getResource()); for (G3DNode no : n.getChild()) stack.push(no); } if (list.size() == 0) return GraphRequestStatus.transactionComplete(); ConfigureMonitorDialog dialog = new ConfigureMonitorDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list); dialog.open(); return GraphRequestStatus.transactionComplete(); } }); } }; configureMonitorAction.setText("Configure monitor"); configureMonitorAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor_edit.png")); actions.add(new FocusAction(parent)); } private AnimationSystem getAnimationSystem() { if (animationSystem == null) { animationSystem = new AnimationSystem(parent.getScenegraphAdapter()); } return animationSystem; } protected void updateMonitor(Graph graph) { if (showMonitorsAction.isChecked()) { List nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects(); if (nodes.size() > 0) { IGraphicsNode selected = nodes.get(0); if (monitor.acceptNode(graph,selected)) monitor.setNode(graph,selected); } monitor.update(); } } protected Monitor createMonitor() { Monitor m = new TextMonitor(parent); m.setTextProvider(new ResourcePathPropertyProvider()); return m; } private void showMonitors(boolean show) { if (show) { if (monitor == null) { monitor = createMonitor(); } final List nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects(); if (nodes.size() > 0) { parent.getSession().asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { monitor.setNode(g,nodes.get(0)); return GraphRequestStatus.transactionComplete(); } }); } } else if (!show && monitor != null) { monitor.remove(); parent.setViewChanged(true); } } private class AnimatePipesAction extends Action { public AnimatePipesAction(String text) { super(text, Action.AS_CHECK_BOX); } public void run() { if (this.isChecked()) { parent.getSession().syncRead(new GraphRequestAdapter() { List list = new ArrayList(); @Override public GraphRequestStatus perform(Graph g) throws Exception { Stack stack = new Stack(); stack.add(getRoot(g)); while (!stack.isEmpty()) { G3DNode n = stack.pop(); if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun)) list.add(new PipeRun(n)); else //piperun will not contain other piperuns for (G3DNode no : n.getChild()) stack.push(no); } for (PipeRun n : list) { PipeAnimationController c = new PipeAnimationController(parent.getRenderingComponent(), n); //animationSystem.add(c); getAnimationSystem().add(c); } return GraphRequestStatus.transactionComplete(); } }); } else { //stopAnimations(); getAnimationSystem().stop(); } } } }