/**
   * The constants used in this Content Widget.
   */
  public static interface CwConstants extends Constants,
      ContentWidget.CwConstants {
    String[] cwTreeBeethovenWorkConcertos();

    String[] cwTreeBeethovenWorkQuartets();

    String[] cwTreeBeethovenWorkSonatas();

    String[] cwTreeBeethovenWorkSymphonies();

    String[] cwTreeBrahmsWorkConcertos();

    String[] cwTreeBrahmsWorkQuartets();

    String[] cwTreeBrahmsWorkSonatas();

    String[] cwTreeBrahmsWorkSymphonies();

    String[] cwTreeComposers();

    String cwTreeConcertos();

    String cwTreeDescription();

    String cwTreeDynamicLabel();

    String cwTreeItem();

    String[] cwTreeMozartWorkConcertos();

    String cwTreeName();

    String cwTreeQuartets();

    String cwTreeSonatas();

    String cwTreeStaticLabel();

    String cwTreeSymphonies();
  }

  /**
   * An instance of the constants.
   */
  private CwConstants constants;

  /**
   * Initialize this example.
   */
  @Override
  public Widget onInitialize() {
    // Create a static tree and a container to hold it
    Tree staticTree = createStaticTree();
    staticTree.setAnimationEnabled(true);
    staticTree.ensureDebugId("cwTree-staticTree");
    ScrollPanel staticTreeWrapper = new ScrollPanel(staticTree);
    staticTreeWrapper.ensureDebugId("cwTree-staticTree-Wrapper");
    staticTreeWrapper.setSize("300px", "300px");

    // Wrap the static tree in a DecoratorPanel
    DecoratorPanel staticDecorator = new DecoratorPanel();
    staticDecorator.setWidget(staticTreeWrapper);

    // Create a dynamically generated tree and a container to hold it
    final Tree dynamicTree = createDynamicTree();
    dynamicTree.ensureDebugId("cwTree-dynamicTree");
    ScrollPanel dynamicTreeWrapper = new ScrollPanel(dynamicTree);
    dynamicTreeWrapper.ensureDebugId("cwTree-dynamicTree-Wrapper");
    dynamicTreeWrapper.setSize("300px", "300px");

    // Wrap the dynamic tree in a DecoratorPanel
    DecoratorPanel dynamicDecorator = new DecoratorPanel();
    dynamicDecorator.setWidget(dynamicTreeWrapper);

    // Combine trees onto the page
    Grid grid = new Grid(2, 3);
    grid.setCellPadding(2);
    grid.getRowFormatter().setVerticalAlign(1, HasVerticalAlignment.ALIGN_TOP);
    grid.setHTML(0, 0, constants.cwTreeStaticLabel());
    grid.setHTML(0, 1, "   ");
    grid.setHTML(0, 2, constants.cwTreeDynamicLabel());
    grid.setWidget(1, 0, staticDecorator);
    grid.setHTML(1, 1, "   ");
    grid.setWidget(1, 2, dynamicDecorator);

    // Wrap the trees in DecoratorPanels
    return grid;
  }

  /**
   * Add a new section of music created by a specific composer.
   * 
   * @param parent the parent {@link TreeItem} where the section will be added
   * @param label the label of the new section of music
   * @param composerWorks an array of works created by the composer
   */
  private void addMusicSection(TreeItem parent, String label,
      String[] composerWorks) {
    TreeItem section = parent.addItem(label);
    for (String work : composerWorks) {
      section.addItem(work);
    }
  }

  /**
   * Create a dynamic tree that will add a random number of children to each
   * node as it is clicked.
   * 
   * @return the new tree
   */
  private Tree createDynamicTree() {
    // Create a new tree
    Tree dynamicTree = new Tree();

    // Add some default tree items
    for (int i = 0; i < 5; i++) {
      TreeItem item = dynamicTree.addItem(constants.cwTreeItem() + " " + i);

      // Temporarily add an item so we can expand this node
      item.addItem("");
    }

    // Add a listener that automatically generates some children
    dynamicTree.addTreeListener(new TreeListener() {
      public void onTreeItemSelected(TreeItem item) {
      }

      public void onTreeItemStateChanged(TreeItem item) {
        if (item.getState() && item.getChildCount() == 1) {
          // Close the item immediately
          item.setState(false, false);

          // Add a random number of children to the item
          String itemText = item.getText();
          int numChildren = Random.nextInt(5) + 2;
          for (int i = 0; i < numChildren; i++) {
            TreeItem child = item.addItem(itemText + "." + i);
            child.addItem("");
          }

          // Remove the temporary item when we finish loading
          item.getChild(0).remove();

          // Reopen the item
          item.setState(true, false);
        }
      }
    });

    // Return the tree
    return dynamicTree;
  }

  /**
   * Create a static tree with some data in it.
   * 
   * @return the new tree
   */
  private Tree createStaticTree() {
    // Create the tree
    String[] composers = constants.cwTreeComposers();
    String concertosLabel = constants.cwTreeConcertos();
    String quartetsLabel = constants.cwTreeQuartets();
    String sonatasLabel = constants.cwTreeSonatas();
    String symphoniesLabel = constants.cwTreeSymphonies();
    Tree staticTree = new Tree();

    // Add some of Beethoven's music
    TreeItem beethovenItem = staticTree.addItem(composers[0]);
    addMusicSection(beethovenItem, concertosLabel,
        constants.cwTreeBeethovenWorkConcertos());
    addMusicSection(beethovenItem, quartetsLabel,
        constants.cwTreeBeethovenWorkQuartets());
    addMusicSection(beethovenItem, sonatasLabel,
        constants.cwTreeBeethovenWorkSonatas());
    addMusicSection(beethovenItem, symphoniesLabel,
        constants.cwTreeBeethovenWorkSymphonies());

    // Add some of Brahms's music
    TreeItem brahmsItem = staticTree.addItem(composers[1]);
    addMusicSection(brahmsItem, concertosLabel,
        constants.cwTreeBrahmsWorkConcertos());
    addMusicSection(brahmsItem, quartetsLabel,
        constants.cwTreeBrahmsWorkQuartets());
    addMusicSection(brahmsItem, sonatasLabel,
        constants.cwTreeBrahmsWorkSonatas());
    addMusicSection(brahmsItem, symphoniesLabel,
        constants.cwTreeBrahmsWorkSymphonies());

    // Add some of Mozart's music
    TreeItem mozartItem = staticTree.addItem(composers[2]);
    addMusicSection(mozartItem, concertosLabel,
        constants.cwTreeMozartWorkConcertos());

    // Return the tree
    return staticTree;
  }