Use LazyPanel for deferred rendering in ShowCase

git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@4146 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/samples/showcase/src/com/google/gwt/sample/showcase/client/ContentWidget.java b/samples/showcase/src/com/google/gwt/sample/showcase/client/ContentWidget.java
index 5861141..4f97aaf 100644
--- a/samples/showcase/src/com/google/gwt/sample/showcase/client/ContentWidget.java
+++ b/samples/showcase/src/com/google/gwt/sample/showcase/client/ContentWidget.java
@@ -26,9 +26,9 @@
 import com.google.gwt.i18n.client.HasDirection;
 import com.google.gwt.i18n.client.LocaleInfo;
 import com.google.gwt.user.client.DOM;
-import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.DeckPanel;
 import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.LazyPanel;
 import com.google.gwt.user.client.ui.TabBar;
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.user.client.ui.Widget;
@@ -43,10 +43,9 @@
  * rules.
  * </p>
  * <p>
- * This {@link Widget} uses a lazy initialization mechanism so that the content
- * is not rendered until the onInitialize method is called, which happens the
- * first time the {@link Widget} is added to the page.. The data in the source
- * and css tabs are loaded using an RPC call to the server.
+ * This {@link Widget} extends {@link LazyPanel} so that the content is not
+ * rendered until the widget first becomes visible. The data in the source and
+ * css tabs are loaded using an RPC call to the server.
  * </p>
  * <h3>CSS Style Rules</h3>
  * <ul class="css">
@@ -57,7 +56,7 @@
  * <li>.sc-ContentWidget-description { Applied to the description }</li>
  * </ul>
  */
-public abstract class ContentWidget extends Composite implements
+public abstract class ContentWidget extends LazyPanel implements
     SelectionHandler<Integer> {
   /**
    * The constants used in this Content Widget.
@@ -83,7 +82,7 @@
   /**
    * An instance of the constants.
    */
-  private CwConstants constants;
+  private final CwConstants constants;
 
   /**
    * The deck panel with the contents.
@@ -91,11 +90,6 @@
   private DeckPanel deckPanel = null;
 
   /**
-   * A boolean indicating whether or not this widget has been initialized.
-   */
-  private boolean initialized = false;
-
-  /**
    * A boolean indicating whether or not the RPC request for the source code has
    * been sent.
    */
@@ -129,13 +123,11 @@
   public ContentWidget(CwConstants constants) {
     this.constants = constants;
     tabBar = new TabBar();
-    deckPanel = new DeckPanel();
-    initWidget(deckPanel);
-    setStyleName(DEFAULT_STYLE_NAME);
   }
 
   /**
-   * Add an item to this content widget.
+   * Add an item to this content widget. Should not be called before
+   * {@link #onInitializeComplete} has been called.
    * 
    * @param w the widget to add
    * @param tabText the text to display in the tab
@@ -185,57 +177,8 @@
   }
 
   /**
-   * Initialize this widget by creating the elements that should be added to the
-   * page.
-   */
-  public final void initialize() {
-    if (initialized == false) {
-      initialized = true;
-
-      // Add a tab handler
-      tabBar.addSelectionHandler(this);
-
-      // Create a container for the main example
-      final VerticalPanel vPanel = new VerticalPanel();
-      add(vPanel, constants.contentWidgetExample());
-
-      // Add the name
-      HTML nameWidget = new HTML(getName());
-      nameWidget.setStyleName(DEFAULT_STYLE_NAME + "-name");
-      vPanel.add(nameWidget);
-
-      // Add the description
-      HTML descWidget = new HTML(getDescription());
-      descWidget.setStyleName(DEFAULT_STYLE_NAME + "-description");
-      vPanel.add(descWidget);
-
-      // Add source code tab
-      if (hasSource()) {
-        sourceWidget = new HTML();
-        add(sourceWidget, constants.contentWidgetSource());
-      } else {
-        sourceLoaded = true;
-      }
-
-      // Add style tab
-      if (hasStyle()) {
-        styleDefs = new HashMap<String, String>();
-        styleWidget = new HTML();
-        add(styleWidget, constants.contentWidgetStyle());
-      }
-
-      // Initialize the widget and add it to the page
-      Widget widget = onInitialize();
-      if (widget != null) {
-        vPanel.add(widget);
-      }
-      onInitializeComplete();
-    }
-  }
-
-  /**
-   * When the widget is first initialize, this method is called. If it returns a
-   * Widget, the widget will be added as the first tab. Return null to disable
+   * When the widget is first initialized, this method is called. If it returns
+   * a Widget, the widget will be added as the first tab. Return null to disable
    * the first tab.
    * 
    * @return the widget to add to the first tab
@@ -302,10 +245,60 @@
     tabBar.selectTab(index);
   }
 
+  /**
+   * Initialize this widget by creating the elements that should be added to the
+   * page.
+   */
+  protected final Widget createWidget() {
+    deckPanel = new DeckPanel();
+
+    setStyleName(DEFAULT_STYLE_NAME);
+
+    // Add a tab handler
+    tabBar.addSelectionHandler(this);
+
+    // Create a container for the main example
+    final VerticalPanel vPanel = new VerticalPanel();
+    add(vPanel, constants.contentWidgetExample());
+
+    // Add the name
+    HTML nameWidget = new HTML(getName());
+    nameWidget.setStyleName(DEFAULT_STYLE_NAME + "-name");
+    vPanel.add(nameWidget);
+
+    // Add the description
+    HTML descWidget = new HTML(getDescription());
+    descWidget.setStyleName(DEFAULT_STYLE_NAME + "-description");
+    vPanel.add(descWidget);
+
+    // Add source code tab
+    if (hasSource()) {
+      sourceWidget = new HTML();
+      add(sourceWidget, constants.contentWidgetSource());
+    } else {
+      sourceLoaded = true;
+    }
+
+    // Add style tab
+    if (hasStyle()) {
+      styleDefs = new HashMap<String, String>();
+      styleWidget = new HTML();
+      add(styleWidget, constants.contentWidgetStyle());
+    }
+
+    // Initialize the showcase widget (if any) and add it to the page
+    Widget widget = onInitialize();
+    if (widget != null) {
+      vPanel.add(widget);
+    }
+    onInitializeComplete();
+    
+    return deckPanel;
+  }
+
   @Override
   protected void onLoad() {
-    // Initialize this widget if we haven't already
-    initialize();
+    ensureWidget();
 
     // Select the first tab
     if (getTabBar().getTabCount() > 0) {