package de.gishmo.gwt.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

import de.gishmo.gwt.editor.client.annotation.IsEditor;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
@IsEditor(Person.class)
public class GwtEditorAptExample
  implements EntryPoint,
             Editor<Person> {

  TextBox name;

  @Path("firstName")
  TextBox firstname;

  @Path("address.street")
  TextBox street;

  @Path("date")
  DateBox date;

  private DialogBox dialogBox = new DialogBox();
  private HTML    serverResponseLabel = new HTML();



  private SimpleBeanEditorDriver<Person, GwtEditorAptExample> driver = new GwtEditorAptExampleEditorDriverImpl();

  private Person person = new Person("Simpson",
                                     "Bart",
                                     new Date(),
                                     new Address("abs",
                                                 "xyz",
                                                 "Springfield"));

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    VerticalPanel vp = new VerticalPanel();
    RootLayoutPanel.get()
                   .add(vp);

    firstname = new TextBox();
    vp.add(firstname);
    name = new TextBox();
    vp.add(name);
    street = new TextBox();
    vp.add(street);
    date = new DateBox();
    vp.add(date);

    Button flushButton = new Button("flush");
    flushButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        person = driver.flush();

        StringBuilder sb                = new StringBuilder("");
        sb.append("<br><br>")
          .append("first name:")
          .append(firstname.getText())
          .append("<br>")
          .append("name:")
          .append(name.getText())
          .append("<br>")
          .append("street:")
          .append(street.getText())
          .append("<br>")
          .append("date:")
          .append(date.getValue().toString())
          .append("<br><br>");

        serverResponseLabel.setHTML(sb.toString());

        dialogBox.center();
      }
    });
    vp.add(flushButton);

    // Create the popup dialog box
    dialogBox.setText("flushed data");
    final Button closeButton = new Button("Close");
    // We can set the id of a widget by accessing its Element
    closeButton.getElement()
               .setId("closeButton");
    final Label  textToServerLabel = new Label("data after flush:");

    VerticalPanel dialogVPanel        = new VerticalPanel();
    dialogVPanel.addStyleName("dialogVPanel");
    dialogVPanel.add(textToServerLabel);
    dialogVPanel.add(serverResponseLabel);
    dialogVPanel.add(closeButton);
    dialogBox.setWidget(dialogVPanel);

    closeButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        dialogBox.hide();
      }
    });

    driver.initialize(this);
    driver.edit(person);
  }
}

@@ -2320,25 +2320,27 @@
 end(
-firstname.getText
+person.getFirstName
 ())%0A
@@ -2414,20 +2414,22 @@
 end(
-name.getText
+person.getName
 ())%0A
@@ -2505,21 +2505,36 @@
 end(
-street.getTex
+person.getAddress().getStree
 t())
@@ -2609,20 +2609,21 @@
 end(
-date.getValu
+person.getDat
 e().

use the model to present data in the dialog box