< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/synth/SynthProgressBarUI.java

Print this page




  32 import javax.swing.plaf.basic.BasicProgressBarUI;
  33 import java.beans.PropertyChangeListener;
  34 import java.beans.PropertyChangeEvent;
  35 import sun.swing.SwingUtilities2;
  36 
  37 /**
  38  * Provides the Synth L&amp;F UI delegate for
  39  * {@link javax.swing.JProgressBar}.
  40  *
  41  * @author Joshua Outwater
  42  * @since 1.7
  43  */
  44 public class SynthProgressBarUI extends BasicProgressBarUI
  45                                 implements SynthUI, PropertyChangeListener {
  46     private SynthStyle style;
  47     private int progressPadding;
  48     private boolean rotateText; // added for Nimbus LAF
  49     private boolean paintOutsideClip;
  50     private boolean tileWhenIndeterminate; //whether to tile indeterminate painting
  51     private int tileWidth; //the width of each tile


  52 
  53     /**
  54      * Creates a new UI object for the given component.
  55      *
  56      * @param x component to create UI object for
  57      * @return the UI object
  58      */
  59     public static ComponentUI createUI(JComponent x) {
  60         return new SynthProgressBarUI();
  61     }
  62 
  63     /**
  64      * {@inheritDoc}
  65      */
  66     @Override
  67     protected void installListeners() {
  68         super.installListeners();
  69         progressBar.addPropertyChangeListener(this);
  70     }
  71 


  97         paintOutsideClip = style.getBoolean(context,
  98                 "ProgressBar.paintOutsideClip", false);
  99         rotateText = style.getBoolean(context,
 100                 "ProgressBar.rotateText", false);
 101         tileWhenIndeterminate = style.getBoolean(context, "ProgressBar.tileWhenIndeterminate", false);
 102         tileWidth = style.getInt(context, "ProgressBar.tileWidth", 15);
 103         // handle scaling for sizeVarients for special case components. The
 104         // key "JComponent.sizeVariant" scales for large/small/mini
 105         // components are based on Apples LAF
 106         String scaleKey = (String)progressBar.getClientProperty(
 107                 "JComponent.sizeVariant");
 108         if (scaleKey != null){
 109             if ("large".equals(scaleKey)){
 110                 tileWidth *= 1.15;
 111             } else if ("small".equals(scaleKey)){
 112                 tileWidth *= 0.857;
 113             } else if ("mini".equals(scaleKey)){
 114                 tileWidth *= 0.784;
 115             }
 116         }


 117         context.dispose();
 118     }
 119 
 120     /**
 121      * {@inheritDoc}
 122      */
 123     @Override
 124     protected void uninstallDefaults() {
 125         SynthContext context = getContext(progressBar, ENABLED);
 126 
 127         style.uninstallDefaults(context);
 128         context.dispose();
 129         style = null;
 130     }
 131 
 132     /**
 133      * {@inheritDoc}
 134      */
 135     @Override
 136     public SynthContext getContext(JComponent c) {


 241      */
 242     protected void paint(SynthContext context, Graphics g) {
 243         JProgressBar pBar = (JProgressBar)context.getComponent();
 244         int x = 0, y = 0, width = 0, height = 0;
 245         if (!pBar.isIndeterminate()) {
 246             Insets pBarInsets = pBar.getInsets();
 247             double percentComplete = pBar.getPercentComplete();
 248             if (percentComplete != 0.0) {
 249                 if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
 250                     x = pBarInsets.left + progressPadding;
 251                     y = pBarInsets.top + progressPadding;
 252                     width = (int)(percentComplete * (pBar.getWidth()
 253                             - (pBarInsets.left + progressPadding
 254                              + pBarInsets.right + progressPadding)));
 255                     height = pBar.getHeight()
 256                             - (pBarInsets.top + progressPadding
 257                              + pBarInsets.bottom + progressPadding);
 258 
 259                     if (!SynthLookAndFeel.isLeftToRight(pBar)) {
 260                         x = pBar.getWidth() - pBarInsets.right - width
 261                                 - progressPadding;
 262                     }
 263                 } else {  // JProgressBar.VERTICAL
 264                     x = pBarInsets.left + progressPadding;
 265                     width = pBar.getWidth()
 266                             - (pBarInsets.left + progressPadding
 267                             + pBarInsets.right + progressPadding);
 268                     height = (int)(percentComplete * (pBar.getHeight()
 269                             - (pBarInsets.top + progressPadding
 270                              + pBarInsets.bottom + progressPadding)));
 271                     y = pBar.getHeight() - pBarInsets.bottom - height
 272                             - progressPadding;
 273 
 274                     // When the progress bar is vertical we always paint
 275                     // from bottom to top, not matter what the component
 276                     // orientation is.
 277                 }
 278             }
 279         } else {
 280             boxRect = getBox(boxRect);
 281             x = boxRect.x + progressPadding;
 282             y = boxRect.y + progressPadding;
 283             width = boxRect.width - progressPadding - progressPadding;
 284             height = boxRect.height - progressPadding - progressPadding;
 285         }
 286 
 287         //if tiling and indeterminate, then paint the progress bar foreground a
 288         //bit wider than it should be. Shift as needed to ensure that there is
 289         //an animated effect
 290         if (tileWhenIndeterminate && pBar.isIndeterminate()) {
 291             double percentComplete = (double)getAnimationIndex() / (double)getFrameCount();
 292             int offset = (int)(percentComplete * tileWidth);
 293             Shape clip = g.getClip();
 294             g.clipRect(x, y, width, height);
 295             if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
 296                 //paint each tile horizontally
 297                 for (int i=x-tileWidth+offset; i<=width; i+=tileWidth) {
 298                     context.getPainter().paintProgressBarForeground(
 299                             context, g, i, y, tileWidth, height, pBar.getOrientation());
 300                 }
 301             } else { //JProgressBar.VERTICAL
 302                 //paint each tile vertically
 303                 for (int i=y-offset; i<height+tileWidth; i+=tileWidth) {
 304                     context.getPainter().paintProgressBarForeground(
 305                             context, g, x, i, width, tileWidth, pBar.getOrientation());
 306                 }
 307             }
 308             g.setClip(clip);
 309         } else {


 310             context.getPainter().paintProgressBarForeground(context, g,
 311                     x, y, width, height, pBar.getOrientation());

 312         }
 313 
 314         if (pBar.isStringPainted()) {
 315             paintText(context, g, pBar.getString());
 316         }
 317     }
 318 
 319     /**
 320      * Paints the component's text.
 321      *
 322      * @param context context for the component being painted
 323      * @param g {@code Graphics} object used for painting
 324      * @param title the text to paint
 325      */
 326     protected void paintText(SynthContext context, Graphics g, String title) {
 327         if (progressBar.isStringPainted()) {
 328             SynthStyle style = context.getStyle();
 329             Font font = style.getFont(context);
 330             FontMetrics fm = SwingUtilities2.getFontMetrics(
 331                     progressBar, g, font);




  32 import javax.swing.plaf.basic.BasicProgressBarUI;
  33 import java.beans.PropertyChangeListener;
  34 import java.beans.PropertyChangeEvent;
  35 import sun.swing.SwingUtilities2;
  36 
  37 /**
  38  * Provides the Synth L&amp;F UI delegate for
  39  * {@link javax.swing.JProgressBar}.
  40  *
  41  * @author Joshua Outwater
  42  * @since 1.7
  43  */
  44 public class SynthProgressBarUI extends BasicProgressBarUI
  45                                 implements SynthUI, PropertyChangeListener {
  46     private SynthStyle style;
  47     private int progressPadding;
  48     private boolean rotateText; // added for Nimbus LAF
  49     private boolean paintOutsideClip;
  50     private boolean tileWhenIndeterminate; //whether to tile indeterminate painting
  51     private int tileWidth; //the width of each tile
  52     private Dimension minBarSize; // minimal visible bar size
  53     private int glowWidth; // Glow around the bar foreground
  54 
  55     /**
  56      * Creates a new UI object for the given component.
  57      *
  58      * @param x component to create UI object for
  59      * @return the UI object
  60      */
  61     public static ComponentUI createUI(JComponent x) {
  62         return new SynthProgressBarUI();
  63     }
  64 
  65     /**
  66      * {@inheritDoc}
  67      */
  68     @Override
  69     protected void installListeners() {
  70         super.installListeners();
  71         progressBar.addPropertyChangeListener(this);
  72     }
  73 


  99         paintOutsideClip = style.getBoolean(context,
 100                 "ProgressBar.paintOutsideClip", false);
 101         rotateText = style.getBoolean(context,
 102                 "ProgressBar.rotateText", false);
 103         tileWhenIndeterminate = style.getBoolean(context, "ProgressBar.tileWhenIndeterminate", false);
 104         tileWidth = style.getInt(context, "ProgressBar.tileWidth", 15);
 105         // handle scaling for sizeVarients for special case components. The
 106         // key "JComponent.sizeVariant" scales for large/small/mini
 107         // components are based on Apples LAF
 108         String scaleKey = (String)progressBar.getClientProperty(
 109                 "JComponent.sizeVariant");
 110         if (scaleKey != null){
 111             if ("large".equals(scaleKey)){
 112                 tileWidth *= 1.15;
 113             } else if ("small".equals(scaleKey)){
 114                 tileWidth *= 0.857;
 115             } else if ("mini".equals(scaleKey)){
 116                 tileWidth *= 0.784;
 117             }
 118         }
 119         minBarSize = (Dimension)style.get(context, "ProgressBar.minBarSize");
 120         glowWidth = style.getInt(context, "ProgressBar.glowWidth", 0);
 121         context.dispose();
 122     }
 123 
 124     /**
 125      * {@inheritDoc}
 126      */
 127     @Override
 128     protected void uninstallDefaults() {
 129         SynthContext context = getContext(progressBar, ENABLED);
 130 
 131         style.uninstallDefaults(context);
 132         context.dispose();
 133         style = null;
 134     }
 135 
 136     /**
 137      * {@inheritDoc}
 138      */
 139     @Override
 140     public SynthContext getContext(JComponent c) {


 245      */
 246     protected void paint(SynthContext context, Graphics g) {
 247         JProgressBar pBar = (JProgressBar)context.getComponent();
 248         int x = 0, y = 0, width = 0, height = 0;
 249         if (!pBar.isIndeterminate()) {
 250             Insets pBarInsets = pBar.getInsets();
 251             double percentComplete = pBar.getPercentComplete();
 252             if (percentComplete != 0.0) {
 253                 if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
 254                     x = pBarInsets.left + progressPadding;
 255                     y = pBarInsets.top + progressPadding;
 256                     width = (int)(percentComplete * (pBar.getWidth()
 257                             - (pBarInsets.left + progressPadding
 258                              + pBarInsets.right + progressPadding)));
 259                     height = pBar.getHeight()
 260                             - (pBarInsets.top + progressPadding
 261                              + pBarInsets.bottom + progressPadding);
 262 
 263                     if (!SynthLookAndFeel.isLeftToRight(pBar)) {
 264                         x = pBar.getWidth() - pBarInsets.right - width
 265                                 - progressPadding - glowWidth;
 266                     }
 267                 } else {  // JProgressBar.VERTICAL
 268                     x = pBarInsets.left + progressPadding;
 269                     width = pBar.getWidth()
 270                             - (pBarInsets.left + progressPadding
 271                             + pBarInsets.right + progressPadding);
 272                     height = (int)(percentComplete * (pBar.getHeight()
 273                             - (pBarInsets.top + progressPadding
 274                              + pBarInsets.bottom + progressPadding)));
 275                     y = pBar.getHeight() - pBarInsets.bottom - height
 276                             - progressPadding;
 277 
 278                     if (SynthLookAndFeel.isLeftToRight(pBar)) {
 279                         y -= glowWidth;
 280                     }
 281                 }
 282             }
 283         } else {
 284             boxRect = getBox(boxRect);
 285             x = boxRect.x + progressPadding;
 286             y = boxRect.y + progressPadding;
 287             width = boxRect.width - progressPadding - progressPadding;
 288             height = boxRect.height - progressPadding - progressPadding;
 289         }
 290 
 291         //if tiling and indeterminate, then paint the progress bar foreground a
 292         //bit wider than it should be. Shift as needed to ensure that there is
 293         //an animated effect
 294         if (tileWhenIndeterminate && pBar.isIndeterminate()) {
 295             double percentComplete = (double)getAnimationIndex() / (double)getFrameCount();
 296             int offset = (int)(percentComplete * tileWidth);
 297             Shape clip = g.getClip();
 298             g.clipRect(x, y, width, height);
 299             if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
 300                 //paint each tile horizontally
 301                 for (int i=x-tileWidth+offset; i<=width; i+=tileWidth) {
 302                     context.getPainter().paintProgressBarForeground(
 303                             context, g, i, y, tileWidth, height, pBar.getOrientation());
 304                 }
 305             } else { //JProgressBar.VERTICAL
 306                 //paint each tile vertically
 307                 for (int i=y-offset; i<height+tileWidth; i+=tileWidth) {
 308                     context.getPainter().paintProgressBarForeground(
 309                             context, g, x, i, width, tileWidth, pBar.getOrientation());
 310                 }
 311             }
 312             g.setClip(clip);
 313         } else {
 314             if (minBarSize == null || (width >= minBarSize.width
 315                     && height >= minBarSize.height)) {
 316                 context.getPainter().paintProgressBarForeground(context, g,
 317                         x, y, width, height, pBar.getOrientation());
 318             }
 319         }
 320 
 321         if (pBar.isStringPainted()) {
 322             paintText(context, g, pBar.getString());
 323         }
 324     }
 325 
 326     /**
 327      * Paints the component's text.
 328      *
 329      * @param context context for the component being painted
 330      * @param g {@code Graphics} object used for painting
 331      * @param title the text to paint
 332      */
 333     protected void paintText(SynthContext context, Graphics g, String title) {
 334         if (progressBar.isStringPainted()) {
 335             SynthStyle style = context.getStyle();
 336             Font font = style.getFont(context);
 337             FontMetrics fm = SwingUtilities2.getFontMetrics(
 338                     progressBar, g, font);


< prev index next >