summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.ui.swing/src/main/java/net/wotonomy/ui/swing/components/DateTextField.java
blob: 8a3b08cebf10872631555ded60f589efae640828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.ui.swing.components;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * DateTextField is a "smart" text field that restricts the user's input. The
 * input is restructed to a string representing a date format.
 *
 * @author rob@straylight.princeton.com
 * @author $Author: cgruber $
 * @version $Revision: 904 $
 */
public class DateTextField extends JTextField {

	/*******************************
	 * CONSTANTS
	 *******************************/

	/**
	 * Use the current date for this text field.
	 */
	public static final int CURRENT_DATE = 0;

	/**
	 * Use blanks for this text field.
	 */
	public static final int BLANKS = 1;

	/**
	 * Use underscores for this text field.
	 */
	public static final int UNDERSCORES = 2;

	/**
	 * Use just a 4-digit year for this text field.
	 */
	public static final int YEAR = 3;

	private static final int BACKSPACE = 8;
	private static final int DELETE = 127;
	private static final int PASTE = 22; // Ctl-V
	private static final int CUT = 24; // Ctl-X

	/*******************************
	 * DATA MEMEBERS
	 *******************************/
	private int defaultType = CURRENT_DATE;

	private boolean warningMessageActive = false;

	/*******************************
	 * PUBLIC METHODS
	 *******************************/

	/**
	 * Default Constructor.
	 */
	public DateTextField() {
		this(1, 1, 1999, 0);

		Calendar rightNow = Calendar.getInstance();

		super.setText(createDateString(rightNow.get(Calendar.MONTH) + 1, rightNow.get(Calendar.DATE),
				rightNow.get(Calendar.YEAR)));
	}

	/**
	 * Constructor.
	 * 
	 * @param month Number of the month, January being 1.
	 * @param data  The day of the month.
	 * @param year  The year.
	 */
	public DateTextField(int month, int date, int year) {
		this(month, date, year, 0);
	}

	/**
	 * Constructor.
	 * 
	 * @param columns Width of the text field (in characters).
	 */
	public DateTextField(int columns) {
		this(1, 1, 1998, columns);

		Calendar rightNow = Calendar.getInstance();

		super.setText(createDateString(rightNow.get(Calendar.MONTH) + 1, rightNow.get(Calendar.DATE),
				rightNow.get(Calendar.YEAR)));
	}

	/**
	 * Constructor.
	 * 
	 * @param month   Number of the month, January being 1.
	 * @param data    The day of the month.
	 * @param year    The year.
	 * @param columns Width of the text field (in characters).
	 */
	public DateTextField(int month, int date, int year, int columns) {
		super("", columns);

		super.setText(createDateString(month, date, year));

		this.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e) {
				if (!(e.isTemporary())) {
					validateDateString(e);
				}
			}
		});
	}

	/**
	 * Sets the date type to display when the user has not entered any date yet.
	 * Default is the current date.
	 * 
	 * @see #CURRENT_DATE
	 * @see #BLANKS
	 * @see #UNDERSCORES
	 * @param newDefaultType The type of date to display when there is no date data.
	 */
	public void setDefaultType(int newDefaultType) {
		if (newDefaultType == BLANKS) {
			defaultType = BLANKS;
			super.setText("  /  /    ");
		} else if (newDefaultType == UNDERSCORES) {
			defaultType = UNDERSCORES;
			super.setText("__/__/____");
		} else if (newDefaultType == YEAR) {
			defaultType = YEAR;
			super.setText("0000");
		} else {
			defaultType = CURRENT_DATE;

			Calendar rightNow = Calendar.getInstance();

			super.setText(createDateString(rightNow.get(Calendar.MONTH) + 1, rightNow.get(Calendar.DATE),
					rightNow.get(Calendar.YEAR)));
		}
	}

	/**
	 * Returns the type of date to display when there is no user input.
	 * 
	 * @see #CURRENT_DATE
	 * @see #BLANKS
	 * @see #UNDERSCORES
	 * @return The type of date to display when there is no date to display.
	 */
	public int getDefaultType() {
		return defaultType;
	}

	/**
	 * Sets the text field to the string representation of the specified date.
	 * 
	 * @param aDate The date to set the text field to.
	 */
	public void setDate(Date aDate) {
		Calendar aCalendar = Calendar.getInstance();

		aCalendar.setTime(aDate);

		super.setText(createDateString(aCalendar.get(Calendar.MONTH) + 1, aCalendar.get(Calendar.DATE),
				aCalendar.get(Calendar.YEAR)));
	}

	/**
	 * Sets the text field directly from a Date object.
	 * 
	 * @param aDate The date to set the text field to.
	 */
	public void setText(Date aDate) {
		setDate(aDate);
	}

	/**
	 * Sets the text field to the date specified in the string. This is overridden
	 * from the parent class to insure a valid date is inputted. The format of the
	 * date expected is the type of date format this text field is currently set to.
	 * 
	 * @param aString A string representing a date in this text field current
	 *                format.
	 */
	public void setText(String aString) {
		Date testDate = null;

		if (aString != null) {
			ParsePosition position = new ParsePosition(0);

			if (defaultType == YEAR) {
				SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy");
				testDate = yearFormatter.parse(aString, position);
			} else {
				SimpleDateFormat fullDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
				testDate = fullDateFormatter.parse(aString, position);
			}
		}

		// The string is not a valid date, use default value for date then.
		if (testDate == null) {
			Calendar aCalendar = Calendar.getInstance();

			testDate = aCalendar.getTime();
		}

		setDate(testDate);
	}

	/**
	 * Returns the date as represented by the date string in the text field.
	 * 
	 * @return The date in the text field.
	 */
	public Date getDate() throws NumberFormatException {
		Calendar aCalendar = Calendar.getInstance();
		int year = 1980;
		int month = 0;
		int date = 1;
		int[] tempArray = { 1, 3, 5, 7, 8, 10, 12 };
		Vector monthsWith31Days = new Vector(7);

		for (int i = 0; i < tempArray.length; ++i) {
			monthsWith31Days.addElement(new Integer(tempArray[i]));
		}

		aCalendar.set(year, month, date, 12, 0, 0);

		try {
			String dateString = getText();
			NumberFormatException nfException = new NumberFormatException(
					new String("Invalid Date String: " + dateString));

			if (defaultType == YEAR) {
				year = Integer.parseInt(dateString);

				aCalendar.set(year, 0, 1, 12, 0, 0);

				return aCalendar.getTime();
			}

			month = Integer.parseInt(dateString.substring(0, 2).trim());
			date = Integer.parseInt(dateString.substring(3, 5).trim());
			year = Integer.parseInt(dateString.substring(6).trim());

			if ((month < 1) || (month > 12)) {
				throw nfException;
			}

			if ((date < 1) || (date > 31)) {
				throw nfException;
			}

			if ((date == 31) && (!(monthsWith31Days.contains(new Integer(month))))) {
				throw nfException;
			}

			if ((date == 30) && (month == 2)) {
				throw nfException;
			}

			if ((date == 29) && (month == 2)) {
				if ((year % 100) == 0) {
					if ((year % 400) != 0) {
						throw nfException;
					}
				} else {
					if ((year % 4) != 0) {
						throw nfException;
					}
				}
			}
		} catch (IndexOutOfBoundsException ioobe) {
			NumberFormatException nfException = new NumberFormatException(
					new String("Invalid Date String: " + getText()));
			throw nfException;
		} catch (NumberFormatException nfe) {
			NumberFormatException nfException = new NumberFormatException(
					new String("Invalid Date String: " + getText()));
			throw nfException;
		}

		aCalendar.set(year, (month - 1), date, 12, 0, 0);

		return aCalendar.getTime();
	}

	public void processKeyEvent(KeyEvent e) {
		String currentString = "";
		String testString = "";
		char newChar = e.getKeyChar();
		int currentLength = 0;
		int currentCaretPosition = 0;
		int selectionStart = 0;
		int selectionEnd = 0;
		int modifierPosition = 0;
		int modifierDirection = 1;
		char modifierCharacter;
		boolean backspace = false;
		boolean delete = false;
		boolean paste = false;
		boolean cut = false;
		boolean keyPressed = false;

		backspace = (newChar == BACKSPACE);
		delete = (newChar == DELETE);
		paste = (newChar == PASTE);
		cut = (newChar == CUT);

		keyPressed = (e.paramString().startsWith("KEY_PRESSED"));

		if ((e.getKeyCode() == KeyEvent.VK_UNDEFINED) || ((backspace) || (delete) || (paste) || (cut))) // A "key-typed"
																										// event
		{
			if (isValidCharacter(newChar)) {
				if ((isPrintableCharacter(newChar)) || (backspace) || (delete)) {
					// Both the key "pressed" and key "released" events get passed
					// in here for the delete and backspace key. Only processes
					// these keys if the event is key "pressed".
					if (((backspace) || (delete)) && (!(keyPressed))) {
						// Don't do anything, pass through to consumption.
					} else {
						// Analyze the current contents of the field
						currentString = getText();
						currentLength = currentString.length();

						char[] tempText = new char[currentLength];

						currentCaretPosition = getCaretPosition();

						selectionStart = getSelectionStart();
						selectionEnd = getSelectionEnd();

						// if a range is selected, then get rid of it and place the caret
						// at the begginning of the range and continue processing.
						if (selectionStart != selectionEnd) {
							selectionEnd = selectionStart;
							setSelectionEnd(selectionEnd);

							currentCaretPosition = selectionStart;
							setCaretPosition(currentCaretPosition);
						}

						if (currentCaretPosition <= currentLength) {
							// a number of delete or backspace was pressed, delete and
							// backspace deletes a number and places a "space" there

							// if caret at start of string and the backspace pressed OR
							// caret at end of string and delete or number pressed THEN
							// don't do anything, otherwise process key stroke
							if (((currentCaretPosition == 0) && (backspace))
									|| ((currentCaretPosition == currentLength) && (!(backspace)))) {
								// Don't do any processing.
							} else {
								modifierPosition = currentCaretPosition;
								if (backspace) {
									modifierDirection = -1;
									modifierPosition += modifierDirection;
								}

								// Overwrite the current position with the new character
								// inputted or overwrite using a space or underscore if
								// the backspace or delete key was pressed.
								if (defaultType != YEAR) {
									modifierCharacter = ((delete) || (backspace))
											? ((defaultType == UNDERSCORES) ? '_' : ' ')
											: newChar;
								} else {
									// We are dealing with a 4-digit year. Overwrite
									// with new character or "0" if delete or backspace
									// was pressed.
									modifierCharacter = ((delete) || (backspace)) ? ('0') : newChar;
								}

								if (currentString.charAt(modifierPosition) == '/') {
									modifierPosition += modifierDirection;
								}

								for (int i = 0; i < currentLength; ++i) {
									if (i == modifierPosition) {
										tempText[i] = modifierCharacter;
									} else {
										tempText[i] = currentString.charAt(i);
									}
								}

								testString = new String(tempText);
								if (isValidString(testString)) {
									super.setText(testString);
									if (backspace) {
										setCaretPosition(modifierPosition);
									} else {
										setCaretPosition(modifierPosition + 1);
									}
								}
							}
						}
					}

					e.consume();
				} else if ((cut) || (paste)) {
					e.consume();
				}
				// else its a non-printable character, let it pass through
			} else {
				e.consume();
			}
		}

		super.processKeyEvent(e);
	}

	private boolean isValidCharacter(char aChar) {
		if (((aChar >= '!') && (aChar <= '/')) || ((aChar >= ':') && (aChar <= '~'))) {
			return false;
		}
		return true;
	}

	private boolean isPrintableCharacter(char inputChar) {
		if ((inputChar >= ' ') && (inputChar <= '~')) {
			return true;
		}
		return false;
	}

	private boolean isValidDate(int month, int date, int year) {
		if ((month < 1) || (month > 12)) {
			return false;
		}

		if ((date < 1) || (date > 31)) {
			return false;
		}

		if ((year < 0) || (year > 9999)) {
			return false;
		}

		return true;
	}

	private boolean isValidString(String aString) {
		return true;
	}

	private String createDateString(int month, int date, int year) {
		String dateString = "";

		if (isValidDate(month, date, year)) {
			if (defaultType != YEAR) {
				if (month < 10) {
					dateString = "0";
				}

				dateString += String.valueOf(month);
				dateString += "/";

				if (date < 10) {
					dateString += "0";
				}

				dateString += String.valueOf(date);
				dateString += "/";
			}

			if (year < 1000) {
				dateString += "0";
				if (year < 100) {
					dateString += "0";
					if (year < 10) {
						dateString += "0";
					}
				}
			}

			dateString += String.valueOf(year);
		} else {
			if (defaultType == YEAR) {
				dateString = "1999";
			} else {
				dateString = "01/01/1999";
			}
		}

		return dateString;
	}

	private void validateDateString(FocusEvent e) {
		if (!(warningMessageActive)) {
			try {
				getDate();
			} catch (NumberFormatException nfe) {
				System.out.println("Invalid Date String!!!");
				warningMessageActive = true;
				JOptionPane.showMessageDialog(this, "Invald Date: " + getText(), "Warning",
						JOptionPane.WARNING_MESSAGE);
				warningMessageActive = false;
				if (defaultType == YEAR) {
					super.setText("1999");
				} else {
					super.setText("01/01/1999");
				}
			}
		}
	}
}