PageRenderTime 58ms CodeModel.GetById 20ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/ftr-gwt-library-date/src/main/java/eu/future/earth/gwt/client/TimeBox.java

http://ftr-gwt-library.googlecode.com/
Java | 239 lines | 188 code | 32 blank | 19 comment | 142 complexity | 03f39c392b37f31607c0dd02219764bf MD5 | raw file
  1/*
  2 * Copyright 2007 Future Earth, info@future-earth.eu
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package eu.future.earth.gwt.client;
 18
 19import java.util.Calendar;
 20import java.util.Date;
 21import java.util.GregorianCalendar;
 22
 23import com.google.gwt.i18n.client.DateTimeFormat;
 24import com.google.gwt.user.client.DOM;
 25import com.google.gwt.user.client.Event;
 26import com.google.gwt.user.client.ui.TextBox;
 27
 28public class TimeBox extends TextBox {
 29
 30	public final static String DEFAULT_TIME_FORMAT = "HH:mm";
 31
 32	private String format = DEFAULT_TIME_FORMAT;
 33
 34	private DateTimeFormat date = null;
 35
 36	public TimeBox() {
 37		this(DEFAULT_TIME_FORMAT);
 38	}
 39
 40	public TimeBox(String format) {
 41        super.sinkEvents(Event.ONKEYDOWN);
 42        setFormat(format);
 43    }
 44
 45	public void onBrowserEvent(Event event) {
 46		int eventType = DOM.eventGetType(event);
 47		int keyCode = DOM.eventGetKeyCode(event);
 48
 49		switch (eventType) {
 50		case Event.ONKEYDOWN: {
 51
 52			if (keyCode == 37 || keyCode == 39 || keyCode == 18 || keyCode == 9) {
 53				super.onBrowserEvent(event);
 54				return;
 55			} else {
 56				if (isTimeKey(keyCode)) {
 57					final int cursor = super.getCursorPos();
 58					if (cursor >= format.length()) {
 59
 60					} else {
 61						final String temp = super.getText();
 62						final char rep = format.charAt(cursor);
 63						if (rep == ':') {
 64							super.setCursorPos(cursor + 1);
 65						} else {
 66							char next = (char)-1;
 67							if(format.length() > cursor + 1){
 68								next = format.charAt(cursor + 1);
 69							}
 70							char newChar = ((char) convert(keyCode));
 71							boolean allow = false;
 72							if (rep == 'H' && next == 'H') {
 73								if (newChar == '0' || newChar == '1' || newChar == '2') {
 74									allow = true;
 75								}
 76							} else {
 77								if (rep == 'H') {
 78									char prev = temp.charAt(cursor - 1);
 79									if (prev == '2') {
 80										if (newChar == '0' || newChar == '1' || newChar == '2' || newChar == '3') {
 81											allow = true;
 82										}
 83									} else {
 84										allow = true;
 85									}
 86
 87								}
 88							}
 89
 90                            if (rep == 'h' && next == 'h') {
 91                                if (newChar == '0' || newChar == '1') {
 92                                    allow = true;
 93                                }
 94                            } else {
 95                                if (rep == 'h') {
 96                                    char prev = temp.charAt(cursor - 1);
 97                                    if (prev == '1') {
 98                                        if (newChar == '0' || newChar == '1' || newChar == '2') {
 99                                            allow = true;
100                                        }
101                                    } else {
102                                        allow = true;
103                                    }
104
105                                }
106                            }
107							
108							if (rep == 'm' && next == 'm') {
109								if (newChar == '0' || newChar == '1' || newChar == '2' || newChar == '3' || newChar == '4' || newChar == '5' || newChar == '6') {
110									allow = true;
111								}
112							} else {
113								if (rep == 'm') {
114									final char prev = temp.charAt(cursor - 1);
115									if (prev == '0' || prev == '1' || prev == '2' || prev == '3' || prev == '4' || prev == '5') {
116										allow = true;
117									} else {
118										if (newChar == '0') {
119											allow = true;
120										}
121									}
122								}
123							}
124
125                            if (rep == 'a' && next == 'a') {
126                                if (newChar == 'A' || newChar == 'a') {
127                                    newChar = 'A';
128                                    allow = true;
129                                } else if (newChar == 'P' || newChar == 'p') {
130                                    newChar = 'P';
131                                    allow = true;
132                                }
133
134                            } else {
135                                if (rep == 'a') {
136                                    if (newChar == 'M' || newChar == 'm') {
137                                        newChar = 'M';
138                                        allow = true;
139                                    }
140                                }
141                            }
142
143                            if (allow) {
144								final String newString = temp.substring(0, cursor) + newChar + temp.substring(cursor + 1, format.length());
145								super.setText(newString);
146								if (next == ':') {
147									super.setCursorPos(cursor + 2);
148								} else {
149									super.setCursorPos(cursor + 1);
150								}
151
152							}
153
154						}
155					}
156					DOM.eventPreventDefault(event);
157					return;
158
159				} else {
160					DOM.eventPreventDefault(event);
161					return;
162				}
163
164			}
165
166		}
167		default:
168			super.onBrowserEvent(event);
169		}
170	}
171
172	private int convert(int keyCode) {
173        // convert 0 - 9 keys on number pad down into normal 0 - 9 range  
174        if (keyCode >= 96 && keyCode <= 105) {
175            return keyCode - 48;
176        }
177        return keyCode;
178	}
179
180	private boolean isTimeKey(int keyCode) {
181	    // 0 - 9 keys above qwerty
182		if (keyCode >= 48 && keyCode <= 57) {
183			return true;
184		}
185		// 0 - 9 keys on number pad 
186        if (keyCode >= 96 && keyCode <= 105) {
187            return true;
188        }
189        // A, M, P keys
190        if (keyCode == 65 || keyCode == 77 || keyCode == 80) {
191            return true;
192        }
193		return false;
194	}
195
196	public void setDate(Date newValue) {
197		if (newValue != null) {
198			super.setText(date.format(newValue));
199		} else {
200			super.setText(format);
201		}
202	}
203
204	public Date getValue(Date date) {
205		if (getText() != null && getText().length() == format.length()) {
206			if(getText().equals(format)){
207				return null;
208			}
209			try {
210			    final Date time = DateTimeFormat.getFormat(format).parse(getText());
211                final GregorianCalendar thelper = new GregorianCalendar();
212                thelper.setTime(time);
213                final GregorianCalendar helper = new GregorianCalendar();
214                helper.setTime(date);
215                helper.set(Calendar.HOUR_OF_DAY, thelper.get(Calendar.HOUR_OF_DAY));
216                helper.set(Calendar.MINUTE, thelper.get(Calendar.MINUTE));
217                helper.set(Calendar.SECOND, 0);
218                helper.set(Calendar.MILLISECOND, 0);
219			    return helper.getTime();
220			} catch (Exception ex) {
221				return null;
222			}
223		}
224		return null;
225	}
226
227	public String getFormat() {
228		return format;
229	}
230
231	public void setFormat(String format) {
232		this.format = format;
233		date = DateTimeFormat.getFormat(format);
234		super.setText(format);
235		super.setMaxLength(format.length());
236		super.setVisibleLength(format.length());
237
238	}
239}