View Javadoc

1   package org.kuali.student.lum.lu.ui.main.client.configuration;
2   
3   import org.kuali.student.common.ui.client.mvc.Callback;
4   import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
5   import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
6   import org.kuali.student.common.ui.client.widgets.search.KSPicker;
7   import org.kuali.student.common.ui.client.widgets.suggestbox.KSSuggestBox;
8   import org.kuali.student.r1.common.assembly.data.Data.Value;
9   import org.kuali.student.r1.common.assembly.data.Metadata;
10  
11  import com.google.gwt.event.dom.client.ChangeEvent;
12  import com.google.gwt.event.dom.client.ChangeHandler;
13  import com.google.gwt.user.client.ui.Composite;
14  import com.google.gwt.user.client.ui.HTML;
15  import com.google.gwt.user.client.ui.HorizontalPanel;
16  import com.google.gwt.user.client.ui.Label;
17  import com.google.gwt.user.client.ui.ListBox;
18  import com.google.gwt.user.client.ui.SimplePanel;
19  import com.google.gwt.user.client.ui.VerticalPanel;
20  import com.google.gwt.user.client.ui.Widget;
21  
22  public class CopyCourseSearchPanel extends Composite {
23  	Callback<Boolean> validationCallback;
24  	private String currentSelection = null;
25  	private HTML validationErrorLabel = null;
26  	private VerticalPanel validationPanel = null;
27  	private KSPicker[] pickers = null;
28  	
29  	protected CopyCourseSearchPanel(Metadata searchMetadata, final Callback<Boolean> validationCallback, String label, String invalidErrorLabel, String[] dropDownLabels, String[] searchIds) {
30  		
31  		if(dropDownLabels==null || searchIds==null || dropDownLabels.length != searchIds.length || searchIds.length<1){
32  			throw new RuntimeException("Invalid Parameters");
33  		}
34  		
35  		this.validationCallback = validationCallback;
36  		
37          final VerticalPanel copyCourseSearchPanel = new VerticalPanel();
38  
39          
40          Label copyToCourseLabel = new Label(label);
41  
42          copyToCourseLabel.addStyleName("bold");
43          
44          validationErrorLabel = new HTML("<UL><LI>"+invalidErrorLabel+"</LI></UL>");
45          validationErrorLabel.addStyleName("ks-form-module-validation-errors");
46          validationErrorLabel.addStyleName("ks-form-module-validation-inline");
47  
48          validationErrorLabel.setVisible(false);
49          
50          validationPanel = new VerticalPanel();
51          
52          copyCourseSearchPanel.add(copyToCourseLabel);
53          copyCourseSearchPanel.add(validationPanel);
54          
55          HorizontalPanel inputPanel = new HorizontalPanel();
56          inputPanel.addStyleName("ks-form-module-elements");
57          inputPanel.addStyleName("ks-form-module-single-line-margin");
58          final ListBox courseSelectionMethodDropdown = new ListBox();
59  
60          for(int i=0;i<searchIds.length;i++){
61              courseSelectionMethodDropdown.addItem(dropDownLabels[i], String.valueOf(i));
62          }
63          inputPanel.add(courseSelectionMethodDropdown);
64          
65          pickers = new KSPicker[searchIds.length];
66          for(int i=0;i<searchIds.length;i++){
67          	Metadata metadata = searchMetadata.getProperties().get(searchIds[i]);
68          	KSPicker picker = new KSPicker(metadata.getInitialLookup(), null);
69          	picker.addValueChangeCallback(new Callback<Value>(){
70      			public void exec(Value result) {
71      				currentSelection = null;
72      				clearValidation();
73      				validationCallback.exec(false);
74      			}
75              });
76          	picker.addSelectionChangeHandler(new SelectionChangeHandler() {
77      			public void onSelectionChange(SelectionChangeEvent event) {
78      				Widget w = event.getWidget();
79      				currentSelection = ((KSSuggestBox)w).getValue();
80      				if(currentSelection!=null){
81      					validationCallback.exec(true);
82      				}
83      			}
84      		});
85          	picker.addFocusLostCallback(new Callback<Boolean>() {
86      			@Override
87      			public void exec(Boolean result) {
88      				if(currentSelection==null){
89      					addError();
90      					validationCallback.exec(false);
91      				}
92      			}
93      		});
94          	pickers[i] = picker;
95          }
96          
97          final SimplePanel coursePickerHolder = new SimplePanel();
98          coursePickerHolder.setWidget(pickers[0]);
99          coursePickerHolder.addStyleName("KS-indented");
100         inputPanel.add(coursePickerHolder);
101         
102         validationPanel.add(inputPanel);
103         validationPanel.add(validationErrorLabel);
104         
105         copyCourseSearchPanel.add(validationPanel);
106         
107         copyCourseSearchPanel.setVisible(false);
108         
109         //Handle the switching of pickers by the drop down
110         courseSelectionMethodDropdown.addChangeHandler(new ChangeHandler(){
111 			public void onChange(ChangeEvent event) {
112 				clear();
113 				coursePickerHolder.setWidget(pickers[courseSelectionMethodDropdown.getSelectedIndex()]);
114 			}
115         });
116 
117         copyCourseSearchPanel.addStyleName("KS-indented");
118         initWidget(copyCourseSearchPanel);
119 	}
120 	
121 	public void clearValidation(){
122 		validationErrorLabel.setVisible(false);
123 		validationPanel.removeStyleName("error");
124 	}
125 	
126 	public void addError(){
127 		validationErrorLabel.setVisible(true);
128 		validationPanel.addStyleName("error");
129 		
130 	}
131 
132 	public void clear() {
133 		for(KSPicker picker:pickers){
134 			picker.clear();
135 		}
136 		clearValidation();
137 		currentSelection = null;
138 	}
139 	
140 	public String getValue(){
141 		return currentSelection;
142 	}
143 }