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