1 /*
   2  * Copyright (c) 1998, 2006, 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&trade;
  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 public class MetalProgressBarUI extends BasicProgressBarUI {
  48 
  49     private Rectangle innards;
  50     private Rectangle box;
  51 
  52     public static ComponentUI createUI(JComponent c) {
  53         return new MetalProgressBarUI();
  54     }
  55 
  56     /**
  57      * Draws a bit of special highlighting on the progress bar.
  58      * The core painting is deferred to the BasicProgressBar's
  59      * <code>paintDeterminate</code> method.
  60      * @since 1.4
  61      */
  62     public void paintDeterminate(Graphics g, JComponent c) {
  63         super.paintDeterminate(g,c);
  64 
  65         if (!(g instanceof Graphics2D)) {
  66             return;
  67         }
  68 
  69         if (progressBar.isBorderPainted()) {
  70             Insets b = progressBar.getInsets(); // area for border
  71             int barRectWidth = progressBar.getWidth() - (b.left + b.right);
  72             int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
  73             int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
  74             boolean isLeftToRight = MetalUtils.isLeftToRight(c);
  75             int startX, startY, endX, endY;
  76 
  77             // The progress bar border is painted according to a light source.
  78             // This light source is stationary and does not change when the
  79             // component orientation changes.
  80             startX = b.left;
  81             startY = b.top;
  82             endX = b.left + barRectWidth - 1;
  83             endY = b.top + barRectHeight - 1;
  84 
  85             Graphics2D g2 = (Graphics2D)g;
  86             g2.setStroke(new BasicStroke(1.f));
  87 
  88             if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  89                 // Draw light line lengthwise across the progress bar.
  90                 g2.setColor(MetalLookAndFeel.getControlShadow());
  91                 g2.drawLine(startX, startY, endX, startY);
  92 
  93                 if (amountFull > 0) {
  94                     // Draw darker lengthwise line over filled area.
  95                     g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  96 
  97                     if (isLeftToRight) {
  98                         g2.drawLine(startX, startY,
  99                                 startX + amountFull - 1, startY);
 100                     } else {
 101                         g2.drawLine(endX, startY,
 102                                 endX - amountFull + 1, startY);
 103                         if (progressBar.getPercentComplete() != 1.f) {
 104                             g2.setColor(MetalLookAndFeel.getControlShadow());
 105                         }
 106                     }
 107                 }
 108                 // Draw a line across the width.  The color is determined by
 109                 // the code above.
 110                 g2.drawLine(startX, startY, startX, endY);
 111 
 112             } else { // VERTICAL
 113                 // Draw light line lengthwise across the progress bar.
 114                 g2.setColor(MetalLookAndFeel.getControlShadow());
 115                 g2.drawLine(startX, startY, startX, endY);
 116 
 117                 if (amountFull > 0) {
 118                     // Draw darker lengthwise line over filled area.
 119                     g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 120                     g2.drawLine(startX, endY,
 121                             startX, endY - amountFull + 1);
 122                 }
 123                 // Draw a line across the width.  The color is determined by
 124                 // the code above.
 125                 g2.setColor(MetalLookAndFeel.getControlShadow());
 126 
 127                 if (progressBar.getPercentComplete() == 1.f) {
 128                     g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 129                 }
 130                 g2.drawLine(startX, startY, endX, startY);
 131             }
 132         }
 133     }
 134 
 135     /**
 136      * Draws a bit of special highlighting on the progress bar
 137      * and bouncing box.
 138      * The core painting is deferred to the BasicProgressBar's
 139      * <code>paintIndeterminate</code> method.
 140      * @since 1.4
 141      */
 142     public void paintIndeterminate(Graphics g, JComponent c) {
 143         super.paintIndeterminate(g, c);
 144 
 145         if (!progressBar.isBorderPainted() || (!(g instanceof Graphics2D))) {
 146             return;
 147         }
 148 
 149         Insets b = progressBar.getInsets(); // area for border
 150         int barRectWidth = progressBar.getWidth() - (b.left + b.right);
 151         int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
 152         int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
 153         boolean isLeftToRight = MetalUtils.isLeftToRight(c);
 154         int startX, startY, endX, endY;
 155         Rectangle box = null;
 156         box = getBox(box);
 157 
 158         // The progress bar border is painted according to a light source.
 159         // This light source is stationary and does not change when the
 160         // component orientation changes.
 161         startX = b.left;
 162         startY = b.top;
 163         endX = b.left + barRectWidth - 1;
 164         endY = b.top + barRectHeight - 1;
 165 
 166         Graphics2D g2 = (Graphics2D)g;
 167         g2.setStroke(new BasicStroke(1.f));
 168 
 169         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 170             // Draw light line lengthwise across the progress bar.
 171             g2.setColor(MetalLookAndFeel.getControlShadow());
 172             g2.drawLine(startX, startY, endX, startY);
 173             g2.drawLine(startX, startY, startX, endY);
 174 
 175             // Draw darker lengthwise line over filled area.
 176             g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 177             g2.drawLine(box.x, startY, box.x + box.width - 1, startY);
 178 
 179         } else { // VERTICAL
 180             // Draw light line lengthwise across the progress bar.
 181             g2.setColor(MetalLookAndFeel.getControlShadow());
 182             g2.drawLine(startX, startY, startX, endY);
 183             g2.drawLine(startX, startY, endX, startY);
 184 
 185             // Draw darker lengthwise line over filled area.
 186             g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
 187             g2.drawLine(startX, box.y, startX, box.y + box.height - 1);
 188         }
 189     }
 190 }