< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicProgressBarUI.java

Print this page




 116      * The rectangle to be updated the next time the
 117      * animation thread calls repaint.  For bouncing-box
 118      * animation this rect should include the union of
 119      * the currently displayed box (which needs to be erased)
 120      * and the box to be displayed next.
 121      * This rectangle's values are set in
 122      * the setAnimationIndex method.
 123      */
 124     private Rectangle nextPaintRect;
 125 
 126     //cache
 127     /** The component's painting area, not including the border. */
 128     private Rectangle componentInnards;    //the current painting area
 129     private Rectangle oldComponentInnards; //used to see if the size changed
 130 
 131     /** For bouncing-box animation, the change in position per frame. */
 132     private double delta = 0.0;
 133 
 134     private int maxPosition = 0; //maximum X (horiz) or Y box location
 135 


 136     /**
 137      * Returns a new instance of {@code BasicProgressBarUI}.
 138      *
 139      * @param x a component
 140      * @return a new instance of {@code BasicProgressBarUI}
 141      */
 142     public static ComponentUI createUI(JComponent x) {
 143         return new BasicProgressBarUI();
 144     }
 145 
 146     public void installUI(JComponent c) {
 147         progressBar = (JProgressBar)c;
 148         installDefaults();
 149         installListeners();
 150         if (progressBar.isIndeterminate()) {
 151             initIndeterminateValues();
 152         }
 153     }
 154 
 155     public void uninstallUI(JComponent c) {


 159         uninstallDefaults();
 160         uninstallListeners();
 161         progressBar = null;
 162     }
 163 
 164     /**
 165      * Installs default properties.
 166      */
 167     protected void installDefaults() {
 168         LookAndFeel.installProperty(progressBar, "opaque", Boolean.TRUE);
 169         LookAndFeel.installBorder(progressBar,"ProgressBar.border");
 170         LookAndFeel.installColorsAndFont(progressBar,
 171                                          "ProgressBar.background",
 172                                          "ProgressBar.foreground",
 173                                          "ProgressBar.font");
 174         cellLength = UIManager.getInt("ProgressBar.cellLength");
 175         if (cellLength == 0) cellLength = 1;
 176         cellSpacing = UIManager.getInt("ProgressBar.cellSpacing");
 177         selectionForeground = UIManager.getColor("ProgressBar.selectionForeground");
 178         selectionBackground = UIManager.getColor("ProgressBar.selectionBackground");

 179     }
 180 
 181     /**
 182      * Unintalls default properties.
 183      */
 184     protected void uninstallDefaults() {
 185         LookAndFeel.uninstallBorder(progressBar);




 186     }
 187 
 188     /**
 189      * Registers listeners.
 190      */
 191     protected void installListeners() {
 192         //Listen for changes in the progress bar's data.
 193         changeListener = getHandler();
 194         progressBar.addChangeListener(changeListener);
 195 
 196         //Listen for changes between determinate and indeterminate state.
 197         progressBar.addPropertyChangeListener(getHandler());
 198     }
 199 
 200     private Handler getHandler() {
 201         if (handler == null) {
 202             handler = new Handler();
 203         }
 204         return handler;
 205     }


 810      *        of the filled portion of the progress bar.
 811      * @param amountFull size of the fill region, either width or height
 812      *        depending upon orientation.
 813      * @param b Insets of the progress bar.
 814      */
 815     private void paintString(Graphics g, int x, int y, int width, int height,
 816                              int fillStart, int amountFull, Insets b) {
 817         if (!(g instanceof Graphics2D)) {
 818             return;
 819         }
 820 
 821         Graphics2D g2 = (Graphics2D)g;
 822         String progressString = progressBar.getString();
 823         g2.setFont(progressBar.getFont());
 824         Point renderLocation = getStringPlacement(g2, progressString,
 825                                                   x, y, width, height);
 826         Rectangle oldClip = g2.getClipBounds();
 827 
 828         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 829             g2.setColor(getSelectionBackground());
 830             SwingUtilities2.drawString(progressBar, g2, progressString,
 831                                        renderLocation.x, renderLocation.y);
 832             g2.setColor(getSelectionForeground());
 833             g2.clipRect(fillStart, y, amountFull, height);
 834             SwingUtilities2.drawString(progressBar, g2, progressString,
 835                                        renderLocation.x, renderLocation.y);
 836         } else { // VERTICAL
 837             g2.setColor(getSelectionBackground());
 838             AffineTransform rotate =
 839                     AffineTransform.getRotateInstance(Math.PI/2);
 840             g2.setFont(progressBar.getFont().deriveFont(rotate));
 841             renderLocation = getStringPlacement(g2, progressString,
 842                                                   x, y, width, height);
 843             SwingUtilities2.drawString(progressBar, g2, progressString,
 844                                        renderLocation.x, renderLocation.y);
 845             g2.setColor(getSelectionForeground());
 846             g2.clipRect(x, fillStart, width, amountFull);
 847             SwingUtilities2.drawString(progressBar, g2, progressString,
 848                                        renderLocation.x, renderLocation.y);
 849         }
 850         g2.setClip(oldClip);
 851     }
 852 
 853 
 854     /**
 855      * Designate the place where the progress string will be painted.
 856      * This implementation places it at the center of the progress
 857      * bar (in both x and y). Override this if you want to right,
 858      * left, top, or bottom align the progress string or if you need
 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
 868      */
 869     protected Point getStringPlacement(Graphics g, String progressString,
 870                                        int x,int y,int width,int height) {
 871         FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g,
 872                                             progressBar.getFont());
 873         int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer,
 874                                                       progressString);
 875 
 876         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 877             return new Point(x + Math.round(width/2 - stringWidth/2),
 878                              y + ((height +
 879                                  fontSizer.getAscent() -
 880                                  fontSizer.getLeading() -
 881                                  fontSizer.getDescent()) / 2));
 882         } else { // VERTICAL
 883             return new Point(x + ((width - fontSizer.getAscent() +
 884                     fontSizer.getLeading() + fontSizer.getDescent()) / 2),
 885                     y + Math.round(height/2 - stringWidth/2));
 886         }
 887     }
 888 
 889 
 890     public Dimension getPreferredSize(JComponent c) {
 891         Dimension       size;
 892         Insets          border = progressBar.getInsets();
 893         FontMetrics     fontSizer = progressBar.getFontMetrics(
 894                                                   progressBar.getFont());
 895 
 896         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 897             size = new Dimension(getPreferredInnerHorizontal());
 898             // Ensure that the progress string will fit
 899             if (progressBar.isStringPainted()) {
 900                 // I'm doing this for completeness.
 901                 String progString = progressBar.getString();
 902                 int stringWidth = SwingUtilities2.stringWidth(
 903                           progressBar, fontSizer, progString);
 904                 if (stringWidth > size.width) {
 905                     size.width = stringWidth;
 906                 }
 907                 // This uses both Height and Descent to be sure that
 908                 // there is more than enough room in the progress bar
 909                 // for everything.
 910                 // This does have a strange dependency on
 911                 // getStringPlacememnt() in a funny way.
 912                 int stringHeight = fontSizer.getHeight() +
 913                                    fontSizer.getDescent();
 914                 if (stringHeight > size.height) {
 915                     size.height = stringHeight;
 916                 }
 917             }
 918         } else {
 919             size = new Dimension(getPreferredInnerVertical());
 920             // Ensure that the progress string will fit.
 921             if (progressBar.isStringPainted()) {
 922                 String progString = progressBar.getString();
 923                 int stringHeight = fontSizer.getHeight() +
 924                         fontSizer.getDescent();
 925                 if (stringHeight > size.width) {
 926                     size.width = stringHeight;
 927                 }
 928                 // This is also for completeness.
 929                 int stringWidth = SwingUtilities2.stringWidth(
 930                                        progressBar, fontSizer, progString);
 931                 if (stringWidth > size.height) {
 932                     size.height = stringWidth;
 933                 }
 934             }
 935         }
 936 
 937         size.width += border.left + border.right;
 938         size.height += border.top + border.bottom;
 939         return size;
 940     }
 941 
 942     /**
 943      * The Minimum size for this component is 10. The rationale here
 944      * is that there should be at least one pixel per 10 percent.
 945      */
 946     public Dimension getMinimumSize(JComponent c) {
 947         Dimension pref = getPreferredSize(progressBar);
 948         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 949             pref.width = 10;




 116      * The rectangle to be updated the next time the
 117      * animation thread calls repaint.  For bouncing-box
 118      * animation this rect should include the union of
 119      * the currently displayed box (which needs to be erased)
 120      * and the box to be displayed next.
 121      * This rectangle's values are set in
 122      * the setAnimationIndex method.
 123      */
 124     private Rectangle nextPaintRect;
 125 
 126     //cache
 127     /** The component's painting area, not including the border. */
 128     private Rectangle componentInnards;    //the current painting area
 129     private Rectangle oldComponentInnards; //used to see if the size changed
 130 
 131     /** For bouncing-box animation, the change in position per frame. */
 132     private double delta = 0.0;
 133 
 134     private int maxPosition = 0; //maximum X (horiz) or Y box location
 135 
 136     private TextUIDrawing textUIDrawing;
 137 
 138     /**
 139      * Returns a new instance of {@code BasicProgressBarUI}.
 140      *
 141      * @param x a component
 142      * @return a new instance of {@code BasicProgressBarUI}
 143      */
 144     public static ComponentUI createUI(JComponent x) {
 145         return new BasicProgressBarUI();
 146     }
 147 
 148     public void installUI(JComponent c) {
 149         progressBar = (JProgressBar)c;
 150         installDefaults();
 151         installListeners();
 152         if (progressBar.isIndeterminate()) {
 153             initIndeterminateValues();
 154         }
 155     }
 156 
 157     public void uninstallUI(JComponent c) {


 161         uninstallDefaults();
 162         uninstallListeners();
 163         progressBar = null;
 164     }
 165 
 166     /**
 167      * Installs default properties.
 168      */
 169     protected void installDefaults() {
 170         LookAndFeel.installProperty(progressBar, "opaque", Boolean.TRUE);
 171         LookAndFeel.installBorder(progressBar,"ProgressBar.border");
 172         LookAndFeel.installColorsAndFont(progressBar,
 173                                          "ProgressBar.background",
 174                                          "ProgressBar.foreground",
 175                                          "ProgressBar.font");
 176         cellLength = UIManager.getInt("ProgressBar.cellLength");
 177         if (cellLength == 0) cellLength = 1;
 178         cellSpacing = UIManager.getInt("ProgressBar.cellSpacing");
 179         selectionForeground = UIManager.getColor("ProgressBar.selectionForeground");
 180         selectionBackground = UIManager.getColor("ProgressBar.selectionBackground");
 181         textUIDrawing = SwingUtilities2.getTextUIDrawing(textUIDrawing);
 182     }
 183 
 184     /**
 185      * Unintalls default properties.
 186      */
 187     protected void uninstallDefaults() {
 188         LookAndFeel.uninstallBorder(progressBar);
 189         if (textUIDrawing != SwingUtilities2.DEFAULT_UI_TEXT_DRAWING
 190                 && textUIDrawing instanceof UIResource) {
 191             textUIDrawing = SwingUtilities2.DEFAULT_UI_TEXT_DRAWING;
 192         }
 193     }
 194 
 195     /**
 196      * Registers listeners.
 197      */
 198     protected void installListeners() {
 199         //Listen for changes in the progress bar's data.
 200         changeListener = getHandler();
 201         progressBar.addChangeListener(changeListener);
 202 
 203         //Listen for changes between determinate and indeterminate state.
 204         progressBar.addPropertyChangeListener(getHandler());
 205     }
 206 
 207     private Handler getHandler() {
 208         if (handler == null) {
 209             handler = new Handler();
 210         }
 211         return handler;
 212     }


 817      *        of the filled portion of the progress bar.
 818      * @param amountFull size of the fill region, either width or height
 819      *        depending upon orientation.
 820      * @param b Insets of the progress bar.
 821      */
 822     private void paintString(Graphics g, int x, int y, int width, int height,
 823                              int fillStart, int amountFull, Insets b) {
 824         if (!(g instanceof Graphics2D)) {
 825             return;
 826         }
 827 
 828         Graphics2D g2 = (Graphics2D)g;
 829         String progressString = progressBar.getString();
 830         g2.setFont(progressBar.getFont());
 831         Point renderLocation = getStringPlacement(g2, progressString,
 832                                                   x, y, width, height);
 833         Rectangle oldClip = g2.getClipBounds();
 834 
 835         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 836             g2.setColor(getSelectionBackground());
 837             textUIDrawing.drawString(progressBar, g2, progressString,
 838                                        renderLocation.x, renderLocation.y);
 839             g2.setColor(getSelectionForeground());
 840             g2.clipRect(fillStart, y, amountFull, height);
 841             textUIDrawing.drawString(progressBar, g2, progressString,
 842                                        renderLocation.x, renderLocation.y);
 843         } else { // VERTICAL
 844             g2.setColor(getSelectionBackground());
 845             AffineTransform rotate =
 846                     AffineTransform.getRotateInstance(Math.PI/2);
 847             g2.setFont(progressBar.getFont().deriveFont(rotate));
 848             renderLocation = getStringPlacement(g2, progressString,
 849                                                   x, y, width, height);
 850             textUIDrawing.drawString(progressBar, g2, progressString,
 851                                        renderLocation.x, renderLocation.y);
 852             g2.setColor(getSelectionForeground());
 853             g2.clipRect(x, fillStart, width, amountFull);
 854             textUIDrawing.drawString(progressBar, g2, progressString,
 855                                        renderLocation.x, renderLocation.y);
 856         }
 857         g2.setClip(oldClip);
 858     }
 859 
 860 
 861     /**
 862      * Designate the place where the progress string will be painted.
 863      * This implementation places it at the center of the progress
 864      * bar (in both x and y). Override this if you want to right,
 865      * left, top, or bottom align the progress string or if you need
 866      * to nudge it around for any reason.
 867      *
 868      * @param g an instance of {@code Graphics}
 869      * @param progressString a text
 870      * @param x an X coordinate
 871      * @param y an Y coordinate
 872      * @param width a width
 873      * @param height a height
 874      * @return the place where the progress string will be painted
 875      */
 876     protected Point getStringPlacement(Graphics g, String progressString,
 877                                        int x,int y,int width,int height) {
 878         FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g,
 879                                             progressBar.getFont());
 880         int stringWidth = textUIDrawing.getStringWidth(progressBar, fontSizer,
 881                                                       progressString);
 882 
 883         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 884             return new Point(x + Math.round(width/2 - stringWidth/2),
 885                              y + ((height +
 886                                  fontSizer.getAscent() -
 887                                  fontSizer.getLeading() -
 888                                  fontSizer.getDescent()) / 2));
 889         } else { // VERTICAL
 890             return new Point(x + ((width - fontSizer.getAscent() +
 891                     fontSizer.getLeading() + fontSizer.getDescent()) / 2),
 892                     y + Math.round(height/2 - stringWidth/2));
 893         }
 894     }
 895 
 896 
 897     public Dimension getPreferredSize(JComponent c) {
 898         Dimension       size;
 899         Insets          border = progressBar.getInsets();
 900         FontMetrics     fontSizer = progressBar.getFontMetrics(
 901                                                   progressBar.getFont());
 902 
 903         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 904             size = new Dimension(getPreferredInnerHorizontal());
 905             // Ensure that the progress string will fit
 906             if (progressBar.isStringPainted()) {
 907                 // I'm doing this for completeness.
 908                 String progString = progressBar.getString();
 909                 int stringWidth = textUIDrawing.getStringWidth(
 910                           progressBar, fontSizer, progString);
 911                 if (stringWidth > size.width) {
 912                     size.width = stringWidth;
 913                 }
 914                 // This uses both Height and Descent to be sure that
 915                 // there is more than enough room in the progress bar
 916                 // for everything.
 917                 // This does have a strange dependency on
 918                 // getStringPlacememnt() in a funny way.
 919                 int stringHeight = fontSizer.getHeight() +
 920                                    fontSizer.getDescent();
 921                 if (stringHeight > size.height) {
 922                     size.height = stringHeight;
 923                 }
 924             }
 925         } else {
 926             size = new Dimension(getPreferredInnerVertical());
 927             // Ensure that the progress string will fit.
 928             if (progressBar.isStringPainted()) {
 929                 String progString = progressBar.getString();
 930                 int stringHeight = fontSizer.getHeight() +
 931                         fontSizer.getDescent();
 932                 if (stringHeight > size.width) {
 933                     size.width = stringHeight;
 934                 }
 935                 // This is also for completeness.
 936                 int stringWidth = textUIDrawing.getStringWidth(
 937                                        progressBar, fontSizer, progString);
 938                 if (stringWidth > size.height) {
 939                     size.height = stringWidth;
 940                 }
 941             }
 942         }
 943 
 944         size.width += border.left + border.right;
 945         size.height += border.top + border.bottom;
 946         return size;
 947     }
 948 
 949     /**
 950      * The Minimum size for this component is 10. The rationale here
 951      * is that there should be at least one pixel per 10 percent.
 952      */
 953     public Dimension getMinimumSize(JComponent c) {
 954         Dimension pref = getPreferredSize(progressBar);
 955         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 956             pref.width = 10;


< prev index next >