| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.widgets.impl; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.Date; | 
  | 19 |  |   | 
  | 20 |  |  import org.kuali.student.common.ui.client.widgets.KSDatePickerAbstract; | 
  | 21 |  |  import org.kuali.student.common.ui.client.widgets.KSTextBox; | 
  | 22 |  |  import org.kuali.student.common.ui.client.widgets.focus.FocusGroup; | 
  | 23 |  |   | 
  | 24 |  |  import com.google.gwt.event.dom.client.BlurEvent; | 
  | 25 |  |  import com.google.gwt.event.dom.client.BlurHandler; | 
  | 26 |  |  import com.google.gwt.event.dom.client.ClickEvent; | 
  | 27 |  |  import com.google.gwt.event.dom.client.ClickHandler; | 
  | 28 |  |  import com.google.gwt.event.dom.client.FocusEvent; | 
  | 29 |  |  import com.google.gwt.event.dom.client.FocusHandler; | 
  | 30 |  |  import com.google.gwt.event.dom.client.HasBlurHandlers; | 
  | 31 |  |  import com.google.gwt.event.dom.client.HasFocusHandlers; | 
  | 32 |  |  import com.google.gwt.event.dom.client.KeyCodes; | 
  | 33 |  |  import com.google.gwt.event.dom.client.KeyDownEvent; | 
  | 34 |  |  import com.google.gwt.event.dom.client.KeyDownHandler; | 
  | 35 |  |  import com.google.gwt.event.dom.client.KeyPressEvent; | 
  | 36 |  |  import com.google.gwt.event.dom.client.KeyPressHandler; | 
  | 37 |  |  import com.google.gwt.event.dom.client.KeyUpEvent; | 
  | 38 |  |  import com.google.gwt.event.dom.client.KeyUpHandler; | 
  | 39 |  |  import com.google.gwt.event.logical.shared.ValueChangeEvent; | 
  | 40 |  |  import com.google.gwt.event.logical.shared.ValueChangeHandler; | 
  | 41 |  |  import com.google.gwt.event.shared.HandlerRegistration; | 
  | 42 |  |  import com.google.gwt.i18n.client.DateTimeFormat; | 
  | 43 |  |  import com.google.gwt.user.client.Command; | 
  | 44 |  |  import com.google.gwt.user.client.DeferredCommand; | 
  | 45 |  |  import com.google.gwt.user.client.ui.PopupPanel; | 
  | 46 |  |  import com.google.gwt.user.datepicker.client.DatePicker; | 
  | 47 |  |   | 
  | 48 | 0 |  public class KSDatePickerImpl extends KSDatePickerAbstract implements HasFocusHandlers, HasBlurHandlers{ | 
  | 49 | 0 |          private DatePicker picker = new DatePicker(); | 
  | 50 | 0 |          private KSTextBox dateField = new KSTextBox(); | 
  | 51 | 0 |          private PopupPanel popup = new PopupPanel(true); | 
  | 52 | 0 |          private Date selectedDate = null; | 
  | 53 | 0 |          private Date currentDate = new Date(); | 
  | 54 | 0 |          private DateTimeFormat df = DateTimeFormat.getFormat("MM/dd/yyyy"); | 
  | 55 | 0 |          private boolean justPicked = false; | 
  | 56 | 0 |          private final FocusGroup focus = new FocusGroup(this); | 
  | 57 | 0 |          private boolean allowedKeyPress = true; | 
  | 58 | 0 |      private boolean hasWatermark = false; | 
  | 59 | 0 |      private boolean watermarkShowing = false; | 
  | 60 |  |      private String watermarkText; | 
  | 61 |  |   | 
  | 62 | 0 |          public KSDatePickerImpl(){ | 
  | 63 | 0 |                  this.initWidget(dateField); | 
  | 64 | 0 |                  focus.addWidget(picker); | 
  | 65 | 0 |                  focus.addWidget(dateField); | 
  | 66 |  |                   | 
  | 67 | 0 |                  popup.add(picker); | 
  | 68 |  |   | 
  | 69 | 0 |                  picker.setWidth(dateField.getOffsetWidth() + "px"); | 
  | 70 | 0 |                  dateField.addBlurHandler(new BlurHandler(){ | 
  | 71 |  |                          public void onBlur(BlurEvent event) { | 
  | 72 | 0 |                                  dateField.removeStyleName("KS-Datefield-Focus"); | 
  | 73 | 0 |                                  if(!picker.isVisible()){ | 
  | 74 | 0 |                                          focus.setSuppressed(false); | 
  | 75 |  |                                  } | 
  | 76 | 0 |                          } | 
  | 77 |  |                  }); | 
  | 78 |  |   | 
  | 79 |  |   | 
  | 80 | 0 |                  dateField.addFocusHandler(new FocusHandler(){ | 
  | 81 |  |                          public void onFocus(FocusEvent event) { | 
  | 82 | 0 |                                  dateField.addStyleName("KS-Datefield-Focus"); | 
  | 83 | 0 |                                  popup.setPopupPosition(getAbsoluteLeft(), getAbsoluteTop() + dateField.getOffsetHeight()); | 
  | 84 | 0 |                                  if(justPicked){ | 
  | 85 | 0 |                                      if (dateField.getText() != null) { | 
  | 86 | 0 |                                              dateField.selectAll(); | 
  | 87 |  |                                      } | 
  | 88 | 0 |                                          justPicked = false; | 
  | 89 |  |                                  } | 
  | 90 |  |                                  else{ | 
  | 91 | 0 |                                          popup.show(); | 
  | 92 | 0 |                                          DeferredCommand.addCommand(new Command(){ | 
  | 93 |  |                                                  @Override | 
  | 94 |  |                                                  public void execute() { | 
  | 95 | 0 |                                                          focus.setSuppressed(true); | 
  | 96 | 0 |                                                  } | 
  | 97 |  |                                          }); | 
  | 98 |  |                                  } | 
  | 99 |  |   | 
  | 100 | 0 |                          } | 
  | 101 |  |                  }); | 
  | 102 |  |   | 
  | 103 | 0 |                  dateField.addClickHandler(new ClickHandler(){ | 
  | 104 |  |   | 
  | 105 |  |                          public void onClick(ClickEvent event) { | 
  | 106 | 0 |                                  popup.setPopupPosition(getAbsoluteLeft(), getAbsoluteTop() + dateField.getOffsetHeight()); | 
  | 107 | 0 |                                  popup.show(); | 
  | 108 |  |                                   | 
  | 109 |  |   | 
  | 110 | 0 |                          } | 
  | 111 |  |   | 
  | 112 |  |                  }); | 
  | 113 |  |   | 
  | 114 | 0 |                  dateField.addKeyPressHandler(new KeyPressHandler(){ | 
  | 115 |  |   | 
  | 116 |  |                          public void onKeyPress(KeyPressEvent event) { | 
  | 117 | 0 |                                  String validInput = "0123456789"; | 
  | 118 | 0 |                                  if(validInput.indexOf(event.getCharCode()) == -1 && !isDeleteOrBackspaceKey(event.getNativeEvent().getKeyCode())){ | 
  | 119 | 0 |                                          event.preventDefault(); | 
  | 120 |  |                                  } | 
  | 121 | 0 |                          } | 
  | 122 |  |   | 
  | 123 |  |                  }); | 
  | 124 |  |   | 
  | 125 | 0 |                  dateField.addKeyDownHandler(new KeyDownHandler(){ | 
  | 126 |  |   | 
  | 127 |  |                          public void onKeyDown(KeyDownEvent event) { | 
  | 128 | 0 |                                  if(event.getNativeKeyCode() == KeyCodes.KEY_TAB){ | 
  | 129 | 0 |                                          popup.hide(); | 
  | 130 | 0 |                                          if(picker.getValue() != null){ | 
  | 131 | 0 |                                                  selectedDate = picker.getValue(); | 
  | 132 |  |                                          } | 
  | 133 |  |                                  } | 
  | 134 | 0 |                          } | 
  | 135 |  |   | 
  | 136 |  |                  }); | 
  | 137 |  |   | 
  | 138 | 0 |                  dateField.addKeyUpHandler(new KeyUpHandler(){ | 
  | 139 |  |   | 
  | 140 |  |                          public void onKeyUp(KeyUpEvent event) { | 
  | 141 | 0 |                                  String dateText = dateField.getText(); | 
  | 142 |  |   | 
  | 143 | 0 |                                  if(!isDeleteOrBackspaceKey(event.getNativeKeyCode())){ | 
  | 144 | 0 |                                          if(dateText.length() == 2){ | 
  | 145 | 0 |                                                  dateField.setText(dateText + "/"); | 
  | 146 | 0 |                                                  String current = df.format(currentDate); | 
  | 147 | 0 |                                                  Date newDate = df.parse(dateField.getText()+ "01" + current.substring(5)); | 
  | 148 | 0 |                                                  picker.setCurrentMonth(newDate); | 
  | 149 | 0 |                                          } | 
  | 150 | 0 |                                          else if(dateText.length() == 5){ | 
  | 151 | 0 |                                                  dateField.setText(dateText + "/"); | 
  | 152 | 0 |                                                  String current = df.format(currentDate); | 
  | 153 | 0 |                                                  Date newDate = df.parse(dateField.getText() + current.substring(6)); | 
  | 154 | 0 |                                                  dateField.setText(df.format(newDate).substring(0, 6)); | 
  | 155 | 0 |                                                  picker.setCurrentMonth(newDate); | 
  | 156 | 0 |                                                  picker.setValue(newDate, false); | 
  | 157 |  |   | 
  | 158 | 0 |                                          } | 
  | 159 | 0 |                                          else if(dateText.length() == 10){ | 
  | 160 | 0 |                                                  Date newDate = df.parse(dateField.getText()); | 
  | 161 | 0 |                                                  picker.setCurrentMonth(newDate); | 
  | 162 | 0 |                                                  picker.setValue(newDate, false); | 
  | 163 | 0 |                                                  selectedDate = picker.getValue(); | 
  | 164 |  |                                          } | 
  | 165 |  |                                  } | 
  | 166 | 0 |                          } | 
  | 167 |  |   | 
  | 168 |  |                  }); | 
  | 169 |  |   | 
  | 170 |  |   | 
  | 171 |  |                   | 
  | 172 | 0 |                  picker.addValueChangeHandler(new ValueChangeHandler<Date>(){ | 
  | 173 |  |   | 
  | 174 |  |                          public void onValueChange(ValueChangeEvent<Date> event) { | 
  | 175 | 0 |                                  if(picker.getValue() != null){ | 
  | 176 | 0 |                                      dateField.setText(df.format(picker.getValue())); | 
  | 177 | 0 |                                          selectedDate = picker.getValue(); | 
  | 178 |  |                                  } | 
  | 179 | 0 |                                  dateField.setFocus(true); | 
  | 180 | 0 |                                  popup.hide(); | 
  | 181 | 0 |                                  justPicked = true; | 
  | 182 | 0 |                                  focus.setSuppressed(false); | 
  | 183 | 0 |                                  fireValueChangeEvent(); | 
  | 184 | 0 |                          } | 
  | 185 |  |                  }); | 
  | 186 |  |   | 
  | 187 | 0 |          } | 
  | 188 |  |   | 
  | 189 |  |          private boolean isDeleteOrBackspaceKey(int code){ | 
  | 190 | 0 |                  return  (code == KeyCodes.KEY_BACKSPACE || code == KeyCodes.KEY_DELETE); | 
  | 191 |  |          } | 
  | 192 |  |   | 
  | 193 |  |          private void fireValueChangeEvent(){ | 
  | 194 | 0 |          ValueChangeEvent.fire(this, selectedDate); | 
  | 195 | 0 |          } | 
  | 196 |  |   | 
  | 197 |  |          public Date getValue(){ | 
  | 198 | 0 |                  Date date = null; | 
  | 199 | 0 |                  if(dateField.getText() != null | 
  | 200 |  |                                  && dateField.getText().trim().length() == 10) | 
  | 201 |  |                  { | 
  | 202 |  |                          try{ | 
  | 203 | 0 |                                  date = df.parseStrict(dateField.getText().trim()); | 
  | 204 |  |                          } | 
  | 205 | 0 |                          catch(IllegalArgumentException e){ | 
  | 206 | 0 |                                  date = null; | 
  | 207 | 0 |                          } | 
  | 208 |  |                  } | 
  | 209 |  |                  else{ | 
  | 210 | 0 |                          dateField.setText(""); | 
  | 211 |  |                  } | 
  | 212 | 0 |                  return date; | 
  | 213 |  |                   | 
  | 214 |  |          } | 
  | 215 |  |   | 
  | 216 |  |          public void setValue(Date date){ | 
  | 217 | 0 |                  if(null==date){ | 
  | 218 | 0 |                          dateField.setText(""); | 
  | 219 |  |                  }else{ | 
  | 220 | 0 |                          dateField.setText(df.format(date)); | 
  | 221 | 0 |                  picker.setCurrentMonth(date); | 
  | 222 | 0 |                  picker.setValue(date, false); | 
  | 223 | 0 |                      selectedDate = date; | 
  | 224 |  |                  } | 
  | 225 |  |   | 
  | 226 | 0 |          } | 
  | 227 |  |           | 
  | 228 |  |          public KSTextBox getDateTextField() { | 
  | 229 | 0 |              return dateField; | 
  | 230 |  |          } | 
  | 231 |  |   | 
  | 232 |  |   | 
  | 233 |  |   | 
  | 234 |  |   | 
  | 235 |  |   | 
  | 236 |  |   | 
  | 237 |  |   | 
  | 238 |  |   | 
  | 239 |  |   | 
  | 240 |  |   | 
  | 241 |  |   | 
  | 242 |  |   | 
  | 243 |  |   | 
  | 244 |  |   | 
  | 245 |  |      @Override | 
  | 246 |  |      public void setValue(Date date, boolean fireEvents) { | 
  | 247 | 0 |          setValue(date); | 
  | 248 | 0 |      } | 
  | 249 |  |   | 
  | 250 |  |      @Override | 
  | 251 |  |      public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) { | 
  | 252 | 0 |          return addHandler(handler, ValueChangeEvent.getType()); | 
  | 253 |  |      } | 
  | 254 |  |   | 
  | 255 |  |          @Override | 
  | 256 |  |          public HandlerRegistration addFocusHandler(FocusHandler handler) { | 
  | 257 | 0 |                  return focus.addFocusHandler(handler); | 
  | 258 |  |          } | 
  | 259 |  |   | 
  | 260 |  |          @Override | 
  | 261 |  |          public HandlerRegistration addBlurHandler(BlurHandler handler) { | 
  | 262 | 0 |                  return focus.addBlurHandler(handler); | 
  | 263 |  |          } | 
  | 264 |  |   | 
  | 265 |  |      @Override | 
  | 266 |  |      public boolean hasWatermark() { | 
  | 267 | 0 |          return dateField.hasWatermark(); | 
  | 268 |  |      } | 
  | 269 |  |   | 
  | 270 |  |      @Override | 
  | 271 |  |      public void setWatermarkText(String text) { | 
  | 272 | 0 |          dateField.setWatermarkText(text); | 
  | 273 | 0 |      } | 
  | 274 |  |   | 
  | 275 |  |      @Override | 
  | 276 |  |      public boolean watermarkShowing() { | 
  | 277 | 0 |          return dateField.watermarkShowing(); | 
  | 278 |  |      } | 
  | 279 |  |   | 
  | 280 |  |  } |