1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.widgets.suggestbox;
17
18 import java.util.Map;
19
20 import org.kuali.student.common.ui.client.mvc.Callback;
21 import org.kuali.student.common.ui.client.mvc.TranslatableValueWidget;
22 import org.kuali.student.common.ui.client.util.UtilConstants;
23 import org.kuali.student.common.ui.client.widgets.HasWatermark;
24 import org.kuali.student.common.ui.client.widgets.KSTextBox;
25 import org.kuali.student.common.ui.client.widgets.list.HasSelectionChangeHandlers;
26 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
27 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
28 import org.kuali.student.common.ui.client.widgets.suggestbox.IdableSuggestOracle.IdableSuggestion;
29
30 import com.google.gwt.event.dom.client.BlurEvent;
31 import com.google.gwt.event.dom.client.BlurHandler;
32 import com.google.gwt.event.logical.shared.SelectionEvent;
33 import com.google.gwt.event.logical.shared.SelectionHandler;
34 import com.google.gwt.event.shared.HandlerRegistration;
35 import com.google.gwt.user.client.ui.SuggestBox;
36 import com.google.gwt.user.client.ui.SuggestOracle;
37
38
39 public class KSSuggestBox extends SuggestBox implements HasSelectionChangeHandlers, TranslatableValueWidget, HasWatermark{
40
41 private IdableSuggestion currentSuggestion = null;
42 private IdableSuggestOracle oracle;
43 private String currentId = "";
44
45 public KSSuggestBox(IdableSuggestOracle oracle) {
46 this(oracle, true);
47 }
48
49 public KSSuggestBox(IdableSuggestOracle oracle, boolean enabled){
50 super(oracle, new KSTextBox());
51 super.getTextBox().setEnabled(enabled);
52 this.oracle = oracle;
53 oracle.addSearchCompletedCallback(new Callback<IdableSuggestion>() {
54 @Override
55 public void exec(IdableSuggestion result) {
56 currentSuggestion = result;
57 currentId = KSSuggestBox.this.getSelectedId();
58 SelectionChangeEvent.fire(KSSuggestBox.this);
59 }
60 });
61 this.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>(){
62
63 @Override
64 public void onSelection(SelectionEvent<SuggestOracle.Suggestion> event) {
65 currentSuggestion = (IdableSuggestion)(event.getSelectedItem());
66 getTextBox().setFocus(true);
67 currentId = KSSuggestBox.this.getSelectedId();
68 SelectionChangeEvent.fire(KSSuggestBox.this);
69 }
70 });
71
72 this.getTextBox().addBlurHandler(new BlurHandler(){
73
74 @Override
75 public void onBlur(BlurEvent event) {
76 String currentText = KSSuggestBox.this.getText();
77 boolean isEmpty = false;
78 if(currentText != null && !currentText.equals("")){
79 if((currentSuggestion != null && !KSSuggestBox.this.getText().equals(currentSuggestion.getReplacementString()))
80 || currentSuggestion == null){
81 currentSuggestion = KSSuggestBox.this.oracle.getSuggestionByText(currentText);
82 }
83
84 if(currentSuggestion == null){
85 currentSuggestion = new IdableSuggestion();
86 String impossibleCharacters = UtilConstants.IMPOSSIBLE_CHARACTERS;
87 currentSuggestion.setId(impossibleCharacters);
88 currentSuggestion.setDisplayString(impossibleCharacters);
89 currentSuggestion.setReplacementString(impossibleCharacters);
90 }
91 }
92 else if(currentText == null || currentText.equals("")){
93 isEmpty = true;
94 currentId = "";
95 currentSuggestion = new IdableSuggestion();
96 currentSuggestion.setId("");
97 currentSuggestion.setDisplayString("");
98 currentSuggestion.setReplacementString("");
99 }
100
101 if(!KSSuggestBox.this.getSelectedId().equals(currentId)){
102 currentId = KSSuggestBox.this.getSelectedId();
103 if(isEmpty){
104 currentId = "";
105 }
106 if (!currentId.equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) {
107 SelectionChangeEvent.fire(KSSuggestBox.this);
108 }
109 }
110 }
111 });
112 }
113
114 public void reset(){
115 this.setText("");
116 currentSuggestion = null;
117 }
118
119 public IdableSuggestion getSelectedSuggestion() {
120 return currentSuggestion;
121 }
122
123 public String getSelectedId() {
124 String id = "";
125 if(currentSuggestion != null){
126 id = currentSuggestion.getId();
127 }
128 if(!currentId.isEmpty() && id.isEmpty()){
129 id = UtilConstants.IMPOSSIBLE_CHARACTERS;
130 }
131 return id;
132 }
133
134 public IdableSuggestOracle getOracle() {
135 return oracle;
136 }
137
138 @Override
139 public String getValue() {
140 return this.getSelectedId();
141 }
142
143 @Override
144 public void setValue(String id) {
145 if(id == null || id.equals("")){
146 currentSuggestion = new IdableSuggestion();
147 currentSuggestion.setId("");
148 currentSuggestion.setDisplayString("");
149 currentSuggestion.setReplacementString("");
150 currentId = KSSuggestBox.this.getSelectedId();
151 KSSuggestBox.this.setText("");
152 }
153 else
154 {
155 oracle.getSuggestionByIdSearch(id, new Callback<IdableSuggestion>(){
156
157 @Override
158 public void exec(IdableSuggestion result) {
159 currentSuggestion = result;
160 KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());
161 currentId = KSSuggestBox.this.getSelectedId();
162 }
163 });
164 }
165 }
166
167 public void setValue(String id, final Callback<IdableSuggestion> callback) {
168 if(id == null || id.equals("")){
169 currentSuggestion = new IdableSuggestion();
170 currentSuggestion.setId("");
171 currentSuggestion.setDisplayString("");
172 currentSuggestion.setReplacementString("");
173 KSSuggestBox.this.setText("");
174 currentId = KSSuggestBox.this.getSelectedId();
175 callback.exec(currentSuggestion);
176 }
177 else
178 {
179 oracle.getSuggestionByIdSearch(id, new Callback<IdableSuggestion>(){
180
181 @Override
182 public void exec(IdableSuggestion result) {
183 currentSuggestion = result;
184 KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());
185 currentId = KSSuggestBox.this.getSelectedId();
186 callback.exec(currentSuggestion);
187 }
188 });
189 }
190 }
191
192 public void setValue(String id, String translation) {
193 currentSuggestion = new IdableSuggestion();
194 currentSuggestion.setId(id);
195 currentSuggestion.setDisplayString(translation);
196 currentSuggestion.setReplacementString(translation);
197 KSSuggestBox.this.setText(translation);
198 currentId = KSSuggestBox.this.getSelectedId();
199 }
200
201 @Override
202 public void setValue(String id, boolean fireEvents) {
203 if(fireEvents == true){
204
205 if(id == null || id.equals("")){
206 currentSuggestion = new IdableSuggestion();
207 currentSuggestion.setId("");
208 currentSuggestion.setDisplayString("");
209 currentSuggestion.setReplacementString("");
210 KSSuggestBox.this.setText("");
211 currentId = KSSuggestBox.this.getSelectedId();
212 }
213 else
214 {
215 oracle.getSuggestionByIdSearch(id, new Callback<IdableSuggestion>(){
216
217 @Override
218 public void exec(IdableSuggestion result) {
219 currentSuggestion = result;
220 KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());
221 if(!KSSuggestBox.this.getSelectedId().equals(currentId)){
222 SelectionChangeEvent.fire(KSSuggestBox.this);
223 currentId = KSSuggestBox.this.getSelectedId();
224 }
225 }
226 });
227 }
228
229 }
230 else
231 {
232 this.setValue(id);
233 }
234
235 }
236
237 public void setValue(IdableSuggestion theSuggestion) {
238 currentSuggestion = theSuggestion;
239 SelectionChangeEvent.fire(KSSuggestBox.this);
240 KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());
241 currentId = KSSuggestBox.this.getSelectedId();
242 }
243
244 @Override
245 public HandlerRegistration addSelectionChangeHandler(
246 SelectionChangeHandler handler) {
247 return addHandler(handler, SelectionChangeEvent.getType());
248 }
249
250 public IdableSuggestion getCurrentSuggestion() {
251 return currentSuggestion;
252 }
253
254 @Override
255 public void setValue(Map<String, String> translations) {
256
257
258 }
259
260 @Override
261 public void setWatermarkText(String text) {
262 ((KSTextBox)super.getTextBox()).setWatermarkText(text);
263 }
264
265 @Override
266 public boolean hasWatermark(){
267 return ((KSTextBox)super.getTextBox()).hasWatermark();
268 }
269
270 @Override
271 public boolean watermarkShowing() {
272 return ((KSTextBox)super.getTextBox()).hasWatermark();
273 }
274
275 }