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

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

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

revision 1.12 by mark, Tue Jul 26 14:16:43 2005 UTC revision 1.13 by rabbit78, Fri Sep 23 20:28:20 2005 UTC
# Line 40  package javax.swing.plaf.basic; Line 40  package javax.swing.plaf.basic;
40    
41  import java.awt.Dimension;  import java.awt.Dimension;
42  import java.awt.Graphics;  import java.awt.Graphics;
43    import java.awt.Point;
44    import java.awt.event.MouseWheelEvent;
45    import java.awt.event.MouseWheelListener;
46    import java.beans.PropertyChangeEvent;
47    import java.beans.PropertyChangeListener;
48    
49  import javax.swing.JComponent;  import javax.swing.JComponent;
50    import javax.swing.JScrollBar;
51  import javax.swing.JScrollPane;  import javax.swing.JScrollPane;
52    import javax.swing.JViewport;
53  import javax.swing.ScrollPaneConstants;  import javax.swing.ScrollPaneConstants;
54  import javax.swing.ScrollPaneLayout;  import javax.swing.ScrollPaneLayout;
55  import javax.swing.UIDefaults;  import javax.swing.UIDefaults;
56  import javax.swing.UIManager;  import javax.swing.UIManager;
57    import javax.swing.event.ChangeEvent;
58    import javax.swing.event.ChangeListener;
59  import javax.swing.plaf.ComponentUI;  import javax.swing.plaf.ComponentUI;
60  import javax.swing.plaf.ScrollPaneUI;  import javax.swing.plaf.ScrollPaneUI;
61    
# Line 54  public class BasicScrollPaneUI extends S Line 63  public class BasicScrollPaneUI extends S
63    implements ScrollPaneConstants    implements ScrollPaneConstants
64  {  {
65    
66      /**
67       * Listens for changes in the state of the horizontal scrollbar's model and
68       * updates the scrollpane accordingly.
69       *
70       * @author Roman Kennke (kennke@aicas.com)
71       */
72      public class HSBChangeListener implements ChangeListener
73      {
74    
75        /**
76         * Receives notification when the state of the horizontal scrollbar
77         * model has changed.
78         *
79         * @param event the change event
80         */
81        public void stateChanged(ChangeEvent event)
82        {
83          JScrollBar hsb = scrollpane.getHorizontalScrollBar();
84          JViewport vp = scrollpane.getViewport();
85          Point viewPosition = vp.getViewPosition();
86          int xpos = hsb.getValue();
87    
88          if (xpos != viewPosition.x)
89            {
90              viewPosition.x = xpos;
91              vp.setViewPosition(viewPosition);
92            }
93    
94          viewPosition.y = 0;
95          JViewport columnHeader = scrollpane.getColumnHeader();
96          if (columnHeader != null
97              && !columnHeader.getViewPosition().equals(viewPosition))
98            columnHeader.setViewPosition(viewPosition);
99        }
100    
101      }
102    
103      /**
104       * Listens for changes in the state of the vertical scrollbar's model and
105       * updates the scrollpane accordingly.
106       *
107       * @author Roman Kennke (kennke@aicas.com)
108       */
109      public class VSBChangeListener implements ChangeListener
110      {
111    
112        /**
113         * Receives notification when the state of the vertical scrollbar
114         * model has changed.
115         *
116         * @param event the change event
117         */
118        public void stateChanged(ChangeEvent event)
119        {
120          JScrollBar vsb = scrollpane.getVerticalScrollBar();
121          JViewport vp = scrollpane.getViewport();
122          Point viewPosition = vp.getViewPosition();
123          int ypos = vsb.getValue();
124    
125          if (ypos != viewPosition.x)
126            {
127              viewPosition.y = ypos;
128              vp.setViewPosition(viewPosition);
129            }
130    
131          viewPosition.x = 0;
132          JViewport rowHeader = scrollpane.getRowHeader();
133          if (rowHeader != null
134              && !rowHeader.getViewPosition().equals(viewPosition))
135            rowHeader.setViewPosition(viewPosition);
136        }
137    
138      }
139    
140      /**
141       * Listens for changes of the viewport's extent size and updates the
142       * scrollpane accordingly.
143       *
144       * @author Roman Kennke (kennke@aicas.com)
145       */
146      public class ViewportChangeHandler implements ChangeListener
147      {
148    
149        /**
150         * Receives notification when the view's size, position or extent size
151         * changes. When the extents size has changed, this method calls
152         * {@link BasicScrollPaneUI#syncScrollPaneWithViewport()} to adjust the
153         * scrollbars extents as well.
154         *
155         * @param event the change event
156         */
157        public void stateChanged(ChangeEvent event)
158        {
159          JViewport vp = scrollpane.getViewport();
160          JScrollBar hsb = scrollpane.getHorizontalScrollBar();
161          JScrollBar vsb = scrollpane.getVerticalScrollBar();
162          Dimension extents = vp.getExtentSize();
163          if (extents.width != hsb.getModel().getExtent()
164              || extents.height != vsb.getModel().getExtent())
165            syncScrollPaneWithViewport();
166        }
167    
168      }
169    
170      /**
171       * Listens for property changes on the scrollpane and update the view
172       * accordingly.
173       *
174       * @author Roman Kennke (kennke@aicas.com)
175       */
176      public class PropertyChangeHandler implements PropertyChangeListener
177      {
178    
179        /**
180         * Receives notification when any of the scrollpane's bound property
181         * changes. This method calls the appropriate update method on the
182         * <code>ScrollBarUI</code>.
183         *
184         * @param e the property change event
185         *
186         * @see BasicScrollPaneUI#updateColumnHeader(PropertyChangeEvent)
187         * @see BasicScrollPaneUI#updateRowHeader(PropertyChangeEvent)
188         * @see BasicScrollPaneUI#updateScrollBarDisplayPolicy(PropertyChangeEvent)
189         * @see BasicScrollPaneUI#updateViewport(PropertyChangeEvent)
190         */
191        public void propertyChange(PropertyChangeEvent e)
192        {
193          if (e.getPropertyName().equals("viewport"))
194            updateViewport(e);
195          else if (e.getPropertyName().equals("rowHeader"))
196            updateRowHeader(e);
197          else if (e.getPropertyName().equals("columnHeader"))
198            updateColumnHeader(e);
199          else if (e.getPropertyName().equals("horizontalScrollBarPolicy")
200              || e.getPropertyName().equals("verticalScrollBarPolicy"))
201            updateScrollBarDisplayPolicy(e);
202        }
203    
204      }
205    
206      /**
207       * Listens for mouse wheel events and update the scrollpane accordingly.
208       *
209       * @author Roman Kennke (kennke@aicas.com)
210       *
211       * @since 1.4
212       */
213      protected class MouseWheelHandler implements MouseWheelListener
214      {
215    
216        /**
217         * Receives notification whenever the mouse wheel is moved.
218         *
219         * @param event the mouse wheel event
220         */
221        public void mouseWheelMoved(MouseWheelEvent event)
222        {
223        }
224    
225      }
226    
227    /** The Scrollpane for which the UI is provided by this class. */    /** The Scrollpane for which the UI is provided by this class. */
228    protected JScrollPane scrollpane;    protected JScrollPane scrollpane;
229    
230      /**
231       * The horizontal scrollbar listener.
232       */
233      protected ChangeListener hsbChangeListener;
234    
235      /**
236       * The vertical scrollbar listener.
237       */
238      protected ChangeListener vsbChangeListener;
239    
240      /**
241       * The viewport listener.
242       */
243      protected ChangeListener viewportChangeListener;
244    
245      /**
246       * The scrollpane property change listener.
247       */
248      protected PropertyChangeListener spPropertyChangeListener;
249    
250      /**
251       * The mousewheel listener for the scrollpane.
252       */
253      MouseWheelListener mouseWheelListener;
254    
255    public static ComponentUI createUI(final JComponent c)    public static ComponentUI createUI(final JComponent c)
256    {    {
257      return new BasicScrollPaneUI();      return new BasicScrollPaneUI();
# Line 85  public class BasicScrollPaneUI extends S Line 280  public class BasicScrollPaneUI extends S
280    public void installUI(final JComponent c)    public void installUI(final JComponent c)
281    {    {
282      super.installUI(c);      super.installUI(c);
283      this.installDefaults((JScrollPane)c);      installDefaults((JScrollPane) c);
284        installListeners((JScrollPane) c);
285      }
286    
287      /**
288       * Installs the listeners on the scrollbars, the viewport and the scrollpane.
289       *
290       * @param sp the scrollpane on which to install the listeners
291       */
292      protected void installListeners(JScrollPane sp)
293      {
294        if (spPropertyChangeListener == null)
295          spPropertyChangeListener = createPropertyChangeListener();
296        sp.addPropertyChangeListener(spPropertyChangeListener);
297    
298        if (hsbChangeListener == null)
299          hsbChangeListener = createHSBChangeListener();
300        sp.getHorizontalScrollBar().getModel().addChangeListener(hsbChangeListener);
301        
302        if (vsbChangeListener == null)
303          vsbChangeListener = createVSBChangeListener();
304        sp.getVerticalScrollBar().getModel().addChangeListener(vsbChangeListener);
305    
306        if (viewportChangeListener == null)
307          viewportChangeListener = createViewportChangeListener();
308        sp.getViewport().addChangeListener(viewportChangeListener);
309    
310        if (mouseWheelListener == null)
311          mouseWheelListener = new MouseWheelHandler();
312        sp.addMouseWheelListener(mouseWheelListener);
313      }
314    
315      /**
316       * Creates and returns the change listener for the horizontal scrollbar.
317       *
318       * @return the change listener for the horizontal scrollbar
319       */
320      protected ChangeListener createHSBChangeListener()
321      {
322        return new HSBChangeListener();
323      }
324    
325      /**
326       * Creates and returns the change listener for the vertical scrollbar.
327       *
328       * @return the change listener for the vertical scrollbar
329       */
330      protected ChangeListener createVSBChangeListener()
331      {
332        return new VSBChangeListener();
333      }
334    
335      /**
336       * Creates and returns the change listener for the viewport.
337       *
338       * @return the change listener for the viewport
339       */
340      protected ChangeListener createViewportChangeListener()
341      {
342        return new ViewportChangeHandler();
343      }
344    
345      /**
346       * Creates and returns the property change listener for the scrollpane.
347       *
348       * @return the property change listener for the scrollpane
349       */
350      protected PropertyChangeListener createPropertyChangeListener()
351      {
352        return new PropertyChangeHandler();
353    }    }
354    
355    public void uninstallUI(final JComponent c)    public void uninstallUI(final JComponent c)
356    {    {
357      super.uninstallUI(c);      super.uninstallUI(c);
358      this.uninstallDefaults((JScrollPane)c);      this.uninstallDefaults((JScrollPane)c);
359        uninstallListeners((JScrollPane) c);
360      }
361    
362      /**
363       * Uninstalls all the listeners that have been installed in
364       * {@link #installListeners(JScrollPane)}.
365       *
366       * @param c the scrollpane from which to uninstall the listeners
367       */
368      protected void uninstallListeners(JComponent c)
369      {
370        JScrollPane sp = (JScrollPane) c;
371        sp.removePropertyChangeListener(spPropertyChangeListener);
372        sp.getHorizontalScrollBar().getModel()
373                                   .removeChangeListener(hsbChangeListener);
374        sp.getVerticalScrollBar().getModel()
375                                 .removeChangeListener(vsbChangeListener);
376        sp.getViewport().removeChangeListener(viewportChangeListener);
377        sp.removeMouseWheelListener(mouseWheelListener);
378    }    }
379    
       
380    public Dimension getMinimumSize(JComponent c)    public Dimension getMinimumSize(JComponent c)
381    {    {
382      JScrollPane p = (JScrollPane ) c;      JScrollPane p = (JScrollPane ) c;
# Line 107  public class BasicScrollPaneUI extends S Line 389  public class BasicScrollPaneUI extends S
389      // do nothing; the normal painting-of-children algorithm, along with      // do nothing; the normal painting-of-children algorithm, along with
390      // ScrollPaneLayout, does all the relevant work.      // ScrollPaneLayout, does all the relevant work.
391    }    }
392    
393      /**
394       * Synchronizes the scrollbars with the viewport's extents.
395       */
396      protected void syncScrollPaneWithViewport()
397      {
398        JViewport vp = scrollpane.getViewport();
399        JScrollBar vsb = scrollpane.getVerticalScrollBar();
400        JScrollBar hsb = scrollpane.getHorizontalScrollBar();
401        hsb.getModel().setExtent(vp.getExtentSize().width);
402        vsb.getModel().setExtent(vp.getExtentSize().height);
403      }
404    
405      /**
406       * Receives notification when the <code>columnHeader</code> property has
407       * changed on the scrollpane.
408       *
409       * @param ev the property change event
410       */
411      protected void updateColumnHeader(PropertyChangeEvent ev)
412      {
413        // TODO: Find out what should be done here. Or is this only a hook?
414      }
415    
416      /**
417       * Receives notification when the <code>rowHeader</code> property has changed
418       * on the scrollpane.
419       *
420       * @param ev the property change event
421       */
422      protected void updateRowHeader(PropertyChangeEvent ev)
423      {
424        // TODO: Find out what should be done here. Or is this only a hook?
425      }
426    
427      /**
428       * Receives notification when the <code>scrollBarDisplayPolicy</code>
429       * property has changed on the scrollpane.
430       *
431       * @param ev the property change event
432       */
433      protected void updateScrollBarDisplayPolicy(PropertyChangeEvent ev)
434      {
435        // TODO: Find out what should be done here. Or is this only a hook?
436      }
437    
438      /**
439       * Receives notification when the <code>viewport</code> property has changed
440       * on the scrollpane.
441       *
442       * This method sets removes the viewportChangeListener from the old viewport
443       * and adds it to the new viewport.
444       *
445       * @param ev the property change event
446       */
447      protected void updateViewport(PropertyChangeEvent ev)
448      {
449        JViewport oldViewport = (JViewport) ev.getOldValue();
450        oldViewport.removeChangeListener(viewportChangeListener);
451        JViewport newViewport = (JViewport) ev.getNewValue();
452        oldViewport.addChangeListener(viewportChangeListener);
453      }
454  }  }
455    
456    

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

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