View Javadoc

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  public class ListOfStringWidget extends Composite implements HasBlurHandlers{
24      private String addItemText;
25  	
26  	private boolean loaded = false;
27  	private FlowPanel mainPanel = new FlowPanel();
28  	private FlowPanel itemsPanel = new FlowPanel();
29  	private ArrayList<String> values = new ArrayList<String>();
30  	final KSTextBox inputText = new KSTextBox();
31  	private FieldDescriptor fd;
32  	private DataModelValidator validator = new DataModelValidator();
33  	public ListOfStringWidget(String addItemText) {
34  		super();
35  		super.initWidget(mainPanel);
36  		this.addItemText = addItemText;
37  	}
38  
39  	public void onLoad() {
40          if (!loaded) {            
41          	KSButton addItemButton = new KSButton(addItemText);
42              addItemButton.addClickHandler(new ClickHandler(){
43  				public void onClick(ClickEvent event) {
44  					//add validate
45  					List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
46  					validator.doValidateString(inputText.getText(),QueryPath.parse(fd.getFieldKey()), fd.getMetadata(), results);
47  					if(results.isEmpty()){
48  						addListItem(inputText.getText());
49  						inputText.setText("");
50  					}else if(fd != null && fd.getFieldElement() != null){
51  						for(ValidationResultInfo vr:results){
52  							fd.getFieldElement().processValidationResult(vr);
53  						}
54  					}
55  				}
56              });
57              mainPanel.add(inputText);
58              mainPanel.add(addItemButton);
59              mainPanel.add(itemsPanel);            
60              loaded = true;
61          }
62      }
63  	
64  	public ArrayList<String> getStringValues(){
65  		return values;
66  	}
67  	
68  	public void setStringValues(ArrayList<String> values){
69  		itemsPanel.clear();
70  		if(values!=null){
71  			for(String value:values){
72  				addListItem(value);
73  			}
74  		}
75  	}
76  	
77  	protected void addListItem(String itemValue){
78  		final FlowPanel item = new FlowPanel();
79  		final String curVal = itemValue;
80  		if (!values.contains(itemValue)) {
81  	        
82  			String fieldHTMLId = HTMLPanel.createUniqueId();
83  	        LabelPanel fieldTitle = new LabelPanel(itemValue, fieldHTMLId);
84  	        
85  	    	AbbrButton delButton = new AbbrButton(AbbrButtonType.DELETE);
86  	    	delButton.addClickHandler(new ClickHandler() {
87  				public void onClick(ClickEvent event) {
88  					itemsPanel.remove(item);
89  					values.remove(curVal);
90  				}
91  			});
92  	        fieldTitle.add(delButton);
93  	        item.add(fieldTitle);
94  			itemsPanel.add(item);
95  			values.add(curVal);
96  		}
97  	}
98  
99  	@Override
100 	public HandlerRegistration addBlurHandler(BlurHandler handler) {
101 		return inputText.addBlurHandler(handler);
102 	}
103 
104 	public void setFd(FieldDescriptor fd) {
105 		this.fd = fd;
106 	}
107 }