source: main/trunk/gli/src/org/greenstone/gatherer/feedback/Movie.java@ 24915

Last change on this file since 24915 was 7315, checked in by kjdon, 20 years ago

Veronika's feedback code - still needs some work, but its disabled by default

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