package edu.colorado.csdms.wmt.client.ui;

import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;

/**
 * Supplies a Grid with links for viewing or downloading the input
 * configuration files generated by the parameters set for a component in a
 * model.
 * 
 * @author Mark Piper (mark.piper@colorado.edu)
 */
public class ViewInputFilesPanel extends Grid {

  private String componentId;
  private String urlHTML;
  private String urlText;
  private String urlJSON;

  /**
   * Creates a new ViewInputFilesPanel with links built for the current state
   * of a model component parameter.
   * 
   * @param componentId the id of the model component, a String
   */
  public ViewInputFilesPanel(String componentId) {

    super(1, 6);
    this.componentId = componentId;
    this.getElement().getStyle().setMarginTop(2, Unit.EM);
    this.setCellPadding(5); // px

    // TODO Should use DataURL.
    String baseURL = "http://csdms.colorado.edu/wmt/components/format/";
    urlHTML = baseURL + this.componentId + "?defaults=True&format=html";
    urlText = baseURL + this.componentId + "?defaults=True&format=text";
    urlJSON = baseURL + this.componentId + "?defaults=True&format=json";
    HTML viewHTML = new HTML("<a href='" + urlHTML + "'>HTML</a>");
    HTML viewText = new HTML("<a href='" + urlHTML + "'>text</a>");
    HTML viewJSON = new HTML("<a href='" + urlHTML + "'>JSON</a>");

    viewHTML.getElement().getStyle().setCursor(Cursor.POINTER);
    viewText.getElement().getStyle().setCursor(Cursor.POINTER);
    viewJSON.getElement().getStyle().setCursor(Cursor.POINTER);

    this.setWidget(0, 0, new HTML("View input files:"));
    this.setWidget(0, 1, viewHTML);
    this.setWidget(0, 2, new HTML("|"));
    this.setWidget(0, 3, viewText);
    this.setWidget(0, 4, new HTML("|"));
    this.setWidget(0, 5, viewJSON);

    viewHTML.addClickHandler(new InterceptClickHandler(urlHTML));
    viewText.addClickHandler(new InterceptClickHandler(urlText));
    viewJSON.addClickHandler(new InterceptClickHandler(urlJSON));
  }

  /**
   * Intercepts the click on the link in the HTML, text or JSON cell and
   * directs it to open in another tab/window.
   */
  public class InterceptClickHandler implements ClickHandler {

    private String url;

    public InterceptClickHandler(String url) {
      this.url = url;
    }

    @Override
    public void onClick(ClickEvent event) {
      event.preventDefault();
      Window.open(url, "viewInputFiles", null);
    }
  }
}

@@ -1858,28 +1858,24 @@
     
-this.setWidget(0, 0,
+HTML viewTitle =
  new
@@ -1900,16 +1900,164 @@
 files:%22)
+;%0A    viewTitle.setTitle(%22View the model configuration files generated by %22%0A        + %22these parameter values%22);%0A%0A    this.setWidget(0, 0, viewTitle
 );%0A    t

Add tooltip to the "View Input Files" text in the ParameterTable