001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.student.common.ui.client.widgets.impl;
017    
018    import java.util.Date;
019    
020    import org.kuali.student.common.ui.client.widgets.KSDatePickerAbstract;
021    import org.kuali.student.common.ui.client.widgets.KSTextBox;
022    import org.kuali.student.common.ui.client.widgets.focus.FocusGroup;
023    
024    import com.google.gwt.event.dom.client.BlurEvent;
025    import com.google.gwt.event.dom.client.BlurHandler;
026    import com.google.gwt.event.dom.client.ClickEvent;
027    import com.google.gwt.event.dom.client.ClickHandler;
028    import com.google.gwt.event.dom.client.FocusEvent;
029    import com.google.gwt.event.dom.client.FocusHandler;
030    import com.google.gwt.event.dom.client.HasBlurHandlers;
031    import com.google.gwt.event.dom.client.HasFocusHandlers;
032    import com.google.gwt.event.dom.client.KeyCodes;
033    import com.google.gwt.event.dom.client.KeyDownEvent;
034    import com.google.gwt.event.dom.client.KeyDownHandler;
035    import com.google.gwt.event.dom.client.KeyPressEvent;
036    import com.google.gwt.event.dom.client.KeyPressHandler;
037    import com.google.gwt.event.dom.client.KeyUpEvent;
038    import com.google.gwt.event.dom.client.KeyUpHandler;
039    import com.google.gwt.event.logical.shared.ValueChangeEvent;
040    import com.google.gwt.event.logical.shared.ValueChangeHandler;
041    import com.google.gwt.event.shared.HandlerRegistration;
042    import com.google.gwt.i18n.client.DateTimeFormat;
043    import com.google.gwt.user.client.Command;
044    import com.google.gwt.user.client.DeferredCommand;
045    import com.google.gwt.user.client.ui.PopupPanel;
046    import com.google.gwt.user.datepicker.client.DatePicker;
047    
048    public class KSDatePickerImpl extends KSDatePickerAbstract implements HasFocusHandlers, HasBlurHandlers{
049            private DatePicker picker = new DatePicker();
050            private KSTextBox dateField = new KSTextBox();
051            private PopupPanel popup = new PopupPanel(true);
052            private Date selectedDate = null;
053            private Date currentDate = new Date();
054            private DateTimeFormat df = DateTimeFormat.getFormat("MM/dd/yyyy");
055            private boolean justPicked = false;
056            private final FocusGroup focus = new FocusGroup(this);
057            private boolean allowedKeyPress = true;
058        private boolean hasWatermark = false;
059        private boolean watermarkShowing = false;
060        private String watermarkText;
061    
062            public KSDatePickerImpl(){
063                    this.initWidget(dateField);
064                    focus.addWidget(picker);
065                    focus.addWidget(dateField);
066                    //pickerWrapper.add(picker);
067                    popup.add(picker);
068    
069                    picker.setWidth(dateField.getOffsetWidth() + "px");
070                    dateField.addBlurHandler(new BlurHandler(){
071                            public void onBlur(BlurEvent event) {
072                                    dateField.removeStyleName("KS-Datefield-Focus");
073                                    if(!picker.isVisible()){
074                                            focus.setSuppressed(false);
075                                    }
076                            }
077                    });
078    
079    
080                    dateField.addFocusHandler(new FocusHandler(){
081                            public void onFocus(FocusEvent event) {
082                                    dateField.addStyleName("KS-Datefield-Focus");
083                                    popup.setPopupPosition(getAbsoluteLeft(), getAbsoluteTop() + dateField.getOffsetHeight());
084                                    if(justPicked){
085                                        if (dateField.getText() != null) {
086                                                dateField.selectAll();
087                                        }
088                                            justPicked = false;
089                                    }
090                                    else{
091                                            popup.show();
092                                            DeferredCommand.addCommand(new Command(){
093                                                    @Override
094                                                    public void execute() {
095                                                            focus.setSuppressed(true);
096                                                    }
097                                            });
098                                    }
099    
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                                    //dateField.selectAll();
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                    //pickerHandlers
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                    //return this.selectedDate;
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    /*      public void setValue(String date){
233                    if(date == null || date.equals("")){
234                            dateField.setText("");
235                    }
236                    else{
237                            Date newDate = df.parse(date);
238                            dateField.setText(date);
239                            picker.setCurrentMonth(newDate);
240                            picker.setValue(newDate, false);
241                        selectedDate = newDate;
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    }