/[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.13 by rabbit78, Wed Oct 12 12:10:00 2005 UTC revision 1.14 by rabbit78, Mon Nov 14 11:07:35 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      protected Rectangle boxRect;
217    
218    /** The timer used to move the bouncing box. */    /** The timer used to move the bouncing box. */
219    private transient Timer animationTimer;    private transient Timer animationTimer;
220    
# Line 172  public class BasicProgressBarUI extends Line 246  public class BasicProgressBarUI extends
246    /** The progressBar for this UI. */    /** The progressBar for this UI. */
247    protected JProgressBar progressBar;    protected JProgressBar progressBar;
248    
249    
250      /**
251       * The size of the box returned by {@link #getBox} in the orientation
252       * direction of the progress bar. This is package private to avoid accessor
253       * method.
254       */
255      transient double boxDependent = - 1;
256    
257      /**
258       * The size of the box returned by {@link #getBox} against the orientation
259       * direction of the progress bar. This is package private to avoid accessor
260       * method.
261       */
262      transient int boxIndependent = - 1;
263    
264      /**
265       * The increment for box animation. This is package private to avoid accessor
266       * method.
267       */
268      transient double incr = -1;
269    
270    /** The length of the cell. The cell is the painted part. */    /** The length of the cell. The cell is the painted part. */
271    private transient int cellLength;    private transient int cellLength;
272    
# Line 185  public class BasicProgressBarUI extends Line 280  public class BasicProgressBarUI extends
280    private transient Color selectionForeground;    private transient Color selectionForeground;
281    
282    /**    /**
283       * Listens for notification when the component becomes showing and
284       * starts/stops the animation timer.
285       */
286      private AncestorListener ancestorListener;
287    
288      /**
289       * Listens for resize events on the progress bar and invalidates some
290       * layout info.
291       */
292      private ComponentListener componentListener;
293    
294      /**
295     * Creates a new BasicProgressBarUI object.     * Creates a new BasicProgressBarUI object.
296     */     */
297    public BasicProgressBarUI()    public BasicProgressBarUI()
# Line 248  public class BasicProgressBarUI extends Line 355  public class BasicProgressBarUI extends
355    {    {
356      if (!progressBar.isIndeterminate())      if (!progressBar.isIndeterminate())
357        return null;        return null;
358      //numFrames has to be an even number as defined by spec.      if (r == null)
359      int iterations = numFrames / 2 + 1;        r = new Rectangle();
360    
361      double boxDependent;      Rectangle vr = new Rectangle();
362      double boxIndependent;      SwingUtilities.calculateInnerArea(progressBar, vr);
363    
364      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)      // Recalculate the metrics only when size of the progressbar has changed.
365        {      if (incr == -1 || boxDependent == -1 || boxIndependent == -1)
         Dimension dims = getPreferredInnerHorizontal();  
         boxDependent = (double) dims.width / iterations;  
         boxIndependent = dims.height;  
       }  
     else  
366        {        {
367          Dimension dims = getPreferredInnerVertical();          //numFrames has to be an even number as defined by spec.
368          boxDependent = (double) dims.height / iterations;          int iterations = numFrames / 2;
369          boxIndependent = dims.width;          if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
370              {
371                boxDependent = vr.width / 6.;
372                incr = ((double) (vr.width - boxDependent)) / (double) iterations;
373                boxIndependent = vr.height;
374              }
375            else
376              {
377                boxDependent = vr.height / 6.;
378                incr = ((double) (vr.height - boxDependent)) / (double) iterations;
379                boxIndependent = vr.width;
380              }
381        }        }
382    
     Rectangle vr = new Rectangle();  
     SwingUtilities.calculateInnerArea(progressBar, vr);  
   
383      int index = getAnimationIndex();      int index = getAnimationIndex();
384      if (animationIndex > (numFrames + 1) / 2)      if (animationIndex > (numFrames) / 2)
385        index = numFrames - getAnimationIndex();        index = numFrames - getAnimationIndex();
386    
387      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
388        {        {
389          r.x = vr.x + (int) (index * boxDependent);          r.x = vr.x + (int) (incr * index);
390          r.y = vr.y;          r.y = vr.y;
391          r.width = (int) boxDependent;          r.width = (int) boxDependent;
392          r.height = (int) boxIndependent;          r.height = (int) boxIndependent;
393        }        }
394      else      else
395        {        {
396          index++;          r.x = vr.x;
397          r.x = vr.x;          r.y = vr.height - (int) (incr * index) + vr.y - (int) boxDependent;
398          r.y = vr.height - (int) (index * boxDependent) + vr.y;          r.width = (int) boxIndependent;
399          r.width = (int) boxIndependent;          r.height = (int) boxDependent;
         r.height = (int) boxDependent;  
400        }        }
   
401      return r;      return r;
402    }    }
403    
# Line 324  public class BasicProgressBarUI extends Line 432  public class BasicProgressBarUI extends
432     */     */
433    public Dimension getMaximumSize(JComponent c)    public Dimension getMaximumSize(JComponent c)
434    {    {
435      return getPreferredSize(c);      Insets insets = c.getInsets();
436        Dimension ret;
437        int orientation = progressBar.getOrientation();
438        if (orientation == JProgressBar.VERTICAL)
439          {
440            ret = getPreferredInnerVertical();
441            ret.height = Short.MAX_VALUE;
442            ret.width += insets.left + insets.right;
443          }
444        else
445          {
446            ret = getPreferredInnerHorizontal();
447            ret.width = Short.MAX_VALUE;
448            ret.height += insets.top + insets.bottom;
449          }
450        return ret;
451    }    }
452    
453    /**    /**
# Line 338  public class BasicProgressBarUI extends Line 461  public class BasicProgressBarUI extends
461     */     */
462    public Dimension getMinimumSize(JComponent c)    public Dimension getMinimumSize(JComponent c)
463    {    {
464      return getPreferredSize(c);      Insets insets = c.getInsets();
465        Dimension ret;
466        int orientation = progressBar.getOrientation();
467        if (orientation == JProgressBar.VERTICAL)
468          {
469            ret = getPreferredInnerVertical();
470            ret.height = 10;
471            ret.width += insets.left + insets.right;
472          }
473        else
474          {
475            ret = getPreferredInnerHorizontal();
476            ret.width = 10;
477            ret.height += insets.top + insets.bottom;
478          }
479        return ret;
480    }    }
481    
482    /**    /**
# Line 351  public class BasicProgressBarUI extends Line 489  public class BasicProgressBarUI extends
489     */     */
490    protected Dimension getPreferredInnerHorizontal()    protected Dimension getPreferredInnerHorizontal()
491    {    {
492      Rectangle vr = new Rectangle();      Font font = progressBar.getFont();
493        FontMetrics fm = progressBar.getFontMetrics(font);
494    
495      SwingUtilities.calculateInnerArea(progressBar, vr);      int stringWidth = 0;
496        String str = progressBar.getString();
497        if (str != null)
498          stringWidth = fm.stringWidth(progressBar.getString());
499        Insets i = progressBar.getInsets();
500        int prefWidth = Math.max(200 - i.left - i.right, stringWidth);
501    
502        int stringHeight = 0;
503        if (str != null)
504          stringHeight = fm.getHeight();
505        int prefHeight = Math.max(16 - i.top - i.bottom, stringHeight);
506    
507      return new Dimension(vr.width, vr.height);      return new Dimension(prefWidth, prefHeight);
508    }    }
509    
510    /**    /**
# Line 368  public class BasicProgressBarUI extends Line 517  public class BasicProgressBarUI extends
517     */     */
518    protected Dimension getPreferredInnerVertical()    protected Dimension getPreferredInnerVertical()
519    {    {
520      Rectangle vr = new Rectangle();      Font font = progressBar.getFont();
521        FontMetrics fm = progressBar.getFontMetrics(font);
522    
523      SwingUtilities.calculateInnerArea(progressBar, vr);      int stringWidth = 0;
524        String str = progressBar.getString();
525        if (str != null)
526          stringWidth = fm.stringWidth(progressBar.getString());
527        Insets i = progressBar.getInsets();
528        int prefHeight = Math.max(200 - i.left - i.right, stringWidth);
529    
530        int stringHeight = 0;
531        if (str != null)
532          stringHeight = fm.getHeight();
533        int prefWidth = Math.max(16 - i.top - i.bottom, stringHeight);
534    
535      return new Dimension(vr.width, vr.height);      return new Dimension(prefWidth, prefHeight);
536    }    }
537    
538    /**    /**
# Line 386  public class BasicProgressBarUI extends Line 546  public class BasicProgressBarUI extends
546     */     */
547    public Dimension getPreferredSize(JComponent c)    public Dimension getPreferredSize(JComponent c)
548    {    {
     // The only thing we need to worry about is  
     // the text size.  
549      Insets insets = c.getInsets();      Insets insets = c.getInsets();
550        Dimension ret;
551      // make a fontrenderer context so that we can make assumptions about      int orientation = progressBar.getOrientation();
552      // the string bounds      if (orientation == JProgressBar.VERTICAL)
553      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;  
       }  
554      else      else
555        {        ret = getPreferredInnerHorizontal();
556          if (textH < 200)      ret.width += insets.left + insets.right;
557            textH = 200;      ret.height += insets.top + insets.bottom;
558          if (textW < 20)      return ret;
           textW = 20;  
       }  
     textW += insets.left + insets.right;  
     textH += insets.top + insets.bottom;  
     return new Dimension(textW, textH);  
559    }    }
560    
561    /**    /**
# Line 514  public class BasicProgressBarUI extends Line 654  public class BasicProgressBarUI extends
654      int min = progressBar.getMinimum();      int min = progressBar.getMinimum();
655      int value = progressBar.getValue();      int value = progressBar.getValue();
656    
657      Rectangle vr = new Rectangle();      Rectangle vr = SwingUtilities.calculateInnerArea(c, new Rectangle());
658      SwingUtilities.calculateInnerArea(c, vr);      Rectangle or = progressBar.getBounds();
   
     Rectangle or = c.getBounds();  
   
659      Insets insets = c.getInsets();      Insets insets = c.getInsets();
660    
661      int amountFull = getAmountFull(insets, or.width, or.height);      int amountFull = getAmountFull(insets, or.width, or.height);
662    
     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);  
   
663          if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)          if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
664            {            {
             double spaceInUnits = space * (double) vr.width / (max - min);  
             double lenInUnits = len * (double) vr.width / (max - min);  
             double currX = vr.x;  
   
665              g.setColor(c.getForeground());              g.setColor(c.getForeground());
666              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;  
                   }  
               }  
667            }            }
668          else          else
669            {            {
             double currY = vr.y;  
             double spaceInUnits = space * (double) vr.height / (max - min);  
             double lenInUnits = len * (double) vr.height / (max - min);  
   
670              g.setColor(c.getForeground());              g.setColor(c.getForeground());
671              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);  
                   }  
               }  
672            }            }
       }  
673    
674      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))
675        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 695  public class BasicProgressBarUI extends
695      SwingUtilities.calculateInnerArea(c, vr);      SwingUtilities.calculateInnerArea(c, vr);
696    
697      g.setColor(c.getBackground());      g.setColor(c.getBackground());
698      g.fill3DRect(vr.x, vr.y, vr.width, vr.height, false);      g.fillRect(vr.x, vr.y, vr.width, vr.height);
699    
700      Rectangle box = new Rectangle();      boxRect = getBox(boxRect);
     getBox(box);  
701    
702      g.setColor(c.getForeground());      g.setColor(c.getForeground());
703      g.fill3DRect(box.x, box.y, box.width, box.height, true);      g.fillRect(boxRect.x, boxRect.y, boxRect.width, boxRect.height);
704    
705      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))      if (progressBar.isStringPainted() && !progressBar.getString().equals(""))
706        paintString(g, 0, 0, or.width, or.height,        paintString(g, 0, 0, or.width, or.height,
# Line 628  public class BasicProgressBarUI extends Line 723  public class BasicProgressBarUI extends
723    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,
724                               int amountFull, Insets b)                               int amountFull, Insets b)
725    {    {
726        // FIXME: We do not support vertical text painting because Java2D is needed
727        // for this.
728        if (progressBar.getOrientation() == JProgressBar.VERTICAL)
729          return;
730    
731      // We want to place in the exact center of the bar.      // We want to place in the exact center of the bar.
732      Point placement = getStringPlacement(g, progressBar.getString(),      Point placement = getStringPlacement(g, progressBar.getString(),
733                                           x + b.left, y + b.top,                                           x + b.left, y + b.top,
734                                           width - b.left - b.right,                                           width - b.left - b.right,
735                                           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);  
736    
737        Color savedColor = g.getColor();
738        Shape savedClip = g.getClip();
739      FontMetrics fm = g.getFontMetrics(progressBar.getFont());      FontMetrics fm = g.getFontMetrics(progressBar.getFont());
740        int full = getAmountFull(b, width, height);
741        String str = progressBar.getString();
742    
743      g.drawString(progressBar.getString(), placement.x,      // We draw this string two times with different clips so that the text
744                   placement.y + fm.getAscent());      // over the filled area is painted with selectionForeground and over
745        // the clear area with selectionBackground.
746      g.setColor(saved);      g.setColor(getSelectionForeground());
747        g.setClip(0, 0, full + b.left, height);
748        g.drawString(str, placement.x, placement.y + fm.getAscent());
749        g.setColor(getSelectionBackground());
750        g.setClip(full + b.left, 0, width - full, height);
751        g.drawString(str, placement.x, placement.y + fm.getAscent());
752        g.setClip(savedClip);
753        g.setColor(savedColor);
754    }    }
755    
756    /**    /**
# Line 766  public class BasicProgressBarUI extends Line 872  public class BasicProgressBarUI extends
872      progressBar.addChangeListener(changeListener);      progressBar.addChangeListener(changeListener);
873      progressBar.addPropertyChangeListener(propertyListener);      progressBar.addPropertyChangeListener(propertyListener);
874      animationTimer.addActionListener(animation);      animationTimer.addActionListener(animation);
875    
876        ancestorListener = new AncestorHandler();
877        progressBar.addAncestorListener(ancestorListener);
878    
879        componentListener = new ComponentHandler();
880        progressBar.addComponentListener(componentListener);
881    }    }
882    
883    /**    /**
# Line 781  public class BasicProgressBarUI extends Line 893  public class BasicProgressBarUI extends
893      changeListener = null;      changeListener = null;
894      propertyListener = null;      propertyListener = null;
895      animation = null;      animation = null;
896    
897        if (ancestorListener != null)
898          progressBar.removeAncestorListener(ancestorListener);
899        ancestorListener = null;
900    
901        if (componentListener != null)
902          progressBar.removeComponentListener(componentListener);
903        componentListener = null;
904    }    }
905    
906    /**    /**
# Line 804  public class BasicProgressBarUI extends Line 924  public class BasicProgressBarUI extends
924          installDefaults();          installDefaults();
925          installListeners();          installListeners();
926        }        }
927        if (progressBar.isIndeterminate())
928          startAnimationTimer();
929    }    }
930    
931    /**    /**
# Line 822  public class BasicProgressBarUI extends Line 944  public class BasicProgressBarUI extends
944      animationTimer = null;      animationTimer = null;
945      progressBar = null;      progressBar = null;
946    }    }
947    
948  }  }

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.14

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