/**
   * Creates a new SWT {@link Composite} with this <code>ViewAppState</code> embedded within it.
   * This method gets an {@link VizEmbeddedView} from the {@link MasterApplication}, connects with
   * that view, and embeds that view in the <code>Composite</code>.
   *
   * @param parent The parent <code>Composite</code>.
   * @return The <code>Composite</code> that has an embedded jME view managed by this <code>
   *     ViewAppState</code>. This <code>Composite</code>'s layout should be set by the caller.
   *     <b>This <code>Composite</code> should be disposed when it is no longer required</b>.
   */
  public Composite createComposite(Composite parent) {

    // Set the default return value and check the parameter.
    Composite composite = null;
    if (parent != null && embeddedView == null) {

      // Get an EmbeddedView that uses the app for rendering.
      embeddedView = app.getEmbeddedView();

      // Create the embedded Composite. When it is disposed, the
      // associated EmbeddedView should be released.
      composite =
          new Composite(parent, SWT.EMBEDDED) {
            @Override
            public void dispose() {
              // Dispose of resources tied with this specific view.
              disposeView(embeddedView);
              super.dispose();
            }
          };
      // Create the AWT frame inside the SWT.EMBEDDED Composite.
      Frame embeddedFrame = SWT_AWT.new_Frame(composite);
      // Add the AwtPanel to the embedded AWT Frame. The panel needs to
      // fill the Frame.
      embeddedFrame.setLayout(new BorderLayout());
      // Attach the EmbeddedView to the embedded Frame.
      embeddedView.addToEmbeddedFrame(embeddedFrame);
      embeddedFrame.pack();
      embeddedFrame.setVisible(true);

      // Wait for a maximum of 5 seconds if the view has not been
      // initialized yet. The ViewAppState must be initialized!!!
      int limit = 5000;
      int count = 0;
      while (!isInitialized() && count < limit) {
        try {
          Thread.sleep(50);
          count += 50;
        } catch (InterruptedException e) {
          logger.error(getClass().getName() + " Exception!", e);
        }
      }

      // Register with the EmbeddedView.
      embeddedView.registerViewClient(this);

      // Make sure the controls are enabled.
      enableControls();
    }

    return composite;
  }