1 package fi.vtt.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.proconf.g3d.actions.ContextAction;
\r
18 import org.simantics.proconf.g3d.actions.FocusAction;
\r
19 import org.simantics.proconf.g3d.animation.Animatable;
\r
20 import org.simantics.proconf.g3d.animation.AnimationController;
\r
21 import org.simantics.proconf.g3d.animation.AnimationSystem;
\r
22 import org.simantics.proconf.g3d.animation.TestAnimationController;
\r
23 import org.simantics.proconf.g3d.animation.ui.AnimationControlCreator;
\r
24 import org.simantics.proconf.g3d.base.EditorContribution;
\r
25 import org.simantics.proconf.g3d.common.StructuredResourceSelection;
\r
26 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;
\r
27 import org.simantics.proconf.g3d.stubs.G3DNode;
\r
28 import org.simantics.utils.ui.jface.MenuTools;
\r
30 import fi.vtt.simantics.processeditor.Activator;
\r
31 import fi.vtt.simantics.processeditor.ProcessResource;
\r
32 import fi.vtt.simantics.processeditor.animations.PipeAnimationController;
\r
33 import fi.vtt.simantics.processeditor.dialogs.ConfigureAnimationDialog;
\r
34 import fi.vtt.simantics.processeditor.dialogs.ConfigureMonitorDialog;
\r
35 import fi.vtt.simantics.processeditor.dialogs.ConfigurePipelineAnimationDialog;
\r
36 import fi.vtt.simantics.processeditor.monitors.Monitor;
\r
37 import fi.vtt.simantics.processeditor.monitors.ResourcePathPropertyProvider;
\r
38 import fi.vtt.simantics.processeditor.monitors.TextMonitor;
\r
39 import fi.vtt.simantics.processeditor.stubs.PipeRun;
\r
40 import fi.vtt.simantics.processeditor.views.ProcessEditor;
\r
42 public class PlantVisualizationContribution implements EditorContribution {
\r
43 private List<ContextAction> actions = new ArrayList<ContextAction>();
\r
44 private ProcessEditor parent;
\r
46 private AnimationSystem animationSystem = null;
\r
47 private Action animatePipesAction = null;
\r
48 private Action animateAction = null;
\r
49 private Action showMonitorsAction = null;
\r
50 private Action configureAnimationAction = null;
\r
51 private Action configurePipelineAnimationAction = null;
\r
52 private Action configureMonitorAction = null;
\r
54 private Monitor monitor;
\r
56 public PlantVisualizationContribution(ProcessEditor parent) {
\r
57 this.parent = parent;
\r
61 public void createControl(Composite parent) {
\r
62 parent.setLayout(new FillLayout());
\r
63 this.parent.getRenderingComposite().setLayoutData(null);
\r
67 public void disposeControl() {
\r
72 public void dispose() {
\r
77 public void fillContextMenu(Graph graph, IMenuManager manager,
\r
78 StructuredResourceSelection selection) {
\r
83 public void fillLocalPullDown(IMenuManager menuManager) {
\r
84 MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(showMonitorsAction);
\r
85 MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(configureMonitorAction);
\r
86 IMenuManager animationMenu = MenuTools.getOrCreate(parent.getMenuID(),"Animations",menuManager);
\r
87 animationMenu.add(configureAnimationAction);
\r
88 animationMenu.add(configurePipelineAnimationAction);
\r
89 MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animateAction);
\r
90 MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animatePipesAction);
\r
95 // public void fillMainMenu(List<IContributionItem> list) {
\r
96 // MenuTools.getOrCreate("Monitors", list).add(showMonitorsAction);
\r
97 // MenuTools.getOrCreate("Monitors", list).add(configureMonitorAction);
\r
98 // IMenuManager animationMenu = MenuTools.getOrCreate("Animations",list);
\r
99 // animationMenu.add(configureAnimationAction);
\r
100 // animationMenu.add(configurePipelineAnimationAction);
\r
101 // MenuTools.getOrCreate("Test", animationMenu).add(animateAction);
\r
102 // MenuTools.getOrCreate("Test", animationMenu).add(animatePipesAction);
\r
107 public void fillLocalToolBar(IToolBarManager manager) {
\r
108 AnimationControlCreator c = new AnimationControlCreator(getAnimationSystem());
\r
109 manager.add(c.createStopAction());
\r
110 manager.add(c.createPauseAction());
\r
111 manager.add(c.createPlayAction());
\r
112 manager.add(showMonitorsAction);
\r
116 public Collection<ContextAction> getActions() {
\r
121 public String getName() {
\r
122 return "Plant Visualization";
\r
126 public void run() {
\r
127 parent.getSession().syncRead(new GraphRequestAdapter() {
\r
129 public GraphRequestStatus perform(Graph g) throws Exception {
\r
130 getAnimationSystem().run(g,0.01);
\r
132 return GraphRequestStatus.transactionComplete();
\r
140 public void initialize(Graph graph) {
\r
141 animateAction = new Action("", Action.AS_CHECK_BOX) {
\r
142 public void run() {
\r
143 if (this.isChecked()) {
\r
144 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
146 public GraphRequestStatus perform(Graph g)
\r
148 AnimationController c = new TestAnimationController();
\r
149 Collection<IGraphicsNode> nodes = parent.getScenegraphAdapter().getNodes();
\r
150 for (IGraphicsNode node : nodes) {
\r
151 if (node instanceof Animatable) {
\r
152 Animatable a = (Animatable) node;
\r
153 if (a.setRandomAnimation(g))
\r
154 c.addAnimatable(a);
\r
157 animationSystem.add(c);
\r
158 return GraphRequestStatus.transactionComplete();
\r
162 // showMessage("Activated " + animatables.size()
\r
163 // + " animations");
\r
165 animationSystem.stop();
\r
170 animateAction.setText("Random animations");
\r
172 animatePipesAction = new AnimatePipesAction("Pipe animations");
\r
174 showMonitorsAction = new Action("Interactive Monitor", Action.AS_CHECK_BOX) {
\r
175 public void run() {
\r
176 showMonitors(this.isChecked());
\r
179 showMonitorsAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor.png"));
\r
183 configureAnimationAction = new Action() {
\r
185 public void run() {
\r
186 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
187 List <Resource> list = new ArrayList<Resource>();
\r
189 public GraphRequestStatus perform(Graph g) throws Exception {
\r
190 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
191 stack.add(parent.getPlant(g).toG3DNode());
\r
192 while (!stack.isEmpty()) {
\r
193 G3DNode n = stack.pop();
\r
194 list.add(n.getResource());
\r
195 for (G3DNode no : n.getChild())
\r
199 if (list.size() == 0)
\r
200 return GraphRequestStatus.transactionComplete();
\r
202 ConfigureAnimationDialog dialog = new ConfigureAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getScenegraphAdapter(),getAnimationSystem());
\r
204 return GraphRequestStatus.transactionComplete();
\r
213 configureAnimationAction.setText("Configure animations");
\r
214 configureAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));
\r
216 configurePipelineAnimationAction = new Action() {
\r
218 public void run() {
\r
219 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
220 List <Resource> list = new ArrayList<Resource>();
\r
222 public GraphRequestStatus perform(Graph g) throws Exception {
\r
223 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
224 stack.add(parent.getPlant(g).toG3DNode());
\r
225 while (!stack.isEmpty()) {
\r
226 G3DNode n = stack.pop();
\r
227 if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))
\r
228 list.add(n.getResource());
\r
229 else //piperun will not contain other piperuns
\r
230 for (G3DNode no : n.getChild())
\r
234 if (list.size() == 0)
\r
235 return GraphRequestStatus.transactionComplete();
\r
237 ConfigurePipelineAnimationDialog dialog = new ConfigurePipelineAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getRenderingComponent(),getAnimationSystem());
\r
239 return GraphRequestStatus.transactionComplete();
\r
248 configurePipelineAnimationAction.setText("Configure pipeline animations");
\r
249 configurePipelineAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));
\r
250 configureMonitorAction = new Action() {
\r
252 public void run() {
\r
253 parent.getSession().syncRead(new GraphRequestAdapter(){
\r
254 List <Resource> list = new ArrayList<Resource>();
\r
256 public GraphRequestStatus perform(Graph g) throws Exception {
\r
257 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
258 stack.add(parent.getPlant(g).toG3DNode());
\r
259 while (!stack.isEmpty()) {
\r
260 G3DNode n = stack.pop();
\r
261 list.add(n.getResource());
\r
262 for (G3DNode no : n.getChild())
\r
266 if (list.size() == 0)
\r
267 return GraphRequestStatus.transactionComplete();
\r
269 ConfigureMonitorDialog dialog = new ConfigureMonitorDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list);
\r
271 return GraphRequestStatus.transactionComplete();
\r
278 configureMonitorAction.setText("Configure monitor");
\r
279 configureMonitorAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor_edit.png"));
\r
281 actions.add(new FocusAction(parent));
\r
284 private AnimationSystem getAnimationSystem() {
\r
285 if (animationSystem == null) {
\r
286 animationSystem = new AnimationSystem(parent.getScenegraphAdapter());
\r
288 return animationSystem;
\r
291 protected void updateMonitor(Graph graph) {
\r
292 if (showMonitorsAction.isChecked()) {
\r
294 List<IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();
\r
295 if (nodes.size() > 0) {
\r
296 IGraphicsNode selected = nodes.get(0);
\r
297 if (monitor.acceptNode(graph,selected))
\r
298 monitor.setNode(graph,selected);
\r
304 protected Monitor createMonitor() {
\r
305 Monitor m = new TextMonitor(parent);
\r
306 m.setTextProvider(new ResourcePathPropertyProvider());
\r
310 private void showMonitors(boolean show) {
\r
312 if (monitor == null) {
\r
313 monitor = createMonitor();
\r
315 final List <IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();
\r
316 if (nodes.size() > 0) {
\r
317 parent.getSession().asyncRead(new GraphRequestAdapter() {
\r
319 public GraphRequestStatus perform(Graph g) throws Exception {
\r
320 monitor.setNode(g,nodes.get(0));
\r
321 return GraphRequestStatus.transactionComplete();
\r
327 } else if (!show && monitor != null) {
\r
329 parent.setViewChanged(true);
\r
333 private class AnimatePipesAction extends Action {
\r
335 public AnimatePipesAction(String text) {
\r
336 super(text, Action.AS_CHECK_BOX);
\r
340 public void run() {
\r
341 if (this.isChecked()) {
\r
342 parent.getSession().syncRead(new GraphRequestAdapter() {
\r
343 List<PipeRun> list = new ArrayList<PipeRun>();
\r
346 public GraphRequestStatus perform(Graph g) throws Exception {
\r
347 Stack<G3DNode> stack = new Stack<G3DNode>();
\r
348 stack.add(parent.getPlant(g).toG3DNode());
\r
349 while (!stack.isEmpty()) {
\r
350 G3DNode n = stack.pop();
\r
351 if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))
\r
352 list.add(new PipeRun(n));
\r
354 //piperun will not contain other piperuns
\r
355 for (G3DNode no : n.getChild())
\r
359 for (PipeRun n : list) {
\r
360 PipeAnimationController c = new PipeAnimationController(parent.getRenderingComponent(), n);
\r
361 //animationSystem.add(c);
\r
362 getAnimationSystem().add(c);
\r
364 return GraphRequestStatus.transactionComplete();
\r
370 //stopAnimations();
\r
371 getAnimationSystem().stop();
\r