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