/**
 * <License>
 */
package edu.colorado.csdms.wmt.client.ui.widgets;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.colorado.csdms.wmt.client.control.DataManager;

/**
 * A customized DialogBox with elements for setting a public or private model,
 * the driver and case of the model, and a name/description of the model. "OK"
 * and "Cancel" buttons are shown on the bottom of the dialog.
 * 
 * @author Mark Piper (mark.piper@colorado.edu)
 */
public class SaveDialogBox extends DialogBox {

  private DataManager data;
  private RadioButtonPanel accessPanel;
  private DroplistPanel driverPanel;
  private DroplistPanel casePanel;
  private FieldPanel namePanel;
  private ChoicePanel choicePanel;
  
  /**
   * Makes a SaveDialogBox with a default name.
   * 
   * @param data the DataManager object for the WMT session
   */
  public SaveDialogBox(DataManager data) {
    this(data, DataManager.DEFAULT_MODEL);
  }
  
  /**
   * Makes a SaveDialogBox with a user-supplied name.
   * 
   * @param data the DataManager object for the WMT session
   * @param modelName a descriptive name for the model
   */
  public SaveDialogBox(DataManager data, String modelName) {

    super(false); // autohide
    this.setModal(true);
    this.setText("Save Model As...");
    this.data = data;

    accessPanel = new RadioButtonPanel();
    accessPanel.getPanelLabel().removeFromParent();
    accessPanel.getLeftButton().setText("Public");
    accessPanel.getRightButton().setText("Private");

    // Displays the driver for the model in a droplist. If no driver has been
    // set, the first component in the list is displayed.
    driverPanel = new DroplistPanel();
    driverPanel.getLabel().removeFromParent();
    for (int i = 0; i < data.getComponents().size(); i++) {
      driverPanel.getDroplist().addItem(data.getComponent(i).getName());
      String driverId =
          data.getPerspective().getModelTree().getDriverComponentCell()
              .getComponentId();
      if (data.getComponent(i).getId().equals(driverId)) {
        driverPanel.getDroplist().setItemSelected(i, true);
      }
    }

    casePanel = new DroplistPanel(new String[] {"default"});
    casePanel.getLabel().removeFromParent();

    namePanel = new FieldPanel();
    namePanel.getLabel().removeFromParent();
    namePanel.getField().setText(modelName);

    Integer nRows = 4, nCols = 2;
    Grid grid = new Grid(nRows, nCols);
    grid.setWidth("100%");
    grid.setWidget(0, 0, new Label("Visibility:"));
    grid.setWidget(0, 1, accessPanel);
    grid.setWidget(1, 0, new Label("Driver:"));
    grid.setWidget(1, 1, driverPanel);
    grid.setWidget(2, 0, new Label("Case:"));
    grid.setWidget(2, 1, casePanel);
    grid.setWidget(3, 0, new Label("Name:"));
    grid.setWidget(3, 1, namePanel);

    for (int i = 0; i < nRows; i++) {
      grid.getCellFormatter().setHorizontalAlignment(i, 0,
          HasHorizontalAlignment.ALIGN_RIGHT);
    }

    choicePanel = new ChoicePanel();
    choicePanel.getOkButton().setHTML("<i class='fa fa-floppy-o'></i> Save");

    VerticalPanel contents = new VerticalPanel();
    contents.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    contents.add(grid);
    contents.add(choicePanel);

    this.setWidget(contents);
  }

  public RadioButtonPanel getAccessPanel() {
    return accessPanel;
  }

  public void setAccessPanel(RadioButtonPanel accessPanel) {
    this.accessPanel = accessPanel;
  }

  public DroplistPanel getDriverPanel() {
    return driverPanel;
  }

  public void setDriverPanel(DroplistPanel driverPanel) {
    this.driverPanel = driverPanel;
  }

  public DroplistPanel getCasePanel() {
    return casePanel;
  }

  public void setCasePanel(DroplistPanel casePanel) {
    this.casePanel = casePanel;
  }

  public FieldPanel getNamePanel() {
    return namePanel;
  }

  public void setNamePanel(FieldPanel namePanel) {
    this.namePanel = namePanel;
  }

  public ChoicePanel getChoicePanel() {
    return choicePanel;
  }

  public void setChoicePanel(ChoicePanel choicePanel) {
    this.choicePanel = choicePanel;
  }
}

@@ -65,16 +65,164 @@
 dgets;%0A%0A
+import com.google.gwt.event.dom.client.ChangeEvent;%0Aimport com.google.gwt.event.dom.client.ChangeHandler;%0Aimport com.google.gwt.user.client.Window;%0A
 import c
@@ -850,16 +850,140 @@
 gBox %7B%0A%0A
+  private static final String DEFAULT_CASE = %22default%22;%0A  private static final String ADD_NEW_CASE = %22%3Cadd a new case%3E%22;%0A  %0A
   privat
@@ -2581,24 +2581,75 @@
    %7D%0A    %7D%0A%0A
+    String%5B%5D cases = %7BDEFAULT_CASE, ADD_NEW_CASE%7D;%0A
     casePane
@@ -2674,32 +2674,13 @@
 nel(
-new String%5B%5D %7B%22default%22%7D
+cases
 );%0A 
@@ -2722,16 +2722,87 @@
 arent();
+%0A    casePanel.getDroplist().addChangeHandler(new AddNewCaseHandler());
 %0A%0A    na
@@ -4667,11 +4667,427 @@
 nel;%0A  %7D
+%0A%0A  /**%0A   * %0A   */%0A  public class AddNewCaseHandler implements ChangeHandler %7B%0A%0A    @Override%0A    public void onChange(ChangeEvent event) %7B%0A%0A      Integer selectedIndex = casePanel.getDroplist().getSelectedIndex();%0A      String selectedItem = casePanel.getDroplist().getItemText(selectedIndex);%0A%0A      // Woo-hoo!%0A      if (selectedItem.equals(ADD_NEW_CASE)) %7B%0A        Window.alert(selectedItem);%0A      %7D%0A%0A    %7D%0A  %7D
 %0A%7D%0A

Add handler for adding new case to SaveDialogBox