Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / ProgressMonitor.java
blob60f1c7145c011dbc847d9d274e8f2d84ada97d42
1 /* ProgressMonitor.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 import java.awt.Component;
41 import java.awt.event.ActionListener;
42 import java.awt.event.ActionEvent;
44 /**
45 * <p>Using this class you can easily monitor tasks where you cannot
46 * estimate the duration exactly.</p>
48 * <p>A ProgressMonitor instance waits until the first time setProgress
49 * is called. When <code>millisToDecideToPopup</code> time elapsed the
50 * instance estimates the duration until the whole operation is completed.
51 * If this duration exceeds <code>millisToPopup</code> a non-modal dialog
52 * with a message and a progress bar is shown.</p>
54 * <p>The value of <code>millisToDecideToPopup</code> defaults to
55 * <code>500</code> and <code>millisToPopup</code> to
56 * <code>2000</code>.</p>
58 * @author Andrew Selkirk
59 * @author Robert Schuster (robertschuster@fsfe.org)
60 * @since 1.2
61 * @status updated to 1.2
63 public class ProgressMonitor
65 /**
66 * parentComponent
68 Component component;
70 /**
71 * note
73 String note;
75 /**
76 * message
78 Object message;
80 /**
81 * millisToDecideToPopup
83 int millisToDecideToPopup = 500;
85 /**
86 * millisToPopup
88 int millisToPopup = 2000;
90 int min, max, progress;
92 JProgressBar progressBar;
94 JLabel noteLabel;
96 JDialog progressDialog;
98 Timer timer;
100 boolean canceled;
103 * Constructor ProgressMonitor
104 * @param component The parent component of the progress dialog or <code>null</code>.
105 * @param message A constant message object which works in the way it does in <code>JOptionPane</code>.
106 * @param note A string message which can be changed while the operation goes on.
107 * @param minimum The minimum value for the operation (start value).
108 * @param maximum The maximum value for the operation (end value).
110 public ProgressMonitor(Component component, Object message,
111 String note, int minimum, int maximum)
114 // Set data.
115 this.component = component;
116 this.message = message;
117 this.note = note;
119 min = minimum;
120 max = maximum;
124 * <p>Hides the dialog and stops any measurements.</p>
126 * <p>Has no effect when <code>setProgress</code> is not at least
127 * called once.</p>
129 public void close()
131 if ( progressDialog != null )
133 progressDialog.setVisible(false);
136 if ( timer != null )
138 timer.stop();
139 timer = null;
144 * <p>Updates the progress value.</p>
146 * <p>When called for the first time this initializes a timer
147 * which decides after <code>millisToDecideToPopup</code> time
148 * whether to show a progress dialog or not.</p>
150 * <p>If the progress value equals or exceeds the maximum
151 * value the progress dialog is closed automatically.</p>
153 * @param progress New progress value.
155 public void setProgress(int progress)
157 this.progress = progress;
159 // Initializes and starts a timer with a task
160 // which measures the duration and displays
161 // a progress dialog if neccessary.
162 if ( timer == null && progressDialog == null )
164 timer = new Timer(25, null);
165 timer.addActionListener(new TimerListener());
166 timer.start();
169 // Cancels timer and hides progress dialog if the
170 // maximum value is reached.
171 if ( progressBar != null && this.progress >= progressBar.getMaximum() )
173 // The reason for using progressBar.getMaximum() instead of max is that
174 // we want to prevent that changes to the value have any effect after the
175 // progress dialog is visible (This is how the JDK behaves.).
176 close();
181 /** Returns the minimum or start value of the operation.
183 * @returns Minimum or start value of the operation.
185 public int getMinimum()
187 return min;
191 * <p>Use this method to set the minimum or start value of
192 * your operation.</p>
194 * <p>For typical application like copy operation this will be
195 * zero.</p>
197 * <p>Keep in mind that changing this value after the progress
198 * dialog is made visible has no effect upon the progress bar.</p>
200 * @param minimum The new minimum value.
202 public void setMinimum(int minimum)
204 min = minimum;
208 * Return the maximum or end value of your operation.
210 * @returns Maximum or end value.
212 public int getMaximum()
214 return max;
218 * <p>Sets the maximum or end value of the operation to the
219 * given integer.</p>
221 * @param maximum
223 public void setMaximum(int maximum)
225 max = maximum;
229 * Returns whether the user canceled the operation.
231 * @returns Whether the operation was canceled.
233 public boolean isCanceled()
235 // The value is predefined to false
236 // and changes only when the user clicks
237 // the cancel button in the progress dialog.
238 return canceled;
242 * Returns the amount of milliseconds to wait
243 * until the ProgressMonitor should decide whether
244 * a progress dialog is to be shown or not.
246 * @returns The duration in milliseconds.
248 public int getMillisToDecideToPopup()
250 return millisToDecideToPopup;
254 * Sets the amount of milliseconds to wait until the
255 * ProgressMonitor should decide whether a progress dialog
256 * is to be shown or not.
258 * <p>This method has no effect when the progress dialog
259 * is already visible.</p>
261 * @param time The duration in milliseconds.
263 public void setMillisToDecideToPopup(int time)
265 millisToDecideToPopup = time;
269 * getMillisToPopup
270 * @returns int
272 public int getMillisToPopup()
274 return millisToPopup;
278 * setMillisToPopup
279 * @param time TODO
281 public void setMillisToPopup(int time)
283 millisToPopup = time;
287 * Returns a message which is shown in the progress dialog.
289 * @returns The changeable message visible in the progress dialog.
291 public String getNote()
293 return note;
297 * <p>Set the message shown in the progess dialog.</p>
299 * <p>Changing the note while the progress dialog is visible
300 * is possible.</p>
302 * @param note A message shown in the progress dialog.
304 public void setNote(String note)
306 if ( noteLabel != null )
308 noteLabel.setText(note);
310 else
312 this.note = note;
316 /** Internal method that creates the progress dialog.
318 void createDialog()
320 // If there is no note we suppress the generation of the
321 // label.
322 Object[] tmp = (note == null) ?
323 new Object[]
325 message,
326 progressBar = new JProgressBar(min, max)
329 new Object[]
331 message,
332 noteLabel = new JLabel(note),
333 progressBar = new JProgressBar(min, max)
336 JOptionPane pane = new JOptionPane(tmp, JOptionPane.INFORMATION_MESSAGE);
338 // FIXME: Internationalize the button
339 JButton cancelButton = new JButton("Cancel");
340 cancelButton.addActionListener(new ActionListener()
342 public void actionPerformed(ActionEvent ae)
344 canceled = true;
348 pane.setOptions(new Object[] { cancelButton });
350 // FIXME: Internationalize the title
351 progressDialog = pane.createDialog(component, "Progress ...");
352 progressDialog.setModal(false);
353 progressDialog.setResizable(true);
355 progressDialog.pack();
356 progressDialog.setVisible(true);
360 /** An ActionListener implementation which does the measurements
361 * and estimations of the ProgressMonitor.
363 class TimerListener implements ActionListener
365 long timestamp;
367 int lastProgress;
369 boolean first = true;
371 TimerListener()
373 timestamp = System.currentTimeMillis();
376 public void actionPerformed(ActionEvent ae)
378 long now = System.currentTimeMillis();
380 if ( first )
382 if (( now - timestamp ) > millisToDecideToPopup )
384 first = false;
385 long expected = ( now - timestamp ) * ( max - min ) / ( progress - min );
387 if ( expected > millisToPopup )
389 createDialog();
392 else
394 // We have not waited long enough to make a decision,
395 // so return and try again when the timer is invoked.
396 return;
399 else if ( progressDialog != null )
401 // The progress dialog is being displayed. We now calculate
402 // whether setting the progress bar to the current progress
403 // value would result in a visual difference.
404 int delta = progress - progressBar.getValue();
406 if ( ( delta * progressBar.getWidth() / (max - min) ) > 0 )
408 // At least one pixel would change.
409 progressBar.setValue(progress);
412 else
414 // No dialog necessary
415 timer.stop();
416 timer = null;
419 timestamp = now;