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

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.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
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.data.ComponentJSO;
import edu.colorado.csdms.wmt.client.ui.handler.DialogCancelHandler;

/**
 * A dialog box used to display metadata (id, url, author, etc.) about a WMT
 * component.
 * 
 * @author Mark Piper (mark.piper@colorado.edu)
 */
public class ComponentInfoDialogBox extends DialogBox {

  private static String[] LABELS = {
      "id", "summary", "url", "author", "email", "version", "license", "doi"};
  private Grid grid;
  private ClosePanel closePanel;

  /**
   * Creates an {@link ComponentInfoDialogBox}. It must be populated later by
   * calling {@link ComponentInfoDialogBox#update()}.
   */
  public ComponentInfoDialogBox() {

    this.setAutoHideEnabled(false);
    this.setModal(false);

    grid = new Grid(LABELS.length, 2);
    grid.setCellPadding(5); // px

    for (int i = 0; i < LABELS.length; i++) {
      Label label = new Label(LABELS[i] + ":");
      label.getElement().getStyle().setMarginLeft(2, Unit.EM);
      grid.setWidget(i, 0, label);
      grid.getCellFormatter().setHorizontalAlignment(i, 0,
          HasHorizontalAlignment.ALIGN_RIGHT);
    }

    VerticalPanel contents = new VerticalPanel();
    contents.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    contents.add(grid);
    contents.setWidth("40em");

    closePanel = new ClosePanel();
    contents.add(closePanel);

    this.setWidget(contents);

    /*
     * Hides the dialog box.
     */
    closePanel.getButton().addClickHandler(new DialogCancelHandler(this));
  }

  /**
   * Updates the {@link ComponentInfoDialogBox} with information from the
   * input component.
   * 
   * @param componentJso a {@link ComponentJSO} instance
   */
  public void update(final ComponentJSO componentJso) {

    this.setText(componentJso.getName());

    String url =
        "<a href='" + componentJso.getURL() + "'>" + componentJso.getURL()
            + "</a>";
    String[] info =
        {
            componentJso.getId(), componentJso.getSummary(), url,
            componentJso.getAuthor(), componentJso.getEmail(),
            componentJso.getVersion(), componentJso.getLicense(),
            componentJso.getDoi()};

    HTML urlHtml = null;
    for (int i = 0; i < LABELS.length; i++) {
      if (url.equals(info[i])) {
        urlHtml = new HTML(info[i]);
        grid.setWidget(i, 1, urlHtml);
      } else {
        grid.setWidget(i, 1, new HTML(info[i]));
      }
    }

    /*
     *  Intercepts the click on the component URL and opens it in a new tab.
     */
    urlHtml.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        event.preventDefault();
        Window.open(componentJso.getURL(), "componentInfoDialog", null);
      }
    });
  }
}

@@ -922,21 +922,8 @@
  = %7B
-%0A      %22id%22, 
 %22sum
@@ -948,46 +948,8 @@
 hor%22
-, %22email%22, %22version%22, %22license%22, %22doi%22
 %7D;%0A 
@@ -2260,21 +2260,23 @@
     
-this.setText(
+String title = 
 comp
@@ -2297,221 +2297,250 @@
 me()
-);%0A
+;
 %0A    
-String url =%0A        %22%3Ca href='%22 + componentJso.getURL() + %22'%3E%22 + componentJso.getURL()%0A            + %22%3C/a%3E%22;%0A    String%5B%5D info =%0A        %7B%0A            componentJso.getId(), componentJso.getSummary(),
+if (componentJso.getVersion() != null) %7B%0A      title += %22 v%22 + componentJso.getVersion();%0A    %7D%0A    if (componentJso.getDoi() != null) %7B%0A      title += %22 (%22 + componentJso.getDoi() + %22)%22;%0A    %7D%0A    this.setText(title);%0A%0A    String
  url
-,
+ =
 %0A   
@@ -2536,35 +2536,45 @@
 g url =%0A        
-   
+%22%3Ca href='%22 +
  componentJso.ge
@@ -2578,17 +2578,22 @@
 .get
-Author(),
+URL() + %22'%3E%22 +
  com
@@ -2609,16 +2609,13 @@
 .get
-Email
+URL
 ()
-,
 %0A   
@@ -2627,73 +2627,70 @@
     
-componentJso.getVersion(), componentJso.getLicense(),%0A           
++ %22%3C/a%3E%22;%0A    String%5B%5D info = %7BcomponentJso.getSummary(), url,
  com
@@ -2702,19 +2702,22 @@
 tJso.get
-Doi
+Author
 ()%7D;%0A%0A  

Modify display of info in ComponentInfoDialogBox