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