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

import java.util.Iterator;

import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

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

/**
 * Encapsulates a scrollable list of labels used to tag and classify models.
 * This menu is modeled on the "Labels" menu in Gmail.
 * 
 * @author Mark Piper (mark.piper@colorado.edu)
 */
public class LabelsMenu extends PopupPanel {

  private static final String MENU_WIDTH = "200px"; // arbitrary, aesthetic
  
  @SuppressWarnings("unused")
  private DataManager data;
  
  /**
   * Makes a new {@link LabelsMenu}.
   * 
   * @param data the DataManager object for the WMT session
   */
  public LabelsMenu(DataManager data) {

    super(true); // autohide
    this.getElement().getStyle().setCursor(Cursor.POINTER); // use pointer
    this.data = data;
    this.setStyleName("wmt-PopupPanel");

    // A VerticalPanel for the menu items. (PopupPanels have only one child.)
    VerticalPanel menu = new VerticalPanel();
    this.add(menu);
    
    // All labels are listed on the labelPanel, which sits on a ScrollPanel.
    VerticalPanel labelPanel = new VerticalPanel();
    ScrollPanel scroller = new ScrollPanel(labelPanel);
    scroller.setWidth(MENU_WIDTH);
    menu.add(scroller);
    
    // XXX Ten temporary labels.
    CheckBox label0 = new CheckBox("low avulsion");
    label0.setValue(true);
    labelPanel.add(label0);
    CheckBox label9 = new CheckBox("high waves");
    labelPanel.add(label9);
    CheckBox label1 = new CheckBox("Ganges");
    labelPanel.add(label1);
    CheckBox label2 = new CheckBox("thesis");
    labelPanel.add(label2);
    CheckBox label3 = new CheckBox("AGU talk");
    labelPanel.add(label3);
    CheckBox label4 = new CheckBox("2013");
    labelPanel.add(label4);
    CheckBox label5 = new CheckBox("Nature paper");
    label5.setValue(true);
    labelPanel.add(label5);
    CheckBox label6 = new CheckBox("CEM");
    label6.setValue(true);
    labelPanel.add(label6);
    CheckBox label7 = new CheckBox("What happens if someone makes a really long label?");
    labelPanel.add(label7);
    CheckBox label8 = new CheckBox("HydroTrend");
    labelPanel.add(label8);
    
    // Apply a style to each label.
    Iterator<Widget> iter = labelPanel.iterator();
    while (iter.hasNext()) {
      CheckBox button = (CheckBox) iter.next();
      button.setWordWrap(false);
      button.setStyleName("wmt-PopupPanelCheckBoxItem");
    }
    
    // These items are always visible on the bottom of the menu.
    HTML separator = new HTML("");
    separator.setStyleName("wmt-PopupPanelSeparator");
    HTML addNewHtml = new HTML("Add new label");
    addNewHtml.setStyleName("wmt-PopupPanelItem");
    HTML manageHtml = new HTML("Manage labels");
    manageHtml.setStyleName("wmt-PopupPanelItem");
    menu.add(separator);
    menu.add(addNewHtml);
    menu.add(manageHtml);
  }
}

@@ -74,24 +74,19 @@
 va.util.
-Iterator
+Map
 ;%0A%0Aimpor
@@ -371,53 +371,8 @@
 nel;
-%0Aimport com.google.gwt.user.client.ui.Widget;
 %0A%0Aim
@@ -751,62 +751,68 @@
  %0A  
-@SuppressWarnings(%22unused%22)%0A  private DataManager data
+private DataManager data;%0A  private VerticalPanel labelPanel
 ;%0A  
@@ -1366,30 +1366,16 @@
 el.%0A    
-VerticalPanel 
 labelPan
@@ -1515,28 +1515,24 @@
 oller);%0A
-    
 %0A    // 
 XXX Ten 
@@ -1527,1404 +1527,845 @@
  // 
-XXX Ten temporary labels.%0A    CheckBox label0 = new CheckBox(%22low avulsion%22);%0A    label0.setValue(true);%0A    labelPanel.add(label0);%0A    CheckBox label9 = new CheckBox(%22high waves%22);%0A    labelPanel.add(label9
+Populate the menu with the stored model labels and their values.%0A    populateMenu(
 );%0A
+%0A
     
-CheckBox label1 = new CheckBox(%22Ganges%22);%0A    labelPanel.add(label1);%0A    CheckBox label2 = new CheckBox(%22thesis%22);%0A    labelPanel.add(label2);%0A    CheckBox label3 = new CheckBox(%22AGU talk%22);%0A    labelPanel.add(label3);%0A    CheckBox label4 = new CheckBox(%222013%22);%0A    labelPanel.add(label4);%0A    CheckBox label5 = new CheckBox(%22Nature paper%22);%0A    label5.setValue(true);%0A    labelPanel.add(label5);%0A    CheckBox label6 = new CheckBox(%22CEM%22);%0A    label6.setValue(true);%0A    labelPanel.add(label6);%0A    CheckBox label7 = new CheckBox(%22What happens if someone makes a really long label?%22);%0A    labelPanel.add(label7);%0A    CheckBox label8 = new CheckBox(%22HydroTrend%22);%0A    labelPanel.add(label8
+// These items are always visible on the bottom of the menu.%0A    HTML separator = new HTML(%22%22);%0A    separator.setStyleName(%22wmt-PopupPanelSeparator%22);%0A    HTML addNewHtml = new HTML(%22Add new label%22);%0A    addNewHtml.setStyleName(%22wmt-PopupPanelItem%22);%0A    HTML deleteHtml = new HTML(%22Delete label%22);%0A    deleteHtml.setStyleName(%22wmt-PopupPanelItem%22);%0A    menu.add(separator);%0A    menu.add(addNewHtml);%0A    menu.add(deleteHtml
 );%0A  
+%7D%0A
   %0A  
-  // Apply a style to eac
+/**%0A   * A helper that loads the menu wit
 h label
+s
 .%0A   
- Iterator%3CWidget%3E iter = labelPanel.iterator();%0A    while (iter.hasNext()) %7B%0A      CheckBox button = (CheckBox) iter.next();%0A      button.setWordWrap(false);%0A      button.setStyleName(%22wmt-PopupPanelCheckBoxItem%22);%0A    %7D%0A    %0A    // These items are always visible on the bottom of the menu.%0A    HTML separator = new HTML(%22%22);%0A    separator.setStyleName(%22wmt-PopupPanelSeparator%22);%0A    HTML addNewHtml = new HTML(%22Add new label%22);%0A    addNewHtml
+*/%0A  public void populateMenu() %7B%0A    for (Map.Entry%3CString, Boolean%3E entry : data.modelLabels.entrySet()) %7B%0A      CheckBox labelBox = new CheckBox(entry.getKey());%0A      labelBox.setValue(entry.getValue());%0A      labelBox.setWordWrap(false);%0A      labelBox
 .set
@@ -2381,32 +2381,40 @@
 (%22wmt-PopupPanel
+CheckBox
 Item%22);%0A    HTML
@@ -2413,180 +2413,41 @@
     
-HTML manageHtml = new HTML(%22Manage labels%22);%0A    manageHtml.setStyleName(%22wmt-PopupPanelItem%22);%0A    menu.add(separator);%0A    menu.add(addNewHtml);%0A    menu.add(manageHtml);
+  labelPanel.add(labelBox);%0A    %7D
 %0A  %7D

Add worker method to LabelsMenu to populate menu