* javax/swing/JToggleButton.java (ToggleButtonModel):
[official-gcc.git] / libjava / javax / swing / JProgressBar.java
blob6ee29334c5aebb9381969eda185b997aa0355843
1 /* JProgressBar.java --
2 Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.Graphics;
41 import java.io.IOException;
42 import java.io.ObjectOutputStream;
43 import javax.accessibility.Accessible;
44 import javax.accessibility.AccessibleContext;
45 import javax.accessibility.AccessibleRole;
46 import javax.accessibility.AccessibleStateSet;
47 import javax.accessibility.AccessibleValue;
48 import javax.swing.event.ChangeEvent;
49 import javax.swing.event.ChangeListener;
50 import javax.swing.event.EventListenerList;
51 import javax.swing.plaf.ProgressBarUI;
54 /**
55 * <p>
56 * The ProgressBar is a widget that displays in two modes. In
57 * determinate mode, it displays fills a percentage of its bar
58 * based on its current value. In indeterminate mode, it creates
59 * box and bounces it between its bounds.
60 * </p>
62 * <p>
63 * JProgressBars have the following properties:
64 * </p>
66 * <table>
67 * <tr><th> Property </td><th> Stored in </td><th> Bound? </td></tr>
68 * <tr><td> borderPainted </td><td> progressBar </td><td> yes </td></tr>
69 * <tr><td> changeListeners </td><td> progressBar </td><td> no </td></tr>
70 * <tr><td> indeterminate </td><td> progressBar </td><td> yes </td></tr>
71 * <tr><td> maximum </td><td> model </td><td> no </td></tr>
72 * <tr><td> minimum </td><td> model </td><td> no </td></tr>
73 * <tr><td> model </td><td> progressBar </td><td> no </td></tr>
74 * <tr><td> orientation </td><td> progressBar </td><td> yes </td></tr>
75 * <tr><td> percentComplete </td><td> progressBar </td><td> no </td></tr>
76 * <tr><td> string </td><td> progressBar </td><td> yes </td></tr>
77 * <tr><td> stringPainted </td><td> progressBar </td><td> yes </td></tr>
78 * <tr><td> value </td><td> model </td><td> no </td></tr>
79 * </table>
81 public class JProgressBar extends JComponent implements SwingConstants,
82 Accessible
84 /**
85 * AccessibleJProgressBar
87 protected class AccessibleJProgressBar extends AccessibleJComponent
88 implements AccessibleValue
90 /**
91 * Constructor AccessibleJProgressBar
93 * @param component TODO
95 protected AccessibleJProgressBar(JProgressBar component)
97 super(component);
101 * getAccessibleStateSet
103 * @return AccessibleStateSet
105 public AccessibleStateSet getAccessibleStateSet()
107 return null;
111 * getAccessibleRole
113 * @return AccessibleRole
115 public AccessibleRole getAccessibleRole()
117 return AccessibleRole.PROGRESS_BAR;
121 * getAccessibleValue
123 * @return AccessibleValue
125 public AccessibleValue getAccessibleValue()
127 return null;
131 * getCurrentAccessibleValue
133 * @return Number
135 public Number getCurrentAccessibleValue()
137 return null;
141 * setCurrentAccessibleValue
143 * @param value0 TODO
145 * @return boolean
147 public boolean setCurrentAccessibleValue(Number value0)
149 return false;
153 * getMinimumAccessibleValue
155 * @return Number
157 public Number getMinimumAccessibleValue()
159 return null;
163 * getMaximumAccessibleValue
165 * @return Number
167 public Number getMaximumAccessibleValue()
169 return null;
173 /** Fired in a PropertyChangeEvent when the "borderPainted" property changes. */
174 public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted";
176 /** Fired in a PropertyChangeEvent when the "orientation" property changes. */
177 public static final String ORIENTATION_CHANGED_PROPERTY = "orientation";
179 /** Fired in a PropertyChangeEvent when the "string" property changes. */
180 public static final String STRING_CHANGED_PROPERTY = "string";
182 /** Fired in a PropertyChangeEvent when the "stringPainted" property changes. */
183 public static final String STRING_PAINTED_CHANGED_PROPERTY = "stringPainted";
185 /** Fired in a PropertyChangeEvent when the "indeterminate" property changes. */
186 public static final String INDETERMINATE_CHANGED_PROPERTY = "indeterminate";
188 /** Whether the ProgressBar is determinate. */
189 private transient boolean indeterminate = false;
191 /** The orientation of the ProgressBar */
192 protected int orientation = HORIZONTAL;
194 /** Whether borders should be painted. */
195 protected boolean paintBorder = true;
197 /** The model describing this ProgressBar. */
198 protected BoundedRangeModel model;
200 /** The string that is displayed by the ProgressBar. */
201 protected String progressString;
203 /** Whether the string should be painted. */
204 protected boolean paintString = false;
206 /** The static changeEvent passed to all ChangeListeners. */
207 protected transient ChangeEvent changeEvent;
209 /** The ChangeListener that listens to the model. */
210 protected ChangeListener changeListener;
213 * Creates a new horizontally oriented JProgressBar object
214 * with a minimum of 0 and a maximum of 100.
216 public JProgressBar()
218 this(0, 100, HORIZONTAL);
222 * Creates a new JProgressBar object with a minimum of 0,
223 * a maximum of 100, and the given orientation.
225 * @param orientation The orientation of the JProgressBar.
227 public JProgressBar(int orientation)
229 this(0, 100, orientation);
233 * Creates a new horizontally oriented JProgressBar object
234 * with the given minimum and maximum.
236 * @param minimum The minimum of the JProgressBar.
237 * @param maximum The maximum of the JProgressBar.
239 public JProgressBar(int minimum, int maximum)
241 this(minimum, maximum, HORIZONTAL);
245 * Creates a new JProgressBar object with the given minimum,
246 * maximum, and orientation.
248 * @param minimum The minimum of the JProgressBar.
249 * @param maximum The maximum of the JProgressBar.
250 * @param orientation The orientation of the JProgressBar.
252 public JProgressBar(int minimum, int maximum, int orientation)
254 model = new DefaultBoundedRangeModel(minimum, 0, minimum, maximum);
255 if (orientation != HORIZONTAL && orientation != VERTICAL)
256 throw new IllegalArgumentException(orientation + " is not a legal orientation");
257 this.orientation = orientation;
258 changeListener = createChangeListener();
259 model.addChangeListener(changeListener);
260 updateUI();
264 * Creates a new horizontally oriented JProgressBar object
265 * with the given model.
267 * @param model The model to be used with the JProgressBar.
269 public JProgressBar(BoundedRangeModel model)
271 this.model = model;
272 changeListener = createChangeListener();
273 model.addChangeListener(changeListener);
274 updateUI();
278 * This method returns the current value of the JProgressBar.
280 * @return The current value of the JProgressBar.
282 public int getValue()
284 return model.getValue();
288 * This method sets the value of the JProgressBar.
290 * @param value The value of the JProgressBar.
292 public void setValue(int value)
294 model.setValue(value);
298 * This method paints the border of the JProgressBar
300 * @param graphics The graphics object to paint with.
302 protected void paintBorder(Graphics graphics)
304 getBorder().paintBorder(this, graphics, 0, 0,
305 getWidth(),
306 getHeight());
310 * This method returns the orientation of the JProgressBar.
312 * @return The orientation of the JProgressBar.
314 public int getOrientation()
316 return orientation;
320 * This method changes the orientation property. The orientation of the
321 * JProgressBar can be either horizontal or vertical.
323 * @param orientation The orientation of the JProgressBar.
325 public void setOrientation(int orientation)
327 if (orientation != VERTICAL && orientation != HORIZONTAL)
328 throw new IllegalArgumentException("orientation must be one of VERTICAL or HORIZONTAL");
329 if (this.orientation != orientation)
331 int oldOrientation = this.orientation;
332 this.orientation = orientation;
333 firePropertyChange(ORIENTATION_CHANGED_PROPERTY, oldOrientation,
334 this.orientation);
339 * This method returns whether the progressString will be painted.
341 * @return Whether the string is painted.
343 public boolean isStringPainted()
345 return paintString;
349 * This method changes the stringPainted property.
351 * @param painted Whether the string is painted.
353 public void setStringPainted(boolean painted)
355 if (paintString != painted)
357 boolean oldPainted = paintString;
358 paintString = painted;
359 firePropertyChange(STRING_PAINTED_CHANGED_PROPERTY, oldPainted,
360 paintString);
365 * This method returns the string that is painted if the
366 * stringPainted property is set to true. If there is no
367 * string set, it will return a string containing the
368 * JProgressBar's value as a percent.
370 * @return The string that is painted.
372 public String getString()
374 if (progressString != null)
375 return progressString;
376 else
377 return (int) (getPercentComplete() * 100) + "%";
381 * This method changes the string property. The string
382 * given will be the one painted. If you want to
383 * revert to the default string given, set the
384 * string to null.
386 * @param string The string to be painted.
388 public void setString(String string)
390 if (((string == null || progressString == null) &&
391 string != progressString) || (string != null &&
392 ! string.equals(progressString)))
394 String oldString = progressString;
395 progressString = string;
396 firePropertyChange(STRING_CHANGED_PROPERTY, oldString, progressString);
401 * This method returns the percent of the bar
402 * that is "complete". (This is the amount value / (max - min)).
404 * @return DOCUMENT ME!
406 public double getPercentComplete()
408 if (getMaximum() == getMinimum())
409 return 1.0;
410 else
411 return (double) (model.getValue() - model.getMinimum()) / (model
412 .getMaximum()
413 - model.getMinimum());
417 * This method returns whether the border is painted.
419 * @return Whether the border is painted.
421 public boolean isBorderPainted()
423 return paintBorder;
427 * This method changes the borderPainted property.
429 * @param painted Whether the border is painted.
431 public void setBorderPainted(boolean painted)
433 if (painted != paintBorder)
435 boolean oldPainted = paintBorder;
436 paintBorder = painted;
437 firePropertyChange(BORDER_PAINTED_CHANGED_PROPERTY, oldPainted,
438 paintBorder);
443 * This method returns the JProgressBar's UI delegate.
445 * @return This JProgressBar's UI delegate.
447 public ProgressBarUI getUI()
449 return (ProgressBarUI) ui;
453 * This method changes the UI property for this JProgressBar.
455 * @param ui The new UI delegate.
457 public void setUI(ProgressBarUI ui)
459 super.setUI(ui);
463 * This method reverts the UI delegate for this JProgressBar
464 * to the default for this Look and Feel.
466 public void updateUI()
468 setUI((ProgressBarUI) UIManager.getUI(this));
469 invalidate();
473 * This method returns the identifier to allow the UIManager
474 * to pick the correct class to act as the UI for
475 * this JProgressBar.
477 * @return The UIClassID: "ProgressBarUI".
479 public String getUIClassID()
481 return "ProgressBarUI";
485 * This method returns a ChangeListener that gets registered
486 * model. By default, the ChangeListener, propagates the
487 * ChangeEvents to the ChangeListeners of the JProgressBar.
489 * @return A new ChangeListener.
491 protected ChangeListener createChangeListener()
493 return new ChangeListener()
495 public void stateChanged(ChangeEvent ce)
497 fireStateChanged();
503 * This method adds a ChangeListener to this JProgressBar.
505 * @param listener The ChangeListener to add to this JProgressBar.
507 public void addChangeListener(ChangeListener listener)
509 listenerList.add(ChangeListener.class, listener);
513 * This method removes a ChangeListener from this JProgressBar.
515 * @param listener The ChangeListener to remove from this JProgressBar.
517 public void removeChangeListener(ChangeListener listener)
519 listenerList.remove(ChangeListener.class, listener);
523 * This method returns an array of all ChangeListeners listening to this
524 * progress bar.
526 * @return An array of ChangeListeners listening to this progress bar.
528 public ChangeListener[] getChangeListeners()
530 return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
534 * This method is called when the JProgressBar receives a ChangeEvent
535 * from its model. This simply propagates the event (changing the source
536 * to the JProgressBar) to the JProgressBar's listeners.
538 protected void fireStateChanged()
540 Object[] changeListeners = listenerList.getListenerList();
541 if (changeEvent == null)
542 changeEvent = new ChangeEvent(this);
543 for (int i = changeListeners.length - 2; i >= 0; i -= 2)
545 if (changeListeners[i] == ChangeListener.class)
546 ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
551 * This method returns the model used with this JProgressBar.
553 * @return The model used with this JProgressBar.
555 public BoundedRangeModel getModel()
557 return model;
561 * This method changes the model property for this JProgressBar.
563 * @param model The model to use with this JProgressBar.
565 public void setModel(BoundedRangeModel model)
567 if (model != this.model)
569 this.model.removeChangeListener(changeListener);
570 this.model = model;
571 this.model.addChangeListener(changeListener);
572 fireStateChanged();
577 * This method returns the minimum value of this JProgressBar.
579 * @return The minimum value of this JProgressBar.
581 public int getMinimum()
583 return model.getMinimum();
587 * This method sets the minimum value of this JProgressBar.
589 * @param minimum The minimum value of this JProgressBar.
591 public void setMinimum(int minimum)
593 model.setMinimum(minimum);
597 * This method returns the maximum value of this JProgressBar.
599 * @return The maximum value of this JProgressBar.
601 public int getMaximum()
603 return model.getMaximum();
607 * This method sets the maximum value of this JProgressBar.
609 * @param maximum The maximum value of this JProgressBar.
611 public void setMaximum(int maximum)
613 model.setMaximum(maximum);
617 * This method returns a string that can be used to
618 * describe this JProgressBar. This method is usually
619 * only used for debugging purposes.
621 * @return A string that describes this JProgressBar.
623 protected String paramString()
625 return "JProgressBar";
629 * This method changes the indeterminate property. If the
630 * JProgressBar is determinate, it paints a percentage
631 * of the bar described by its value. If it is indeterminate,
632 * it simply bounces a box between the ends of the bar; the
633 * value of the JProgressBar is ignored.
635 * @param newValue Whether the JProgressBar is indeterminate.
637 public void setIndeterminate(boolean newValue)
639 if (indeterminate != newValue)
641 boolean olddeter = indeterminate;
642 indeterminate = newValue;
643 firePropertyChange(INDETERMINATE_CHANGED_PROPERTY, olddeter,
644 indeterminate);
649 * This method returns whether the JProgressBar is indeterminate.
651 * @return Whether this JProgressBar is indeterminate.
653 public boolean isIndeterminate()
655 return indeterminate;
659 * DOCUMENT ME!
661 * @return DOCUMENT ME!
663 public AccessibleContext getAccessibleContext()
665 if (accessibleContext == null)
666 accessibleContext = new AccessibleJProgressBar(this);
667 return accessibleContext;