]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
72ae6ec73ed0e54cbba9a23be6841a40e4875dee
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers.simulationPlayback;\r
13 \r
14 import java.util.Map;\r
15 \r
16 import org.eclipse.core.commands.AbstractHandler;\r
17 import org.eclipse.core.commands.ExecutionEvent;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.jface.resource.ImageDescriptor;\r
20 import org.eclipse.ui.commands.IElementUpdater;\r
21 import org.eclipse.ui.menus.UIElement;\r
22 import org.simantics.simulation.experiment.ExperimentState;\r
23 import org.simantics.simulation.experiment.IExperiment;\r
24 import org.simantics.simulation.project.IExperimentManager;\r
25 import org.simantics.sysdyn.manager.SysdynPlaybackExperiment;\r
26 import org.simantics.sysdyn.ui.Activator;\r
27 import org.simantics.ui.SimanticsUI;\r
28 \r
29 public class PlaybackExperimentHandler extends AbstractHandler implements IElementUpdater {\r
30     \r
31     public static final String COMMAND = "org.simantics.sysdyn.ui.playback";\r
32 \r
33     @Override\r
34     public Object execute(ExecutionEvent event) throws ExecutionException {\r
35         IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
36         IExperiment experiment = manager.getActiveExperiment();\r
37         if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) \r
38             return null;\r
39         SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment;\r
40         if(!spe.isPlaybackRunning())\r
41             spe.startPlayback();\r
42         else\r
43             spe.stopPlayback();\r
44         return null;\r
45     }\r
46 \r
47     @SuppressWarnings("rawtypes")\r
48     @Override\r
49     public void updateElement(UIElement element, Map parameters) {\r
50         IExperimentManager manager = \r
51             SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
52         IExperiment experiment = manager.getActiveExperiment();\r
53         if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) \r
54             return;\r
55         SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment;      \r
56         \r
57         ExperimentState state = experiment.getState();\r
58         if(state == ExperimentState.RUNNING && !spe.isPlaybackRunning()) {\r
59             // RUNNING == simulation, not playback\r
60             this.setBaseEnabled(false);\r
61         } else {\r
62             this.setBaseEnabled(true);\r
63         }\r
64 \r
65         if(spe.isPlaybackRunning()) {\r
66             element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_pause.png")));\r
67             element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_pause_blue.png")));\r
68         } else {\r
69             long duration = spe.getPlaybackDuration();\r
70             if(duration == SysdynPlaybackExperiment.DURATION_SLOW) {\r
71                 element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_step.png")));\r
72                 element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_step_blue.png")));\r
73             } else if(duration == SysdynPlaybackExperiment.DURATION_FAST) {\r
74                 element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_fastforward.png")));\r
75                 element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_fastforward_blue.png")));\r
76             } else {\r
77                 element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_play.png")));\r
78                 element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_play_blue.png")));\r
79             }\r
80         }\r
81     }\r
82 }\r