This fixes a couple of issues with the BasicScrollPaneUI.
2006-09-19 Roman Kennke <[EMAIL PROTECTED]>
* javax/swing/plaf/basic/BasicScrollPaneUI.java
(HSBChangeListener.stateChanged): Moved handling of header to
syncScrollPaneWithViewport().
(VSBChangeListener.stateChanged): Moved handling of header to
syncScrollPaneWithViewport().
(ViewportChangedHandler.stateChanged): Removed unused statements.
(syncScrollPaneWithViewport): Added null checks. Use setValues
rather then the single setter methods to avoid multiple
adjustments and side effects. Also snyc the headers here.
(updateScrollBarDisplayPolicy): Revalidate and repaint here.
(uninstallUI): Removed unnecessary cast and this qualifier as well
as the call to super.
/Roman
Index: javax/swing/plaf/basic/BasicScrollPaneUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicScrollPaneUI.java,v
retrieving revision 1.29
diff -u -1 -5 -r1.29 BasicScrollPaneUI.java
--- javax/swing/plaf/basic/BasicScrollPaneUI.java 14 Aug 2006 13:46:04 -0000 1.29
+++ javax/swing/plaf/basic/BasicScrollPaneUI.java 19 Sep 2006 09:12:21 -0000
@@ -96,98 +96,83 @@
*
* @param event the change event
*/
public void stateChanged(ChangeEvent event)
{
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
JViewport vp = scrollpane.getViewport();
Point viewPosition = vp.getViewPosition();
int xpos = hsb.getValue();
if (xpos != viewPosition.x)
{
viewPosition.x = xpos;
vp.setViewPosition(viewPosition);
}
-
- viewPosition.y = 0;
- JViewport columnHeader = scrollpane.getColumnHeader();
- if (columnHeader != null
- && !columnHeader.getViewPosition().equals(viewPosition))
- columnHeader.setViewPosition(viewPosition);
}
}
/**
* Listens for changes in the state of the vertical scrollbar's model and
* updates the scrollpane accordingly.
*
* @author Roman Kennke ([EMAIL PROTECTED])
*/
public class VSBChangeListener implements ChangeListener
{
/**
* Receives notification when the state of the vertical scrollbar
* model has changed.
*
* @param event the change event
*/
public void stateChanged(ChangeEvent event)
{
JScrollBar vsb = scrollpane.getVerticalScrollBar();
JViewport vp = scrollpane.getViewport();
Point viewPosition = vp.getViewPosition();
int ypos = vsb.getValue();
if (ypos != viewPosition.y)
{
viewPosition.y = ypos;
vp.setViewPosition(viewPosition);
}
-
- viewPosition.x = 0;
- JViewport rowHeader = scrollpane.getRowHeader();
- if (rowHeader != null
- && !rowHeader.getViewPosition().equals(viewPosition))
- rowHeader.setViewPosition(viewPosition);
}
}
/**
* Listens for changes of the viewport's extent size and updates the
* scrollpane accordingly.
*
* @author Roman Kennke ([EMAIL PROTECTED])
*/
public class ViewportChangeHandler implements ChangeListener
{
/**
* Receives notification when the view's size, position or extent size
* changes. When the extents size has changed, this method calls
* [EMAIL PROTECTED] BasicScrollPaneUI#syncScrollPaneWithViewport()} to adjust the
* scrollbars extents as well.
*
* @param event the change event
*/
public void stateChanged(ChangeEvent event)
{
- JViewport vp = scrollpane.getViewport();
- JScrollBar hsb = scrollpane.getHorizontalScrollBar();
- JScrollBar vsb = scrollpane.getVerticalScrollBar();
syncScrollPaneWithViewport();
}
}
/**
* Listens for property changes on the scrollpane and update the view
* accordingly.
*
* @author Roman Kennke ([EMAIL PROTECTED])
*/
public class PropertyChangeHandler implements PropertyChangeListener
{
/**
@@ -766,32 +751,32 @@
/**
* Creates and returns the mouse wheel listener for the scrollpane.
*
* @return the mouse wheel listener for the scrollpane
*
* @since 1.4
*/
protected MouseWheelListener createMouseWheelListener()
{
return new MouseWheelHandler();
}
public void uninstallUI(final JComponent c)
{
super.uninstallUI(c);
- this.uninstallDefaults((JScrollPane) c);
- uninstallListeners((JScrollPane) c);
+ uninstallDefaults((JScrollPane) c);
+ uninstallListeners(c);
installKeyboardActions((JScrollPane) c);
}
/**
* Uninstalls all the listeners that have been installed in
* [EMAIL PROTECTED] #installListeners(JScrollPane)}.
*
* @param c the scrollpane from which to uninstall the listeners
*/
protected void uninstallListeners(JComponent c)
{
JScrollPane sp = (JScrollPane) c;
sp.removePropertyChangeListener(spPropertyChangeListener);
sp.getHorizontalScrollBar().getModel()
.removeChangeListener(hsbChangeListener);
@@ -803,57 +788,88 @@
v.removeContainerListener(containerListener);
for (int i = 0; i < v.getComponentCount(); i++)
v.getComponent(i).removeMouseWheelListener(mouseWheelListener);
}
public Dimension getMinimumSize(JComponent c)
{
JScrollPane p = (JScrollPane) c;
ScrollPaneLayout sl = (ScrollPaneLayout) p.getLayout();
return sl.minimumLayoutSize(c);
}
public void paint(Graphics g, JComponent c)
- {
+ {
Border vpBorder = scrollpane.getViewportBorder();
if (vpBorder != null)
{
Rectangle r = scrollpane.getViewportBorderBounds();
vpBorder.paintBorder(scrollpane, g, r.x, r.y, r.width, r.height);
}
}
/**
- * Synchronizes the scrollbars with the viewport's extents.
+ * Synchronizes the scrollbar and header settings positions and extent
+ * with the viewport's view position and extent.
*/
protected void syncScrollPaneWithViewport()
{
JViewport vp = scrollpane.getViewport();
- // Update the horizontal scrollbar.
- JScrollBar hsb = scrollpane.getHorizontalScrollBar();
- hsb.setMaximum(vp.getViewSize().width);
- hsb.setValue(vp.getViewPosition().x);
- hsb.setVisibleAmount(vp.getExtentSize().width);
-
- // Update the vertical scrollbar.
- JScrollBar vsb = scrollpane.getVerticalScrollBar();
- vsb.setMaximum(vp.getViewSize().height);
- vsb.setValue(vp.getViewPosition().y);
- vsb.setVisibleAmount(vp.getExtentSize().height);
+ if (vp != null)
+ {
+ Dimension extentSize = vp.getExtentSize();
+ Rectangle viewBounds = vp.getViewRect();
+
+ // Update the vertical scrollbar.
+ JScrollBar vsb = scrollpane.getVerticalScrollBar();
+ if (vsb != null)
+ {
+ int extent = extentSize.height;
+ int max = viewBounds.height;
+ vsb.setValues(Math.max(0, Math.min(viewBounds.y, max - extent)),
+ extent, 0, max);
+ }
+
+ // Update the horizontal scrollbar.
+ JScrollBar hsb = scrollpane.getHorizontalScrollBar();
+ if (hsb != null)
+ {
+ int extent = extentSize.width;
+ int max = viewBounds.width;
+ vsb.setValues(Math.max(0, Math.min(viewBounds.x, max - extent)),
+ extent, 0, max);
+ }
+
+ // Update the row header.
+ JViewport rowHeader = scrollpane.getRowHeader();
+ if (rowHeader != null)
+ {
+ Point p = new Point(0, viewBounds.y);
+ rowHeader.setViewPosition(p);
+ }
+
+ // Update the column header.
+ JViewport colHeader = scrollpane.getColumnHeader();
+ if (colHeader != null)
+ {
+ Point p = new Point(viewBounds.x, 0);
+ colHeader.setViewPosition(p);
+ }
+ }
}
/**
* Receives notification when the <code>columnHeader</code> property has
* changed on the scrollpane.
*
* @param ev the property change event
*/
protected void updateColumnHeader(PropertyChangeEvent ev)
{
// TODO: Find out what should be done here. Or is this only a hook?
}
/**
* Receives notification when the <code>rowHeader</code> property has changed
@@ -862,31 +878,32 @@
* @param ev the property change event
*/
protected void updateRowHeader(PropertyChangeEvent ev)
{
// TODO: Find out what should be done here. Or is this only a hook?
}
/**
* Receives notification when the <code>scrollBarDisplayPolicy</code>
* property has changed on the scrollpane.
*
* @param ev the property change event
*/
protected void updateScrollBarDisplayPolicy(PropertyChangeEvent ev)
{
- // TODO: Find out what should be done here. Or is this only a hook?
+ scrollpane.revalidate();
+ scrollpane.repaint();
}
/**
* Receives notification when the <code>viewport</code> property has changed
* on the scrollpane.
*
* This method sets removes the viewportChangeListener from the old viewport
* and adds it to the new viewport.
*
* @param ev the property change event
*/
protected void updateViewport(PropertyChangeEvent ev)
{
JViewport oldViewport = (JViewport) ev.getOldValue();
oldViewport.removeChangeListener(viewportChangeListener);