GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* JProgressBar.java -- 2: Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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. 10: 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. 15: 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. 20: 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. 25: 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. */ 37: 38: 39: package javax.swing; 40: 41: import java.awt.Graphics; 42: 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.border.Border; 49: import javax.swing.event.ChangeEvent; 50: import javax.swing.event.ChangeListener; 51: import javax.swing.plaf.ProgressBarUI; 52: 53: /** 54: * The ProgressBar is a widget that displays in two modes. In 55: * determinate mode, it displays fills a percentage of its bar 56: * based on its current value. In indeterminate mode, it creates 57: * box and bounces it between its bounds. 58: * 59: * <p> 60: * JProgressBars have the following properties: 61: * </p> 62: * 63: * <table> 64: * <tr><th> Property </th><th> Stored in </th><th> Bound? </th></tr> 65: * <tr><td> borderPainted </td><td> progressBar </td><td> yes </td></tr> 66: * <tr><td> changeListeners </td><td> progressBar </td><td> no </td></tr> 67: * <tr><td> indeterminate </td><td> progressBar </td><td> yes </td></tr> 68: * <tr><td> maximum </td><td> model </td><td> no </td></tr> 69: * <tr><td> minimum </td><td> model </td><td> no </td></tr> 70: * <tr><td> model </td><td> progressBar </td><td> no </td></tr> 71: * <tr><td> orientation </td><td> progressBar </td><td> yes </td></tr> 72: * <tr><td> percentComplete </td><td> progressBar </td><td> no </td></tr> 73: * <tr><td> string </td><td> progressBar </td><td> yes </td></tr> 74: * <tr><td> stringPainted </td><td> progressBar </td><td> yes </td></tr> 75: * <tr><td> value </td><td> model </td><td> no </td></tr> 76: * </table> 77: */ 78: public class JProgressBar extends JComponent implements SwingConstants, 79: Accessible 80: { 81: /** 82: * AccessibleJProgressBar 83: */ 84: protected class AccessibleJProgressBar extends AccessibleJComponent 85: implements AccessibleValue 86: { 87: private static final long serialVersionUID = -2938130009392721813L; 88: 89: /** 90: * Constructor AccessibleJProgressBar 91: */ 92: protected AccessibleJProgressBar() 93: { 94: } 95: 96: /** 97: * getAccessibleStateSet 98: * 99: * @return AccessibleStateSet 100: */ 101: public AccessibleStateSet getAccessibleStateSet() 102: { 103: return null; 104: } 105: 106: /** 107: * getAccessibleRole 108: * 109: * @return AccessibleRole 110: */ 111: public AccessibleRole getAccessibleRole() 112: { 113: return AccessibleRole.PROGRESS_BAR; 114: } 115: 116: /** 117: * getAccessibleValue 118: * 119: * @return AccessibleValue 120: */ 121: public AccessibleValue getAccessibleValue() 122: { 123: return null; 124: } 125: 126: /** 127: * getCurrentAccessibleValue 128: * 129: * @return Number 130: */ 131: public Number getCurrentAccessibleValue() 132: { 133: return null; 134: } 135: 136: /** 137: * setCurrentAccessibleValue 138: * 139: * @param value0 TODO 140: * 141: * @return boolean 142: */ 143: public boolean setCurrentAccessibleValue(Number value0) 144: { 145: return false; 146: } 147: 148: /** 149: * getMinimumAccessibleValue 150: * 151: * @return Number 152: */ 153: public Number getMinimumAccessibleValue() 154: { 155: return null; 156: } 157: 158: /** 159: * getMaximumAccessibleValue 160: * 161: * @return Number 162: */ 163: public Number getMaximumAccessibleValue() 164: { 165: return null; 166: } 167: } 168: 169: private static final long serialVersionUID = 1980046021813598781L; 170: 171: /** Whether the ProgressBar is determinate. */ 172: private transient boolean indeterminate = false; 173: 174: /** The orientation of the ProgressBar */ 175: protected int orientation = HORIZONTAL; 176: 177: /** Whether borders should be painted. */ 178: protected boolean paintBorder = true; 179: 180: /** The model describing this ProgressBar. */ 181: protected BoundedRangeModel model; 182: 183: /** The string that is displayed by the ProgressBar. */ 184: protected String progressString; 185: 186: /** Whether the string should be painted. */ 187: protected boolean paintString = false; 188: 189: /** The static changeEvent passed to all ChangeListeners. */ 190: protected transient ChangeEvent changeEvent; 191: 192: /** The ChangeListener that listens to the model. */ 193: protected ChangeListener changeListener; 194: 195: /** 196: * Creates a new horizontally oriented JProgressBar object 197: * with a minimum of 0 and a maximum of 100. 198: */ 199: public JProgressBar() 200: { 201: this(HORIZONTAL, 0, 100); 202: } 203: 204: /** 205: * Creates a new JProgressBar object with a minimum of 0, 206: * a maximum of 100, and the given orientation. 207: * 208: * @param orientation The orientation of the JProgressBar. 209: * 210: * @throws IllegalArgumentException if <code>orientation</code> is not either 211: * {@link #HORIZONTAL} or {@link #VERTICAL}. 212: */ 213: public JProgressBar(int orientation) 214: { 215: this(orientation, 0, 100); 216: } 217: 218: /** 219: * Creates a new horizontally oriented JProgressBar object 220: * with the given minimum and maximum. 221: * 222: * @param minimum The minimum of the JProgressBar. 223: * @param maximum The maximum of the JProgressBar. 224: */ 225: public JProgressBar(int minimum, int maximum) 226: { 227: this(HORIZONTAL, minimum, maximum); 228: } 229: 230: /** 231: * Creates a new JProgressBar object with the given minimum, 232: * maximum, and orientation. 233: * 234: * @param minimum The minimum of the JProgressBar. 235: * @param maximum The maximum of the JProgressBar. 236: * @param orientation The orientation of the JProgressBar. 237: * 238: * @throws IllegalArgumentException if <code>orientation</code> is not either 239: * {@link #HORIZONTAL} or {@link #VERTICAL}. 240: */ 241: public JProgressBar(int orientation, int minimum, int maximum) 242: { 243: model = new DefaultBoundedRangeModel(minimum, 0, minimum, maximum); 244: if (orientation != HORIZONTAL && orientation != VERTICAL) 245: throw new IllegalArgumentException(orientation + " is not a legal orientation"); 246: this.orientation = orientation; 247: changeListener = createChangeListener(); 248: model.addChangeListener(changeListener); 249: updateUI(); 250: } 251: 252: /** 253: * Creates a new horizontally oriented JProgressBar object 254: * with the given model. 255: * 256: * @param model The model to be used with the JProgressBar. 257: */ 258: public JProgressBar(BoundedRangeModel model) 259: { 260: this.model = model; 261: changeListener = createChangeListener(); 262: model.addChangeListener(changeListener); 263: updateUI(); 264: } 265: 266: /** 267: * This method returns the current value of the JProgressBar. 268: * 269: * @return The current value of the JProgressBar. 270: */ 271: public int getValue() 272: { 273: return model.getValue(); 274: } 275: 276: /** 277: * This method sets the value of the JProgressBar. 278: * 279: * @param value The value of the JProgressBar. 280: */ 281: public void setValue(int value) 282: { 283: model.setValue(value); 284: } 285: 286: /** 287: * This method paints the border of the JProgressBar 288: * 289: * @param graphics The graphics object to paint with. 290: */ 291: protected void paintBorder(Graphics graphics) 292: { 293: Border border = getBorder(); 294: if (paintBorder && border != null) 295: border.paintBorder(this, graphics, 0, 0, 296: getWidth(), 297: getHeight()); 298: } 299: 300: /** 301: * This method returns the orientation of the JProgressBar. 302: * 303: * @return The orientation of the JProgressBar. 304: */ 305: public int getOrientation() 306: { 307: return orientation; 308: } 309: 310: /** 311: * This method changes the orientation property. The orientation of the 312: * JProgressBar can be either horizontal or vertical. 313: * 314: * @param orientation The orientation of the JProgressBar. 315: */ 316: public void setOrientation(int orientation) 317: { 318: if (orientation != VERTICAL && orientation != HORIZONTAL) 319: throw new IllegalArgumentException("orientation must be one of VERTICAL or HORIZONTAL"); 320: if (this.orientation != orientation) 321: { 322: int oldOrientation = this.orientation; 323: this.orientation = orientation; 324: firePropertyChange("orientation", oldOrientation, 325: this.orientation); 326: } 327: } 328: 329: /** 330: * This method returns whether the progressString will be painted. 331: * 332: * @return Whether the string is painted. 333: */ 334: public boolean isStringPainted() 335: { 336: return paintString; 337: } 338: 339: /** 340: * This method changes the stringPainted property. 341: * 342: * @param painted Whether the string is painted. 343: */ 344: public void setStringPainted(boolean painted) 345: { 346: if (paintString != painted) 347: { 348: boolean oldPainted = paintString; 349: paintString = painted; 350: firePropertyChange("stringPainted", oldPainted, 351: paintString); 352: } 353: } 354: 355: /** 356: * This method returns the string that is painted if the 357: * stringPainted property is set to true. If there is no 358: * string set, it will return a string containing the 359: * JProgressBar's value as a percent. 360: * 361: * @return The string that is painted. 362: */ 363: public String getString() 364: { 365: if (progressString != null) 366: return progressString; 367: else 368: return (int) (getPercentComplete() * 100) + "%"; 369: } 370: 371: /** 372: * This method changes the string property. The string 373: * given will be the one painted. If you want to 374: * revert to the default string given, set the 375: * string to null. 376: * 377: * @param string The string to be painted. 378: */ 379: public void setString(String string) 380: { 381: if (((string == null || progressString == null) && 382: string != progressString) || (string != null && 383: ! string.equals(progressString))) 384: { 385: String oldString = progressString; 386: progressString = string; 387: firePropertyChange("string", oldString, progressString); 388: } 389: } 390: 391: /** 392: * This method returns the percent of the bar 393: * that is "complete". (This is the amount value / (max - min)). 394: * 395: * @return DOCUMENT ME! 396: */ 397: public double getPercentComplete() 398: { 399: if (getMaximum() == getMinimum()) 400: return 1.0; 401: else 402: return (double) (model.getValue() - model.getMinimum()) / (model 403: .getMaximum() 404: - model.getMinimum()); 405: } 406: 407: /** 408: * This method returns whether the border is painted. 409: * 410: * @return Whether the border is painted. 411: */ 412: public boolean isBorderPainted() 413: { 414: return paintBorder; 415: } 416: 417: /** 418: * This method changes the borderPainted property. 419: * 420: * @param painted Whether the border is painted. 421: */ 422: public void setBorderPainted(boolean painted) 423: { 424: if (painted != paintBorder) 425: { 426: boolean oldPainted = paintBorder; 427: paintBorder = painted; 428: firePropertyChange("borderPainted", oldPainted, 429: paintBorder); 430: } 431: } 432: 433: /** 434: * This method returns the JProgressBar's UI delegate. 435: * 436: * @return This JProgressBar's UI delegate. 437: */ 438: public ProgressBarUI getUI() 439: { 440: return (ProgressBarUI) ui; 441: } 442: 443: /** 444: * This method changes the UI property for this JProgressBar. 445: * 446: * @param ui The new UI delegate. 447: */ 448: public void setUI(ProgressBarUI ui) 449: { 450: super.setUI(ui); 451: } 452: 453: /** 454: * This method reverts the UI delegate for this JProgressBar 455: * to the default for this Look and Feel. 456: */ 457: public void updateUI() 458: { 459: setUI((ProgressBarUI) UIManager.getUI(this)); 460: invalidate(); 461: } 462: 463: /** 464: * This method returns the identifier to allow the UIManager 465: * to pick the correct class to act as the UI for 466: * this JProgressBar. 467: * 468: * @return The UIClassID: "ProgressBarUI". 469: */ 470: public String getUIClassID() 471: { 472: return "ProgressBarUI"; 473: } 474: 475: /** 476: * This method returns a ChangeListener that gets registered 477: * model. By default, the ChangeListener, propagates the 478: * ChangeEvents to the ChangeListeners of the JProgressBar. 479: * 480: * @return A new ChangeListener. 481: */ 482: protected ChangeListener createChangeListener() 483: { 484: return new ChangeListener() 485: { 486: public void stateChanged(ChangeEvent ce) 487: { 488: fireStateChanged(); 489: } 490: }; 491: } 492: 493: /** 494: * This method adds a ChangeListener to this JProgressBar. 495: * 496: * @param listener The ChangeListener to add to this JProgressBar. 497: */ 498: public void addChangeListener(ChangeListener listener) 499: { 500: listenerList.add(ChangeListener.class, listener); 501: } 502: 503: /** 504: * This method removes a ChangeListener from this JProgressBar. 505: * 506: * @param listener The ChangeListener to remove from this JProgressBar. 507: */ 508: public void removeChangeListener(ChangeListener listener) 509: { 510: listenerList.remove(ChangeListener.class, listener); 511: } 512: 513: /** 514: * This method returns an array of all ChangeListeners listening to this 515: * progress bar. 516: * 517: * @return An array of ChangeListeners listening to this progress bar. 518: */ 519: public ChangeListener[] getChangeListeners() 520: { 521: return (ChangeListener[]) listenerList.getListeners(ChangeListener.class); 522: } 523: 524: /** 525: * This method is called when the JProgressBar receives a ChangeEvent 526: * from its model. This simply propagates the event (changing the source 527: * to the JProgressBar) to the JProgressBar's listeners. 528: */ 529: protected void fireStateChanged() 530: { 531: Object[] changeListeners = listenerList.getListenerList(); 532: if (changeEvent == null) 533: changeEvent = new ChangeEvent(this); 534: for (int i = changeListeners.length - 2; i >= 0; i -= 2) 535: { 536: if (changeListeners[i] == ChangeListener.class) 537: ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent); 538: } 539: } 540: 541: /** 542: * This method returns the model used with this JProgressBar. 543: * 544: * @return The model used with this JProgressBar. 545: */ 546: public BoundedRangeModel getModel() 547: { 548: return model; 549: } 550: 551: /** 552: * This method changes the model property for this JProgressBar. 553: * 554: * @param model The model to use with this JProgressBar. 555: */ 556: public void setModel(BoundedRangeModel model) 557: { 558: if (model != this.model) 559: { 560: this.model.removeChangeListener(changeListener); 561: this.model = model; 562: this.model.addChangeListener(changeListener); 563: fireStateChanged(); 564: } 565: } 566: 567: /** 568: * This method returns the minimum value of this JProgressBar. 569: * 570: * @return The minimum value of this JProgressBar. 571: */ 572: public int getMinimum() 573: { 574: return model.getMinimum(); 575: } 576: 577: /** 578: * This method sets the minimum value of this JProgressBar. 579: * 580: * @param minimum The minimum value of this JProgressBar. 581: */ 582: public void setMinimum(int minimum) 583: { 584: model.setMinimum(minimum); 585: } 586: 587: /** 588: * This method returns the maximum value of this JProgressBar. 589: * 590: * @return The maximum value of this JProgressBar. 591: */ 592: public int getMaximum() 593: { 594: return model.getMaximum(); 595: } 596: 597: /** 598: * This method sets the maximum value of this JProgressBar. 599: * 600: * @param maximum The maximum value of this JProgressBar. 601: */ 602: public void setMaximum(int maximum) 603: { 604: model.setMaximum(maximum); 605: } 606: 607: /** 608: * This method returns a string that can be used to 609: * describe this JProgressBar. This method is usually 610: * only used for debugging purposes. 611: * 612: * @return A string that describes this JProgressBar. 613: */ 614: protected String paramString() 615: { 616: return "JProgressBar"; 617: } 618: 619: /** 620: * This method changes the indeterminate property. If the 621: * JProgressBar is determinate, it paints a percentage 622: * of the bar described by its value. If it is indeterminate, 623: * it simply bounces a box between the ends of the bar; the 624: * value of the JProgressBar is ignored. 625: * 626: * @param newValue Whether the JProgressBar is indeterminate. 627: */ 628: public void setIndeterminate(boolean newValue) 629: { 630: if (indeterminate != newValue) 631: { 632: boolean olddeter = indeterminate; 633: indeterminate = newValue; 634: firePropertyChange("indeterminate", olddeter, 635: indeterminate); 636: } 637: } 638: 639: /** 640: * This method returns whether the JProgressBar is indeterminate. 641: * 642: * @return Whether this JProgressBar is indeterminate. 643: */ 644: public boolean isIndeterminate() 645: { 646: return indeterminate; 647: } 648: 649: /** 650: * DOCUMENT ME! 651: * 652: * @return DOCUMENT ME! 653: */ 654: public AccessibleContext getAccessibleContext() 655: { 656: if (accessibleContext == null) 657: accessibleContext = new AccessibleJProgressBar(); 658: 659: return accessibleContext; 660: } 661: }
GNU Classpath (0.18) |