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

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

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

revision 1.7.2.6 by gnu_andrew, Sat Sep 10 15:31:52 2005 UTC revision 1.7.2.7 by gnu_andrew, Wed Nov 2 00:43:58 2005 UTC
# Line 58  import javax.swing.BoundedRangeModel; Line 58  import javax.swing.BoundedRangeModel;
58  import javax.swing.JButton;  import javax.swing.JButton;
59  import javax.swing.JComponent;  import javax.swing.JComponent;
60  import javax.swing.JScrollBar;  import javax.swing.JScrollBar;
61    import javax.swing.LookAndFeel;
62  import javax.swing.SwingConstants;  import javax.swing.SwingConstants;
63  import javax.swing.SwingUtilities;  import javax.swing.SwingUtilities;
64  import javax.swing.Timer;  import javax.swing.Timer;
# Line 80  public class BasicScrollBarUI extends Sc Line 81  public class BasicScrollBarUI extends Sc
81     */     */
82    protected class ArrowButtonListener extends MouseAdapter    protected class ArrowButtonListener extends MouseAdapter
83    {    {
84      
85      /**      /**
86       * Move the thumb in the direction specified by the  button's arrow. If       * Move the thumb in the direction specified by the  button's arrow. If
87       * this button is held down, then it should keep moving the thumb.       * this button is held down, then it should keep moving the thumb.
# Line 91  public class BasicScrollBarUI extends Sc Line 93  public class BasicScrollBarUI extends Sc
93        scrollTimer.stop();        scrollTimer.stop();
94        scrollListener.setScrollByBlock(false);        scrollListener.setScrollByBlock(false);
95        if (e.getSource() == incrButton)        if (e.getSource() == incrButton)
96          scrollListener.setDirection(POSITIVE_SCROLL);            scrollListener.setDirection(POSITIVE_SCROLL);
97        else        else if (e.getSource() == decrButton)
98          scrollListener.setDirection(NEGATIVE_SCROLL);            scrollListener.setDirection(NEGATIVE_SCROLL);
99          scrollTimer.setDelay(100);
100        scrollTimer.start();        scrollTimer.start();
101      }      }
102    
# Line 105  public class BasicScrollBarUI extends Sc Line 108  public class BasicScrollBarUI extends Sc
108      public void mouseReleased(MouseEvent e)      public void mouseReleased(MouseEvent e)
109      {      {
110        scrollTimer.stop();        scrollTimer.stop();
111          scrollTimer.setDelay(300);
112          if (e.getSource() == incrButton)
113              scrollByUnit(POSITIVE_SCROLL);
114          else if (e.getSource() == decrButton)
115            scrollByUnit(NEGATIVE_SCROLL);
116      }      }
117    }    }
118    
# Line 120  public class BasicScrollBarUI extends Sc Line 128  public class BasicScrollBarUI extends Sc
128       */       */
129      public void stateChanged(ChangeEvent e)      public void stateChanged(ChangeEvent e)
130      {      {
       //       System.err.println(this + ".stateChanged()");  
131        calculatePreferredSize();        calculatePreferredSize();
132        getThumbBounds();        updateThumbRect();
133        scrollbar.repaint();        scrollbar.repaint();
134      }      }
135    }    }
# Line 141  public class BasicScrollBarUI extends Sc Line 148  public class BasicScrollBarUI extends Sc
148      {      {
149        if (e.getPropertyName().equals("model"))        if (e.getPropertyName().equals("model"))
150          {          {
151            ((BoundedRangeModel) e.getOldValue()).removeChangeListener(modelListener);            ((BoundedRangeModel) e.getOldValue()).removeChangeListener(modelListener);
152            scrollbar.getModel().addChangeListener(modelListener);            scrollbar.getModel().addChangeListener(modelListener);
153            getThumbBounds();            updateThumbRect();
154          }          }
155        else if (e.getPropertyName().equals("orientation"))        else if (e.getPropertyName().equals("orientation"))
156          {          {
157            incrButton.removeMouseListener(buttonListener);            uninstallListeners();
158            decrButton.removeMouseListener(buttonListener);            uninstallComponents();
159            int orientation = scrollbar.getOrientation();            uninstallDefaults();
160            switch (orientation)            installDefaults();
161              {            installComponents();
162              case (JScrollBar.HORIZONTAL):            installListeners();
163                incrButton = createIncreaseButton(EAST);          }
164                decrButton = createDecreaseButton(WEST);        else if (e.getPropertyName().equals("enabled"))
165                break;          {
166              default:            Boolean b = (Boolean) e.getNewValue();
167                incrButton = createIncreaseButton(SOUTH);            if (incrButton != null)
168                decrButton = createDecreaseButton(NORTH);              incrButton.setEnabled(b.booleanValue());
169                break;            if (decrButton != null)
170              }              decrButton.setEnabled(b.booleanValue());
           incrButton.addMouseListener(buttonListener);  
           decrButton.addMouseListener(buttonListener);  
           calculatePreferredSize();  
171          }          }
       scrollbar.repaint();  
172      }      }
173    }    }
174    
# Line 242  public class BasicScrollBarUI extends Sc Line 245  public class BasicScrollBarUI extends Sc
245                scrollbar.repaint();                scrollbar.repaint();
246                return;                return;
247              }              }
248            scrollByBlock(direction);              scrollByBlock(direction);
249          }          }
250        else        else
251          scrollByUnit(direction);          scrollByUnit(direction);
# Line 316  public class BasicScrollBarUI extends Sc Line 319  public class BasicScrollBarUI extends Sc
319        else        else
320          value = valueForYPosition(currentMouseY);          value = valueForYPosition(currentMouseY);
321    
       if (value == scrollbar.getValue())  
         return;  
   
322        if (! thumbRect.contains(e.getPoint()))        if (! thumbRect.contains(e.getPoint()))
323          {          {
324            scrollTimer.stop();            scrollTimer.stop();
# Line 333  public class BasicScrollBarUI extends Sc Line 333  public class BasicScrollBarUI extends Sc
333                trackHighlight = DECREASE_HIGHLIGHT;                trackHighlight = DECREASE_HIGHLIGHT;
334                scrollListener.setDirection(NEGATIVE_SCROLL);                scrollListener.setDirection(NEGATIVE_SCROLL);
335              }              }
336          scrollTimer.setDelay(100);
337            scrollTimer.start();            scrollTimer.start();
338          }          }
339        else        else
# Line 343  public class BasicScrollBarUI extends Sc Line 344  public class BasicScrollBarUI extends Sc
344            // "lower" edge of the thumb. The value at which            // "lower" edge of the thumb. The value at which
345            // the cursor is at must be greater or equal            // the cursor is at must be greater or equal
346            // to that value.            // to that value.
347    
348          scrollListener.setScrollByBlock(false);
349            scrollbar.setValueIsAdjusting(true);            scrollbar.setValueIsAdjusting(true);
350            offset = value - scrollbar.getValue();        offset = value - scrollbar.getValue();
351          }          }
352        scrollbar.repaint();        scrollbar.repaint();
353      }      }
# Line 357  public class BasicScrollBarUI extends Sc Line 360  public class BasicScrollBarUI extends Sc
360       */       */
361      public void mouseReleased(MouseEvent e)      public void mouseReleased(MouseEvent e)
362      {      {
       trackHighlight = NO_HIGHLIGHT;  
363        scrollTimer.stop();        scrollTimer.stop();
364          scrollTimer.setDelay(300);
365          currentMouseX = e.getX();
366          currentMouseY = e.getY();
367    
368        if (scrollbar.getValueIsAdjusting())        if (shouldScroll(POSITIVE_SCROLL))
369          scrollbar.setValueIsAdjusting(false);          scrollByBlock(POSITIVE_SCROLL);
370          else if (shouldScroll(NEGATIVE_SCROLL))
371            scrollByBlock(NEGATIVE_SCROLL);
372    
373          trackHighlight = NO_HIGHLIGHT;
374          scrollListener.setScrollByBlock(false);
375          scrollbar.setValueIsAdjusting(true);
376        scrollbar.repaint();        scrollbar.repaint();
377      }      }
378    
# Line 381  public class BasicScrollBarUI extends Sc Line 392  public class BasicScrollBarUI extends Sc
392        else        else
393          value = valueForYPosition(currentMouseY);          value = valueForYPosition(currentMouseY);
394    
395          if (thumbRect.contains(currentMouseX, currentMouseY))
396            return false;
397          
398        if (direction == POSITIVE_SCROLL)        if (direction == POSITIVE_SCROLL)
399          return (value > scrollbar.getValue());          return (value > scrollbar.getValue());
400        else        else
# Line 517  public class BasicScrollBarUI extends Sc Line 531  public class BasicScrollBarUI extends Sc
531     */     */
532    protected JButton createIncreaseButton(int orientation)    protected JButton createIncreaseButton(int orientation)
533    {    {
534      if (incrButton == null)      return new BasicArrowButton(orientation);
       incrButton = new BasicArrowButton(orientation);  
     else  
       ((BasicArrowButton) incrButton).setDirection(orientation);  
     return incrButton;  
535    }    }
536    
537    /**    /**
# Line 534  public class BasicScrollBarUI extends Sc Line 544  public class BasicScrollBarUI extends Sc
544     */     */
545    protected JButton createDecreaseButton(int orientation)    protected JButton createDecreaseButton(int orientation)
546    {    {
547      if (decrButton == null)      return new BasicArrowButton(orientation);
       decrButton = new BasicArrowButton(orientation);  
     else  
       ((BasicArrowButton) decrButton).setDirection(orientation);  
     return decrButton;  
548    }    }
549    
550    /**    /**
# Line 602  public class BasicScrollBarUI extends Sc Line 608  public class BasicScrollBarUI extends Sc
608     */     */
609    public Dimension getMaximumSize(JComponent c)    public Dimension getMaximumSize(JComponent c)
610    {    {
611      return getPreferredSize(c);      return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
612    }    }
613    
614    /**    /**
# Line 644  public class BasicScrollBarUI extends Sc Line 650  public class BasicScrollBarUI extends Sc
650     */     */
651    void calculatePreferredSize()    void calculatePreferredSize()
652    {    {
     // System.err.println(this + ".calculatePreferredSize()");  
653      int height;      int height;
654      int width;      int width;
655      height = width = 0;      height = width = 0;
# Line 707  public class BasicScrollBarUI extends Sc Line 712  public class BasicScrollBarUI extends Sc
712     */     */
713    protected Rectangle getThumbBounds()    protected Rectangle getThumbBounds()
714    {    {
     int max = scrollbar.getMaximum();  
     int min = scrollbar.getMinimum();  
     int value = scrollbar.getValue();  
     int extent = scrollbar.getVisibleAmount();  
   
     // System.err.println(this + ".getThumbBounds()");  
     if (max == min)  
       {  
         thumbRect.x = trackRect.x;  
         thumbRect.y = trackRect.y;  
         if (scrollbar.getOrientation() == HORIZONTAL)  
           {  
             thumbRect.width = getMinimumThumbSize().width;  
             thumbRect.height = trackRect.height;  
           }  
         else  
           {  
             thumbRect.width = trackRect.width;  
             thumbRect.height = getMinimumThumbSize().height;  
           }  
         return thumbRect;  
       }  
   
     if (scrollbar.getOrientation() == HORIZONTAL)  
       {  
         thumbRect.x = trackRect.x;  
         thumbRect.x += (value - min) * trackRect.width / (max - min);  
         thumbRect.y = trackRect.y;  
   
         thumbRect.width = Math.max(extent * trackRect.width / (max - min),  
                                    getMinimumThumbSize().width);  
         thumbRect.height = trackRect.height;  
       }  
     else  
       {  
         thumbRect.x = trackRect.x;  
         thumbRect.y = trackRect.y + value * trackRect.height / (max - min);  
   
         thumbRect.width = trackRect.width;  
         thumbRect.height = Math.max(extent * trackRect.height / (max - min),  
                                     getMinimumThumbSize().height);  
       }  
715      return thumbRect;      return thumbRect;
716    }    }
717    
# Line 760  public class BasicScrollBarUI extends Sc Line 723  public class BasicScrollBarUI extends Sc
723     */     */
724    protected Rectangle getTrackBounds()    protected Rectangle getTrackBounds()
725    {    {
     SwingUtilities.calculateInnerArea(scrollbar, trackRect);  
   
     if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL)  
       {  
         trackRect.width -= incrButton.getPreferredSize().getWidth();  
         trackRect.width -= decrButton.getPreferredSize().getWidth();  
   
         trackRect.x += decrButton.getPreferredSize().getWidth();  
       }  
     else  
       {  
         trackRect.height -= incrButton.getPreferredSize().getHeight();  
         trackRect.height -= decrButton.getPreferredSize().getHeight();  
   
         trackRect.y += incrButton.getPreferredSize().getHeight();  
       }  
726      return trackRect;      return trackRect;
727    }    }
728    
# Line 785  public class BasicScrollBarUI extends Sc Line 732  public class BasicScrollBarUI extends Sc
732     */     */
733    protected void installComponents()    protected void installComponents()
734    {    {
735        if (incrButton != null)
736          scrollbar.add(incrButton);
737        if (decrButton != null)
738          scrollbar.add(decrButton);
739      }
740    
741      /**
742       * This method installs the defaults for the scrollbar specified by the
743       * Basic Look and Feel.
744       */
745      protected void installDefaults()
746      {
747      int orientation = scrollbar.getOrientation();      int orientation = scrollbar.getOrientation();
748      switch (orientation)      switch (orientation)
749        {        {
# Line 797  public class BasicScrollBarUI extends Sc Line 756  public class BasicScrollBarUI extends Sc
756          decrButton = createDecreaseButton(NORTH);          decrButton = createDecreaseButton(NORTH);
757          break;          break;
758        }        }
     scrollbar.add(incrButton);  
     scrollbar.add(decrButton);  
   }  
   
   /**  
    * This method installs the defaults for the scrollbar specified by the  
    * Basic Look and Feel.  
    */  
   protected void installDefaults()  
   {  
     UIDefaults defaults = UIManager.getLookAndFeelDefaults();  
759    
760      scrollbar.setForeground(defaults.getColor("ScrollBar.foreground"));      LookAndFeel.installColors(scrollbar, "ScrollBar.background",
761      scrollbar.setBackground(defaults.getColor("ScrollBar.background"));                                "ScrollBar.foreground");
762      scrollbar.setBorder(defaults.getBorder("ScrollBar.border"));      LookAndFeel.installBorder(scrollbar, "ScrollBar.border");
763      scrollbar.setOpaque(true);      scrollbar.setOpaque(true);
764      scrollbar.setLayout(this);      scrollbar.setLayout(this);
765    
766      thumbColor = defaults.getColor("ScrollBar.thumb");      thumbColor = UIManager.getColor("ScrollBar.thumb");
767      thumbDarkShadowColor = defaults.getColor("ScrollBar.thumbDarkShadow");      thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow");
768      thumbHighlightColor = defaults.getColor("ScrollBar.thumbHighlight");      thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
769      thumbLightShadowColor = defaults.getColor("ScrollBar.thumbShadow");      thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow");
770    
771      maximumThumbSize = defaults.getDimension("ScrollBar.maximumThumbSize");      maximumThumbSize = UIManager.getDimension("ScrollBar.maximumThumbSize");
772      minimumThumbSize = defaults.getDimension("ScrollBar.minimumThumbSize");      minimumThumbSize = UIManager.getDimension("ScrollBar.minimumThumbSize");
773    }    }
774    
775    /**    /**
# Line 873  public class BasicScrollBarUI extends Sc Line 821  public class BasicScrollBarUI extends Sc
821          trackRect = new Rectangle();          trackRect = new Rectangle();
822          thumbRect = new Rectangle();          thumbRect = new Rectangle();
823    
824          scrollTimer = new Timer(50, null);          scrollTimer = new Timer(300, null);
         scrollTimer.setRepeats(true);  
825    
826            installDefaults();
827          installComponents();          installComponents();
         installDefaults();  
828          configureScrollBarColors();          configureScrollBarColors();
829          installListeners();          installListeners();
830    
# Line 908  public class BasicScrollBarUI extends Sc Line 855  public class BasicScrollBarUI extends Sc
855     */     */
856    protected void layoutHScrollbar(JScrollBar sb)    protected void layoutHScrollbar(JScrollBar sb)
857    {    {
     // All we have to do is layout the 2 buttons?  
858      Rectangle vr = new Rectangle();      Rectangle vr = new Rectangle();
859      SwingUtilities.calculateInnerArea(scrollbar, vr);      SwingUtilities.calculateInnerArea(scrollbar, vr);
860    
     // Update the rectangles.  
     getTrackBounds();  
     getThumbBounds();  
   
861      Dimension incrDims = incrButton.getPreferredSize();      Dimension incrDims = incrButton.getPreferredSize();
862      Dimension decrDims = decrButton.getPreferredSize();      Dimension decrDims = decrButton.getPreferredSize();
863        
864        // calculate and update the track bounds
865        SwingUtilities.calculateInnerArea(scrollbar, trackRect);
866        trackRect.width -= incrDims.getWidth();
867        trackRect.width -= decrDims.getWidth();
868        trackRect.x += decrDims.getWidth();
869    
870        updateThumbRect();
871        
872      decrButton.setBounds(vr.x, vr.y, decrDims.width, trackRect.height);      decrButton.setBounds(vr.x, vr.y, decrDims.width, trackRect.height);
873      incrButton.setBounds(trackRect.x + trackRect.width, vr.y, incrDims.width,      incrButton.setBounds(trackRect.x + trackRect.width, vr.y, incrDims.width,
874                           trackRect.height);                           trackRect.height);
# Line 934  public class BasicScrollBarUI extends Sc Line 884  public class BasicScrollBarUI extends Sc
884      Rectangle vr = new Rectangle();      Rectangle vr = new Rectangle();
885      SwingUtilities.calculateInnerArea(scrollbar, vr);      SwingUtilities.calculateInnerArea(scrollbar, vr);
886    
     // Update rectangles  
     getTrackBounds();  
     getThumbBounds();  
   
887      Dimension incrDims = incrButton.getPreferredSize();      Dimension incrDims = incrButton.getPreferredSize();
888      Dimension decrDims = decrButton.getPreferredSize();      Dimension decrDims = decrButton.getPreferredSize();
889        
890        // Update rectangles
891        SwingUtilities.calculateInnerArea(scrollbar, trackRect);
892        trackRect.height -= incrDims.getHeight();
893        trackRect.height -= decrDims.getHeight();
894        trackRect.y += decrDims.getHeight();
895        
896        updateThumbRect();
897    
898      decrButton.setBounds(vr.x, vr.y, trackRect.width, decrDims.height);      decrButton.setBounds(vr.x, vr.y, trackRect.width, decrDims.height);
899      incrButton.setBounds(vr.x, trackRect.y + trackRect.height,      incrButton.setBounds(vr.x, trackRect.y + trackRect.height,
# Line 947  public class BasicScrollBarUI extends Sc Line 901  public class BasicScrollBarUI extends Sc
901    }    }
902    
903    /**    /**
904       * Updates the thumb rect.
905       */
906      void updateThumbRect()
907      {
908        int max = scrollbar.getMaximum();
909        int min = scrollbar.getMinimum();
910        int value = scrollbar.getValue();
911        int extent = scrollbar.getVisibleAmount();
912        if (max - extent <= min)
913          {
914            if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
915              {
916                thumbRect.x = trackRect.x;
917                thumbRect.y = trackRect.y;
918                thumbRect.width = getMinimumThumbSize().width;
919                thumbRect.height = trackRect.height;
920              }
921            else
922              {
923                thumbRect.x = trackRect.x;
924                thumbRect.y = trackRect.y;
925                thumbRect.width = trackRect.width;
926                thumbRect.height = getMinimumThumbSize().height;
927              }
928          }
929        else
930          {
931            if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL)
932              {
933                thumbRect.x = trackRect.x;
934                thumbRect.width = Math.max(extent * trackRect.width / (max - min),
935                    getMinimumThumbSize().width);
936                int availableWidth = trackRect.width - thumbRect.width;
937                thumbRect.x += (value - min) * availableWidth / (max - min - extent);
938                thumbRect.y = trackRect.y;
939                thumbRect.height = trackRect.height;
940              }
941            else
942              {
943                thumbRect.x = trackRect.x;
944                thumbRect.height = Math.max(extent * trackRect.height / (max - min),
945                        getMinimumThumbSize().height);
946                int availableHeight = trackRect.height - thumbRect.height;
947                thumbRect.y = trackRect.y
948                  + (value - min) * availableHeight / (max - min - extent);
949                thumbRect.width = trackRect.width;
950              }
951          }
952    
953      }
954      
955      /**
956     * This method returns the minimum size required for the layout.     * This method returns the minimum size required for the layout.
957     *     *
958     * @param scrollbarContainer The Container that is laid out.     * @param scrollbarContainer The Container that is laid out.
# Line 1124  public class BasicScrollBarUI extends Sc Line 1130  public class BasicScrollBarUI extends Sc
1130     */     */
1131    protected void uninstallComponents()    protected void uninstallComponents()
1132    {    {
1133      scrollbar.remove(incrButton);      if (incrButton != null)
1134      scrollbar.remove(decrButton);        scrollbar.remove(incrButton);
1135      incrButton = null;      if (decrButton != null)
1136      decrButton = null;        scrollbar.remove(decrButton);
1137    }    }
1138    
1139    /**    /**
# Line 1138  public class BasicScrollBarUI extends Sc Line 1144  public class BasicScrollBarUI extends Sc
1144    {    {
1145      scrollbar.setForeground(null);      scrollbar.setForeground(null);
1146      scrollbar.setBackground(null);      scrollbar.setBackground(null);
1147      scrollbar.setBorder(null);      LookAndFeel.uninstallBorder(scrollbar);
1148        incrButton = null;
1149        decrButton = null;
1150    }    }
1151    
1152    /**    /**
# Line 1155  public class BasicScrollBarUI extends Sc Line 1163  public class BasicScrollBarUI extends Sc
1163     */     */
1164    protected void uninstallListeners()    protected void uninstallListeners()
1165    {    {
1166      scrollTimer.removeActionListener(scrollListener);      if (scrollTimer != null)
1167          scrollTimer.removeActionListener(scrollListener);
1168    
1169      scrollbar.getModel().removeChangeListener(modelListener);      if (scrollbar != null)
1170      scrollbar.removePropertyChangeListener(propertyChangeListener);        {
1171            scrollbar.getModel().removeChangeListener(modelListener);
1172      decrButton.removeMouseListener(buttonListener);          scrollbar.removePropertyChangeListener(propertyChangeListener);
1173      incrButton.removeMouseListener(buttonListener);          scrollbar.removeMouseListener(trackListener);
1174            scrollbar.removeMouseMotionListener(trackListener);
1175      scrollbar.removeMouseListener(trackListener);        }
     scrollbar.removeMouseMotionListener(trackListener);  
1176    
1177        if (decrButton != null)
1178          decrButton.removeMouseListener(buttonListener);
1179        if (incrButton != null)
1180          incrButton.removeMouseListener(buttonListener);
1181        
1182      propertyChangeListener = null;      propertyChangeListener = null;
1183      modelListener = null;      modelListener = null;
1184      buttonListener = null;      buttonListener = null;
# Line 1182  public class BasicScrollBarUI extends Sc Line 1195  public class BasicScrollBarUI extends Sc
1195     */     */
1196    public void uninstallUI(JComponent c)    public void uninstallUI(JComponent c)
1197    {    {
     uninstallDefaults();  
1198      uninstallListeners();      uninstallListeners();
1199        uninstallDefaults();
1200      uninstallComponents();      uninstallComponents();
1201    
1202      scrollTimer = null;      scrollTimer = null;

Legend:
Removed from v.1.7.2.6  
changed lines
  Added in v.1.7.2.7

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