| 1 |  |  package org.kuali.student.common.ui.client.widgets; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.ArrayList; | 
  | 4 |  |  import java.util.List; | 
  | 5 |  |   | 
  | 6 |  |  import org.kuali.student.common.assembly.data.QueryPath; | 
  | 7 |  |  import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; | 
  | 8 |  |  import org.kuali.student.common.ui.client.validator.DataModelValidator; | 
  | 9 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; | 
  | 10 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; | 
  | 11 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.LabelPanel; | 
  | 12 |  |  import org.kuali.student.common.validation.dto.ValidationResultInfo; | 
  | 13 |  |   | 
  | 14 |  |  import com.google.gwt.event.dom.client.BlurHandler; | 
  | 15 |  |  import com.google.gwt.event.dom.client.ClickEvent; | 
  | 16 |  |  import com.google.gwt.event.dom.client.ClickHandler; | 
  | 17 |  |  import com.google.gwt.event.dom.client.HasBlurHandlers; | 
  | 18 |  |  import com.google.gwt.event.shared.HandlerRegistration; | 
  | 19 |  |  import com.google.gwt.user.client.ui.Composite; | 
  | 20 |  |  import com.google.gwt.user.client.ui.FlowPanel; | 
  | 21 |  |  import com.google.gwt.user.client.ui.HTMLPanel; | 
  | 22 |  |   | 
  | 23 | 0 |  public class ListOfStringWidget extends Composite implements HasBlurHandlers{ | 
  | 24 |  |      private String addItemText; | 
  | 25 |  |           | 
  | 26 | 0 |          private boolean loaded = false; | 
  | 27 | 0 |          private FlowPanel mainPanel = new FlowPanel(); | 
  | 28 | 0 |          private FlowPanel itemsPanel = new FlowPanel(); | 
  | 29 | 0 |          private ArrayList<String> values = new ArrayList<String>(); | 
  | 30 | 0 |          final KSTextBox inputText = new KSTextBox(); | 
  | 31 |  |          private FieldDescriptor fd; | 
  | 32 | 0 |          private DataModelValidator validator = new DataModelValidator(); | 
  | 33 |  |          public ListOfStringWidget(String addItemText) { | 
  | 34 | 0 |                  super(); | 
  | 35 | 0 |                  super.initWidget(mainPanel); | 
  | 36 | 0 |                  this.addItemText = addItemText; | 
  | 37 | 0 |          } | 
  | 38 |  |   | 
  | 39 |  |          public void onLoad() { | 
  | 40 | 0 |          if (!loaded) {             | 
  | 41 | 0 |                  KSButton addItemButton = new KSButton(addItemText); | 
  | 42 | 0 |              addItemButton.addClickHandler(new ClickHandler(){ | 
  | 43 |  |                                  public void onClick(ClickEvent event) { | 
  | 44 |  |                                           | 
  | 45 | 0 |                                          List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>(); | 
  | 46 | 0 |                                          validator.doValidateString(inputText.getText(),QueryPath.parse(fd.getFieldKey()), fd.getMetadata(), results); | 
  | 47 | 0 |                                          if(results.isEmpty()){ | 
  | 48 | 0 |                                                  addListItem(inputText.getText()); | 
  | 49 | 0 |                                                  inputText.setText(""); | 
  | 50 | 0 |                                          }else if(fd != null && fd.getFieldElement() != null){ | 
  | 51 | 0 |                                                  for(ValidationResultInfo vr:results){ | 
  | 52 | 0 |                                                          fd.getFieldElement().processValidationResult(vr); | 
  | 53 |  |                                                  } | 
  | 54 |  |                                          } | 
  | 55 | 0 |                                  } | 
  | 56 |  |              }); | 
  | 57 | 0 |              mainPanel.add(inputText); | 
  | 58 | 0 |              mainPanel.add(addItemButton); | 
  | 59 | 0 |              mainPanel.add(itemsPanel);             | 
  | 60 | 0 |              loaded = true; | 
  | 61 |  |          } | 
  | 62 | 0 |      } | 
  | 63 |  |           | 
  | 64 |  |          public ArrayList<String> getStringValues(){ | 
  | 65 | 0 |                  return values; | 
  | 66 |  |          } | 
  | 67 |  |           | 
  | 68 |  |          public void setStringValues(ArrayList<String> values){ | 
  | 69 | 0 |                  itemsPanel.clear(); | 
  | 70 | 0 |                  if(values!=null){ | 
  | 71 | 0 |                          for(String value:values){ | 
  | 72 | 0 |                                  addListItem(value); | 
  | 73 |  |                          } | 
  | 74 |  |                  } | 
  | 75 | 0 |          } | 
  | 76 |  |           | 
  | 77 |  |          protected void addListItem(String itemValue){ | 
  | 78 | 0 |                  final FlowPanel item = new FlowPanel(); | 
  | 79 | 0 |                  final String curVal = itemValue; | 
  | 80 | 0 |                  if (!values.contains(itemValue)) { | 
  | 81 |  |                   | 
  | 82 | 0 |                          String fieldHTMLId = HTMLPanel.createUniqueId(); | 
  | 83 | 0 |                  LabelPanel fieldTitle = new LabelPanel(itemValue, fieldHTMLId); | 
  | 84 |  |                   | 
  | 85 | 0 |                      AbbrButton delButton = new AbbrButton(AbbrButtonType.DELETE); | 
  | 86 | 0 |                      delButton.addClickHandler(new ClickHandler() { | 
  | 87 |  |                                  public void onClick(ClickEvent event) { | 
  | 88 | 0 |                                          itemsPanel.remove(item); | 
  | 89 | 0 |                                          values.remove(curVal); | 
  | 90 | 0 |                                  } | 
  | 91 |  |                          }); | 
  | 92 | 0 |                  fieldTitle.add(delButton); | 
  | 93 | 0 |                  item.add(fieldTitle); | 
  | 94 | 0 |                          itemsPanel.add(item); | 
  | 95 | 0 |                          values.add(curVal); | 
  | 96 |  |                  } | 
  | 97 | 0 |          } | 
  | 98 |  |   | 
  | 99 |  |          @Override | 
  | 100 |  |          public HandlerRegistration addBlurHandler(BlurHandler handler) { | 
  | 101 | 0 |                  return inputText.addBlurHandler(handler); | 
  | 102 |  |          } | 
  | 103 |  |   | 
  | 104 |  |          public void setFd(FieldDescriptor fd) { | 
  | 105 | 0 |                  this.fd = fd; | 
  | 106 | 0 |          } | 
  | 107 |  |  } |