[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[albireo-dev] Re: populate

What can we do avoid this pitfall?

I was tempted to propose to change the SwingControl constructor to call
populate().

But then we have a constructor which indirectly calls an overridden method.
And this causes trouble, because when this overridden method is invoked,
the fields of that subclass are still not initialized, and initialization
code   { ... }   has not been executed.

So, I'm applying this pitfall catcher.

Bruno


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.5
diff -c -3 -r1.5 SwingControl.java
*** src/org/eclipse/albireo/core/SwingControl.java	25 Jan 2008 21:24:50 -0000	1.5
--- src/org/eclipse/albireo/core/SwingControl.java	25 Jan 2008 21:51:46 -0000
***************
*** 22,27 ****
--- 22,28 ----
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.SWTException;
  import org.eclipse.swt.awt.SWT_AWT;
+ import org.eclipse.swt.graphics.Rectangle;
  import org.eclipse.swt.layout.FillLayout;
  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.swt.widgets.Display;
***************
*** 214,219 ****
--- 215,234 ----
      protected void afterComponentCreatedSWTThread() {
      }
  
+     private boolean populatedChecked = false;
+     /**
+      * Checks that <code>populate()</code> has already been called.
+      * This is a convenience for the programmers.
+      */
+     private void checkPopulated() {
+         if (!populatedChecked) {
+             populatedChecked = true;
+             if (!populated) {
+                 System.err.println("org.eclipse.albireo.SwingControl instance "+this+" is still empty - did you forget to call populate()?");
+             }
+         }
+     }
+ 
      // ========================================================================
      // Accessors
  
***************
*** 237,240 ****
--- 252,275 ----
      }
  
      // ========================================================================
+     // Size management
+ 
+     // This is called by the layout when it assigns a size and position to this
+     // Control.
+     /**
+      * Overridden.
+      */
+     public void setBounds(Rectangle rect) {
+         checkPopulated();
+         super.setBounds(rect);
+     }
+     /**
+      * Overridden.
+      */
+     public void setBounds(int x, int y, int width, int height) {
+         checkPopulated();
+         super.setBounds(x, y, width, height);
+     }
+ 
+     // ========================================================================
  }