8020927: JLightweightFrame API should export layout properties change notifications
authorant
Tue, 30 Jul 2013 16:15:31 +0400
changeset 19026 96141cb8d7eb
parent 19025 b8fcddc01969
child 19027 aedec0ba69c7
8020927: JLightweightFrame API should export layout properties change notifications Reviewed-by: anthony
jdk/src/share/classes/sun/swing/JLightweightFrame.java
jdk/src/share/classes/sun/swing/LightweightContent.java
--- a/jdk/src/share/classes/sun/swing/JLightweightFrame.java	Mon Jul 29 18:48:54 2013 +0400
+++ b/jdk/src/share/classes/sun/swing/JLightweightFrame.java	Tue Jul 30 16:15:31 2013 +0400
@@ -29,12 +29,18 @@
 import java.awt.Color;
 import java.awt.Component;
 import java.awt.Container;
+import java.awt.Dimension;
 import java.awt.EventQueue;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Rectangle;
+import java.awt.event.ComponentListener;
+import java.awt.event.ContainerEvent;
+import java.awt.event.ContainerListener;
 import java.awt.image.BufferedImage;
 import java.awt.image.DataBufferInt;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import java.security.AccessController;
 
 import javax.swing.JLayeredPane;
@@ -80,6 +86,8 @@
     private boolean copyBufferEnabled;
     private int[] copyBuffer;
 
+    private PropertyChangeListener layoutSizeListener;
+
     /**
      * Constructs a new, initially invisible {@code JLightweightFrame}
      * instance.
@@ -94,6 +102,23 @@
         if (getGraphicsConfiguration().isTranslucencyCapable()) {
             setBackground(new Color(0, 0, 0, 0));
         }
+
+        layoutSizeListener = new PropertyChangeListener() {
+            @Override
+            public void propertyChange(PropertyChangeEvent e) {
+                Dimension d = (Dimension)e.getNewValue();
+
+                if ("preferredSize".equals(e.getPropertyName())) {
+                    content.preferredSizeChanged(d.width, d.height);
+
+                } else if ("maximumSize".equals(e.getPropertyName())) {
+                    content.maximumSizeChanged(d.width, d.height);
+
+                } else if ("minimumSize".equals(e.getPropertyName())) {
+                    content.minimumSizeChanged(d.width, d.height);
+                }
+            }
+        };
     }
 
     /**
@@ -104,10 +129,23 @@
      *
      * @param content the {@link LightweightContent} instance
      */
-    public void setContent(LightweightContent content) {
+    public void setContent(final LightweightContent content) {
+        if (content == null) {
+            System.err.println("JLightweightFrame.setContent: content may not be null!");
+            return;
+        }
         this.content = content;
         this.component = content.getComponent();
 
+        Dimension d = this.component.getPreferredSize();
+        content.preferredSizeChanged(d.width, d.height);
+
+        d = this.component.getMaximumSize();
+        content.maximumSizeChanged(d.width, d.height);
+
+        d = this.component.getMinimumSize();
+        content.minimumSizeChanged(d.width, d.height);
+
         initInterior();
     }
 
@@ -202,6 +240,25 @@
         contentPane.setLayout(new BorderLayout());
         contentPane.add(component);
         setContentPane(contentPane);
+
+        contentPane.addContainerListener(new ContainerListener() {
+            @Override
+            public void componentAdded(ContainerEvent e) {
+                Component c = JLightweightFrame.this.component;
+                if (e.getChild() == c) {
+                    c.addPropertyChangeListener("preferredSize", layoutSizeListener);
+                    c.addPropertyChangeListener("maximumSize", layoutSizeListener);
+                    c.addPropertyChangeListener("minimumSize", layoutSizeListener);
+                }
+            }
+            @Override
+            public void componentRemoved(ContainerEvent e) {
+                Component c = JLightweightFrame.this.component;
+                if (e.getChild() == c) {
+                    c.removePropertyChangeListener(layoutSizeListener);
+                }
+            }
+        });
     }
 
     @SuppressWarnings("deprecation")
--- a/jdk/src/share/classes/sun/swing/LightweightContent.java	Mon Jul 29 18:48:54 2013 +0400
+++ b/jdk/src/share/classes/sun/swing/LightweightContent.java	Tue Jul 30 16:15:31 2013 +0400
@@ -161,4 +161,22 @@
      * application that the frame has ungrabbed focus.
      */
     public void focusUngrabbed();
+
+    /**
+     * {@code JLightweightFrame} calls this method to notify the client
+     * application that the content preferred size has changed.
+     */
+    public void preferredSizeChanged(int width, int height);
+
+    /**
+     * {@code JLightweightFrame} calls this method to notify the client
+     * application that the content maximum size has changed.
+     */
+    public void maximumSizeChanged(int width, int height);
+
+    /**
+     * {@code JLightweightFrame} calls this method to notify the client
+     * application that the content minimum size has changed.
+     */
+    public void minimumSizeChanged(int width, int height);
 }