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