1 | package org.greenstone.gatherer.feedback;
|
---|
2 |
|
---|
3 | import java.awt.image.*;
|
---|
4 | import javax.swing.*;
|
---|
5 | import java.awt.event.*;
|
---|
6 | import java.awt.*;
|
---|
7 | import javax.swing.event.*;
|
---|
8 | import java.io.*;
|
---|
9 | import java.util.ResourceBundle;
|
---|
10 | import java.text.MessageFormat;
|
---|
11 | import javax.swing.text.NumberFormatter;
|
---|
12 | import java.text.NumberFormat;
|
---|
13 | import java.beans.*;
|
---|
14 | import java.util.ArrayList;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This is the class to generate a animation type from a list of ImageIcon.
|
---|
18 | * It will display a JDialog window and then user can specify the frames
|
---|
19 | * per second rate that the user want to use to view the list of the ImageIcon.
|
---|
20 | * @author Veronica Liesaputra
|
---|
21 | */
|
---|
22 | public class Movie extends JDialog
|
---|
23 | implements ActionListener,ChangeListener,PropertyChangeListener
|
---|
24 | {
|
---|
25 | /**
|
---|
26 | * This variable will hold the resource of the words that is stored in Messages.properties file.
|
---|
27 | * The calling using messages.getString(someString) will caused someString to be translated
|
---|
28 | * into some other string that is hold in that file.usually it will caused it to be translated
|
---|
29 | * to the language that the user use or choose to have as stated in Locale.
|
---|
30 | */
|
---|
31 | private static ResourceBundle messages;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * This is the label that we use to display the ImageIcon.
|
---|
35 | */
|
---|
36 | private JLabel picture;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * This is a textField where user can set how many frames per second they want to view the
|
---|
40 | * ImageIcon.If user enter value that is bigger than the FPS_MAX then it will beep and user
|
---|
41 | * will need to enter new valid value or it will stay as the previous valid value.
|
---|
42 | * If user enter 0 then it will stop the animation.
|
---|
43 | */
|
---|
44 | private JFormattedTextField textField;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * This is a slider where user can set how many frames per second they want to view the
|
---|
48 | * ImageIcon.If user slide to 0 then animation stop.
|
---|
49 | */
|
---|
50 | private JSlider framesPerSecond;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * This is the minimum frame per second value.
|
---|
54 | */
|
---|
55 | private int FPS_MIN = 0;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * This is the maximum frame per second value. The value is the twice the amount of
|
---|
59 | * ImageIcon in the list.
|
---|
60 | */
|
---|
61 | private int FPS_MAX;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * This is the initial frame per second value to view the ImageIcon.
|
---|
65 | */
|
---|
66 | private int FPS_INIT = 1;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * This is the ImageIcon index that are currently showing in the label.
|
---|
70 | */
|
---|
71 | private int frameNumber;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * This is the total number of ImageIcon in the list.
|
---|
75 | */
|
---|
76 | private int NUM_FRAMES;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * This hold the list of the ImageIcon that are to be displayed.
|
---|
80 | */
|
---|
81 | private ArrayList images;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * This is telling how long is the delay for each frame to be refreshed.
|
---|
85 | */
|
---|
86 | private int delay;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * This is the timer that generate the animation of the list of ImageIcon.
|
---|
90 | * The animation will be paused twice per cycle by restarting the timer.
|
---|
91 | * If the the prame per second chosen by the user is 0 then the timer
|
---|
92 | * will be stop.
|
---|
93 | */
|
---|
94 | private Timer timer;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * This the flag to say when the animation should stop.
|
---|
98 | * If frozen = true then the animation should stop otherwise the animation
|
---|
99 | * should keep running.
|
---|
100 | */
|
---|
101 | private boolean frozen = false;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * This will sho a modal-dialog and set up the value for the animation to run properly
|
---|
105 | * according to the list of ImageIcon passed in.
|
---|
106 | * @param msg hold the resource of the words that is stored in Messages.properties file.
|
---|
107 | * @param img hold the list of ImageIcon to be displayed.
|
---|
108 | */
|
---|
109 | public Movie (ResourceBundle msg,ArrayList img)
|
---|
110 | {
|
---|
111 | super();
|
---|
112 |
|
---|
113 | images = img;
|
---|
114 | NUM_FRAMES = img.size();
|
---|
115 | FPS_MAX = img.size() * 2;
|
---|
116 |
|
---|
117 | messages = msg;
|
---|
118 | setTitle(messages.getString("GenerateMovie"));
|
---|
119 | setModal(true);
|
---|
120 | setBackground(new Color (176,208,176));
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * This method will seeting up the content pane of the dialog window.
|
---|
125 | * It also setting up the timer and the delay.
|
---|
126 | * @return the content pane of the window.
|
---|
127 | */
|
---|
128 | public JPanel create_UI()
|
---|
129 | {
|
---|
130 | JPanel cont;
|
---|
131 | cont = new JPanel();
|
---|
132 | cont.setOpaque(true);
|
---|
133 | cont.setBackground(new Color (176,208,176));
|
---|
134 |
|
---|
135 | JButton cls;
|
---|
136 | cls = new JButton(messages.getString("Close"));
|
---|
137 | cls.addActionListener(new ActionListener ()
|
---|
138 | {
|
---|
139 | public void actionPerformed (ActionEvent e)
|
---|
140 | {
|
---|
141 | dispose();
|
---|
142 | if (timer != null)
|
---|
143 | timer.stop();
|
---|
144 | }
|
---|
145 | });
|
---|
146 | cls.setDefaultCapable(true);
|
---|
147 | cls.setActionCommand(messages.getString("Close"));
|
---|
148 | cls.setBackground(new Color(176,208,176));
|
---|
149 |
|
---|
150 |
|
---|
151 | if (images.size() <= 0)
|
---|
152 | {
|
---|
153 | JLabel empty_lbl;
|
---|
154 | empty_lbl = new JLabel ("No Image to be displayed ");
|
---|
155 | cont.add(empty_lbl);
|
---|
156 | cont.add(cls);
|
---|
157 | return cont;
|
---|
158 | }
|
---|
159 |
|
---|
160 | cont.setLayout(new BoxLayout(cont, BoxLayout.PAGE_AXIS));
|
---|
161 |
|
---|
162 | delay = 1000 / FPS_INIT;
|
---|
163 |
|
---|
164 | JLabel sliderLabel;
|
---|
165 | sliderLabel = new JLabel(messages.getString("FramesPerSecond") + ": ", JLabel.CENTER);
|
---|
166 | sliderLabel.setBackground(new Color(176,208,176));
|
---|
167 | sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
---|
168 |
|
---|
169 | NumberFormat numberFormat;
|
---|
170 | numberFormat = NumberFormat.getIntegerInstance();
|
---|
171 | NumberFormatter formatter;
|
---|
172 | formatter = new NumberFormatter(numberFormat);
|
---|
173 | formatter.setMinimum(new Integer(FPS_MIN));
|
---|
174 | formatter.setMaximum(new Integer(FPS_MAX));
|
---|
175 |
|
---|
176 | textField = new JFormattedTextField(formatter);
|
---|
177 | textField.setBackground(new Color(224,240,224));
|
---|
178 | textField.setValue(new Integer(FPS_INIT));
|
---|
179 | textField.setColumns(5);
|
---|
180 | textField.addPropertyChangeListener(this);
|
---|
181 |
|
---|
182 | textField.getInputMap().put(KeyStroke.getKeyStroke(
|
---|
183 | KeyEvent.VK_ENTER, 0),
|
---|
184 | "check");
|
---|
185 | textField.getActionMap().put("check", new AbstractAction()
|
---|
186 | {
|
---|
187 | public void actionPerformed(ActionEvent e)
|
---|
188 | {
|
---|
189 | if (!textField.isEditValid())
|
---|
190 | {
|
---|
191 | Toolkit.getDefaultToolkit().beep();
|
---|
192 | textField.selectAll();
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | try
|
---|
197 | {
|
---|
198 | textField.commitEdit();
|
---|
199 | }
|
---|
200 | catch (java.text.ParseException exc) { }
|
---|
201 | }
|
---|
202 | }
|
---|
203 | });
|
---|
204 |
|
---|
205 | framesPerSecond = new JSlider(JSlider.HORIZONTAL,
|
---|
206 | FPS_MIN, FPS_MAX, FPS_INIT);
|
---|
207 | framesPerSecond.addChangeListener(this);
|
---|
208 | framesPerSecond.setBackground(new Color(176,208,176));
|
---|
209 | framesPerSecond.setMajorTickSpacing(10);
|
---|
210 | framesPerSecond.setMinorTickSpacing(1);
|
---|
211 | framesPerSecond.setPaintTicks(true);
|
---|
212 | framesPerSecond.setPaintLabels(true);
|
---|
213 | framesPerSecond.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
|
---|
214 |
|
---|
215 | picture = new JLabel();
|
---|
216 | Dimension dim;
|
---|
217 | int iw,ih;
|
---|
218 | dim = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
219 | iw = (int) (dim.width - 30);
|
---|
220 | ih = (int) (dim.height * 0.75);
|
---|
221 | picture.setPreferredSize(new Dimension(iw,ih));
|
---|
222 | picture.setHorizontalAlignment(JLabel.CENTER);
|
---|
223 | picture.setAlignmentX(Component.CENTER_ALIGNMENT);
|
---|
224 | picture.setBorder(BorderFactory.createCompoundBorder(
|
---|
225 | BorderFactory.createLoweredBevelBorder(),
|
---|
226 | BorderFactory.createEmptyBorder(10,10,10,10)));
|
---|
227 | picture.setBackground(new Color(176,208,176));
|
---|
228 | updatePicture(0);
|
---|
229 |
|
---|
230 |
|
---|
231 | JPanel labelAndTextField;
|
---|
232 | labelAndTextField = new JPanel();
|
---|
233 | labelAndTextField.add(sliderLabel);
|
---|
234 | labelAndTextField.add(textField);
|
---|
235 | labelAndTextField.add(cls);
|
---|
236 | labelAndTextField.setBackground(new Color(176,208,176));
|
---|
237 |
|
---|
238 | cont.add(picture);
|
---|
239 | cont.add(framesPerSecond);
|
---|
240 | cont.add(labelAndTextField);
|
---|
241 |
|
---|
242 | cont.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
|
---|
243 |
|
---|
244 | timer = new Timer(delay, this);
|
---|
245 | timer.setInitialDelay(delay * 7);
|
---|
246 | timer.setCoalesce(true);
|
---|
247 | timer.start();
|
---|
248 |
|
---|
249 | return cont;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * This method will tell what should happen when the slider's arrow
|
---|
254 | * change its position.
|
---|
255 | * <br>If user adjusting the frame per second value using the slider
|
---|
256 | * then it will set the Textfield to have the value that slider point
|
---|
257 | * to at the moment.If the value is 0 then it will stop the animation
|
---|
258 | * otherwise it will keep animate with the new frame per second value.
|
---|
259 | * @param e is the slider when the slider's arrow change its position.
|
---|
260 | */
|
---|
261 | public void stateChanged(ChangeEvent e)
|
---|
262 | {
|
---|
263 | JSlider source;
|
---|
264 | source = (JSlider)e.getSource();
|
---|
265 | int fps;
|
---|
266 | fps = (int)source.getValue();
|
---|
267 | if (!source.getValueIsAdjusting())
|
---|
268 | {
|
---|
269 | textField.setValue(new Integer(fps));
|
---|
270 | if (fps == 0)
|
---|
271 | {
|
---|
272 | if (!frozen)
|
---|
273 | stopAnimation();
|
---|
274 | }
|
---|
275 | else
|
---|
276 | {
|
---|
277 | delay = 1000 / fps;
|
---|
278 | timer.setDelay(delay);
|
---|
279 | timer.setInitialDelay(delay * 10);
|
---|
280 | if (frozen)
|
---|
281 | startAnimation();
|
---|
282 | }
|
---|
283 | }
|
---|
284 | else
|
---|
285 | {
|
---|
286 | textField.setText(String.valueOf(fps));
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * This method will set the slider value when the user adjusting
|
---|
292 | * the frame per second value using the textfield.
|
---|
293 | * <br>If user adjusting the frame per second value using text field, the
|
---|
294 | * slider's arrow position will be changed into that value.
|
---|
295 | * @param e the text field
|
---|
296 | */
|
---|
297 | public void propertyChange(PropertyChangeEvent e)
|
---|
298 | {
|
---|
299 | if ("value".equals(e.getPropertyName()))
|
---|
300 | {
|
---|
301 | Number value;
|
---|
302 | value = (Number)e.getNewValue();
|
---|
303 | if (framesPerSecond != null && value != null)
|
---|
304 | {
|
---|
305 | framesPerSecond.setValue(value.intValue());
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * This method will start the timer.
|
---|
312 | */
|
---|
313 | public void startAnimation()
|
---|
314 | {
|
---|
315 | timer.start();
|
---|
316 | frozen = false;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * This method will stop the timer.
|
---|
321 | */
|
---|
322 | public void stopAnimation()
|
---|
323 | {
|
---|
324 | timer.stop();
|
---|
325 | frozen = true;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * This method will tell what should happen when the timer is start.
|
---|
330 | * When the timer start it will increase the framenumber and will
|
---|
331 | * update the label with the new image icon.
|
---|
332 | * If the timer already display all the image icon in the list, it
|
---|
333 | * will restart the timer and reanimate the image icon again.
|
---|
334 | * @param e fires when the timer start.
|
---|
335 | */
|
---|
336 | public void actionPerformed(ActionEvent e)
|
---|
337 | {
|
---|
338 | if (frameNumber == (NUM_FRAMES - 1))
|
---|
339 | frameNumber = 0;
|
---|
340 | else
|
---|
341 | frameNumber++;
|
---|
342 |
|
---|
343 | updatePicture(frameNumber);
|
---|
344 |
|
---|
345 | if ((frameNumber == (NUM_FRAMES - 1)) ||
|
---|
346 | (frameNumber==(NUM_FRAMES/2 - 1)))
|
---|
347 | timer.restart();
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * This will update the label with the new ImageIcon.
|
---|
352 | * If the ImageIcon is null then the label will display an error message
|
---|
353 | * instead of the picture.
|
---|
354 | * @param frameNum this is the index of the ImageIcon to be displayed.
|
---|
355 | */
|
---|
356 | protected void updatePicture(int frameNum)
|
---|
357 | {
|
---|
358 | if (images.get(frameNumber) != null)
|
---|
359 | {
|
---|
360 | picture.setIcon((ImageIcon) images.get(frameNumber));
|
---|
361 | }
|
---|
362 | else
|
---|
363 | {
|
---|
364 | picture.setText(messages.getString("image")+" #"
|
---|
365 | + frameNumber + " " +
|
---|
366 | messages.getString("notfound"));
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 |
|
---|
373 |
|
---|
374 |
|
---|
375 |
|
---|