1 package org.simantics.processeditor.tools;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
5 import java.util.List;
\r
6 import java.util.Stack;
\r
8 import org.eclipse.jface.action.Action;
\r
9 import org.eclipse.jface.action.IMenuManager;
\r
10 import org.eclipse.jface.action.IToolBarManager;
\r
11 import org.eclipse.swt.layout.FillLayout;
\r
12 import org.eclipse.swt.widgets.Composite;
\r
13 import org.simantics.db.Graph;
\r
14 import org.simantics.db.GraphRequestAdapter;
\r
15 import org.simantics.db.GraphRequestStatus;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.processeditor.Activator;
\r
18 import org.simantics.processeditor.ProcessResource;
\r
19 import org.simantics.processeditor.animations.PipeAnimationController;
\r
20 import org.simantics.processeditor.dialogs.ConfigureAnimationDialog;
\r
21 import org.simantics.processeditor.dialogs.ConfigureMonitorDialog;
\r
22 import org.simantics.processeditor.dialogs.ConfigurePipelineAnimationDialog;
\r
23 import org.simantics.processeditor.monitors.Monitor;
\r
24 import org.simantics.processeditor.monitors.ResourcePathPropertyProvider;
\r
25 import org.simantics.processeditor.monitors.TextMonitor;
\r
26 import org.simantics.processeditor.stubs.PipeRun;
\r
27 import org.simantics.proconf.g3d.actions.ContextAction;
\r
28 import org.simantics.proconf.g3d.actions.FocusAction;
\r
29 import org.simantics.proconf.g3d.animation.Animatable;
\r
30 import org.simantics.proconf.g3d.animation.AnimationController;
\r
31 import org.simantics.proconf.g3d.animation.AnimationSystem;
\r
32 import org.simantics.proconf.g3d.animation.TestAnimationController;
\r
33 import org.simantics.proconf.g3d.animation.ui.AnimationControlCreator;
\r
34 import org.simantics.proconf.g3d.base.EditorContribution;
\r
35 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;
\r
36 import org.simantics.proconf.g3d.common.StructuredResourceSelection;
\r
37 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;
\r
38 import org.simantics.proconf.g3d.stubs.G3DNode;
\r
39 import org.simantics.utils.ui.jface.MenuTools;
\r
42 * Plant Visualization Contribution
\r
44 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
47 public class PlantVisualizationContribution implements EditorContribution {
\r
48 private List<ContextAction> actions = new ArrayList<ContextAction>();
\r
49 private ThreeDimensionalEditorBase parent;
\r
51 private AnimationSystem animationSystem = null;
\r
52 private Action animatePipesAction = null;
\r
53 private Action animateAction = null;
\r
54 private Action showMonitorsAction = null;
\r
55 private Action configureAnimationAction = null;
\r
56 private Action configurePipelineAnimationAction = null;
\r
57 private Action configureMonitorAction = null;
\r
59 private Monitor monitor;
\r
61 public PlantVisualizationContribution(ThreeDimensionalEditorBase parent) {
\r
62 this.parent = parent;
\r
66 public void createControl(Composite parent) {
\r
67 parent.setLayout(new FillLayout());
\r
68 this.parent.getRenderingComposite().setLayoutData(null);
\r
72 public void disposeControl() {
\r
77 public void dispose() {
\r
82 public void fillContextMenu(Graph graph, IMenuManager manager,
\r
83 StructuredResourceSelection selection) {
\r
88 public void fillLocalPullDown(IMenuManager menuManager) {
\r
89 MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(showMonitorsAction);
\r
90 MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(configureMonitorAction);
\r
91 IMenuManager animationMenu = MenuTools.getOrCreate(parent.getMenuID(),"Animations",menuManager);
\r
92 animationMenu.add(configureAnimationAction);
\r
93 animationMenu.add(configurePipelineAnimationAction);
\r
94 MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animateAction);
\r
95 MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animatePipesAction);
\r
100 // public void fillMainMenu(List<IContributionItem> list) {
\r
101 // MenuTools.getOrCreate("Monitors", list).add(showMonitorsAction);
\r
102 // MenuTools.getOrCreate("Monitors", list).add(configureMonitorAction);
\r
103 // IMenuManager animationMenu = MenuTools.getOrCreate("Animations",list);
\r
104 // animationMenu.add(configureAnimationAction);
\r
105 // animationMenu.add(configurePipelineAnimationAction);
\r
106 // MenuTools.getOrCreate("Test", animationMenu).add(animateAction);
\r
107 // MenuTools.getOrCreate("Test", animationMenu).add(animatePipesAction);
\r
112 public void fillLocalToolBar(IToolBarManager manager) {
\r
113 AnimationControlCreator c = new AnimationControlCreator(getAnimationSystem());
\r
114 manager.add(c.createStopAction());
\r
115 manager.add(c.createPauseAction());
\r
116 manager.add(c.createPlayAction());
\r
117 manager.add(showMonitorsAction);
\r
121 public Collection<ContextAction> getActions() {
\r
126 public String getName() {
\r
127 return "Plant Visualization";
\r
131 public void run() {
\r
132 parent.getSession().syncRead(new GraphRequestAdapter() {
\r
134 public GraphRequestStatus perform(Graph g) throws Exception {
\r
135 getAnimationSystem().run(g,0.01);
\r
137 return GraphRequestStatus.transactionComplete();
\r
144 private G3DNode getRoot(Graph g) {
\r
145 return new G3DNode(g,parent.getScenegraphAdapter().getRootResource());
\r
149 public void initialize(Graph graph) {
\r
150 animateAction = new Action("", Action.AS_CHECK_BOX) {
\r
151 public void run() {
\r
152 if (this.isChecked()) {
\r
153 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
155 public GraphRequestStatus perform(Graph g)
\r
157 AnimationController c = new TestAnimationController();
\r
158 Collection<IGraphicsNode> nodes = parent.getScenegraphAdapter().getNodes();
\r
159 for (IGraphicsNode node : nodes) {
\r
160 if (node instanceof Animatable) {
\r
161 Animatable a = (Animatable) node;
\r
162 if (a.setRandomAnimation(g))
\r
163 c.addAnimatable(a);
\r
166 animationSystem.add(c);
\r
167 return GraphRequestStatus.transactionComplete();
\r
171 // showMessage("Activated " + animatables.size()
\r
172 // + " animations");
\r
174 animationSystem.stop();
\r
179 animateAction.setText("Random animations");
\r
181 animatePipesAction = new AnimatePipesAction("Pipe animations");
\r
183 showMonitorsAction = new Action("Interactive Monitor", Action.AS_CHECK_BOX) {
\r
184 public void run() {
\r
185 showMonitors(this.isChecked());
\r
188 showMonitorsAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor.png"));
\r
192 configureAnimationAction = new Action() {
\r
194 public void run() {
\r
195 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
196 List <Resource> list = new ArrayList<Resource>();
\r
198 public GraphRequestStatus perform(Graph g) throws Exception {
\r
199 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
200 stack.add(getRoot(g));
\r
201 while (!stack.isEmpty()) {
\r
202 G3DNode n = stack.pop();
\r
203 list.add(n.getResource());
\r
204 for (G3DNode no : n.getChild())
\r
208 if (list.size() == 0)
\r
209 return GraphRequestStatus.transactionComplete();
\r
211 ConfigureAnimationDialog dialog = new ConfigureAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getScenegraphAdapter(),getAnimationSystem());
\r
213 return GraphRequestStatus.transactionComplete();
\r
222 configureAnimationAction.setText("Configure animations");
\r
223 configureAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));
\r
225 configurePipelineAnimationAction = new Action() {
\r
227 public void run() {
\r
228 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
229 List <Resource> list = new ArrayList<Resource>();
\r
231 public GraphRequestStatus perform(Graph g) throws Exception {
\r
232 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
233 stack.add(getRoot(g));
\r
234 while (!stack.isEmpty()) {
\r
235 G3DNode n = stack.pop();
\r
236 if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))
\r
237 list.add(n.getResource());
\r
238 else //piperun will not contain other piperuns
\r
239 for (G3DNode no : n.getChild())
\r
243 if (list.size() == 0)
\r
244 return GraphRequestStatus.transactionComplete();
\r
246 ConfigurePipelineAnimationDialog dialog = new ConfigurePipelineAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getRenderingComponent(),getAnimationSystem());
\r
248 return GraphRequestStatus.transactionComplete();
\r
257 configurePipelineAnimationAction.setText("Configure pipeline animations");
\r
258 configurePipelineAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));
\r
259 configureMonitorAction = new Action() {
\r
261 public void run() {
\r
262 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
263 List <Resource> list = new ArrayList<Resource>();
\r
265 public GraphRequestStatus perform(Graph g) throws Exception {
\r
266 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
267 stack.add(getRoot(g));
\r
268 while (!stack.isEmpty()) {
\r
269 G3DNode n = stack.pop();
\r
270 list.add(n.getResource());
\r
271 for (G3DNode no : n.getChild())
\r
275 if (list.size() == 0)
\r
276 return GraphRequestStatus.transactionComplete();
\r
278 ConfigureMonitorDialog dialog = new ConfigureMonitorDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list);
\r
280 return GraphRequestStatus.transactionComplete();
\r
287 configureMonitorAction.setText("Configure monitor");
\r
288 configureMonitorAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor_edit.png"));
\r
290 actions.add(new FocusAction(parent));
\r
293 private AnimationSystem getAnimationSystem() {
\r
294 if (animationSystem == null) {
\r
295 animationSystem = new AnimationSystem(parent.getScenegraphAdapter());
\r
297 return animationSystem;
\r
300 protected void updateMonitor(Graph graph) {
\r
301 if (showMonitorsAction.isChecked()) {
\r
303 List<IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();
\r
304 if (nodes.size() > 0) {
\r
305 IGraphicsNode selected = nodes.get(0);
\r
306 if (monitor.acceptNode(graph,selected))
\r
307 monitor.setNode(graph,selected);
\r
313 protected Monitor createMonitor() {
\r
314 Monitor m = new TextMonitor(parent);
\r
315 m.setTextProvider(new ResourcePathPropertyProvider());
\r
319 private void showMonitors(boolean show) {
\r
321 if (monitor == null) {
\r
322 monitor = createMonitor();
\r
324 final List <IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();
\r
325 if (nodes.size() > 0) {
\r
326 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
328 public GraphRequestStatus perform(Graph g) throws Exception {
\r
329 monitor.setNode(g,nodes.get(0));
\r
330 return GraphRequestStatus.transactionComplete();
\r
336 } else if (!show && monitor != null) {
\r
338 parent.setViewChanged(true);
\r
342 private class AnimatePipesAction extends Action {
\r
344 public AnimatePipesAction(String text) {
\r
345 super(text, Action.AS_CHECK_BOX);
\r
349 public void run() {
\r
350 if (this.isChecked()) {
\r
351 parent.getSession().syncRead(new GraphRequestAdapter() {
\r
352 List<PipeRun> list = new ArrayList<PipeRun>();
\r
355 public GraphRequestStatus perform(Graph g) throws Exception {
\r
356 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
357 stack.add(getRoot(g));
\r
358 while (!stack.isEmpty()) {
\r
359 G3DNode n = stack.pop();
\r
360 if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))
\r
361 list.add(new PipeRun(n));
\r
363 //piperun will not contain other piperuns
\r
364 for (G3DNode no : n.getChild())
\r
368 for (PipeRun n : list) {
\r
369 PipeAnimationController c = new PipeAnimationController(parent.getRenderingComponent(), n);
\r
370 //animationSystem.add(c);
\r
371 getAnimationSystem().add(c);
\r
373 return GraphRequestStatus.transactionComplete();
\r
379 //stopAnimations();
\r
380 getAnimationSystem().stop();
\r