-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAnimationControl.java
More file actions
409 lines (382 loc) · 13.5 KB
/
AnimationControl.java
File metadata and controls
409 lines (382 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.controls;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import org.opensourcephysics.display.GUIUtils;
/**
* A GUI consisting of an input text area, a message area, and various buttons
* to initialize and control an Animation.
*
* @author Wolfgang Christian
* @author Joshua Gould
* @version 1.0
*/
public class AnimationControl extends OSPControl {
String resetToolTipText = ControlsRes.ANIMATION_RESET_TIP;
String initToolTipText = ControlsRes.ANIMATION_INIT_TIP;
String startToolTipText = ControlsRes.ANIMATION_START_TIP;
String stopToolTipText = ControlsRes.ANIMATION_STOP_TIP;
String newToolTipText = ControlsRes.ANIMATION_NEW_TIP;
String stepToolTipText = ControlsRes.ANIMATION_STEP_TIP;
String initText = ControlsRes.ANIMATION_INIT;
String startText = ControlsRes.ANIMATION_START;
String stopText = ControlsRes.ANIMATION_STOP;
String resetText = ControlsRes.ANIMATION_RESET;
String newText = ControlsRes.ANIMATION_NEW;
boolean stepModeEditing = true; // enables input editing while single stepping
JButton startBtn = new JButton(ControlsRes.ANIMATION_INIT); // changes to start, stop
JButton stepBtn = new JButton(ControlsRes.ANIMATION_STEP);
JButton resetBtn = new JButton(ControlsRes.ANIMATION_RESET); // changes to new
/**
* AnimationControl constructor.
*
* @param animation the animation for this AnimationControl
*/
public AnimationControl(Animation animation) {
super(animation);
if(model!=null) {
String name = model.getClass().getName();
setTitle(name.substring(1+name.lastIndexOf("."))+" Controller"); //$NON-NLS-1$ //$NON-NLS-2$
}
startBtn.addActionListener(new StartBtnListener());
startBtn.setToolTipText(initToolTipText);
stepBtn.addActionListener(new StepBtnListener());
stepBtn.setToolTipText(stepToolTipText);
resetBtn.addActionListener(new ResetBtnListener());
resetBtn.setToolTipText(resetToolTipText);
stepBtn.setEnabled(false);
buttonPanel.add(startBtn);
buttonPanel.add(stepBtn);
buttonPanel.add(resetBtn);
validate();
pack();
}
/**
* Refreshes the user interface in response to display changes such as Language.
*/
protected void refreshGUI() {
super.refreshGUI();
resetToolTipText = ControlsRes.ANIMATION_RESET_TIP;
initToolTipText = ControlsRes.ANIMATION_INIT_TIP;
startToolTipText = ControlsRes.ANIMATION_START_TIP;
stopToolTipText = ControlsRes.ANIMATION_STOP_TIP;
newToolTipText = ControlsRes.ANIMATION_NEW_TIP;
stepToolTipText = ControlsRes.ANIMATION_STEP_TIP;
if(stepBtn==null) {
return;
}
stepBtn.setText(ControlsRes.ANIMATION_STEP);
stepBtn.setToolTipText(stepToolTipText);
if(startBtn.getText().equals(startText)) {
startBtn.setText(ControlsRes.ANIMATION_START);
startBtn.setToolTipText(startToolTipText);
} else if(startBtn.getText().equals(stopText)) {
startBtn.setText(ControlsRes.ANIMATION_STOP);
startBtn.setToolTipText(stopToolTipText);
} else {
startBtn.setText(ControlsRes.ANIMATION_INIT);
startBtn.setToolTipText(initToolTipText);
}
if(resetBtn.getText().equals(newText)) {
resetBtn.setText(ControlsRes.ANIMATION_NEW);
resetBtn.setToolTipText(newToolTipText);
} else {
resetBtn.setText(ControlsRes.ANIMATION_RESET);
resetBtn.setToolTipText(resetToolTipText);
}
initText = ControlsRes.ANIMATION_INIT;
startText = ControlsRes.ANIMATION_START;
resetText = ControlsRes.ANIMATION_RESET;
stopText = ControlsRes.ANIMATION_STOP;
newText = ControlsRes.ANIMATION_NEW;
}
/**
* Disposes all resources.
*/
public void dispose() {
if(model instanceof AbstractAnimation) {
// stops the animation
((AbstractAnimation) model).animationThread = null;
}
super.dispose();
}
/**
* Signals the control that the animation has completed.
* The control should reset itself in preparation for a new
* animation. The given message is printed in the message area.
*
* @param message
*/
public void calculationDone(final String message) {
// always update a Swing component from the event thread
if(model instanceof Animation) {
((Animation) model).stopAnimation();
}
Runnable doNow = new Runnable() {
public void run() {
startBtnActionPerformed(new ActionEvent(this, 0, stopText));
resetBtnActionPerformed(new ActionEvent(this, 0, newText));
resetBtn.setEnabled(true);
org.opensourcephysics.display.GUIUtils.enableMenubars(true);
if(message!=null) {
println(message);
}
}
};
try {
if(SwingUtilities.isEventDispatchThread()) {
doNow.run();
} else { // paint within the event thread
SwingUtilities.invokeAndWait(doNow);
}
} catch(java.lang.reflect.InvocationTargetException ex1) {}
catch(InterruptedException ex1) {}
}
/**
* Method startBtnActionPerformed
*
* @param e
*/
void startBtnActionPerformed(ActionEvent e) {
// table.getDefaultEditor(Object.class).stopCellEditing();
if(e.getActionCommand().equals(initText)) {
stepBtn.setEnabled(true);
startBtn.setText(startText);
startBtn.setToolTipText(startToolTipText);
resetBtn.setText(newText);
resetBtn.setToolTipText(newToolTipText);
resetBtn.setEnabled(true);
readItem.setEnabled(stepModeEditing);
table.setEnabled(stepModeEditing);
messageTextArea.setEditable(false);
GUIUtils.clearDrawingFrameData(false);
if(model==null) {
println("This AnimationControl's model is null."); //$NON-NLS-1$
} else {
((Animation) model).initializeAnimation();
}
org.opensourcephysics.display.GUIUtils.showDrawingAndTableFrames();
} else if(e.getActionCommand().equals(startText)) {
setCustomButtonsEnabled(false);
startBtn.setText(stopText);
startBtn.setToolTipText(stopToolTipText);
stepBtn.setEnabled(false);
resetBtn.setEnabled(false);
readItem.setEnabled(false);
table.setEnabled(false);
org.opensourcephysics.display.GUIUtils.enableMenubars(false);
((Animation) model).startAnimation();
} else { // action command = Stop
startBtn.setText(startText);
setCustomButtonsEnabled(true);
startBtn.setToolTipText(startToolTipText);
stepBtn.setEnabled(true);
resetBtn.setEnabled(true);
org.opensourcephysics.display.GUIUtils.enableMenubars(true);
readItem.setEnabled(stepModeEditing);
table.setEnabled(stepModeEditing);
((Animation) model).stopAnimation();
}
}
/**
* Method resetBtnActionPerformed
*
* @param e
*/
void resetBtnActionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(resetText)) {
GUIUtils.clearDrawingFrameData(true);
if(model==null) {
println("This AnimationControl's model is null."); //$NON-NLS-1$
return;
}
((Animation) model).resetAnimation();
if(xmlDefault!=null) {
xmlDefault.loadObject(getOSPApp(), true, true);
}
table.refresh();
} else { // action command = New
startBtn.setText(initText);
startBtn.setToolTipText(initToolTipText);
resetBtn.setText(resetText);
resetBtn.setToolTipText(resetToolTipText);
stepBtn.setEnabled(false);
readItem.setEnabled(true);
table.setEnabled(true);
messageTextArea.setEditable(true);
setCustomButtonsEnabled(true);
}
}
/**
* Method stepBtnActionPerformed
*
* @param e
*/
void stepBtnActionPerformed(ActionEvent e) {
((Animation) model).stepAnimation();
}
private void setCustomButtonsEnabled(boolean enabled) {
if(customButtons!=null) {
for(Iterator<JButton> it = customButtons.iterator(); it.hasNext(); ) {
(it.next()).setEnabled(enabled);
}
}
}
/**
* Class StartBtnListener
*/
class StartBtnListener implements ActionListener {
/**
* Method actionPerformed
*
* @param e
*/
public void actionPerformed(ActionEvent e) {
startBtnActionPerformed(e);
}
}
/**
* Class ResetBtnListener
*/
class ResetBtnListener implements ActionListener {
/**
* Method actionPerformed
*
* @param e
*/
public void actionPerformed(ActionEvent e) {
resetBtnActionPerformed(e);
}
}
/**
* Class StepBtnListener
*/
class StepBtnListener implements ActionListener {
/**
* Method actionPerformed
*
* @param e
*/
public void actionPerformed(ActionEvent e) {
stepBtnActionPerformed(e);
}
}
/**
* Returns an XML.ObjectLoader to save and load data for this object.
*
* @return the object loader
*/
public static XML.ObjectLoader getLoader() {
return new AnimationControlLoader();
}
/**
* A class to save and load data for OSPControls.
*/
static class AnimationControlLoader extends OSPControlLoader {
/**
* Saves object data to an XMLControl.
*
* @param control the control to save to
* @param obj the object to save
*/
public void saveObject(XMLControl control, Object obj) {
AnimationControl ac = (AnimationControl) obj;
if(ac.startBtn.getText().equals(ac.stopText)) {
ac.startBtn.doClick(); // stop the animation if it is running
}
control.setValue("initialize_mode", ac.startBtn.getText().equals(ac.initText)); //$NON-NLS-1$
super.saveObject(control, obj);
}
/**
* Creates an object using data from an XMLControl.
*
* @param control the control
* @return the newly created object
*/
public Object createObject(XMLControl control) {
return new AnimationControl(null);
}
/**
* Loads an object with data from an XMLControl.
*
* @param control the control
* @param obj the object
* @return the loaded object
*/
public Object loadObject(XMLControl control, Object obj) {
AnimationControl ac = (AnimationControl) obj;
if(ac.startBtn.getText().equals(ac.stopText)) {
ac.startBtn.doClick(); // stop the animation if it is running
}
boolean initMode = control.getBoolean("initialize_mode"); //$NON-NLS-1$
control.setValue("initialize_mode", null); // don't show this internal parameter //$NON-NLS-1$
super.loadObject(control, obj); // load the control's parameters and the model
// put the animation into the initialize state if it was in this state when it was stopped
if(initMode) {
control.setValue("initialize_mode", true); //$NON-NLS-1$
}
if(initMode&&ac.startBtn.getText().equals(ac.startText)) {
ac.resetBtn.doClick();
}
if(!initMode&&ac.startBtn.getText().equals(ac.initText)) {
ac.startBtn.doClick();
}
ac.clearMessages();
return obj;
}
}
/**
* Creates an animation control and establishes communication between the control and the model.
*
* @param model Animation
* @return AnimationControl
*/
public static AnimationControl createApp(Animation model) {
AnimationControl control = new AnimationControl(model);
model.setControl(control);
return control;
}
/**
* Creates a animation control and establishes communication between the control and the model.
* Initial parameters are set using the xml data.
*
* @param model Animation
* @param xml String[]
* @return AnimationControl
*/
public static AnimationControl createApp(Animation model, String[] xml) {
AnimationControl control = createApp(model);
control.loadXML(xml);
return control;
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/