classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Implemented ViewListener in javax.swing.JViewport


From: Roman Kennke
Subject: [cp-patches] FYI: Implemented ViewListener in javax.swing.JViewport
Date: Mon, 23 May 2005 14:53:22 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204

I implemented the inner class ViewListener in javax.swing.JViewport and added related code to JViewport.

See in-code comments for some details.

2005-05-23  Roman Kennke  <address@hidden>

       * javax/swing/JViewport.java:
       (ViewListener): Added and implemented inner class.
       (createViewListener): Added and implemented new method.
       (setView): Add and remove ViewListener to/from the view component.

/Roman

Index: javax/swing/JViewport.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JViewport.java,v
retrieving revision 1.17
diff -u -r1.17 JViewport.java
--- javax/swing/JViewport.java  13 Feb 2005 23:34:01 -0000      1.17
+++ javax/swing/JViewport.java  23 May 2005 12:50:41 -0000
@@ -44,6 +44,9 @@
 import java.awt.Insets;
 import java.awt.Point;
 import java.awt.Rectangle;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
+import java.io.Serializable;
 
 import javax.swing.border.Border;
 import javax.swing.event.ChangeEvent;
@@ -93,6 +96,59 @@
  */
 public class JViewport extends JComponent
 {
+
+  /**
+   * A address@hidden java.awt.event.ComponentListener} that listens for
+   * changes of the view's size. This class forbids changes of the view
+   * component's size that would exceed the viewport's size.
+   */
+  protected class ViewListener
+    extends ComponentAdapter
+    implements Serializable
+  {
+    /**
+     * Creates a new instance of ViewListener.
+     */
+    protected ViewListener()
+    {
+    }
+
+    /**
+     * Receives notification when a component (in this case: the view
+     * component) changes it's size.
+     *
+     * @param ev the ComponentEvent describing the change
+     */
+    public void componentResized(ComponentEvent ev)
+    {
+      // According to some tests that I did with Sun's implementation
+      // this class is supposed to make sure that the view component
+      // is not resized to a larger size than the viewport.
+      // This is not documented anywhere. What I did is: I subclassed JViewport
+      // and ViewListener and 'disabled' the componentResized method by
+      // overriding it and not calling super.componentResized().
+      // When this method is disabled I can set the size on the view component
+      // normally, when it is enabled, it gets immediatly resized back,
+      // after a resize attempt that would exceed the Viewport's size.
+      Component comp = ev.getComponent();
+      Dimension newSize = comp.getSize();
+      Dimension viewportSize = getSize();
+      boolean revert = false;
+      if (newSize.width > viewportSize.width)
+        {
+          newSize.width = viewportSize.width;
+          revert = true;
+        }
+      if (newSize.height > viewportSize.height)
+        {
+          newSize.height = viewportSize.height;
+          revert = true;
+        }
+      if (revert == true)
+        comp.setSize(newSize);
+    }
+  }
+
   private static final long serialVersionUID = -6925142919680527970L;
   
   public static final int SIMPLE_SCROLL_MODE = 0;
@@ -125,6 +181,11 @@
 
   Point lastPaintPosition;
 
+  /**
+   * The ViewListener instance.
+   */
+  ViewListener viewListener;
+
   public JViewport()
   {
     setOpaque(true);
@@ -267,9 +328,17 @@
   public void setView(Component v)
   {
     while (getComponentCount() > 0)
-      remove(0);
+      {
+        if (viewListener != null)
+          getView().removeComponentListener(viewListener);
+        remove(0);
+      }
+
     if (v != null)
       {
+        if (viewListener == null)
+          viewListener = createViewListener();
+        v.addComponentListener(viewListener);
         add(v);
         fireStateChanged();
       }
@@ -392,4 +461,15 @@
     if (border != null)
       throw new IllegalArgumentException();
   }
+
+  /**
+   * Creates a address@hidden ViewListener} that is supposed to listen for
+   * size changes on the view component.
+   *
+   * @return a ViewListener instance
+   */
+  protected ViewListener createViewListener()
+  {
+    return new ViewListener();
+  }
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]