Hey,
This patch fixes a couple of bugs in ScrollPane's getScrollPosition and
setScrollPosition methods:
- Both methods should throw a NPE if the scrollpane does have a child
(i.e. no componenent).
- In the setScrollPosition(int, int) method, we need to check that both
parameters are within the allowed bounds. That is, x >= 0, y >=0 ,
width <= (child's width - viewport width) and
height <= (child's height - viewport height).
This patch passes a couple of failing Harmony's tests. I have also
commited mauve tests for these changes.
Cheers,
Tania
2006-12-08 Tania Bento <[EMAIL PROTECTED]>
* java/awt/ScrollPane.java
(getScrollPosition): Throw NullPointerException if scrollpane
does have a child.
(setScrollPosition(int, int)): Throw NullPointerException if
scrollpane does have a child. Check that both ints are within
the allowed bounds; If they are not, scroll to the closest
allowed
bound.
Index: java/awt/ScrollPane.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/ScrollPane.java,v
retrieving revision 1.30
diff -u -r1.30 ScrollPane.java
--- java/awt/ScrollPane.java 6 Dec 2006 20:13:27 -0000 1.30
+++ java/awt/ScrollPane.java 8 Dec 2006 16:36:16 -0000
@@ -338,10 +338,15 @@
* Returns the current scroll position of the viewport.
*
* @return The current scroll position of the viewport.
+ *
+ * @throws NullPointerException if the scrollpane does have a child.
*/
public Point
getScrollPosition()
{
+ if (getComponentCount() == 0)
+ throw new NullPointerException();
+
int x = 0;
int y = 0;
@@ -380,20 +385,35 @@
* @param x The new X coordinate of the scroll position.
* @param y The new Y coordinate of the scroll position.
*
+ * @throws NullPointerException if scrollpane does not have a child.
+ *
* @exception IllegalArgumentException If the specified value is outside
* the legal scrolling range.
*/
public void
setScrollPosition(int x, int y)
{
+ if (getComponentCount() == 0)
+ throw new NullPointerException("child is null");
+
+ if (x > (int) (getComponent(0).getWidth() - getViewportSize().getWidth()))
+ x = (int) (getComponent(0).getWidth() - getViewportSize().getWidth());
+ if (y > (int) (getComponent(0).getHeight() - getViewportSize().getHeight()))
+ y = (int) (getComponent(0).getHeight() - getViewportSize().getHeight());
+
+ if (x < 0)
+ x = 0;
+ if (y < 0)
+ y = 0;
+
Adjustable h = getHAdjustable();
Adjustable v = getVAdjustable();
-
+
if (h != null)
h.setValue(x);
if (v != null)
v.setValue(y);
-
+
ScrollPanePeer spp = (ScrollPanePeer)getPeer();
if (spp != null)
spp.setScrollPosition(x, y);