View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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  // TODO implement some form of focus handling for SuggestBox
38  public class KSSuggestBox extends SuggestBox implements HasSelectionChangeHandlers, TranslatableValueWidget, HasWatermark{
39      
40      private IdableSuggestion currentSuggestion = null;
41      private IdableSuggestOracle oracle;
42      private String currentId = "";
43      
44      public KSSuggestBox(IdableSuggestOracle oracle) {
45      	this(oracle, true);
46      }
47      
48      public KSSuggestBox(IdableSuggestOracle oracle, boolean enabled){
49      	super(oracle, new KSTextBox());
50      	super.getTextBox().setEnabled(enabled);
51          this.oracle = oracle;
52          oracle.addSearchCompletedCallback(new Callback<IdableSuggestion>() {
53              @Override
54              public void exec(IdableSuggestion result) {
55                  currentSuggestion = result;
56                  currentId = KSSuggestBox.this.getSelectedId();
57                  SelectionChangeEvent.fire(KSSuggestBox.this);
58              }
59          });
60          this.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>(){
61  
62              @Override
63              public void onSelection(SelectionEvent<SuggestOracle.Suggestion> event) {
64                  currentSuggestion = (IdableSuggestion)(event.getSelectedItem());
65                  currentId = KSSuggestBox.this.getSelectedId();
66                  SelectionChangeEvent.fire(KSSuggestBox.this);
67              }
68          });
69          
70          this.getTextBox().addBlurHandler(new BlurHandler(){
71  
72              @Override
73              public void onBlur(BlurEvent event) {
74                  String currentText = KSSuggestBox.this.getText();
75                  if(currentText != null && !currentText.equals("")){
76                  	if((currentSuggestion != null && !KSSuggestBox.this.getText().equals(currentSuggestion.getReplacementString()))
77                  			|| currentSuggestion == null){
78                  		currentSuggestion = KSSuggestBox.this.oracle.getSuggestionByText(currentText);
79                  	}
80                      
81                      if(currentSuggestion == null){
82                      	currentSuggestion = new IdableSuggestion();
83                      	currentSuggestion.setId("");
84                      	currentSuggestion.setDisplayString("");
85                      	currentSuggestion.setReplacementString("");
86                      }
87                  }
88                  else if(currentText == null || currentText.equals("")){
89                  	currentSuggestion = new IdableSuggestion();
90                  	currentSuggestion.setId("");
91                  	currentSuggestion.setDisplayString("");
92                  	currentSuggestion.setReplacementString("");                	
93                  }
94                  
95                  if(!KSSuggestBox.this.getSelectedId().equals(currentId)){
96                  	currentId = KSSuggestBox.this.getSelectedId();
97                  	SelectionChangeEvent.fire(KSSuggestBox.this);
98                  }                
99              }
100         });
101     }
102     
103     public void reset(){
104         this.setText("");
105         currentSuggestion = null;
106     }
107 
108     public IdableSuggestion getSelectedSuggestion() {
109         return currentSuggestion;
110     }   
111     
112     public String getSelectedId() {
113         String id = "";
114         if(currentSuggestion != null){
115             id = currentSuggestion.getId();
116         }
117         return id;
118     }
119 
120     public IdableSuggestOracle getOracle() {
121         return oracle;
122     }
123 
124     @Override
125     public String getValue() {
126         return this.getSelectedId();
127     }
128 
129     @Override
130     public void setValue(String id) {
131     	if(id == null || id.equals("")){
132         	currentSuggestion = new IdableSuggestion();
133         	currentSuggestion.setId("");
134         	currentSuggestion.setDisplayString("");
135         	currentSuggestion.setReplacementString("");
136         	KSSuggestBox.this.setText("");
137         	currentId = KSSuggestBox.this.getSelectedId();
138     	}
139     	else
140     	{
141 	        oracle.getSuggestionByIdSearch(id, new Callback<IdableSuggestion>(){
142 	
143 	            @Override
144 	            public void exec(IdableSuggestion result) {
145 	                currentSuggestion = result;
146 	                KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());
147 	                currentId = KSSuggestBox.this.getSelectedId();
148 	            }
149 	        });
150     	}
151     }   
152     
153     public void setValue(String id, final Callback<IdableSuggestion> callback) {
154     	if(id == null || id.equals("")){
155         	currentSuggestion = new IdableSuggestion();
156         	currentSuggestion.setId("");
157         	currentSuggestion.setDisplayString("");
158         	currentSuggestion.setReplacementString("");
159         	KSSuggestBox.this.setText("");
160         	currentId = KSSuggestBox.this.getSelectedId();
161         	callback.exec(currentSuggestion);
162     	}
163     	else
164     	{
165 	        oracle.getSuggestionByIdSearch(id, new Callback<IdableSuggestion>(){
166 	
167 	            @Override
168 	            public void exec(IdableSuggestion result) {
169 	                currentSuggestion = result;
170 	                KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());
171 	                currentId = KSSuggestBox.this.getSelectedId();
172 	                callback.exec(currentSuggestion);
173 	            }
174 	        });
175     	}
176     }
177     
178     public void setValue(String id, String translation) {
179     	currentSuggestion = new IdableSuggestion();
180     	currentSuggestion.setId(id);
181     	currentSuggestion.setDisplayString(translation);
182     	currentSuggestion.setReplacementString(translation);
183     	KSSuggestBox.this.setText(translation);
184     	currentId = KSSuggestBox.this.getSelectedId();
185     }
186     
187     @Override
188     public void setValue(String id, boolean fireEvents) {
189     	if(fireEvents == true){
190     		
191 	    	if(id == null || id.equals("")){
192 	        	currentSuggestion = new IdableSuggestion();
193 	        	currentSuggestion.setId("");
194 	        	currentSuggestion.setDisplayString("");
195 	        	currentSuggestion.setReplacementString("");
196 	        	KSSuggestBox.this.setText("");
197 	        	currentId = KSSuggestBox.this.getSelectedId();
198 	    	}
199 	    	else
200 	    	{
201 		        oracle.getSuggestionByIdSearch(id, new Callback<IdableSuggestion>(){
202 		
203 		            @Override
204 		            public void exec(IdableSuggestion result) {
205 		                currentSuggestion = result;
206 		                KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());         
207 		            	if(!KSSuggestBox.this.getSelectedId().equals(currentId)){
208 		            		SelectionChangeEvent.fire(KSSuggestBox.this);
209 		            		currentId = KSSuggestBox.this.getSelectedId();
210 		            	}
211 		            }
212 		        });
213 	    	}
214 	    	
215     	}
216     	else
217     	{
218     		this.setValue(id);
219     	}
220 
221     }
222     
223     public void setValue(IdableSuggestion theSuggestion) {
224     	currentSuggestion = theSuggestion;
225     	SelectionChangeEvent.fire(KSSuggestBox.this);
226     	KSSuggestBox.this.setText((currentSuggestion == null) ? "" : currentSuggestion.getReplacementString());
227     	currentId = KSSuggestBox.this.getSelectedId();
228     }
229     
230 	@Override
231 	public HandlerRegistration addSelectionChangeHandler(
232 			SelectionChangeHandler handler) {
233 		return addHandler(handler, SelectionChangeEvent.getType());
234 	}
235 
236     public IdableSuggestion getCurrentSuggestion() {
237         return currentSuggestion;
238     }
239 
240     @Override
241     public void setValue(Map<String, String> translations) {
242         // TODO ryan - THIS METHOD NEEDS JAVADOCS
243         
244     }
245     
246 	@Override
247 	public void setWatermarkText(String text) {
248 		((KSTextBox)super.getTextBox()).setWatermarkText(text);
249 	}
250 	
251 	@Override
252 	public boolean hasWatermark(){
253 		return ((KSTextBox)super.getTextBox()).hasWatermark();
254 	}
255 	
256 	@Override
257 	public boolean watermarkShowing() {
258 		return ((KSTextBox)super.getTextBox()).hasWatermark();
259 	}
260 
261 }