Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4252e9cbf09a112374016cff4b484a0253a1a2ab (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Text example snippet: verify input (format for date)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet179 {

public static void main(String[] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	shell.setLayout(new GridLayout());
	final Text text = new Text(shell, SWT.BORDER);
	text.setText("YYYY/MM/DD");
	final Calendar calendar = Calendar.getInstance();
	text.addListener(SWT.Verify, new Listener() {
		boolean ignore;
		@Override
		public void handleEvent(Event e) {
			if (ignore) return;
			e.doit = false;
			StringBuilder buffer = new StringBuilder(e.text);
			char[] chars = new char[buffer.length()];
			buffer.getChars(0, chars.length, chars, 0);
			if (e.character == '\b') {
				for (int i = e.start; i < e.end; i++) {
					switch (i) {
						case 0: /* [Y]YYY */
						case 1: /* Y[Y]YY */
						case 2: /* YY[Y]Y */
						case 3: /* YYY[Y] */ {
							buffer.append('Y'); 	break;
						}
						case 5: /* [M]M*/
						case 6: /* M[M] */{
							buffer.append('M'); break;
						}
						case 8: /* [D]D */
						case 9: /* D[D] */ {
							buffer.append('D'); break;
						}
						case 4: /* YYYY[/]MM */
						case 7: /* MM[/]DD */ {
							buffer.append('/'); break;
						}
						default:
							return;
					}
				}
				text.setSelection(e.start, e.start + buffer.length());
				ignore = true;
				text.insert(buffer.toString());
				ignore = false;
				text.setSelection(e.start, e.start);
				return;
			}

			int start = e.start;
			if (start > 9) return;
			int index = 0;
			for (int i = 0; i < chars.length; i++) {
				if (start + index == 4 || start + index == 7) {
					if (chars[i] == '/') {
						index++;
						continue;
					}
					buffer.insert(index++, '/');
				}
				if (chars[i] < '0' || '9' < chars[i]) return;
				if (start + index == 5 &&  '1' < chars[i]) return; /* [M]M */
				if (start + index == 8 &&  '3' < chars[i]) return; /* [D]D */
				index++;
			}
			String newText = buffer.toString();
			int length = newText.length();
			StringBuilder date = new StringBuilder(text.getText());
			date.replace(e.start, e.start + length, newText);
			calendar.set(Calendar.YEAR, 1901);
			calendar.set(Calendar.MONTH, Calendar.JANUARY);
			calendar.set(Calendar.DATE, 1);
			String yyyy = date.substring(0, 4);
			if (yyyy.indexOf('Y') == -1) {
				int year = Integer.parseInt(yyyy);
				calendar.set(Calendar.YEAR, year);
			}
			String mm = date.substring(5, 7);
			if (mm.indexOf('M') == -1) {
				int month =  Integer.parseInt(mm) - 1;
				int maxMonth = calendar.getActualMaximum(Calendar.MONTH);
				if (0 > month || month > maxMonth) return;
				calendar.set(Calendar.MONTH, month);
			}
			String dd = date.substring(8,10);
			if (dd.indexOf('D') == -1) {
				int day = Integer.parseInt(dd);
				int maxDay = calendar.getActualMaximum(Calendar.DATE);
				if (1 > day || day > maxDay) return;
				calendar.set(Calendar.DATE, day);
			} else {
				if (calendar.get(Calendar.MONTH)  == Calendar.FEBRUARY) {
					char firstChar = date.charAt(8);
					if (firstChar != 'D' && '2' < firstChar) return;
				}
			}
			text.setSelection(e.start, e.start + length);
			ignore = true;
			text.insert(newText);
			ignore = false;
		}
	});
	shell.pack();
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
	display.dispose();
}
}

Back to the top