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

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;

import edu.colorado.csdms.wmt.client.data.ParameterJSO;

/**
 * Builds a table of parameters for a single WMT component. The value of the
 * parameter is editable.
 * 
 * @author Mark Piper (mark.piper@colorado.edu)
 */
public class ParameterTable extends FlexTable {

  private DataManager data;
  private String componentId; // the id of the displayed component

  /**
   * Initializes a table of parameters for a single WMT component. The table is
   * empty until the loadTable method is called.
   * 
   * @param data the DataManager instance for the WMT session
   */
  public ParameterTable(DataManager data) {

    this.data = data;
    this.data.setParameterTable(this);
    this.setWidth("100%");

    if (this.data.getSelectedComponent() != null) {
      loadTable();
    }
  }

  /**
   * A worker that loads the ParameterTable with parameter values for the
   * selected component.
   */
  public void loadTable() {

    // The component whose parameters are to be displayed.
    setComponentId(data.getSelectedComponent());

    // Return if the selected component doesn't have parameters.
    if (data.getComponent(componentId).getParameters() == null) {
      clearTable();
      Window.alert("No parameters defined for this component.");
      return;
    }

    // Set the component name on the viewSouth tab.
    String componentName = data.getComponent(componentId).getName();
    String tabTitle = "Parameters (" + componentName + ")";
    data.getPerspective().getViewEast().setTabText(0, tabTitle);

    // Build the parameter table.
    Integer nParameters =
        data.getComponent(componentId).getParameters().length();
    Integer parameterIndex = 0;
    for (int i = 0; i < nParameters; i++) {
      ParameterJSO parameter =
          data.getComponent(componentId).getParameters().get(i);
      if (parameter.getKey().matches("simulation_name")) {
        continue;
      }
      this.setWidget(parameterIndex, 0, new DescriptionCell(parameter));
      if (parameter.getKey().matches("separator")) {
        this.getFlexCellFormatter().setColSpan(parameterIndex, 0, 2);
        this.getFlexCellFormatter().setStyleName(parameterIndex, 0,
            "wmt-ParameterSeparator");
      } else {
        this.setWidget(parameterIndex, 1, new ValueCell(parameter));
        this.getFlexCellFormatter().setStyleName(parameterIndex, 0,
            "wmt-ParameterDescription");
      }
      parameterIndex++;
    }
  }

  /**
   * Stores the modified value of a parameter of a component in the WMT
   * DataManager.
   * 
   * @param parameter the ParameterJSO object for the parameter being modified
   * @param value the new parameter value, a String
   */
  public void setValue(ParameterJSO parameter, String value) {
    // TODO Massive amounts of checking on value.
    String key = parameter.getKey();
    GWT.log(componentId + ": " + key + ": " + value);
    data.getComponent(componentId).getParameter(key).getValue().setDefault(
        value);
  }

  /**
   * Deletes the contents of the ParameterTable and resets the tab title to
   * "Parameters". Unsets the selectedComponent in the DataManager.
   */
  public void clearTable() {
    data.setSelectedComponent(null); // should also be in ControlCell#delete?
    setComponentId(null);
    data.getPerspective().getViewEast().setTabText(0, "Parameters");
    this.removeAllRows();
    this.clear(true);
  }

  /**
   * Returns the id of the component (a String) whose parameters are displayed
   * in the ParameterTable.
   */
  public String getComponentId() {
    return componentId;
  }

  /**
   * Stores the id of the component (a String) whose parameters are displayed in
   * the ParameterTable.
   * 
   * @param componentId a component id, a String
   */
  public void setComponentId(String componentId) {
    this.componentId = componentId;
  }
}

@@ -289,32 +289,38 @@
 or a single WMT 
+model 
 component. The v
@@ -330,15 +330,15 @@
 e of
+%0A *
  the
-%0A *
  par
@@ -620,16 +620,22 @@
 gle WMT 
+model 
 componen
@@ -640,16 +640,21 @@
 ent. The
+%0A   *
  table i
@@ -654,21 +654,16 @@
 table is
-%0A   *
  empty u
@@ -667,20 +667,24 @@
 y until 
-the 
+%7B@link #
 loadTabl
@@ -688,15 +688,11 @@
 able
- method
+()%7D
  is 
@@ -1075,32 +1075,38 @@
 e%0A   * selected 
+model 
 component.%0A   */
@@ -1194,24 +1194,29 @@
 played.%0A    
+this.
 setComponent
@@ -1322,32 +1322,37 @@
     if (data.get
+Model
 Component(compon
@@ -1391,16 +1391,21 @@
 %7B%0A      
+this.
 clearTab
@@ -1537,21 +1537,38 @@
 the 
-viewSouth tab
+tab holding the ParameterTable
 .%0A  
@@ -1592,32 +1592,37 @@
 tName = data.get
+Model
 Component(compon
@@ -1833,32 +1833,37 @@
         data.get
+Model
 Component(compon
@@ -2012,32 +2012,37 @@
         data.get
+Model
 Component(compon
@@ -2753,16 +2753,22 @@
 er of a 
+model 
 componen
@@ -2784,16 +2784,23 @@
 WMT%0A   *
+ %7B@link
  DataMan
@@ -2799,24 +2799,25 @@
  DataManager
+%7D
 .%0A   * %0A   *
@@ -3165,16 +3165,21 @@
 data.get
+Model
 Componen
@@ -3221,16 +3221,25 @@
 tValue()
+%0A        
 .setDefa
@@ -3242,25 +3242,16 @@
 Default(
-%0A        
 value);%0A
@@ -3522,16 +3522,21 @@
 te?%0A    
+this.
 setCompo
@@ -3696,32 +3696,38 @@
 s the id of the 
+model 
 component (a Str
@@ -3743,32 +3743,37 @@
 e parameters are
+%0A   *
  displayed%0A   * 
@@ -3766,21 +3766,16 @@
 isplayed
-%0A   *
  in the 
@@ -3888,24 +3888,30 @@
 e id of the 
+model 
 component (a
@@ -3939,16 +3939,21 @@
 ters are
+%0A   *
  display
@@ -3957,21 +3957,16 @@
 layed in
-%0A   *
  the Par

Use model components in parameter table