/[classpath]/classpath/javax/swing/plaf/basic/BasicProgressBarUI.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/basic/BasicProgressBarUI.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5.2.8 by gnu_andrew, Wed Nov 2 00:43:56 2005 UTC revision 1.5.2.9 by gnu_andrew, Sun Nov 27 21:00:40 2005 UTC
# Line 46  import java.awt.Graphics; Line 46  import java.awt.Graphics;
46  import java.awt.Insets;  import java.awt.Insets;
47  import java.awt.Point;  import java.awt.Point;
48  import java.awt.Rectangle;  import java.awt.Rectangle;
49    import java.awt.Shape;
50  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
51  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
52  import java.awt.font.FontRenderContext;  import java.awt.event.ComponentAdapter;
53  import java.awt.geom.AffineTransform;  import java.awt.event.ComponentEvent;
54  import java.awt.geom.Rectangle2D;  import java.awt.event.ComponentListener;
55  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
56  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
57    
# Line 61  import javax.swing.SwingConstants; Line 62  import javax.swing.SwingConstants;
62  import javax.swing.SwingUtilities;  import javax.swing.SwingUtilities;
63  import javax.swing.Timer;  import javax.swing.Timer;
64  import javax.swing.UIManager;  import javax.swing.UIManager;
65    import javax.swing.event.AncestorEvent;
66    import javax.swing.event.AncestorListener;
67  import javax.swing.event.ChangeEvent;  import javax.swing.event.ChangeEvent;
68  import javax.swing.event.ChangeListener;  import javax.swing.event.ChangeListener;
69  import javax.swing.plaf.ComponentUI;  import javax.swing.plaf.ComponentUI;
# Line 110  public class BasicProgressBarUI extends Line 113  public class BasicProgressBarUI extends
113      {      {
114        // Only need to listen for indeterminate changes.        // Only need to listen for indeterminate changes.
115        // All other things are done on a repaint.        // All other things are done on a repaint.
116        if (e.getPropertyName().equals("inderterminate"))        if (e.getPropertyName().equals("indeterminate"))
117          if (((Boolean) e.getNewValue()).booleanValue())          if (((Boolean) e.getNewValue()).booleanValue()
118            startAnimationTimer();              && progressBar.isShowing())
119          else            startAnimationTimer();
120            stopAnimationTimer();          else
121        else            stopAnimationTimer();
122          progressBar.repaint();      }
123      }
124    
125      /**
126       * Receives notification when the progressbar is becoming visible or
127       * invisible and starts/stops the animation timer accordingly.
128       *
129       * @author Roman Kennke (kennke@aicas.com)
130       */
131      private class AncestorHandler implements AncestorListener
132      {
133    
134        /**
135         * Receives notification when the progressbar is becoming visible. This
136         * starts the animation timer if the progressbar is indeterminate.
137         *
138         * @param event the ancestor event
139         */
140        public void ancestorAdded(AncestorEvent event)
141        {
142          if (progressBar.isIndeterminate())
143            startAnimationTimer();
144        }
145    
146        /**
147         * Receives notification when the progressbar is becoming invisible. This
148         * stops the animation timer if the progressbar is indeterminate.
149         *
150         * @param event the ancestor event
151         */
152        public void ancestorRemoved(AncestorEvent event)
153        {
154          stopAnimationTimer();
155        }
156    
157        /**
158         * Receives notification when an ancestor has been moved. We don't need to
159         * do anything here.
160         */
161        public void ancestorMoved(AncestorEvent event)
162        {
163          // Nothing to do here.
164      }      }
165        
166    }    }
167    
168    /**    /**
# Line 141  public class BasicProgressBarUI extends Line 186  public class BasicProgressBarUI extends
186      }      }
187    }    }
188    
189      /**
190       * Receives notification when the size of the progress bar changes and
191       * invalidates the layout information for the box calculation in
192       * {@link BasicProgressBarUI#getBox(Rectangle)}.
193       *
194       * @author Roman Kennke (kennke@aicas.com)
195       */
196      private class ComponentHandler extends ComponentAdapter
197      {
198        /**
199         * Receives notification when the size of the progress bar changes and
200         * invalidates the layout information for the box calculation in
201         * {@link BasicProgressBarUI#getBox}.
202         *
203         * @param e the component event
204         */
205        public void componentResized(ComponentEvent e)
206        {
207          boxDependent = -1;
208          boxIndependent = -1;
209          incr = -1;
210        }
211      }
212    
213      /**
214       * Holds the value of the bouncing box that is returned by {@link #getBox}.
215       *
216       * @since 1.5
217       */
218      protected Rectangle boxRect;
219    
220    /** The timer used to move the bouncing box. */    /** The timer used to move the bouncing box. */
221    private transient Timer animationTimer;    private transient Timer animationTimer;
222    
# Line 172  public class BasicProgressBarUI extends Line 248  public class BasicProgressBarUI extends
248    /** The progressBar for this UI. */    /** The progressBar for this UI. */
249    protected JProgressBar progressBar;    protected JProgressBar progressBar;
250    
251    
252      /**
253       * The size of the box returned by {@link #getBox} in the orientation
254       * direction of the progress bar. This is package private to avoid accessor
255       * method.
256       */
257      transient double boxDependent = - 1;
258    
259      /**
260       * The size of the box returned by {@link #getBox} against the orientation
261       * direction of the progress bar. This is package private to avoid accessor
262       * method.
263       */
264      transient int boxIndependent = - 1;
265    
266      /**
267       * The increment for box animation. This is package private to avoid accessor
268       * method.
269       */
270      transient double incr = -1;
271    
272    /** The length of the cell. The cell is the painted part. */    /** The length of the cell. The cell is the painted part. */
273    private transient int cellLength;    private transient int cellLength;
274    
# Line 185  public class BasicProgressBarUI extends Line 282  public class BasicProgressBarUI extends
282    private transient Color selectionForeground;    private transient Color selectionForeground;
283    
284    /**    /**
285       * Listens for notification when the component becomes showing and
286       * starts/stops the animation timer.
287       */
288      private AncestorListener ancestorListener;
289    
290      /**
291       * Listens for resize events on the progress bar and invalidates some
292       * layout info.
293       */
294      private ComponentListener componentListener;
295    
296      /**
297     * Creates a new BasicProgressBarUI object.     * Creates a new BasicProgressBarUI object.
298     */     */
299    public BasicProgressBarUI()    public BasicProgressBarUI()
# Line 248  public class BasicProgressBarUI extends Line 357  public class BasicProgressBarUI extends
357    {    {
358      if (!progressBar.isIndeterminate())      if (!progressBar.isIndeterminate())
359        return null;        return null;
360      //numFrames has to be an even number as defined by spec.      if (r == null)
361      int iterations = numFrames / 2 + 1;        r = new Rectangle();
362    
363      double boxDependent;      Rectangle vr = new Rectangle();
364      double boxIndependent;      SwingUtilities.calculateInnerArea(progressBar, vr);
365    
366      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)      // Recalculate the metrics only when size of the progressbar has changed.
367        {      if (incr == -1 || boxDependent == -1 || boxIndependent == -1)
         Dimension dims = getPreferredInnerHorizontal();  
         boxDependent = (double) dims.width / iterations;  
         boxIndependent = dims.height;  
       }  
     else  
368        {        {
369          Dimension dims = getPreferredInnerVertical();          //numFrames has to be an even number as defined by spec.
370          boxDependent = (double) dims.height / iterations;          int iterations = numFrames / 2;
371          boxIndependent = dims.width;          if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
372              {
373                boxDependent = vr.width / 6.;
374                incr = ((double) (vr.width - boxDependent)) / (double) iterations;
375                boxIndependent = vr.height;
376              }
377            else
378              {
379                boxDependent = vr.height / 6.;
380                incr = ((double) (vr.height - boxDependent)) / (double) iterations;
381                boxIndependent = vr.width;
382              }
383        }        }
384    
     Rectangle vr = new Rectangle();  
     SwingUtilities.calculateInnerArea(progressBar, vr);  
   
385      int index = getAnimationIndex();      int index = getAnimationIndex();
386      if (animationIndex > (numFrames + 1) / 2)      if (animationIndex > (numFrames) / 2)
387        index = numFrames - getAnimationIndex();        index = numFrames - getAnimationIndex();
388    
389      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
390        {        {
391          r.x = vr.x + (int) (index * boxDependent);          r.x = vr.x + (int) (incr * index);
392          r.y = vr.y;          r.y = vr.y;
393          r.width = (int) boxDependent;          r.width = (int) boxDependent;
394          r.height = (int) boxIndependent;          r.height = (int) boxIndependent;
395        }        }
396      else      else
397        {        {
398          index++;          r.x = vr.x;
399          r.x = vr.x;          r.y = vr.height - (int) (incr * index) + vr.y - (int) boxDependent;
400          r.y = vr.height - (int) (index * boxDependent) + vr.y;          r.width = (int) boxIndependent;
401          r.width = (int) boxIndependent;          r.height = (int) boxDependent;
         r.height = (int) boxDependent;  
402        }        }
   
403      return r;      return r;
404    }    }
405    
# Line 324  public class BasicProgressBarUI extends Line 434  public class BasicProgressBarUI extends
434     */     */
435    public Dimension getMaximumSize(JComponent c)    public Dimension getMaximumSize(JComponent c)
436    {    {
437      return getPreferredSize(c);      Insets insets = c.getInsets();
438        Dimension ret;
439        int orientation = progressBar.getOrientation();
440        if (orientation == JProgressBar.VERTICAL)
441          {
442            ret = getPreferredInnerVertical();
443            ret.height = Short.MAX_VALUE;
444            ret.width += insets.left + insets.right;
445          }
446        else
447          {
448            ret = getPreferredInnerHorizontal();
449            ret.width = Short.MAX_VALUE;
450            ret.height += insets.top + insets.bottom;
451          }
452        return ret;
453    }    }
454    
455    /**    /**
# Line 338  public class BasicProgressBarUI extends Line 463  public class BasicProgressBarUI extends
463     */     */
464    public Dimension getMinimumSize(JComponent c)    public Dimension getMinimumSize(JComponent c)
465    {    {
466      return getPreferredSize(c);      Insets insets = c.getInsets();
467        Dimension ret;
468        int orientation = progressBar.getOrientation();
469        if (orientation == JProgressBar.VERTICAL)
470          {
471            ret = getPreferredInnerVertical();
472            ret.height = 10;
473            ret.width += insets.left + insets.right;
474          }
475        else
476          {
477            ret = getPreferredInnerHorizontal();
478            ret.width = 10;
479            ret.height += insets.top + insets.bottom;
480          }
481        return ret;
482    }    }
483    
484    /**    /**
# Line 351  public class BasicProgressBarUI extends Line 491  public class BasicProgressBarUI extends
491     */     */
492    protected Dimension getPreferredInnerHorizontal()    protected Dimension getPreferredInnerHorizontal()
493    {    {
494      Rectangle vr = new Rectangle();      Font font = progressBar.getFont();
495        FontMetrics fm = progressBar.getFontMetrics(font);
496    
497      SwingUtilities.calculateInnerArea(progressBar, vr);      int stringWidth = 0;
498        String str = progressBar.getString();
499        if (str != null)
500          stringWidth = fm.stringWidth(progressBar.getString());
501        Insets i = progressBar.getInsets();
502        int prefWidth = Math.max(200 - i.left - i.right, stringWidth);
503    
504        int stringHeight = 0;
505        if (str != null)
506          stringHeight = fm.getHeight();
507        int prefHeight = Math.max(16 - i.top - i.bottom, stringHeight);
508    
509      return new Dimension(vr.width, vr.height);      return new Dimension(prefWidth, prefHeight);
510    }    }
511    
512    /**    /**
# Line 368  public class BasicProgressBarUI extends Line 519  public class BasicProgressBarUI extends
519     */     */
520    protected Dimension getPreferredInnerVertical()    protected Dimension getPreferredInnerVertical()
521    {    {
522      Rectangle vr = new Rectangle();      Font font = progressBar.getFont();
523        FontMetrics fm = progressBar.getFontMetrics(font);
524    
525      SwingUtilities.calculateInnerArea(progressBar, vr);      int stringWidth = 0;
526        String str = progressBar.getString();
527        if (str != null)
528          stringWidth = fm.stringWidth(progressBar.getString());
529        Insets i = progressBar.getInsets();
530        int prefHeight = Math.max(200 - i.left - i.right, stringWidth);
531    
532        int stringHeight = 0;
533        if (str != null)
534          stringHeight = fm.getHeight();
535        int prefWidth = Math.max(16 - i.top - i.bottom, stringHeight);
536    
537      return new Dimension(vr.width, vr.height);      return new Dimension(prefWidth, prefHeight);
538    }    }
539    
540    /**    /**
# Line 386  public class BasicProgressBarUI extends Line 548  public class BasicProgressBarUI extends
548     */     */
549    public Dimension getPreferredSize(JComponent c)    public Dimension getPreferredSize(JComponent c)
550    {    {
     // The only thing we need to worry about is  
     // the text size.  
551      Insets insets = c.getInsets();      Insets insets = c.getInsets();
552        Dimension ret;
553      // make a fontrenderer context so that we can make assumptions about      int orientation = progressBar.getOrientation();
554      // the string bounds      if (orientation == JProgressBar.VERTICAL)
555      FontRenderContext ctx = new FontRenderContext(new AffineTransform(),        ret = getPreferredInnerVertical();
                                                   false, false);  
     Rectangle2D bounds = c.getFont().getStringBounds(progressBar.getString(),  
                                                      ctx);  
     int textW = (int) bounds.getWidth();  
     int textH = (int) bounds.getHeight();  
   
     if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)  
       {  
         if (textH < 20)  
           textH = 20;  
         if (textW < 200)  
           textW = 200;  
       }  
556      else      else
557        {        ret = getPreferredInnerHorizontal();
558          if (textH < 200)      ret.width += insets.left + insets.right;
559            textH = 200;      ret.height += insets.top + insets.bottom;
560          if (textW < 20)      return ret;
           textW = 20;  
       }  
     textW += insets.left + insets.right;  
     textH += insets.top + insets.bottom;  
     return new Dimension(textW, textH);  
561    }    }
562    
563    /**    /**
# Line 514  public class BasicProgressBarUI extends Line 656  public class BasicProgressBarUI extends
656      int min = progressBar.getMinimum();      int min = progressBar.getMinimum();
657      int value = progressBar.getValue();      int value = progressBar.getValue();
658    
659      Rectangle vr = new Rectangle();      Rectangle vr = SwingUtilities.calculateInnerArea(c, new Rectangle());
660      SwingUtilities.calculateInnerArea(c, vr);      Rectangle or = progressBar.getBounds();
   
     Rectangle or = c.getBounds();  
   
661      Insets insets = c.getInsets();      Insets insets = c.getInsets();
662    
663      int amountFull = getAmountFull(insets, or.width, or.height);      int amountFull = getAmountFull(insets, or.width, or.height);
664    
     g.setColor(c.getBackground());  
     g.fill3DRect(vr.x, vr.y, vr.width, vr.height, false);  
   
     if (max != min && len != 0 && value > min)  
       {  
         int iterations = value / (space + len);  
   
665          if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)          if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
666            {            {
             double spaceInUnits = space * (double) vr.width / (max - min);  
             double lenInUnits = len * (double) vr.width / (max - min);  
             double currX = vr.x;  
   
667              g.setColor(c.getForeground());              g.setColor(c.getForeground());
668              g.fill3DRect(vr.x, vr.y, amountFull, vr.height, true);              g.fillRect(vr.x, vr.y, amountFull, vr.height);
   
             g.setColor(c.getBackground());  
             if (spaceInUnits != 0)  
               {  
                 for (int i = 0; i < iterations; i++)  
                   {  
                     currX += lenInUnits;  
                     g.fill3DRect((int) currX, vr.y, (int) spaceInUnits,  
                                  vr.height, true);  
                     currX += spaceInUnits;  
                   }  
               }  
669            }            }
670          else          else
671            {            {
             double currY = vr.y;  
             double spaceInUnits = space * (double) vr.height / (max - min);  
             double lenInUnits = len * (double) vr.height / (max - min);  
   
672              g.setColor(c.getForeground());              g.setColor(c.getForeground());
673              g.fill3DRect(vr.x, vr.y + vr.height - amountFull, vr.width,              g.fillRect(vr.x, vr.y + vr.height - amountFull, vr.width, amountFull);
                          amountFull, true);  
   
             g.setColor(c.getBackground());  
   
             if (spaceInUnits != 0)  
               {  
                 for (int i = 0; i < iterations; i++)  
                   {  
                     currY -= lenInUnits + spaceInUnits;  
                     g.fill3DRect(vr.x, (int) currY, vr.width,  
                                  (int) spaceInUnits, true);  
                   }  
               }  
674            }            }
       }  
675    
676      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))
677        paintString(g, 0, 0, or.width, or.height, amountFull, insets);        paintString(g, 0, 0, or.width, or.height, amountFull, insets);
# Line 599  public class BasicProgressBarUI extends Line 697  public class BasicProgressBarUI extends
697      SwingUtilities.calculateInnerArea(c, vr);      SwingUtilities.calculateInnerArea(c, vr);
698    
699      g.setColor(c.getBackground());      g.setColor(c.getBackground());
700      g.fill3DRect(vr.x, vr.y, vr.width, vr.height, false);      g.fillRect(vr.x, vr.y, vr.width, vr.height);
701    
702      Rectangle box = new Rectangle();      boxRect = getBox(boxRect);
     getBox(box);  
703    
704      g.setColor(c.getForeground());      g.setColor(c.getForeground());
705      g.fill3DRect(box.x, box.y, box.width, box.height, true);      g.fillRect(boxRect.x, boxRect.y, boxRect.width, boxRect.height);
706    
707      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))
708        paintString(g, 0, 0, or.width, or.height,        paintString(g, 0, 0, or.width, or.height,
# Line 628  public class BasicProgressBarUI extends Line 725  public class BasicProgressBarUI extends
725    protected void paintString(Graphics g, int x, int y, int width, int height,    protected void paintString(Graphics g, int x, int y, int width, int height,
726                               int amountFull, Insets b)                               int amountFull, Insets b)
727    {    {
728        // FIXME: We do not support vertical text painting because Java2D is needed
729        // for this.
730        if (progressBar.getOrientation() == JProgressBar.VERTICAL)
731          return;
732    
733      // We want to place in the exact center of the bar.      // We want to place in the exact center of the bar.
734      Point placement = getStringPlacement(g, progressBar.getString(),      Point placement = getStringPlacement(g, progressBar.getString(),
735                                           x + b.left, y + b.top,                                           x + b.left, y + b.top,
736                                           width - b.left - b.right,                                           width - b.left - b.right,
737                                           height - b.top - b.bottom);                                           height - b.top - b.bottom);
     Color saved = g.getColor();  
   
     // FIXME: The Color of the text should use selectionForeground and selectionBackground  
     // but that can't be done right now, so we'll use white in the mean time.  
     g.setColor(Color.WHITE);  
738    
739        Color savedColor = g.getColor();
740        Shape savedClip = g.getClip();
741      FontMetrics fm = g.getFontMetrics(progressBar.getFont());      FontMetrics fm = g.getFontMetrics(progressBar.getFont());
742        int full = getAmountFull(b, width, height);
743        String str = progressBar.getString();
744    
745      g.drawString(progressBar.getString(), placement.x,      // We draw this string two times with different clips so that the text
746                   placement.y + fm.getAscent());      // over the filled area is painted with selectionForeground and over
747        // the clear area with selectionBackground.
748      g.setColor(saved);      g.setColor(getSelectionForeground());
749        g.setClip(0, 0, full + b.left, height);
750        g.drawString(str, placement.x, placement.y + fm.getAscent());
751        g.setColor(getSelectionBackground());
752        g.setClip(full + b.left, 0, width - full, height);
753        g.drawString(str, placement.x, placement.y + fm.getAscent());
754        g.setClip(savedClip);
755        g.setColor(savedColor);
756    }    }
757    
758    /**    /**
# Line 766  public class BasicProgressBarUI extends Line 874  public class BasicProgressBarUI extends
874      progressBar.addChangeListener(changeListener);      progressBar.addChangeListener(changeListener);
875      progressBar.addPropertyChangeListener(propertyListener);      progressBar.addPropertyChangeListener(propertyListener);
876      animationTimer.addActionListener(animation);      animationTimer.addActionListener(animation);
877    
878        ancestorListener = new AncestorHandler();
879        progressBar.addAncestorListener(ancestorListener);
880    
881        componentListener = new ComponentHandler();
882        progressBar.addComponentListener(componentListener);
883    }    }
884    
885    /**    /**
# Line 781  public class BasicProgressBarUI extends Line 895  public class BasicProgressBarUI extends
895      changeListener = null;      changeListener = null;
896      propertyListener = null;      propertyListener = null;
897      animation = null;      animation = null;
898    
899        if (ancestorListener != null)
900          progressBar.removeAncestorListener(ancestorListener);
901        ancestorListener = null;
902    
903        if (componentListener != null)
904          progressBar.removeComponentListener(componentListener);
905        componentListener = null;
906    }    }
907    
908    /**    /**
# Line 804  public class BasicProgressBarUI extends Line 926  public class BasicProgressBarUI extends
926          installDefaults();          installDefaults();
927          installListeners();          installListeners();
928        }        }
929        if (progressBar.isIndeterminate())
930          startAnimationTimer();
931    }    }
932    
933    /**    /**
# Line 822  public class BasicProgressBarUI extends Line 946  public class BasicProgressBarUI extends
946      animationTimer = null;      animationTimer = null;
947      progressBar = null;      progressBar = null;
948    }    }
949    
950  }  }

Legend:
Removed from v.1.5.2.8  
changed lines
  Added in v.1.5.2.9

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26