Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [albireo-dev] Re: populate and size/layout management

Sorry, I meant to include the diffs (not including the new view)

### Eclipse Workspace Patch 1.0
#P org.eclipse.albireo.core
Index: src/org/eclipse/albireo/core/SwingControl.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/SwingControl.java,v
retrieving revision 1.6
diff -u -r1.6 SwingControl.java
--- src/org/eclipse/albireo/core/SwingControl.java	25 Jan 2008 21:52:39 -0000	1.6
+++ src/org/eclipse/albireo/core/SwingControl.java	27 Jan 2008 00:21:27 -0000
@@ -12,6 +12,7 @@
 package org.eclipse.albireo.core;
 
 import java.awt.Container;
+import java.awt.Dimension;
 import java.awt.EventQueue;
 import java.awt.Frame;
 
@@ -22,6 +23,7 @@
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.SWTException;
 import org.eclipse.swt.awt.SWT_AWT;
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Composite;
@@ -33,6 +35,11 @@
     private Frame frame;
     private JComponent swingComponent;
     private boolean populated = false;
+    private boolean computeSizeDefault = false;
+    private Dimension cachedMinSize = new Dimension(0,0);
+    private Dimension cachedPrefSize = new Dimension(0,0);
+    private Dimension cachedMaxSize = new Dimension(0,0);
+    private boolean cachedSizesInitialized = false;
 
     // ========================================================================
     // Constructors
@@ -96,7 +103,7 @@
      *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the SWT event thread
      * </ul>
      */
-    public void populate() {
+    protected void populate() {
         checkWidget();
         
         if (!populated) {
@@ -181,7 +188,27 @@
         // only single component that satisfies all the above. This does not imply that 
         // we have a true applet; in particular, there is no notion of an applet lifecycle in this
         // context. 
-        JApplet applet = new JApplet();
+        JApplet applet = new JApplet() {
+            
+            private boolean validateCalled;
+
+            // Keep the sizes cache up to date.
+            public void invalidate() {
+                super.invalidate();
+                // Avoid updating the size cache until the first validate. This ensures that the initial cached
+                // sizes are correct and that the relayout of the SwingControl isn't done until that time. 
+                // (See updateCachedAwtSizes()).
+                if (validateCalled) {
+                    validate();
+                }
+            }
+            
+            protected void validateTree() {
+                super.validateTree();
+                updateCachedAWTSizes(getMinimumSize(), getPreferredSize(), getMaximumSize());
+                validateCalled = true;
+            }
+        };
         frame.add(applet);
         return applet;
     }
@@ -224,7 +251,7 @@
         if (!populatedChecked) {
             populatedChecked = true;
             if (!populated) {
-                System.err.println("org.eclipse.albireo.SwingControl instance "+this+" is still empty - did you forget to call populate()?");
+                populate();
             }
         }
     }
@@ -254,22 +281,104 @@
     // ========================================================================
     // Size management
 
+    // The min/max/preferred sizes are changed and accessed atomically so that they are always 
+    // consistent with each other. 
+    protected void updateCachedAWTSizes(Dimension min, Dimension pref, Dimension max) {
+        // System.out.println("Updated component sizes from AWT: " + min + " <= " + pref + " <= " + max);
+        boolean firstSizeUpdate = !cachedSizesInitialized;
+        
+        synchronized (this) {
+            cachedSizesInitialized = true;
+            cachedMinSize = min;
+            cachedPrefSize = pref;
+            cachedMaxSize = max;
+        }
+        
+        if (firstSizeUpdate) {
+            // Preferred (and min/max) sizes are available for the AWT component for the first time. Layout the
+            // composite so that those sizes can be taken into account 
+            getDisplay().asyncExec(new Runnable() {
+                public void run() {
+                    // System.out.println("Laying out after first size update");
+                    getParent().layout();
+                }
+            });
+        }
+    }
+    
+    protected void getCachedAWTSizes(Dimension min, Dimension pref, Dimension max) {
+        synchronized(this) {
+            min.setSize(cachedMinSize);
+            pref.setSize(cachedPrefSize);
+            max.setSize(cachedMaxSize);
+        }
+    }
+    
+    protected void setAWTSize(final int width, final int height) {
+        // System.out.println("Setting size from SWT: " + width + " x " + height + " for " + swingComponent);
+        EventQueue.invokeLater(
+            new Runnable() {
+              public void run() {
+                if (frame != null) {
+                    frame.setBounds(0, 0, Math.max(width, 0), Math.max(height, 0));
+                    frame.validate();
+                }
+              }
+            });
+    }
+
+    protected void handleSetBounds(int width, int height) {
+        checkPopulated();
+        if (!computeSizeDefault) {
+            setAWTSize(width, height);
+        }
+        computeSizeDefault = false;
+    }
+    
+    public Point computeSize(int widthHint, int heightHint, boolean changed) {
+        assert Display.getCurrent() != null;
+        
+        computeSizeDefault = !cachedSizesInitialized;
+        
+        if (computeSizeDefault) {
+//            System.out.println("Uninitialized AWT sizes for " + swingComponent);
+            return super.computeSize(widthHint, heightHint, changed);
+        } else {
+            Dimension min = new Dimension();
+            Dimension pref = new Dimension();
+            Dimension max = new Dimension();
+            getCachedAWTSizes(min, pref, max);
+
+            int width = (widthHint == SWT.DEFAULT ? pref.width :
+                widthHint < min.width ? min.width :
+                    widthHint > max.width ? max.width :
+                        widthHint);
+            int height = (heightHint == SWT.DEFAULT ? pref.height :
+                heightHint < min.width ? min.height :
+                    heightHint > max.width ? max.height :
+                        heightHint);
+//            System.out.println("Computed size from SWT: " + width + " x " + height + " for " + swingComponent);
+            return new Point(width, height);
+        }
+    }
+
     // This is called by the layout when it assigns a size and position to this
     // Control.
     /**
      * Overridden.
      */
     public void setBounds(Rectangle rect) {
-        checkPopulated();
+        handleSetBounds(rect.width, rect.height);
         super.setBounds(rect);
     }
     /**
      * Overridden.
      */
     public void setBounds(int x, int y, int width, int height) {
-        checkPopulated();
+        handleSetBounds(width, height);
         super.setBounds(x, y, width, height);
     }
 
+
     // ========================================================================
 }
#P org.eclipse.albireo.examples
Index: src/org/eclipse/albireo/examples/EmbeddedJTable.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples/src/org/eclipse/albireo/examples/EmbeddedJTable.java,v
retrieving revision 1.1
diff -u -r1.1 EmbeddedJTable.java
--- src/org/eclipse/albireo/examples/EmbeddedJTable.java	7 Dec 2007 21:16:02 -0000	1.1
+++ src/org/eclipse/albireo/examples/EmbeddedJTable.java	27 Jan 2008 00:21:28 -0000
@@ -48,7 +48,6 @@
                 return createTable();
             }
         };
-        control.populate();
         
         shell.open();
         while(!shell.isDisposed()) {
#P org.eclipse.albireo.examples.plugin
Index: src/org/eclipse/albireo/examples/plugin/views/ZooView.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/src/org/eclipse/albireo/examples/plugin/views/ZooView.java,v
retrieving revision 1.1
diff -u -r1.1 ZooView.java
--- src/org/eclipse/albireo/examples/plugin/views/ZooView.java	24 Jan 2008 19:01:22 -0000	1.1
+++ src/org/eclipse/albireo/examples/plugin/views/ZooView.java	27 Jan 2008 00:21:31 -0000
@@ -92,7 +92,6 @@
                 return scrollable;
             }
         };
-        swingControl.populate();
     }
 
     private void addButtons(JComponent panel) {
Index: src/org/eclipse/albireo/examples/plugin/views/TestScreenCoordinatesView.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/src/org/eclipse/albireo/examples/plugin/views/TestScreenCoordinatesView.java,v
retrieving revision 1.1
diff -u -r1.1 TestScreenCoordinatesView.java
--- src/org/eclipse/albireo/examples/plugin/views/TestScreenCoordinatesView.java	24 Jan 2008 20:07:32 -0000	1.1
+++ src/org/eclipse/albireo/examples/plugin/views/TestScreenCoordinatesView.java	27 Jan 2008 00:21:30 -0000
@@ -217,7 +217,6 @@
                 return entirePanel;
             }
         };
-        swingControl.populate();
     }
 
     public void setFocus() {
Index: src/org/eclipse/albireo/examples/plugin/views/EventLoggerView.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/src/org/eclipse/albireo/examples/plugin/views/EventLoggerView.java,v
retrieving revision 1.2
diff -u -r1.2 EventLoggerView.java
--- src/org/eclipse/albireo/examples/plugin/views/EventLoggerView.java	24 Jan 2008 23:03:06 -0000	1.2
+++ src/org/eclipse/albireo/examples/plugin/views/EventLoggerView.java	27 Jan 2008 00:21:30 -0000
@@ -59,7 +59,6 @@
                 }
             }
         };
-        swingControl.populate();
     }
 
     static void logEvent(Component c, EventObject event) {
Index: src/org/eclipse/albireo/examples/plugin/views/EmbeddedJTableView.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/src/org/eclipse/albireo/examples/plugin/views/EmbeddedJTableView.java,v
retrieving revision 1.2
diff -u -r1.2 EmbeddedJTableView.java
--- src/org/eclipse/albireo/examples/plugin/views/EmbeddedJTableView.java	23 Jan 2008 20:15:30 -0000	1.2
+++ src/org/eclipse/albireo/examples/plugin/views/EmbeddedJTableView.java	27 Jan 2008 00:21:30 -0000
@@ -47,8 +47,6 @@
                 return scrollPane;
             }
         };
-
-        swingControl.populate();
     }
 
     public void setFocus() {
Index: src/org/eclipse/albireo/examples/plugin/views/SWTBug58308.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/src/org/eclipse/albireo/examples/plugin/views/SWTBug58308.java,v
retrieving revision 1.2
diff -u -r1.2 SWTBug58308.java
--- src/org/eclipse/albireo/examples/plugin/views/SWTBug58308.java	25 Jan 2008 21:56:19 -0000	1.2
+++ src/org/eclipse/albireo/examples/plugin/views/SWTBug58308.java	27 Jan 2008 00:21:30 -0000
@@ -53,7 +53,6 @@
                         return button;
                     }
                 };
-            ((SwingControl)swingControl).populate();
         }
     }
 
Index: plugin.xml
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/plugin.xml,v
retrieving revision 1.6
diff -u -r1.6 plugin.xml
--- plugin.xml	25 Jan 2008 18:27:01 -0000	1.6
+++ plugin.xml	27 Jan 2008 00:21:30 -0000
@@ -51,6 +51,12 @@
       </view>
       <!-- Now the bug views. -->
       <view
+            class="org.eclipse.albireo.examples.plugin.views.GridLayoutView"
+            id="org.eclipse.albireo.examples.plugin.gridLayoutView"
+            category="org.eclipse.albireo"
+            name="Grid Layout">
+      </view>
+      <view
             class="org.eclipse.albireo.examples.plugin.views.SWTBug58308"
             id="org.eclipse.albireo.examples.plugin.58308"
             category="org.eclipse.albireo"

Back to the top