initial import
[iDMC.git] / src / java / org / tsho / dmc2 / ui / trajectory / TrajectorySM.java
blob06a7bd24456e17fc0cdedab8dc7636c6170abeab
1 /*
2 * iDMC the interactive Dynamical Model Calculator simulates and performs
3 * graphical and numerical analysis of systems of differential and
4 * difference equations.
6 * Copyright (C) 2004 Marji Lines and Alfredo Medio.
8 * Written by Daniele Pizzoni <auouo@tin.it>.
9 * Extended by Alexei Grigoriev <alexei_grigoriev@libero.it>.
13 * The software program was developed within a research project financed
14 * by the Italian Ministry of Universities, the Universities of Udine and
15 * Ca'Foscari of Venice, the Friuli-Venezia Giulia Region.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or any
20 * later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 package org.tsho.dmc2.ui.trajectory;
29 import org.tsho.dmc2.managers.TrajectoryManager;
30 import org.tsho.dmc2.sm.ComponentStateMachine;
31 import org.tsho.dmc2.sm.Condition;
32 import org.tsho.dmc2.sm.Input;
33 import org.tsho.dmc2.sm.ManagerError;
34 import org.tsho.dmc2.sm.ManagerInput;
35 import org.tsho.dmc2.sm.State;
36 import org.tsho.dmc2.sm.Transition;
37 import org.tsho.dmc2.sm.UserActionInput;
38 import org.tsho.dmc2.core.chart.jfree.DmcChartPanel;
39 import org.tsho.dmc2.ui.AbstractPlotComponent;
42 class TrajectorySM extends ComponentStateMachine {
44 private final TrajectorySMItf frame;
46 private static final State initS = new State("init"); // initial state
48 private static final State normalReadyS = new State("normalReady");
49 private static final State normalRunningS = new State("normalRunning");
50 private static final State normalLockedS = new State("normalLocked");
51 private static final State normalClearingS = new State("normalClearing");
53 private static final State timeReadyS = new State("timeReady");
54 private static final State timeRunningS = new State("timeRunning");
55 private static final State timeLockedS = new State("timeLocked");
56 private static final State timeClearingS = new State("timeClearing");
58 private static final State finiS = new State("fini"); // final state
60 TrajectorySM(final TrajectorySMItf frame) {
61 super("ScatterSM", initS);
62 this.frame = frame;
64 setUp(table);
68 * check for 1-dimension model
70 private final Condition singleDim = new Condition() {
71 public boolean condition(final Input i) {
72 return (frame.getModel().getNVar() == 1 ? true : false);
76 private final Condition render = new Condition() {
77 public boolean condition(final Input i) {
78 TrajectoryManager manager = (TrajectoryManager) frame.getManager();
80 boolean ok = false;
81 if (i == UserActionInput.start) {
82 ok = manager.doRendering(false);
84 else if (i == UserActionInput.continua) {
85 ok = manager.continueRendering();
87 else if (i == UserActionInput.redraw) {
88 ok = manager.doRendering(true);
90 else {
91 //Modification: line below commented
92 // assert (false);
95 if (!ok) {
96 frame.showInvalidDataDialog(
97 frame.getManager().getErrorMessage());
98 return false;
100 return true;
104 private final Condition showErrorDialog = new Condition() {
105 public boolean condition(final Input i) {
106 frame.showInvalidDataDialog(((ManagerError) i).getMessage());
107 return true;
111 private final Transition initMachine = new Transition() {
112 public void transition(final Input i) {
114 addSensibleItem(frame.getTimePlotMenuItem());
115 addSensibleItem(frame.getControlForm());
117 if (getNextState() == normalReadyS) {
118 addSensibleItem(frame.getNormalPlotMenuItem());
120 // frame.getShiftedPlotMenuItem().setEnabled(false);
121 frame.getContinueAction().setEnabled(false);
123 frame.getNormalPlotMenuItem().setSelected(true);
125 initNormalMode.transition(null);
126 normalResetted.transition(null);
128 return;
130 else if (getNextState() == timeReadyS) {
131 // addSensibleItem(frame.getShiftedPlotMenuItem());
133 frame.getNormalPlotMenuItem().setEnabled(false);
134 frame.getTimePlotMenuItem().setSelected(true);
136 initTimeMode.transition(null);
137 timeReset.transition(null);
139 return;
142 throw new Error();
148 * Normal mode
150 private final Transition initNormalMode = new Transition() {
151 public void transition(final Input i) {
152 ((TrajectoryControlForm2)frame.getControlForm()).setTime(false);
154 frame.getContinueAction().setVisible(true);
155 frame.getRedrawAction().setVisible(true);
156 frame.getResetAction().setVisible(true);
158 frame.getSlider().setVisible(true);
162 private final Transition normalStopOnly = new Transition() {
163 public void transition(final Input i) {
164 setSensibleItemsEnabled(false);
165 setNoRunItemsEnabled(false);
167 DmcChartPanel chartPanel=(DmcChartPanel) ((AbstractPlotComponent) frame).getManager().getChartPanel();
168 chartPanel.setCrosshairNotBlocked(false);
170 frame.getStartAction().setEnabled(false);
171 frame.getStopAction().setEnabled(true);
172 frame.getContinueAction().setEnabled(false);
173 frame.getRedrawAction().setEnabled(false);
174 frame.getResetAction().setEnabled(false);
178 private final Transition normalLock = new Transition() {
179 public void transition(final Input i) {
180 DmcChartPanel chartPanel=(DmcChartPanel) ((AbstractPlotComponent) frame).getManager().getChartPanel();
181 chartPanel.setCrosshairNotBlocked(true);
183 ((TrajectoryControlForm2)frame.getControlForm()).setIterationsEnabled(true);
185 setNoRunItemsEnabled(true);
187 frame.getStartAction().setEnabled(false);
188 frame.getStopAction().setEnabled(false);
189 frame.getContinueAction().setEnabled(true);
190 frame.getRedrawAction().setEnabled(true);
191 frame.getResetAction().setEnabled(true);
195 private final Transition normalResetted = new Transition() {
196 public void transition(final Input i) {
197 setSensibleItemsEnabled(true);
198 setNoRunItemsEnabled(true);
200 frame.getStartAction().setEnabled(true);
201 frame.getStopAction().setEnabled(false);
202 frame.getContinueAction().setEnabled(false);
203 frame.getRedrawAction().setEnabled(false);
204 frame.getResetAction().setEnabled(false);
209 * Time mode
211 private final Transition initTimeMode = new Transition() {
212 public void transition(final Input i) {
213 ((TrajectoryControlForm2)frame.getControlForm()).setTime(true);
215 frame.getContinueAction().setVisible(false);
216 frame.getRedrawAction().setVisible(true);
217 frame.getResetAction().setVisible(true);
219 frame.getSlider().setVisible(true);
223 private final Transition timeStopOnly = new Transition() {
224 public void transition(final Input i) {
225 setSensibleItemsEnabled(false);
226 setNoRunItemsEnabled(false);
228 DmcChartPanel chartPanel=(DmcChartPanel) ((AbstractPlotComponent) frame).getManager().getChartPanel();
229 chartPanel.setCrosshairNotBlocked(false);
231 frame.getStartAction().setEnabled(false);
232 frame.getStopAction().setEnabled(true);
233 frame.getRedrawAction().setEnabled(false);
234 frame.getResetAction().setEnabled(false);
238 private final Transition timeLock = new Transition() {
239 public void transition(final Input i) {
240 setSensibleItemsEnabled(false);
241 setNoRunItemsEnabled(true);
243 DmcChartPanel chartPanel=(DmcChartPanel) ((AbstractPlotComponent) frame).getManager().getChartPanel();
244 chartPanel.setCrosshairNotBlocked(true);
246 frame.getStartAction().setEnabled(false);
247 frame.getStopAction().setEnabled(false);
248 frame.getRedrawAction().setEnabled(true);
249 frame.getResetAction().setEnabled(true);
253 private final Transition timeReset = new Transition() {
254 public void transition(final Input i) {
255 setSensibleItemsEnabled(true);
256 setNoRunItemsEnabled(true);
258 frame.getStartAction().setEnabled(true);
259 frame.getStopAction().setEnabled(false);
260 frame.getRedrawAction().setEnabled(false);
261 frame.getResetAction().setEnabled(false);
266 * Misc
268 private final Transition managerClear = new Transition() {
269 public void transition(final Input i) {
270 frame.getManager().clear();
274 // note that we don't wait for thread to really finish...
275 private final Transition cleanup = new Transition() {
276 public void transition(final Input i) {
277 frame.getManager().stopRendering();
282 * Transition matrix
284 private final Object[][][] table = new Object[][][]
286 // init
288 {initS},
289 {Input.go, singleDim, initMachine, timeReadyS},
290 {Input.go, null, initMachine, normalReadyS},
291 {UserActionInput.close, null, cleanup, finiS} // fast user... or this is executued by the awt thread mmhh...
294 // normal
296 {normalReadyS},
297 {UserActionInput.start, render, normalStopOnly, normalRunningS},
298 {UserActionInput.start, null, null, normalReadyS},
299 {UserActionInput.clear, null, managerClear, normalClearingS},
300 {UserActionInput.close, null, cleanup, finiS},
301 {UserMenuInput.normalMode, null, null, normalReadyS},
302 {UserMenuInput.timeMode, null, initTimeMode, timeReadyS},
304 {ManagerInput.start, null, normalStopOnly, normalRunningS}, // user zoomed
306 {ManagerInput.end, null, null, normalReadyS} // this arrives after an error
309 {normalRunningS},
310 {ManagerInput.start, null, null, normalRunningS}, // the thread sends an input we ignore
311 {ManagerInput.end, null, normalLock, normalLockedS},
312 {UserActionInput.close, null, cleanup, finiS},
314 {ManagerError.class, showErrorDialog, normalResetted, normalReadyS}
317 {normalLockedS},
318 {UserActionInput.continua, render, normalStopOnly, normalRunningS},
319 {UserActionInput.continua, null, null, normalLockedS},
320 {UserActionInput.redraw, render, normalStopOnly, normalRunningS},
321 {UserActionInput.redraw, null, null, normalLockedS},
322 {UserActionInput.reset, null, normalResetted, normalReadyS},
323 {UserActionInput.clear, null, managerClear, normalClearingS},
324 {UserActionInput.close, null, cleanup, finiS},
326 {ManagerInput.start, null, normalStopOnly, normalRunningS}, // user zoomed
327 {ManagerInput.end, null, normalLock, normalLockedS},
330 {normalClearingS},
331 {ManagerInput.start, null, null, normalClearingS},
332 {ManagerInput.end, null, normalResetted, normalReadyS},
333 {UserActionInput.close, null, cleanup, finiS}
335 // time
337 {timeReadyS},
338 {UserActionInput.start, render, timeStopOnly, timeRunningS},
339 {UserActionInput.start, null, null, timeReadyS},
340 {UserActionInput.clear, null, managerClear, timeClearingS},
341 {UserActionInput.close, null, cleanup, finiS},
342 {UserActionInput.redraw, render, timeStopOnly, timeRunningS},
343 {UserActionInput.redraw, null, null, timeReadyS},
345 {UserMenuInput.normalMode, null, initNormalMode, normalReadyS},
346 {UserMenuInput.timeMode, null, null, timeReadyS},
348 {ManagerInput.start, null, timeStopOnly, timeRunningS}, // user zoomed
350 {ManagerInput.end, null, null, timeReadyS} // this arrives after an error
353 {timeLockedS},
354 {UserActionInput.redraw, render, timeStopOnly, timeRunningS},
355 {UserActionInput.redraw, null, null, timeLockedS},
356 {UserActionInput.reset, null, timeReset, timeReadyS},
357 {UserActionInput.clear, null, managerClear, timeClearingS},
358 {UserActionInput.close, null, cleanup, finiS},
360 {UserMenuInput.timeMode, null, initTimeMode, timeReadyS},
362 {ManagerInput.start, null, timeStopOnly, timeRunningS}, // user zoomed
363 {ManagerInput.end, null, timeLock, timeLockedS},
366 {timeRunningS},
367 {ManagerInput.start, null, null, timeRunningS},
368 {ManagerInput.end, null, timeLock, timeLockedS},
369 {UserActionInput.close, null, cleanup, finiS},
370 {ManagerError.class, showErrorDialog,timeReset, timeReadyS}
373 {timeClearingS},
374 {ManagerInput.start, null, null, timeClearingS},
375 {ManagerInput.end, null, timeReset, timeReadyS},
376 {UserActionInput.close, null, cleanup, finiS}
378 // finish
380 {finiS}, // final state
381 {ManagerInput.class, null, null, finiS},
382 {UserActionInput.class, null, null, finiS}
387 final class UserMenuInput extends Input {
389 UserMenuInput(final String name) {
390 super(name);
393 static final UserMenuInput normalMode = new UserMenuInput("normalMode");
394 static final UserMenuInput timeMode = new UserMenuInput("timeMode");