jdk/src/share/classes/javax/swing/plaf/basic/BasicProgressBarUI.java
changeset 25765 88051e6ecdc7
parent 23010 6dadb192ad81
equal deleted inserted replaced
25764:6333e4aa8a70 25765:88051e6ecdc7
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    51     // is for the text over the unfilled progress bar area.
    51     // is for the text over the unfilled progress bar area.
    52     private Color selectionForeground, selectionBackground;
    52     private Color selectionForeground, selectionBackground;
    53 
    53 
    54     private Animator animator;
    54     private Animator animator;
    55 
    55 
       
    56     /**
       
    57      * The instance of {@code JProgressBar}.
       
    58      */
    56     protected JProgressBar progressBar;
    59     protected JProgressBar progressBar;
       
    60     /**
       
    61      * The instance of {@code ChangeListener}.
       
    62      */
    57     protected ChangeListener changeListener;
    63     protected ChangeListener changeListener;
    58     private Handler handler;
    64     private Handler handler;
    59 
    65 
    60     /**
    66     /**
    61      * The current state of the indeterminate animation's cycle.
    67      * The current state of the indeterminate animation's cycle.
   125     /** For bouncing-box animation, the change in position per frame. */
   131     /** For bouncing-box animation, the change in position per frame. */
   126     private double delta = 0.0;
   132     private double delta = 0.0;
   127 
   133 
   128     private int maxPosition = 0; //maximum X (horiz) or Y box location
   134     private int maxPosition = 0; //maximum X (horiz) or Y box location
   129 
   135 
   130 
   136     /**
       
   137      * Returns a new instance of {@code BasicProgressBarUI}.
       
   138      *
       
   139      * @param x a component
       
   140      * @return a new instance of {@code BasicProgressBarUI}
       
   141      */
   131     public static ComponentUI createUI(JComponent x) {
   142     public static ComponentUI createUI(JComponent x) {
   132         return new BasicProgressBarUI();
   143         return new BasicProgressBarUI();
   133     }
   144     }
   134 
   145 
   135     public void installUI(JComponent c) {
   146     public void installUI(JComponent c) {
   148         uninstallDefaults();
   159         uninstallDefaults();
   149         uninstallListeners();
   160         uninstallListeners();
   150         progressBar = null;
   161         progressBar = null;
   151     }
   162     }
   152 
   163 
       
   164     /**
       
   165      * Installs default properties.
       
   166      */
   153     protected void installDefaults() {
   167     protected void installDefaults() {
   154         LookAndFeel.installProperty(progressBar, "opaque", Boolean.TRUE);
   168         LookAndFeel.installProperty(progressBar, "opaque", Boolean.TRUE);
   155         LookAndFeel.installBorder(progressBar,"ProgressBar.border");
   169         LookAndFeel.installBorder(progressBar,"ProgressBar.border");
   156         LookAndFeel.installColorsAndFont(progressBar,
   170         LookAndFeel.installColorsAndFont(progressBar,
   157                                          "ProgressBar.background",
   171                                          "ProgressBar.background",
   162         cellSpacing = UIManager.getInt("ProgressBar.cellSpacing");
   176         cellSpacing = UIManager.getInt("ProgressBar.cellSpacing");
   163         selectionForeground = UIManager.getColor("ProgressBar.selectionForeground");
   177         selectionForeground = UIManager.getColor("ProgressBar.selectionForeground");
   164         selectionBackground = UIManager.getColor("ProgressBar.selectionBackground");
   178         selectionBackground = UIManager.getColor("ProgressBar.selectionBackground");
   165     }
   179     }
   166 
   180 
       
   181     /**
       
   182      * Unintalls default properties.
       
   183      */
   167     protected void uninstallDefaults() {
   184     protected void uninstallDefaults() {
   168         LookAndFeel.uninstallBorder(progressBar);
   185         LookAndFeel.uninstallBorder(progressBar);
   169     }
   186     }
   170 
   187 
       
   188     /**
       
   189      * Registers listeners.
       
   190      */
   171     protected void installListeners() {
   191     protected void installListeners() {
   172         //Listen for changes in the progress bar's data.
   192         //Listen for changes in the progress bar's data.
   173         changeListener = getHandler();
   193         changeListener = getHandler();
   174         progressBar.addChangeListener(changeListener);
   194         progressBar.addChangeListener(changeListener);
   175 
   195 
   289     // protected void installComponents()
   309     // protected void installComponents()
   290     // protected void uninstallComponents()
   310     // protected void uninstallComponents()
   291     // protected void installKeyboardActions()
   311     // protected void installKeyboardActions()
   292     // protected void uninstallKeyboardActions()
   312     // protected void uninstallKeyboardActions()
   293 
   313 
       
   314     /**
       
   315      * Returns preferred size of the horizontal {@code JProgressBar}.
       
   316      *
       
   317      * @return preferred size of the horizontal {@code JProgressBar}
       
   318      */
   294     protected Dimension getPreferredInnerHorizontal() {
   319     protected Dimension getPreferredInnerHorizontal() {
   295         Dimension horizDim = (Dimension)DefaultLookup.get(progressBar, this,
   320         Dimension horizDim = (Dimension)DefaultLookup.get(progressBar, this,
   296             "ProgressBar.horizontalSize");
   321             "ProgressBar.horizontalSize");
   297         if (horizDim == null) {
   322         if (horizDim == null) {
   298             horizDim = new Dimension(146, 12);
   323             horizDim = new Dimension(146, 12);
   299         }
   324         }
   300         return horizDim;
   325         return horizDim;
   301     }
   326     }
   302 
   327 
       
   328     /**
       
   329      * Returns preferred size of the vertical {@code JProgressBar}.
       
   330      *
       
   331      * @return preferred size of the vertical {@code JProgressBar}
       
   332      */
   303     protected Dimension getPreferredInnerVertical() {
   333     protected Dimension getPreferredInnerVertical() {
   304         Dimension vertDim = (Dimension)DefaultLookup.get(progressBar, this,
   334         Dimension vertDim = (Dimension)DefaultLookup.get(progressBar, this,
   305             "ProgressBar.verticalSize");
   335             "ProgressBar.verticalSize");
   306         if (vertDim == null) {
   336         if (vertDim == null) {
   307             vertDim = new Dimension(12, 146);
   337             vertDim = new Dimension(12, 146);
   310     }
   340     }
   311 
   341 
   312     /**
   342     /**
   313      * The "selectionForeground" is the color of the text when it is painted
   343      * The "selectionForeground" is the color of the text when it is painted
   314      * over a filled area of the progress bar.
   344      * over a filled area of the progress bar.
       
   345      *
       
   346      * @return the color of the selected foreground
   315      */
   347      */
   316     protected Color getSelectionForeground() {
   348     protected Color getSelectionForeground() {
   317         return selectionForeground;
   349         return selectionForeground;
   318     }
   350     }
   319 
   351 
   320     /**
   352     /**
   321      * The "selectionBackground" is the color of the text when it is painted
   353      * The "selectionBackground" is the color of the text when it is painted
   322      * over an unfilled area of the progress bar.
   354      * over an unfilled area of the progress bar.
       
   355      *
       
   356      * @return the color of the selected background
   323      */
   357      */
   324     protected Color getSelectionBackground() {
   358     protected Color getSelectionBackground() {
   325         return selectionBackground;
   359         return selectionBackground;
   326     }
   360     }
   327 
   361 
   350         } else {
   384         } else {
   351             return cellLength;
   385             return cellLength;
   352         }
   386         }
   353     }
   387     }
   354 
   388 
       
   389     /**
       
   390      * Sets the cell length.
       
   391      *
       
   392      * @param cellLen a new cell length
       
   393      */
   355     protected void setCellLength(int cellLen) {
   394     protected void setCellLength(int cellLen) {
   356         this.cellLength = cellLen;
   395         this.cellLength = cellLen;
   357     }
   396     }
   358 
   397 
   359     /**
   398     /**
   372         } else {
   411         } else {
   373             return cellSpacing;
   412             return cellSpacing;
   374         }
   413         }
   375     }
   414     }
   376 
   415 
       
   416     /**
       
   417      * Sets the cell spacing.
       
   418      *
       
   419      * @param cellSpace a new cell spacing
       
   420      */
   377     protected void setCellSpacing(int cellSpace) {
   421     protected void setCellSpacing(int cellSpace) {
   378         this.cellSpacing = cellSpace;
   422         this.cellSpacing = cellSpace;
   379     }
   423     }
   380 
   424 
   381     /**
   425     /**
   382      * This determines the amount of the progress bar that should be filled
   426      * This determines the amount of the progress bar that should be filled
   383      * based on the percent done gathered from the model. This is a common
   427      * based on the percent done gathered from the model. This is a common
   384      * operation so it was abstracted out. It assumes that your progress bar
   428      * operation so it was abstracted out. It assumes that your progress bar
   385      * is linear. That is, if you are making a circular progress indicator,
   429      * is linear. That is, if you are making a circular progress indicator,
   386      * you will want to override this method.
   430      * you will want to override this method.
       
   431      *
       
   432      * @param b insets
       
   433      * @param width a width
       
   434      * @param height a height
       
   435      * @return the amount of the progress bar that should be filled
   387      */
   436      */
   388     protected int getAmountFull(Insets b, int width, int height) {
   437     protected int getAmountFull(Insets b, int width, int height) {
   389         int amountFull = 0;
   438         int amountFull = 0;
   390         BoundedRangeModel model = progressBar.getModel();
   439         BoundedRangeModel model = progressBar.getModel();
   391 
   440 
   575      * All purpose paint method that should do the right thing for all
   624      * All purpose paint method that should do the right thing for all
   576      * linear bouncing-box progress bars.
   625      * linear bouncing-box progress bars.
   577      * Override this if you are making another kind of
   626      * Override this if you are making another kind of
   578      * progress bar.
   627      * progress bar.
   579      *
   628      *
       
   629      * @param g an instance of {@code Graphics}
       
   630      * @param c a component
   580      * @see #paintDeterminate
   631      * @see #paintDeterminate
   581      *
   632      *
   582      * @since 1.4
   633      * @since 1.4
   583      */
   634      */
   584     protected void paintIndeterminate(Graphics g, JComponent c) {
   635     protected void paintIndeterminate(Graphics g, JComponent c) {
   626      * the defaults
   677      * the defaults
   627      * table, things should work just fine to paint your progress bar.
   678      * table, things should work just fine to paint your progress bar.
   628      * Naturally, override this if you are making a circular or
   679      * Naturally, override this if you are making a circular or
   629      * semi-circular progress bar.
   680      * semi-circular progress bar.
   630      *
   681      *
       
   682      * @param g an instance of {@code Graphics}
       
   683      * @param c a component
   631      * @see #paintIndeterminate
   684      * @see #paintIndeterminate
   632      *
   685      *
   633      * @since 1.4
   686      * @since 1.4
   634      */
   687      */
   635     protected void paintDeterminate(Graphics g, JComponent c) {
   688     protected void paintDeterminate(Graphics g, JComponent c) {
   701                         barRectWidth, barRectHeight,
   754                         barRectWidth, barRectHeight,
   702                         amountFull, b);
   755                         amountFull, b);
   703         }
   756         }
   704     }
   757     }
   705 
   758 
   706 
   759     /**
       
   760      * Paints the progress string.
       
   761      *
       
   762      * @param g an instance of {@code Graphics}
       
   763      * @param x X location of bounding box
       
   764      * @param y Y location of bounding box
       
   765      * @param width width of bounding box
       
   766      * @param height height of bounding box
       
   767      * @param amountFull size of the fill region, either width or height
       
   768      *        depending upon orientation.
       
   769      * @param b Insets of the progress bar.
       
   770      */
   707     protected void paintString(Graphics g, int x, int y,
   771     protected void paintString(Graphics g, int x, int y,
   708                                int width, int height,
   772                                int width, int height,
   709                                int amountFull, Insets b) {
   773                                int amountFull, Insets b) {
   710         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
   774         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
   711             if (BasicGraphicsUtils.isLeftToRight(progressBar)) {
   775             if (BasicGraphicsUtils.isLeftToRight(progressBar)) {
   791      * Designate the place where the progress string will be painted.
   855      * Designate the place where the progress string will be painted.
   792      * This implementation places it at the center of the progress
   856      * This implementation places it at the center of the progress
   793      * bar (in both x and y). Override this if you want to right,
   857      * bar (in both x and y). Override this if you want to right,
   794      * left, top, or bottom align the progress string or if you need
   858      * left, top, or bottom align the progress string or if you need
   795      * to nudge it around for any reason.
   859      * to nudge it around for any reason.
       
   860      *
       
   861      * @param g an instance of {@code Graphics}
       
   862      * @param progressString a text
       
   863      * @param x an X coordinate
       
   864      * @param y an Y coordinate
       
   865      * @param width a width
       
   866      * @param height a height
       
   867      * @return the place where the progress string will be painted
   796      */
   868      */
   797     protected Point getStringPlacement(Graphics g, String progressString,
   869     protected Point getStringPlacement(Graphics g, String progressString,
   798                                        int x,int y,int width,int height) {
   870                                        int x,int y,int width,int height) {
   799         FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g,
   871         FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g,
   800                                             progressBar.getFont());
   872                                             progressBar.getFont());
   892     }
   964     }
   893 
   965 
   894     /**
   966     /**
   895      * Gets the index of the current animation frame.
   967      * Gets the index of the current animation frame.
   896      *
   968      *
       
   969      * @return the index of the current animation frame
   897      * @since 1.4
   970      * @since 1.4
   898      */
   971      */
   899     protected int getAnimationIndex() {
   972     protected int getAnimationIndex() {
   900         return animationIndex;
   973         return animationIndex;
   901     }
   974     }