1 /*
   2  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.synth;
  27 
  28 import java.awt.*;
  29 import java.awt.geom.AffineTransform;
  30 import javax.swing.*;
  31 import javax.swing.plaf.*;
  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&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 
  72     /**
  73      * @inheritDoc
  74      */
  75     @Override
  76     protected void uninstallListeners() {
  77         super.uninstallListeners();
  78         progressBar.removePropertyChangeListener(this);
  79     }
  80 
  81     /**
  82      * @inheritDoc
  83      */
  84     @Override
  85     protected void installDefaults() {
  86         updateStyle(progressBar);
  87     }
  88 
  89     private void updateStyle(JProgressBar c) {
  90         SynthContext context = getContext(c, ENABLED);
  91         SynthStyle oldStyle = style;
  92         style = SynthLookAndFeel.updateStyle(context, this);
  93         setCellLength(style.getInt(context, "ProgressBar.cellLength", 1));
  94         setCellSpacing(style.getInt(context, "ProgressBar.cellSpacing", 0));
  95         progressPadding = style.getInt(context,
  96                 "ProgressBar.progressPadding", 0);
  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) {
 137         return getContext(c, getComponentState(c));
 138     }
 139 
 140     private SynthContext getContext(JComponent c, int state) {
 141         return SynthContext.getContext(SynthContext.class, c,
 142                             SynthLookAndFeel.getRegion(c), style, state);
 143     }
 144 
 145     private int getComponentState(JComponent c) {
 146         return SynthLookAndFeel.getComponentState(c);
 147     }
 148 
 149     /**
 150      * @inheritDoc
 151      */
 152     @Override
 153     public int getBaseline(JComponent c, int width, int height) {
 154         super.getBaseline(c, width, height);
 155         if (progressBar.isStringPainted() &&
 156                 progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 157             SynthContext context = getContext(c);
 158             Font font = context.getStyle().getFont(context);
 159             FontMetrics metrics = progressBar.getFontMetrics(font);
 160             context.dispose();
 161             return (height - metrics.getAscent() - metrics.getDescent()) / 2 +
 162                     metrics.getAscent();
 163         }
 164         return -1;
 165     }
 166 
 167     /**
 168      * @inheritDoc
 169      */
 170     @Override
 171     protected Rectangle getBox(Rectangle r) {
 172         if (tileWhenIndeterminate) {
 173             return SwingUtilities.calculateInnerArea(progressBar, r);
 174         } else {
 175             return super.getBox(r);
 176         }
 177     }
 178 
 179     /**
 180      * @inheritDoc
 181      */
 182     @Override
 183     protected void setAnimationIndex(int newValue) {
 184         if (paintOutsideClip) {
 185             if (getAnimationIndex() == newValue) {
 186                 return;
 187             }
 188             super.setAnimationIndex(newValue);
 189             progressBar.repaint();
 190         } else {
 191             super.setAnimationIndex(newValue);
 192         }
 193     }
 194 
 195     /**
 196      * Notifies this UI delegate to repaint the specified component.
 197      * This method paints the component background, then calls
 198      * the {@link #paint(SynthContext,Graphics)} method.
 199      *
 200      * <p>In general, this method does not need to be overridden by subclasses.
 201      * All Look and Feel rendering code should reside in the {@code paint} method.
 202      *
 203      * @param g the {@code Graphics} object used for painting
 204      * @param c the component being painted
 205      * @see #paint(SynthContext,Graphics)
 206      */
 207     @Override
 208     public void update(Graphics g, JComponent c) {
 209         SynthContext context = getContext(c);
 210 
 211         SynthLookAndFeel.update(context, g);
 212         context.getPainter().paintProgressBarBackground(context,
 213                           g, 0, 0, c.getWidth(), c.getHeight(),
 214                           progressBar.getOrientation());
 215         paint(context, g);
 216         context.dispose();
 217     }
 218 
 219     /**
 220      * Paints the specified component according to the Look and Feel.
 221      * <p>This method is not used by Synth Look and Feel.
 222      * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
 223      *
 224      * @param g the {@code Graphics} object used for painting
 225      * @param c the component being painted
 226      * @see #paint(SynthContext,Graphics)
 227      */
 228     @Override
 229     public void paint(Graphics g, JComponent c) {
 230         SynthContext context = getContext(c);
 231 
 232         paint(context, g);
 233         context.dispose();
 234     }
 235 
 236     /**
 237      * Paints the specified component.
 238      *
 239      * @param context context for the component being painted
 240      * @param g the {@code Graphics} object used for painting
 241      * @see #update(Graphics,JComponent)
 242      */
 243     protected void paint(SynthContext context, Graphics g) {
 244         JProgressBar pBar = (JProgressBar)context.getComponent();
 245         int x = 0, y = 0, width = 0, height = 0;
 246         if (!pBar.isIndeterminate()) {
 247             Insets pBarInsets = pBar.getInsets();
 248             double percentComplete = pBar.getPercentComplete();
 249             if (percentComplete != 0.0) {
 250                 if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
 251                     x = pBarInsets.left + progressPadding;
 252                     y = pBarInsets.top + progressPadding;
 253                     width = (int)(percentComplete * (pBar.getWidth()
 254                             - (pBarInsets.left + progressPadding
 255                              + pBarInsets.right + progressPadding)));
 256                     height = pBar.getHeight()
 257                             - (pBarInsets.top + progressPadding
 258                              + pBarInsets.bottom + progressPadding);
 259 
 260                     if (!SynthLookAndFeel.isLeftToRight(pBar)) {
 261                         x = pBar.getWidth() - pBarInsets.right - width
 262                                 - progressPadding;
 263                     }
 264                 } else {  // JProgressBar.VERTICAL
 265                     x = pBarInsets.left + progressPadding;
 266                     width = pBar.getWidth()
 267                             - (pBarInsets.left + progressPadding
 268                             + pBarInsets.right + progressPadding);
 269                     height = (int)(percentComplete * (pBar.getHeight()
 270                             - (pBarInsets.top + progressPadding
 271                              + pBarInsets.bottom + progressPadding)));
 272                     y = pBar.getHeight() - pBarInsets.bottom - height
 273                             - progressPadding;
 274 
 275                     // When the progress bar is vertical we always paint
 276                     // from bottom to top, not matter what the component
 277                     // orientation is.
 278                 }
 279             }
 280         } else {
 281             boxRect = getBox(boxRect);
 282             x = boxRect.x + progressPadding;
 283             y = boxRect.y + progressPadding;
 284             width = boxRect.width - progressPadding - progressPadding;
 285             height = boxRect.height - progressPadding - progressPadding;
 286         }
 287 
 288         //if tiling and indeterminate, then paint the progress bar foreground a
 289         //bit wider than it should be. Shift as needed to ensure that there is
 290         //an animated effect
 291         if (tileWhenIndeterminate && pBar.isIndeterminate()) {
 292             double percentComplete = (double)getAnimationIndex() / (double)getFrameCount();
 293             int offset = (int)(percentComplete * tileWidth);
 294             Shape clip = g.getClip();
 295             g.clipRect(x, y, width, height);
 296             if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
 297                 //paint each tile horizontally
 298                 for (int i=x-tileWidth+offset; i<=width; i+=tileWidth) {
 299                     context.getPainter().paintProgressBarForeground(
 300                             context, g, i, y, tileWidth, height, pBar.getOrientation());
 301                 }
 302             } else { //JProgressBar.VERTICAL
 303                 //paint each tile vertically
 304                 for (int i=y-offset; i<height+tileWidth; i+=tileWidth) {
 305                     context.getPainter().paintProgressBarForeground(
 306                             context, g, x, i, width, tileWidth, pBar.getOrientation());
 307                 }
 308             }
 309             g.setClip(clip);
 310         } else {
 311             context.getPainter().paintProgressBarForeground(context, g,
 312                     x, y, width, height, pBar.getOrientation());
 313         }
 314 
 315         if (pBar.isStringPainted()) {
 316             paintText(context, g, pBar.getString());
 317         }
 318     }
 319 
 320     /**
 321      * Paints the component's text.
 322      *
 323      * @param context context for the component being painted
 324      * @param g {@code Graphics} object used for painting
 325      * @param title the text to paint
 326      */
 327     protected void paintText(SynthContext context, Graphics g, String title) {
 328         if (progressBar.isStringPainted()) {
 329             SynthStyle style = context.getStyle();
 330             Font font = style.getFont(context);
 331             FontMetrics fm = SwingUtilities2.getFontMetrics(
 332                     progressBar, g, font);
 333             int strLength = style.getGraphicsUtils(context).
 334                 computeStringWidth(context, font, fm, title);
 335             Rectangle bounds = progressBar.getBounds();
 336 
 337             if (rotateText &&
 338                     progressBar.getOrientation() == JProgressBar.VERTICAL){
 339                 Graphics2D g2 = (Graphics2D)g;
 340                 // Calculate the position for the text.
 341                 Point textPos;
 342                 AffineTransform rotation;
 343                 if (progressBar.getComponentOrientation().isLeftToRight()){
 344                     rotation = AffineTransform.getRotateInstance(-Math.PI/2);
 345                     textPos = new Point(
 346                         (bounds.width+fm.getAscent()-fm.getDescent())/2,
 347                            (bounds.height+strLength)/2);
 348                 } else {
 349                     rotation = AffineTransform.getRotateInstance(Math.PI/2);
 350                     textPos = new Point(
 351                         (bounds.width-fm.getAscent()+fm.getDescent())/2,
 352                            (bounds.height-strLength)/2);
 353                 }
 354 
 355                 // Progress bar isn't wide enough for the font.  Don't paint it.
 356                 if (textPos.x < 0) {
 357                     return;
 358                 }
 359 
 360                 // Paint the text.
 361                 font = font.deriveFont(rotation);
 362                 g2.setFont(font);
 363                 g2.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
 364                 style.getGraphicsUtils(context).paintText(context, g, title,
 365                                                      textPos.x, textPos.y, -1);
 366             } else {
 367                 // Calculate the bounds for the text.
 368                 Rectangle textRect = new Rectangle(
 369                     (bounds.width / 2) - (strLength / 2),
 370                     (bounds.height -
 371                         (fm.getAscent() + fm.getDescent())) / 2,
 372                     0, 0);
 373 
 374                 // Progress bar isn't tall enough for the font.  Don't paint it.
 375                 if (textRect.y < 0) {
 376                     return;
 377                 }
 378 
 379                 // Paint the text.
 380                 g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
 381                 g.setFont(font);
 382                 style.getGraphicsUtils(context).paintText(context, g, title,
 383                                                      textRect.x, textRect.y, -1);
 384             }
 385         }
 386     }
 387 
 388     /**
 389      * @inheritDoc
 390      */
 391     @Override
 392     public void paintBorder(SynthContext context, Graphics g, int x,
 393                             int y, int w, int h) {
 394         context.getPainter().paintProgressBarBorder(context, g, x, y, w, h,
 395                                                     progressBar.getOrientation());
 396     }
 397 
 398     /**
 399      * @inheritDoc
 400      */
 401     @Override
 402     public void propertyChange(PropertyChangeEvent e) {
 403         if (SynthLookAndFeel.shouldUpdateStyle(e) ||
 404                 "indeterminate".equals(e.getPropertyName())) {
 405             updateStyle((JProgressBar)e.getSource());
 406         }
 407     }
 408 
 409     /**
 410      * @inheritDoc
 411      */
 412     @Override
 413     public Dimension getPreferredSize(JComponent c) {
 414         Dimension size = null;
 415         Insets border = progressBar.getInsets();
 416         FontMetrics fontSizer = progressBar.getFontMetrics(progressBar.getFont());
 417         String progString = progressBar.getString();
 418         int stringHeight = fontSizer.getHeight() + fontSizer.getDescent();
 419 
 420         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 421             size = new Dimension(getPreferredInnerHorizontal());
 422             if (progressBar.isStringPainted()) {
 423                 // adjust the height if necessary to make room for the string
 424                 if (stringHeight > size.height) {
 425                     size.height = stringHeight;
 426                 }
 427 
 428                 // adjust the width if necessary to make room for the string
 429                 int stringWidth = SwingUtilities2.stringWidth(
 430                                        progressBar, fontSizer, progString);
 431                 if (stringWidth > size.width) {
 432                     size.width = stringWidth;
 433                 }
 434             }
 435         } else {
 436             size = new Dimension(getPreferredInnerVertical());
 437             if (progressBar.isStringPainted()) {
 438                 // make sure the width is big enough for the string
 439                 if (stringHeight > size.width) {
 440                     size.width = stringHeight;
 441                 }
 442 
 443                 // make sure the height is big enough for the string
 444                 int stringWidth = SwingUtilities2.stringWidth(
 445                                        progressBar, fontSizer, progString);
 446                 if (stringWidth > size.height) {
 447                     size.height = stringWidth;
 448                 }
 449             }
 450         }
 451 
 452         // handle scaling for sizeVarients for special case components. The
 453         // key "JComponent.sizeVariant" scales for large/small/mini
 454         // components are based on Apples LAF
 455         String scaleKey = (String)progressBar.getClientProperty(
 456                 "JComponent.sizeVariant");
 457         if (scaleKey != null){
 458             if ("large".equals(scaleKey)){
 459                 size.width *= 1.15f;
 460                 size.height *= 1.15f;
 461             } else if ("small".equals(scaleKey)){
 462                 size.width *= 0.90f;
 463                 size.height *= 0.90f;
 464             } else if ("mini".equals(scaleKey)){
 465                 size.width *= 0.784f;
 466                 size.height *= 0.784f;
 467             }
 468         }
 469 
 470         size.width += border.left + border.right;
 471         size.height += border.top + border.bottom;
 472 
 473         return size;
 474     }
 475 }