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