21
23
import javax.swing.JToggleButton;
22
24
import javax.swing.JToolBar;
23
25
import javax.swing.KeyStroke;
26
import javax.swing.border.EmptyBorder;
25
28
import pipe.dataLayer.NetType;
26
import pipe.exception.InvariantViolatedAnimationException;
27
29
import pipe.gui.action.GuiAction;
30
import dk.aau.cs.model.tapn.simulation.FiringMode;
30
33
* Implementes af class handling drawing of animation functions
33
* Author Kenneth Yrke Joergensen <kenneth@yrke.dk>
34
* Based on code from GuiFrame
35
* Copyright 2009 Author Kenneth Yrke Joergensen <kenneth@yrke.dk> Based on code
36
38
* Licensed under the Open Software License version 3.0
84
85
AnimateAction startAction, stepforwardAction, stepbackwardAction,
85
randomAction, randomAnimateAction, timeAction;
87
public AnimationController(String text) throws
88
javax.swing.text.BadLocationException {
91
startAction = new AnimateAction("Simulation mode",
92
Pipe.START, "Toggle Simulation Mode", "Ctrl A", true);
96
new AnimateAction("Back", Pipe.STEPBACKWARD,
97
"Step backward a firing", "typed 4" );
99
new AnimateAction("Forward", Pipe.STEPFORWARD,
100
"Step forward a firing", "typed 6" );
86
randomAction, randomAnimateAction, timeAction;
88
public AnimationController() {
89
startAction = new AnimateAction("Simulation mode", Pipe.START,
90
"Toggle Simulation Mode", "Ctrl A", true);
92
stepbackwardAction = new AnimateAction("Back", Pipe.STEPBACKWARD,
93
"Step backward a firing", "typed 4");
94
stepforwardAction = new AnimateAction("Forward", Pipe.STEPFORWARD,
95
"Step forward a firing", "typed 6");
102
97
stepbackwardAction.setEnabled(false);
103
98
stepforwardAction.setEnabled(false);
105
//timeAction = new AnimateAction("Time", Pipe.TIMEPASS, "Let Time pass", "_");
108
new AnimateAction("Random", Pipe.RANDOM,
109
"Randomly fire a transition", "typed 5");
110
randomAnimateAction =
111
new AnimateAction("Simulate", Pipe.ANIMATE,
112
"Randomly fire a number of transitions", "typed 7",true);
100
// timeAction = new AnimateAction("Time", Pipe.TIMEPASS,
101
// "Let Time pass", "_");
103
randomAction = new AnimateAction("Random", Pipe.RANDOM,
104
"Randomly fire a transition", "typed 5");
105
randomAnimateAction = new AnimateAction("Simulate", Pipe.ANIMATE,
106
"Randomly fire a number of transitions", "typed 7", true);
115
108
setLayout(new GridBagLayout());
116
109
GridBagConstraints c = new GridBagConstraints();
120
//Use the default FlowLayout.
123
JLabel label = new JLabel("Simulator");
125
c.fill = GridBagConstraints.HORIZONTAL;
111
// Use the default FlowLayout.
112
// Create everything.
131
114
firermodebox = new JComboBox(FIRINGMODES);
132
115
updateFiringModeComboBox();
135
117
firermodebox.addActionListener(new java.awt.event.ActionListener() {
136
118
public void actionPerformed(java.awt.event.ActionEvent evt) {
138
CreateGui.getAnimator().setFiringmode(firermodebox.getSelectedItem().toString());
119
CreateGui.getAnimator().setFiringmode(
120
(String) firermodebox.getSelectedItem());
142
124
JToolBar animationToolBar = new JToolBar();
143
125
animationToolBar.setFloatable(false);
126
animationToolBar.setBorder(new EmptyBorder(0, 0, 0, 0));
144
127
addButton(animationToolBar, stepbackwardAction);
145
128
addButton(animationToolBar, stepforwardAction);
149
132
c.fill = GridBagConstraints.HORIZONTAL;
153
add(animationToolBar, c);
155
if(!CreateGui.getModel().netType().equals(NetType.UNTIMED)){
136
add(animationToolBar, c);
138
if (!CreateGui.getModel().netType().equals(NetType.UNTIMED)) {
156
139
JPanel firemode = new JPanel(new FlowLayout(FlowLayout.LEFT));
158
label = new JLabel("Token selection mode: ");
141
JLabel label = new JLabel("Token selection mode: ");
160
143
firemode.add(label);
161
144
firemode.add(firermodebox);
168
149
add(firemode, c);
171
151
JPanel timedelayPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
174
153
okButton = new javax.swing.JButton();
176
155
okButton.setText("Time delay");
177
//okButton.setMaximumSize(new java.awt.Dimension(75, 25));
156
// okButton.setMaximumSize(new java.awt.Dimension(75, 25));
178
157
okButton.setMinimumSize(new java.awt.Dimension(75, 25));
179
//okButton.setPreferredSize(new java.awt.Dimension(75, 25));
158
// okButton.setPreferredSize(new java.awt.Dimension(75, 25));
180
159
okButton.addActionListener(new java.awt.event.ActionListener() {
181
160
public void actionPerformed(java.awt.event.ActionEvent evt) {
182
//okButtonHandler(evt);
161
// okButtonHandler(evt);
183
162
addTimeDelayToHistory();
187
TimeDelayField.addKeyListener(new KeyListener(){
166
TimeDelayField.addKeyListener(new KeyListener() {
188
167
public void keyPressed(KeyEvent e) {
189
if (e.getKeyCode() == KeyEvent.VK_ENTER){
168
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
190
169
addTimeDelayToHistory();
194
173
public void keyReleased(KeyEvent e) {
199
177
public void keyTyped(KeyEvent e) {
212
190
timedelayPanel.add(TimeDelayField);
213
191
timedelayPanel.add(okButton);
215
// c.fill = GridBagConstraints.HORIZONTAL;
219
// add(timedelayPanel, c);
220
animationToolBar.add(timedelayPanel);
193
// c.fill = GridBagConstraints.HORIZONTAL;
197
// add(timedelayPanel, c);
198
animationToolBar.add(timedelayPanel);
201
setBorder(BorderFactory.createCompoundBorder(BorderFactory
202
.createTitledBorder("Simulation Control"), BorderFactory
203
.createEmptyBorder(3, 3, 3, 3)));
204
this.setPreferredSize(new Dimension(275, 100));
205
this.setMinimumSize(new Dimension(275, 100));
224
208
public void updateFiringModeComboBox() {
225
String currentFiringMode = CreateGui.getAnimator().getFiringmode().getName();
226
firermodebox.setSelectedItem(currentFiringMode);
229
public void addTimeDelayToHistory(BigDecimal delay){
230
AnimationHistory animBox = CreateGui.getAnimationHistory();
231
animBox.clearStepsForward();
234
BigDecimal timeDelayToSet = delay;
237
// BigDecimal timeDelayToSet = new BigDecimal(TimeDelayField.getText(), new MathContext(Pipe.AGE_PRECISION));
238
if (timeDelayToSet.compareTo(new BigDecimal(0l))<=0){
239
//Nothing to do, illegal value
240
System.err.println("Illegal value");
242
CreateGui.getAnimator().letTimePass(timeDelayToSet);
244
} catch (NumberFormatException e) {
245
//Do nothing, invalud number
246
} catch (InvariantViolatedAnimationException e) {
247
// TODO Auto-generated catch block
248
System.err.println("Invariant Violated, TimeDelay Not allowed");
251
setAnimationButtonsEnabled();
254
private void addTimeDelayToHistory(){
255
AnimationHistory animBox = CreateGui.getAnimationHistory();
256
animBox.clearStepsForward();
259
//Hack to allow usage of localised numbes
209
FiringMode currentFiringMode = CreateGui.getAnimator().getFiringmode();
210
if (currentFiringMode == null) {
211
firermodebox.setSelectedItem("Manual");
213
firermodebox.setSelectedItem(currentFiringMode.toString());
218
private void addTimeDelayToHistory() {
219
AnimationHistoryComponent animBox = CreateGui.getAnimationHistory();
220
animBox.clearStepsForward();
223
// Hack to allow usage of localised numbes
260
224
DecimalFormat df = new DecimalFormat();
261
225
df.setMaximumFractionDigits(Pipe.AGE_DECIMAL_PRECISION);
262
226
df.setMinimumFractionDigits(Pipe.AGE_DECIMAL_PRECISION);
266
230
parser.setMaximumFractionDigits(Pipe.AGE_DECIMAL_PRECISION);
267
231
parser.setMinimumFractionDigits(Pipe.AGE_DECIMAL_PRECISION);
269
Number parseTime = parser.parse(TimeDelayField.getText()); // Parse the number localised
233
Number parseTime = parser.parse(TimeDelayField.getText()); // Parse
272
BigDecimal timeDelayToSet = new BigDecimal(parseTime.toString(), new MathContext(Pipe.AGE_PRECISION));
275
// BigDecimal timeDelayToSet = new BigDecimal(TimeDelayField.getText(), new MathContext(Pipe.AGE_PRECISION));
276
if (timeDelayToSet.compareTo(new BigDecimal(0l))<=0){
277
//Nothing to do, illegal value
239
BigDecimal timeDelayToSet = new BigDecimal(parseTime.toString(),
240
new MathContext(Pipe.AGE_PRECISION));
242
// BigDecimal timeDelayToSet = new
243
// BigDecimal(TimeDelayField.getText(), new
244
// MathContext(Pipe.AGE_PRECISION));
245
if (timeDelayToSet.compareTo(new BigDecimal(0l)) <= 0) {
246
// Nothing to do, illegal value
278
247
System.err.println("Illegal value");
280
CreateGui.getAnimator().letTimePass(timeDelayToSet);
249
CreateGui.getAnimator().letTimePass(timeDelayToSet);
282
251
} catch (NumberFormatException e) {
283
//Do nothing, invalud number
284
} catch (InvariantViolatedAnimationException e) {
285
// TODO Auto-generated catch block
286
System.err.println("Invariant Violated, TimeDelay Not allowed");
252
// Do nothing, invalud number
287
253
} catch (ParseException e) {
288
254
// TODO Auto-generated catch block
289
255
e.printStackTrace();
293
258
setAnimationButtonsEnabled();
301
266
private static final long serialVersionUID = -4066032248332540289L;
302
267
private int typeID;
303
private AnimationHistory animBox;
306
AnimateAction(String name, int typeID, String tooltip, String keystroke){
268
private AnimationHistoryComponent animBox;
270
AnimateAction(String name, int typeID, String tooltip, String keystroke) {
307
271
super(name, tooltip, keystroke);
308
272
this.typeID = typeID;
311
AnimateAction(String name, int typeID, String tooltip, String keystroke,
275
AnimateAction(String name, int typeID, String tooltip,
276
String keystroke, boolean toggleable) {
313
277
super(name, tooltip, keystroke, toggleable);
314
278
this.typeID = typeID;
318
281
public AnimateAction(String name, int typeID, String tooltip,
319
282
KeyStroke keyStroke) {
320
283
super(name, tooltip, keyStroke);
321
284
this.typeID = typeID;
324
public void actionPerformed(ActionEvent ae){
287
public void actionPerformed(ActionEvent ae) {
326
289
animBox = CreateGui.getAnimationHistory();
329
292
case Pipe.TIMEPASS:
330
293
animBox.clearStepsForward();
332
CreateGui.getAnimator().letTimePass(new BigDecimal(1, new MathContext(Pipe.AGE_PRECISION)));
333
} catch (InvariantViolatedAnimationException e) {
334
// TODO Auto-generated catch block
338
setAnimationButtonsEnabled();
343
animBox.clearStepsForward();
344
CreateGui.getAnimator().doRandomFiring();
346
setAnimationButtonsEnabled();
294
CreateGui.getAnimator().letTimePass(
295
new BigDecimal(1, new MathContext(Pipe.AGE_PRECISION)));
297
setAnimationButtonsEnabled();
302
// animBox.clearStepsForward();
303
// CreateGui.getAnimator().doRandomFiring();
305
// setAnimationButtonsEnabled();
349
308
case Pipe.STEPFORWARD:
350
309
animBox.stepForward();
383
328
stepbackwardAction.setEnabled(b);
386
332
private void setEnabledStepforwardAction(boolean b) {
387
333
stepforwardAction.setEnabled(b);
391
public void setAnimationButtonsEnabled(){
392
AnimationHistory animationHistory = CreateGui.getAnimationHistory();
337
public void setAnimationButtonsEnabled() {
338
AnimationHistoryComponent animationHistory = CreateGui.getAnimationHistory();
394
340
setEnabledStepforwardAction(animationHistory.isStepForwardAllowed());
395
341
setEnabledStepbackwardAction(animationHistory.isStepBackAllowed());
397
343
CreateGui.appGui.setEnabledStepForwardAction(animationHistory.isStepForwardAllowed());
398
344
CreateGui.appGui.setEnabledStepBackwardAction(animationHistory.isStepBackAllowed());
400
// setEnabledStepforwardAction(false);
401
// setEnabledStepbackwardAction(false);
404
347
JTextField TimeDelayField = new JTextField();