Skip to main content
summaryrefslogtreecommitdiffstats
blob: e301daedad81fc4a6f1fb8fe0f93d1185c97b6f7 (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
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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;

import java.text.SimpleDateFormat;
import java.util.*;

import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.internal.ui.dialogs.DialogArea;
import org.eclipse.ui.help.WorkbenchHelp;

/**
 * Dialog for obtaining a date from the user
 */
public class DateTagDialog extends Dialog {

	DateArea dateArea;
	TimeArea timeArea;
	IDialogSettings settings;
	private Date dateEntered;

	public class DateArea extends DialogArea {

		private Combo fromDayCombo;
		private Combo fromMonthCombo;
		private Combo fromYearCombo;
		
		public DateArea(Dialog parentDialog, IDialogSettings settings) {
			super(parentDialog, settings);
		}

		public void createArea(Composite parent) {
			Composite composite = createComposite(parent, 4);
			initializeDialogUnits(composite);
			createLabel(composite, Policy.bind("DateTagDialog.0"), 1); //$NON-NLS-1$
			fromMonthCombo = new Combo(composite, SWT.READ_ONLY);
			fromDayCombo = new Combo(composite, SWT.READ_ONLY);
			fromDayCombo.setTextLimit(2);
			fromYearCombo = new Combo(composite, SWT.NONE);
			fromYearCombo.setTextLimit(4);
			
			//set day, month and year combos with numbers
			//years allows a selection from the past 5 years
			//or any year written in
			String days[] = new String[31];
			for (int i = 0; i < 31; i++) {
				days[i] = String.valueOf(i+1);
			}

			String months[] = new String[12];
			SimpleDateFormat format = new SimpleDateFormat("MMMM"); //$NON-NLS-1$
			Calendar calendar = Calendar.getInstance();
			for (int i = 0; i < 12; i++) {
				calendar.set(Calendar.MONTH, i);
				months[i] = format.format(calendar.getTime());
			}

			String years[] = new String[5];
			Calendar calender = Calendar.getInstance();
			for (int i = 0; i < 5; i++) {
				years[i] = String.valueOf(calender.get(1) - i);
			}
			fromDayCombo.setItems(days);
			fromMonthCombo.setItems(months);
			fromYearCombo.setItems(years);
		}
		
		public void initializeValues(Calendar calendar ) {
			fromDayCombo.select(calendar.get(Calendar.DATE) - 1);
			fromMonthCombo.select(calendar.get(Calendar.MONTH));
			String yearValue = String.valueOf(calendar.get(Calendar.YEAR));
			int index = fromYearCombo.indexOf(yearValue);
			if (index == -1) {
				fromYearCombo.add(yearValue);
				index = fromYearCombo.indexOf(yearValue);
			}
			fromYearCombo.select(index);
			timeArea.initializeValues(calendar);
		}

		public void updateWidgetEnablements() {
			// Do nothing
		}
		
		public void adjustCalendar(Calendar calendar) {
			calendar.set(
					Integer.parseInt(String.valueOf(fromYearCombo.getText())),
					fromMonthCombo.getSelectionIndex(),
					Integer.parseInt(String.valueOf(fromDayCombo.getText())),
					0,0,0);
		}
	}
	public class TimeArea extends DialogArea {

		private Combo hourCombo;
		private Combo minuteCombo;
		private Combo secondCombo;
		private Button includeTime, localTime, utcTime;
		
		public TimeArea(Dialog parentDialog, IDialogSettings settings) {
			super(parentDialog, settings);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.team.internal.ui.dialogs.DialogArea#createArea(org.eclipse.swt.widgets.Composite)
		 */
		public void createArea(Composite parent) {
			Composite composite = createComposite(parent, 2);
			initializeDialogUnits(composite);
			includeTime = createCheckbox(composite, Policy.bind("DateTagDialog.1"), 2);  //$NON-NLS-1$
			createLabel(composite, Policy.bind("DateTagDialog.2"), 1); //$NON-NLS-1$
			Composite dateComposite = new Composite(composite, SWT.NONE);
			GridLayout dateLayout = new GridLayout();
			dateLayout.numColumns = 3;
			dateComposite.setLayout(dateLayout);
			hourCombo = new Combo(dateComposite, SWT.READ_ONLY);
			hourCombo.setTextLimit(2);
			minuteCombo = new Combo(dateComposite, SWT.READ_ONLY);
			minuteCombo.setTextLimit(2);
			secondCombo = new Combo(dateComposite, SWT.READ_ONLY);
			secondCombo.setTextLimit(2);
			localTime = createRadioButton(composite, Policy.bind("DateTagDialog.3"), 2);  //$NON-NLS-1$
			utcTime = createRadioButton(composite, Policy.bind("DateTagDialog.4"), 2);  //$NON-NLS-1$
			
			String sixty[] = new String[60];
			for (int i = 0; i < 60; i++) {
				sixty[i] = String.valueOf(i);
			}
			String hours[] = new String[24];
			for (int i = 0; i < 24; i++) {
				hours[i] = String.valueOf(i);
			}
			hourCombo.setItems(hours);
			minuteCombo.setItems(sixty);
			secondCombo.setItems(sixty);
			
			includeTime.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					updateWidgetEnablements();
				}
			});
		}
		
		public void initializeValues(Calendar calendar) {
			hourCombo.select(calendar.get(Calendar.HOUR_OF_DAY));//24 hour clock
			minuteCombo.select(calendar.get(Calendar.MINUTE));
			secondCombo.select(calendar.get(Calendar.SECOND));
			
			includeTime.setSelection(settings.getBoolean("includeTime")); //$NON-NLS-1$
			localTime.setSelection(!settings.getBoolean("utcTime")); //$NON-NLS-1$
			utcTime.setSelection(settings.getBoolean("utcTime")); //$NON-NLS-1$
		}
		public void updateWidgetEnablements() {
			hourCombo.setEnabled(includeTime.getSelection());
			minuteCombo.setEnabled(includeTime.getSelection());
			secondCombo.setEnabled(includeTime.getSelection());
			localTime.setEnabled(includeTime.getSelection());
			utcTime.setEnabled(includeTime.getSelection());
		}
		public void adjustCalendar(Calendar calendar) {
			if (includeTime.getSelection()) {
				calendar.set(Calendar.HOUR_OF_DAY, hourCombo.getSelectionIndex());//24 hour clock
				calendar.set(Calendar.MINUTE, minuteCombo.getSelectionIndex());
				calendar.set(Calendar.SECOND, secondCombo.getSelectionIndex());
				if (utcTime.getSelection()) {
					calendar.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
				}
			}
		}
	}
	
	public DateTagDialog(Shell parentShell) {
		super(parentShell);
		IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
		this.settings = workbenchSettings.getSection("DateTagDialog");//$NON-NLS-1$
		if (this.settings == null) {
			this.settings = workbenchSettings.addNewSection("DateTagDialog");//$NON-NLS-1$
		}
	}
	
	/* (non-Javadoc)
	 * Method declared on Window.
	 */
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Policy.bind("DateTagDialog.5")); //$NON-NLS-1$
	} 

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	protected Control createDialogArea(Composite parent) {
		Composite topLevel = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		initializeDialogUnits(topLevel);
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		topLevel.setLayout(layout);
		
		createDateArea(topLevel);
		createTimeArea(topLevel);
		initializeValues();
		updateWidgetEnablements();
		
		// set F1 help
		WorkbenchHelp.setHelp(topLevel, IHelpContextIds.DATE_TAG_DIALOG);
		Dialog.applyDialogFont(parent);
		return topLevel;
	}

	private void createDateArea(Composite topLevel) {
		dateArea = new DateArea(this, settings);
		dateArea.createArea(topLevel);
	}

	private void createTimeArea(Composite topLevel) {
		timeArea = new TimeArea(this, settings);
		timeArea.createArea(topLevel);
	}

	private void initializeValues() {
		Calendar calendar = Calendar.getInstance();
		dateArea.initializeValues(calendar);
		timeArea.initializeValues(calendar);
	}
	
	private void updateWidgetEnablements() {
		timeArea.updateWidgetEnablements();
		dateArea.updateWidgetEnablements();
	}

	/**
	 * Return the date specified by the user in UTC.
	 * @return the date specified by the user
	 */
	public Date getDate() {
		return dateEntered;
	}
	
	private Date privateGetDate() {
		Calendar calendar = Calendar.getInstance();
		dateArea.adjustCalendar(calendar);
		timeArea.adjustCalendar(calendar);
		return calendar.getTime();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 */
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			dateEntered = privateGetDate();
		}
		super.buttonPressed(buttonId);
	}

}

Back to the top