View Javadoc
1   package org.kuali.student.lum.common.client.widgets;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import com.google.gwt.user.client.ui.ListBox;
8   
9   /**
10   * Dropdown list components. Extends ListBox to add useful methods and support for underlying model.
11   * Each item in dropdown list is associated with corresponding model item.
12   *
13   * @author Igor
14   */
15  public class DropdownList extends ListBox {
16  
17      /**
18       * Components model.
19       */
20      private Map<String, Integer> model = new HashMap<String, Integer>();
21  
22      /**
23       * Selected index.
24       */
25      private Integer index = 0;
26  
27      public DropdownList() {
28      }
29  
30      public DropdownList(List<String> list) {
31          for (String s : list) {
32              addItem(s);
33          }
34      }
35  
36      @Override
37      public void addItem(String value) {
38          model.put(value, index++);
39          super.addItem(value);
40      }
41  
42      public void setSelectedItem(String value) {
43          if (model.get(value) == null) {
44              setSelectedIndex(0);
45          } else {
46              setSelectedIndex(model.get(value));
47          }
48      }
49  
50      public String getSelectedValue() {
51          return getValue(getSelectedIndex());
52      }
53  
54      public void setList(List<String> relationship) {
55          for (String s : relationship) {
56              addItem(s);
57          }
58      }
59  
60      public void reset() {
61          setSelectedIndex(0);
62      }
63  
64      public void clear() {
65          super.clear();
66          model.clear();
67          index = 0;
68      }
69  
70      public void removeItem(String value) {
71          if (null != model.get(value)) {
72              removeItem(model.get(value));
73          }
74      }
75  }