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;
  
  /**
   * 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.
    urlHTML =
        "http://csdms.colorado.edu/wmt/components/format/" + this.componentId
            + "?defaults=True";
    HTML viewHTML = new HTML("<a href='" + urlHTML + "'>HTML</a>");
    HTML viewText = new HTML("text"); // TODO Add link.
    HTML viewJSON = new HTML("JSON"); // TODO Add link.

    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);

    /*
     * Intercepts the click on the link in the viewHTML cell and directs it
     * to open in another tab/window.
     */
    viewHTML.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        event.preventDefault();
        Window.open(urlHTML, "viewInputFiles", null);
      }
    });
  }

}

@@ -681,16 +681,66 @@
 HTML;%0A  
+private String urlText;%0A  private String urlJSON;%0A
 %0A  /**%0A 
@@ -815,17 +815,16 @@
 nt state
- 
 %0A   * of
@@ -1168,25 +1168,24 @@
     
-urlHTML =%0A       
+String baseURL =
  %22ht
@@ -1231,16 +1231,185 @@
 format/%22
+;%0A    urlHTML = baseURL + this.componentId + %22?defaults=True&format=html%22;%0A    urlText = baseURL + this.componentId + %22?defaults=True&format=text%22;%0A    urlJSON = baseURL
  + this.
@@ -1423,20 +1423,8 @@
 ntId
-%0A           
  + %22
@@ -1437,16 +1437,28 @@
 lts=True
+&format=json
 %22;%0A    H
@@ -1554,89 +1554,113 @@
 ML(%22
-text%22); // TODO Add link.%0A    HTML viewJSON = new HTML(%22JSON%22); // TODO Add link.
+%3Ca href='%22 + urlHTML + %22'%3Etext%3C/a%3E%22);%0A    HTML viewJSON = new HTML(%22%3Ca href='%22 + urlHTML + %22'%3EJSON%3C/a%3E%22);
 %0A%0A  
@@ -2106,13 +2106,213 @@
     
-/*%0A  
+viewHTML.addClickHandler(new InterceptClickHandler(urlHTML));%0A    viewText.addClickHandler(new InterceptClickHandler(urlText));%0A    viewJSON.addClickHandler(new InterceptClickHandler(urlJSON));%0A  %7D%0A%0A  /**%0A
    *
@@ -2352,24 +2352,34 @@
  in the 
-view
 HTML
+, text or JSON
  cell an
@@ -2379,16 +2379,21 @@
 cell and
+%0A   *
  directs
@@ -2395,23 +2395,16 @@
 rects it
-%0A     *
  to open
@@ -2434,65 +2434,176 @@
 %0A   
-  
 */%0A  
-  viewHTML.addClickHandler(new ClickHandler() %7B%0A  
+public class InterceptClickHandler implements ClickHandler %7B%0A%0A    private String url;%0A%0A    public InterceptClickHandler(String url) %7B%0A      this.url = url;%0A    %7D%0A%0A
     
@@ -2616,18 +2616,16 @@
 ide%0A    
-  
 public v
@@ -2662,18 +2662,16 @@
 %7B%0A      
-  
 event.pr
@@ -2686,18 +2686,16 @@
 ault();%0A
-  
       Wi
@@ -2707,20 +2707,16 @@
 open(url
-HTML
 , %22viewI
@@ -2742,23 +2742,12 @@
     
- 
- 
 %7D%0A
-  
   %7D
-);%0A  %7D%0A
 %0A%7D%0A

Implement new component/format links