1 14 package org.wings; 15 16 import org.wings.plaf.ProgressBarCG; 17 18 import javax.swing.*; 19 import javax.swing.event.ChangeEvent ; 20 import javax.swing.event.ChangeListener ; 21 import java.awt.*; 22 import java.io.Serializable ; 23 import java.text.Format ; 24 import java.text.NumberFormat ; 25 26 27 36 public class SProgressBar extends SComponent { 37 38 public static final String STRING_PROPERTY = "_String_Property"; 39 public static final String STRING_PAINTED_PROPERTY = "_String_Painted_Property"; 40 public static final String BORDER_PAINTED_PROPERTY = "_Border_Painted_Property"; 41 public static final String ORIENTATION_PROPERTY = "_Orientation_Property"; 42 43 44 50 protected int orientation; 51 52 58 protected boolean paintBorder; 59 60 65 protected BoundedRangeModel model; 66 67 74 protected String progressString; 75 76 88 protected boolean paintString; 89 90 93 static final private int defaultMinimum = 0; 94 97 static final private int defaultMaximum = 100; 98 101 static final private int defaultOrientation = SConstants.HORIZONTAL; 102 103 108 protected transient ChangeEvent changeEvent = null; 109 110 118 protected ChangeListener changeListener = null; 119 120 123 private transient Format format; 124 125 132 private boolean indeterminate; 133 134 137 private Color borderColor; 138 139 142 private Color filledColor; 143 144 147 private Color unfilledColor; 148 149 152 private SDimension progressBarDimension; 153 154 166 public SProgressBar() { 167 this(defaultOrientation); 168 } 169 170 186 public SProgressBar(int orient) { 187 this(orient, defaultMinimum, defaultMaximum); 188 } 189 190 191 209 public SProgressBar(int min, int max) { 210 this(defaultOrientation, min, max); 211 } 212 213 214 233 public SProgressBar(int orient, int min, int max) { 234 setModel(new DefaultBoundedRangeModel(min, 0, min, max)); 238 239 setOrientation(orient); setBorderPainted(true); setStringPainted(false); setString(null); setIndeterminate(false); } 245 246 247 260 public SProgressBar(BoundedRangeModel newModel) { 261 setModel(newModel); 262 263 setOrientation(defaultOrientation); setBorderPainted(true); setStringPainted(false); setString(null); setIndeterminate(false); } 269 270 271 280 public int getOrientation() { 281 return orientation; 282 } 283 284 285 296 public void setOrientation(int newOrientation) { 297 if (orientation != newOrientation) { 298 switch (newOrientation) { 299 case SConstants.VERTICAL: 300 case SConstants.HORIZONTAL: 301 int oldOrientation = orientation; 302 orientation = newOrientation; 303 reloadIfChange(oldOrientation, newOrientation); 304 break; 305 default: 306 throw new IllegalArgumentException (newOrientation + 307 " is not a legal orientation"); 308 } 309 } 310 } 311 312 313 320 public boolean isStringPainted() { 321 return paintString; 322 } 323 324 325 338 public void setStringPainted(boolean b) { 339 boolean oldValue = paintString; 342 paintString = b; 343 reloadIfChange(oldValue, paintString); 344 } 345 346 347 357 public String getString() { 358 if (progressString != null) { 359 return progressString; 360 } else { 361 if (format == null) { 362 format = NumberFormat.getPercentInstance(); 363 } 364 return format.format(new Double (getPercentComplete())); 365 } 366 } 367 368 385 public void setString(String s) { 386 String oldValue = progressString; 387 progressString = s; 388 reloadIfChange(oldValue, progressString); 389 } 390 391 397 public double getPercentComplete() { 398 long span = model.getMaximum() - model.getMinimum(); 399 double currentValue = model.getValue(); 400 double pc = (currentValue - model.getMinimum()) / span; 401 return pc; 402 } 403 404 410 public boolean isBorderPainted() { 411 return paintBorder; 412 } 413 414 426 public void setBorderPainted(boolean b) { 427 boolean oldValue = paintBorder; 428 paintBorder = b; 429 reloadIfChange(oldValue, paintBorder); 430 } 431 432 437 public void setBorderColor(Color c) { 438 borderColor = c; 439 } 440 441 446 public Color getBorderColor() { 447 return borderColor; 448 } 449 450 455 public void setFilledColor(Color c) { 456 filledColor = c; 457 } 458 459 462 public Color getFilledColor() { 463 return filledColor; 464 } 465 466 471 public void setUnfilledColor(Color c) { 472 unfilledColor = c; 473 } 474 475 478 public Color getUnfilledColor() { 479 return unfilledColor; 480 } 481 482 487 public void setCG(ProgressBarCG cg) { 488 super.setCG(cg); 489 } 490 491 503 private class ModelListener implements ChangeListener , Serializable { 504 public void stateChanged(ChangeEvent e) { 505 reload(); 506 fireStateChanged(); 507 } 508 } 509 510 520 protected ChangeListener createChangeListener() { 521 return new ModelListener(); 522 } 523 524 529 public void addChangeListener(ChangeListener l) { 530 addEventListener(ChangeListener .class, l); 531 } 532 533 538 public void removeChangeListener(ChangeListener l) { 539 removeEventListener(ChangeListener .class, l); 540 } 541 542 550 public ChangeListener [] getChangeListeners() { 551 return (ChangeListener []) getListeners(ChangeListener .class); 552 } 553 554 562 protected void fireStateChanged() { 563 Object [] listeners = getListenerList(); 565 for (int i = listeners.length - 2; i >= 0; i -= 2) { 568 if (listeners[i] == ChangeListener .class) { 569 if (changeEvent == null) 571 changeEvent = new ChangeEvent (this); 572 ((ChangeListener ) listeners[i + 1]).stateChanged(changeEvent); 573 } 574 } 575 } 576 577 583 public BoundedRangeModel getModel() { 584 return model; 585 } 586 587 593 public void setModel(BoundedRangeModel newModel) { 594 BoundedRangeModel oldModel = getModel(); 596 597 if (newModel != oldModel) { 598 if (oldModel != null) { 599 oldModel.removeChangeListener(changeListener); 600 changeListener = null; 601 } 602 603 model = newModel; 604 605 if (newModel != null) { 606 changeListener = createChangeListener(); 607 newModel.addChangeListener(changeListener); 608 } 609 610 if (model != null) { 611 model.setExtent(0); 612 } 613 reload(); 614 } 615 } 616 617 618 619 620 631 public int getValue() { return getModel().getValue(); } 632 633 642 public int getMinimum() { return getModel().getMinimum(); } 643 644 653 public int getMaximum() { return getModel().getMaximum(); } 654 655 670 public void setValue(int n) { 671 BoundedRangeModel brm = getModel(); 672 int oldValue = brm.getValue(); 673 brm.setValue(n); 674 } 675 676 692 public void setMinimum(int n) { getModel().setMinimum(n); } 693 694 709 public void setMaximum(int n) { getModel().setMaximum(n); } 710 711 736 public void setIndeterminate(boolean newValue) { 737 boolean oldValue = indeterminate; 738 indeterminate = newValue; 739 } 740 741 749 public boolean isIndeterminate() { 750 return indeterminate; 751 } 752 753 757 public void setProgressBarDimension(SDimension dimension) { 758 progressBarDimension = dimension; 759 760 } 761 762 765 public SDimension getProgressBarDimension() { 766 return progressBarDimension; 767 } 768 769 770 771 } 773 | Popular Tags |