/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.tests.book;

import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Window;
import com.itmill.toolkit.ui.Window.CloseEvent;

/** Component contains a button that allows opening a window. */
public class WindowOpener extends CustomComponent implements
        Window.CloseListener {
    Window mainwindow; // Reference to main window
    Window mywindow; // The window to be opened
    Button openbutton; // Button for opening the window
    Button closebutton; // A button in the window
    Label explanation; // A descriptive text

    public WindowOpener(String label, Window main) {
        mainwindow = main;

        /* The component consists of a button that opens the window. */
        final OrderedLayout layout = new OrderedLayout();
        layout.addComponent(openbutton = new Button("Open Window", this,
                "openButtonClick"));
        layout.addComponent(explanation = new Label("Explanation"));
        setCompositionRoot(layout);
    }

    /** Handle the clicks for the two buttons. */
    public void openButtonClick(Button.ClickEvent event) {
        /* Create a new window. */
        mywindow = new Window("My Dialog");

        /* Listen for close events for the window. */
        mywindow.addListener(this);

        /* Add components in the window. */
        mywindow.addComponent(new Label("A text label in the window."));
        closebutton = new Button("Close", this, "closeButtonClick");
        mywindow.addComponent(closebutton);

        /* Add the window inside the main window. */
        mainwindow.addWindow(mywindow);

        /* Allow opening only one window at a time. */
        openbutton.setEnabled(false);

        explanation.setValue("Window opened");
    }

    /** Handle Close button click and close the window. */
    public void closeButtonClick(Button.ClickEvent event) {
        /* Windows are managed by the application object. */
        mainwindow.removeWindow(mywindow);

        /* Return to initial state. */
        openbutton.setEnabled(true);

        explanation.setValue("Closed with button");
    }

    /** In case the window is closed otherwise. */
    public void windowClose(CloseEvent e) {
        /* Return to initial state. */
        openbutton.setEnabled(true);

        explanation.setValue("Closed with window controls");
    }
}

@@ -440,16 +440,42 @@
 omponent
+%0A                         
  impleme
@@ -477,24 +477,16 @@
 plements
-%0A       
  Window.
@@ -519,24 +519,25 @@
  mainwindow;
+ 
  // Referenc
@@ -573,16 +573,19 @@
 ywindow;
+   
  // The 
@@ -622,24 +622,25 @@
  openbutton;
+ 
  // Button f
@@ -721,16 +721,17 @@
   Label 
+ 
 explanat
@@ -977,36 +977,25 @@
         
-layout.addComponent(
+%0A        
 openbutt
@@ -1034,100 +1034,147 @@
 his,
-%0A                %22openButtonClick%22));%0A        layout.addComponent(explanation = new Label(%22E
+ %22openButtonClick%22);%0A        explanation = new Label(%22Explanation%22);%0A        layout.addComponent(openbutton);%0A        layout.addComponent(e
 xpla
@@ -1179,21 +1179,28 @@
 lanation
-%22)
 );%0A
+        %0A
         
@@ -1421,16 +1421,182 @@
 ialog%22);
+%0A        mywindow.setPositionX(200);%0A        mywindow.setPositionY(100);%0A%0A        /* Add the window inside the main window. */%0A        mainwindow.addWindow(mywindow);
 %0A%0A      
@@ -1858,32 +1858,32 @@
 eButtonClick%22);%0A
+
         mywindow
@@ -1915,102 +1915,8 @@
 );%0A%0A
-        /* Add the window inside the main window. */%0A        mainwindow.addWindow(mywindow);%0A%0A
     

Reformatted window opener example code.