1 /*
   2  * Copyright (c) 1998, 2014, 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.metal;
  27 
  28 import javax.swing.*;
  29 import javax.swing.plaf.*;
  30 import javax.swing.plaf.basic.*;
  31 import java.awt.*;
  32 
  33 /**
  34  * The Metal implementation of ProgressBarUI.
  35  * <p>
  36  * <strong>Warning:</strong>
  37  * Serialized objects of this class will not be compatible with
  38  * future Swing releases. The current serialization support is
  39  * appropriate for short term storage or RMI between applications running
  40  * the same version of Swing.  As of 1.4, support for long term storage
  41  * of all JavaBeans
  42  * has been added to the <code>java.beans</code> package.
  43  * Please see {@link java.beans.XMLEncoder}.
  44  *
  45  * @author Michael C. Albers
  46  */
  47 @SuppressWarnings("serial") // Same-version serialization only
  48 public class MetalProgressBarUI extends BasicProgressBarUI {
  49 
  50     private Rectangle innards;
  51     private Rectangle box;
  52 
  53     /**
  54      * Constructs an instance of {@code MetalProgressBarUI}.
  55      *
  56      * @param c a component
  57      * @return an instance of {@code MetalProgressBarUI}
  58      */
  59     public static ComponentUI createUI(JComponent c) {
  60         return new MetalProgressBarUI();
  61     }
  62 
  63     /**
  64      * Draws a bit of special highlighting on the progress bar.
  65      * The core painting is deferred to the BasicProgressBar's
  66      * <code>paintDeterminate</code> method.
  67      * @since 1.4
  68      */
  69     public void paintDeterminate(Graphics g, JComponent c) {
  70         super.paintDeterminate(g,c);
  71 
  72         if (!(g instanceof Graphics2D)) {
  73             return;
  74         }
  75 
  76         if (progressBar.isBorderPainted()) {
  77             Insets b = progressBar.getInsets(); // area for border
  78             int barRectWidth = progressBar.getWidth() - (b.left + b.right);
  79             int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
  80             int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
  81             boolean isLeftToRight = MetalUtils.isLeftToRight(c);
  82             int startX, startY, endX, endY;
  83 
  84             // The progress bar border is painted according to a light source.
  85             // This light source is stationary and does not change when the
  86             // component orientation changes.
  87             startX = b.left;
  88             startY = b.top;
  89             endX = b.left + barRectWidth - 1;
  90             endY = b.top + barRectHeight - 1;
  91 
  92             Graphics2D g2 = (Graphics2D)g;
  93             g2.setStroke(new BasicStroke(1.f));
  94 
  95             if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  96                 // Draw light line lengthwise across the progress bar.
  97                 g2.setColor(MetalLookAndFeel.getControlShadow());
  98                 g2.drawLine(startX, startY, endX, startY);
  99 
 100                 if (amountFull > 0) {
 101                     // Draw darker lengthwise line over filled area.
 102                     g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 103 
 104                     if (isLeftToRight) {
 105                         g2.drawLine(startX, startY,
 106                                 startX + amountFull - 1, startY);
 107                     } else {
 108                         g2.drawLine(endX, startY,
 109                                 endX - amountFull + 1, startY);
 110                         if (progressBar.getPercentComplete() != 1.f) {
 111                             g2.setColor(MetalLookAndFeel.getControlShadow());
 112                         }
 113                     }
 114                 }
 115                 // Draw a line across the width.  The color is determined by
 116                 // the code above.
 117                 g2.drawLine(startX, startY, startX, endY);
 118 
 119             } else { // VERTICAL
 120                 // Draw light line lengthwise across the progress bar.
 121                 g2.setColor(MetalLookAndFeel.getControlShadow());
 122                 g2.drawLine(startX, startY, startX, endY);
 123 
 124                 if (amountFull > 0) {
 125                     // Draw darker lengthwise line over filled area.
 126                     g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 127                     g2.drawLine(startX, endY,
 128                             startX, endY - amountFull + 1);
 129                 }
 130                 // Draw a line across the width.  The color is determined by
 131                 // the code above.
 132                 g2.setColor(MetalLookAndFeel.getControlShadow());
 133 
 134                 if (progressBar.getPercentComplete() == 1.f) {
 135                     g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 136                 }
 137                 g2.drawLine(startX, startY, endX, startY);
 138             }
 139         }
 140     }
 141 
 142     /**
 143      * Draws a bit of special highlighting on the progress bar
 144      * and bouncing box.
 145      * The core painting is deferred to the BasicProgressBar's
 146      * <code>paintIndeterminate</code> method.
 147      * @since 1.4
 148      */
 149     public void paintIndeterminate(Graphics g, JComponent c) {
 150         super.paintIndeterminate(g, c);
 151 
 152         if (!progressBar.isBorderPainted() || (!(g instanceof Graphics2D))) {
 153             return;
 154         }
 155 
 156         Insets b = progressBar.getInsets(); // area for border
 157         int barRectWidth = progressBar.getWidth() - (b.left + b.right);
 158         int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
 159         int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
 160         boolean isLeftToRight = MetalUtils.isLeftToRight(c);
 161         int startX, startY, endX, endY;
 162         Rectangle box = null;
 163         box = getBox(box);
 164 
 165         // The progress bar border is painted according to a light source.
 166         // This light source is stationary and does not change when the
 167         // component orientation changes.
 168         startX = b.left;
 169         startY = b.top;
 170         endX = b.left + barRectWidth - 1;
 171         endY = b.top + barRectHeight - 1;
 172 
 173         Graphics2D g2 = (Graphics2D)g;
 174         g2.setStroke(new BasicStroke(1.f));
 175 
 176         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 177             // Draw light line lengthwise across the progress bar.
 178             g2.setColor(MetalLookAndFeel.getControlShadow());
 179             g2.drawLine(startX, startY, endX, startY);
 180             g2.drawLine(startX, startY, startX, endY);
 181 
 182             // Draw darker lengthwise line over filled area.
 183             g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 184             g2.drawLine(box.x, startY, box.x + box.width - 1, startY);
 185 
 186         } else { // VERTICAL
 187             // Draw light line lengthwise across the progress bar.
 188             g2.setColor(MetalLookAndFeel.getControlShadow());
 189             g2.drawLine(startX, startY, startX, endY);
 190             g2.drawLine(startX, startY, endX, startY);
 191 
 192             // Draw darker lengthwise line over filled area.
 193             g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 194             g2.drawLine(startX, box.y, startX, box.y + box.height - 1);
 195         }
 196     }
 197 }