Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 290318f6c05497042dbebddab4086be5d052b34c (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
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
/*******************************************************************************
 * Copyright (C) 2015 Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * All rights reserved. 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
 *******************************************************************************/
package org.eclipse.egit.ui.internal.preferences;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TimeZone;

import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.egit.ui.internal.SWTUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.util.GitDateFormatter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/** Preferences concerning date formats in EGit. */
public class DateFormatPreferencePage extends FieldEditorPreferencePage
		implements IWorkbenchPreferencePage {

	private static final PersonIdent SAMPLE = new PersonIdent("", "", //$NON-NLS-1$ //$NON-NLS-2$
			new Date(System.currentTimeMillis() - 24 * 3600 * 1000),
			getDifferentTimeZone());

	private static final Map<GitDateFormatter.Format, FormatInfo> DATA = initializeData();

	private ComboFieldEditor formatChooser;

	private StringFieldEditor dateFormat;

	private Label dateFormatPreview;

	private Label formatExplanation;

	private String lastCustomValue;

	/** Creates a new instance. */
	public DateFormatPreferencePage() {
		super(GRID);
		initializeData();
		setTitle(UIText.DateFormatPreferencePage_title);
	}

	@Override
	public void init(IWorkbench workbench) {
		// Nothing to do
	}

	@Override
	protected IPreferenceStore doGetPreferenceStore() {
		return Activator.getDefault().getPreferenceStore();
	}

	@Override
	protected void createFieldEditors() {
		String[][] values = new String[DATA.size()][2];
		int i = 0;
		for (Map.Entry<GitDateFormatter.Format, FormatInfo> entry : DATA
				.entrySet()) {
			values[i][0] = entry.getValue().name;
			values[i][1] = entry.getKey() == null
					? UIPreferences.DATE_FORMAT_CUSTOM : entry.getKey().name();
			i++;
		}
		final Composite pane = getFieldEditorParent();
		formatChooser = new ComboFieldEditor(
				UIPreferences.DATE_FORMAT_CHOICE,
				UIText.DateFormatPreferencePage_formatChooser_label,
				values,
				pane);
		addField(formatChooser);
		dateFormat = new StringFieldEditor(
				UIPreferences.DATE_FORMAT,
				UIText.DateFormatPreferencePage_formatInput_label,
				StringFieldEditor.UNLIMITED,
				StringFieldEditor.VALIDATE_ON_KEY_STROKE, pane) {
			@Override
			protected boolean doCheckState() {
				// Validate the contents. If we're disabled, we're showing some
				// built-in format string, which we always consider as valid.
				if (!getTextControl(pane).isEnabled()) {
					return true;
				}
				try {
					updatePreview(
							new SimpleDateFormat(getStringValue().trim()));
					return true;
				} catch (IllegalArgumentException e) {
					dateFormatPreview.setText(""); //$NON-NLS-1$
					return false;
				}
			}

			@Override
			protected void doLoad() {
				// Set explicitly below
			}

			@Override
			protected void doStore() {
				// Never store invalid values, or built-in values
				if (getTextControl(pane).isEnabled() && doCheckState()) {
					super.doStore();
				}
			}

			@Override
			public void setStringValue(String value) {
				super.setStringValue(value);
				refreshValidState();
			}
		};
		dateFormat.setEmptyStringAllowed(false);
		dateFormat.setErrorMessage(
				UIText.DateFormatPreferencePage_invalidDateFormat_message);
		addField(dateFormat);
		// We know that the layout will have two columns
		Label dpLabel = SWTUtils.createLabel(pane,
				UIText.DateFormatPreferencePage_datePreview_label);
		dpLabel.setLayoutData(SWTUtils.createGridData(SWT.DEFAULT, SWT.DEFAULT,
				false, false));
		dateFormatPreview = SWTUtils.createLabel(pane, null, 1);
		Label dummyLabel = SWTUtils.createLabel(pane, ""); //$NON-NLS-1$
		dummyLabel.setLayoutData(SWTUtils.createGridData(SWT.DEFAULT,
				SWT.DEFAULT, false, false));
		formatExplanation = new Label(pane, SWT.LEFT | SWT.WRAP);
		GridData layout = SWTUtils.createGridData(SWT.DEFAULT, SWT.DEFAULT,
				true, false);
		layout.widthHint = 150; // For wrapping
		formatExplanation.setLayoutData(layout);
		// Setup based on initial values. We don't get any events by the editors
		// on initial load!
		lastCustomValue = getPreferenceStore()
				.getString(UIPreferences.DATE_FORMAT);
		String initialValue = getPreferenceStore()
				.getString(UIPreferences.DATE_FORMAT_CHOICE);
		updateFields(initialValue);
	}

	@Override
	protected void initialize() {
		super.initialize();
		// When the chooser's selection changes, update the dateFormat &
		// enablement
		formatChooser.setPropertyChangeListener(new IPropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if (FieldEditor.VALUE.equals(event.getProperty())) {
					GitDateFormatter.Format format = fromString(
							(String) event.getOldValue());
					if (format == null) {
						lastCustomValue = dateFormat.getStringValue();
					}
					updateFields((String) event.getNewValue());
				}
			}
		});
	}

	private void updateFields(String newSelection) {
		GitDateFormatter.Format format = fromString(newSelection);
		FormatInfo info = DATA.get(format);
		formatExplanation.setText(info.explanation);
		if (format == null) {
			dateFormat.getTextControl(getFieldEditorParent()).setEnabled(true);
			dateFormat.setStringValue(lastCustomValue);
		} else {
			dateFormat.getTextControl(getFieldEditorParent()).setEnabled(false);
			dateFormat.setStringValue(info.format);
			updatePreview(format);
		}
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		// We don't get property changed events when the default values are
		// restored...
		lastCustomValue = getPreferenceStore()
				.getDefaultString(UIPreferences.DATE_FORMAT);
		updateFields(getPreferenceStore()
				.getDefaultString(UIPreferences.DATE_FORMAT_CHOICE));
	}

	private GitDateFormatter.Format fromString(String value) {
		try {
			return GitDateFormatter.Format.valueOf(value);
		} catch (IllegalArgumentException | NullPointerException e) {
			return null;
		}
	}

	private void updatePreview(SimpleDateFormat format) {
		dateFormatPreview.setText(format.format(SAMPLE.getWhen()));
	}

	private void updatePreview(GitDateFormatter.Format format) {
		dateFormatPreview
				.setText(new GitDateFormatter(format).formatDate(SAMPLE));
	}

	private static Map<GitDateFormatter.Format, FormatInfo> initializeData() {
		Map<GitDateFormatter.Format, FormatInfo> d = new LinkedHashMap<>();
		d.put(GitDateFormatter.Format.DEFAULT,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceGitDefault_label,
						"EEE MMM dd HH:mm:ss yyyy Z", //$NON-NLS-1$
						UIText.DateFormatPreferencePage_helpGitDefault_label));
		d.put(GitDateFormatter.Format.LOCAL,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceGitLocal_label,
						"EEE MMM dd HH:mm:ss yyyy", //$NON-NLS-1$
						UIText.DateFormatPreferencePage_helpGitLocal_label));
		d.put(GitDateFormatter.Format.RELATIVE,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceGitRelative_label,
						UIText.DateFormatPreferencePage_gitRelative_format_text,
						UIText.DateFormatPreferencePage_helpGitRelative_label));
		d.put(GitDateFormatter.Format.ISO,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceGitIso_label,
						"yyyy-MM-dd HH:mm:ss Z", //$NON-NLS-1$
						UIText.DateFormatPreferencePage_helpGitIso_label));
		d.put(GitDateFormatter.Format.RFC,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceGitRfc_label,
						"EEE, dd MMM yyyy HH:mm:ss Z", //$NON-NLS-1$
						UIText.DateFormatPreferencePage_helpGitRfc_label));
		d.put(GitDateFormatter.Format.SHORT,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceGitShort_label,
						"yyyy-MM-dd", //$NON-NLS-1$
						UIText.DateFormatPreferencePage_helpGitShort_label));
		DateFormat systemFormat = DateFormat
				.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);
		String localeLocalFormat = (systemFormat instanceof SimpleDateFormat)
				? ((SimpleDateFormat) systemFormat).toPattern()
				: UIText.DateFormatPreferencePage_gitLocaleLocal_format_text;
		String localeFormat = (systemFormat instanceof SimpleDateFormat)
				? localeLocalFormat + " Z" //$NON-NLS-1$
				: UIText.DateFormatPreferencePage_gitLocale_format_text;
		d.put(GitDateFormatter.Format.LOCALE,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceGitLocale_label,
						localeFormat,
						UIText.DateFormatPreferencePage_helpGitLocale_label));
		d.put(GitDateFormatter.Format.LOCALELOCAL, new FormatInfo(
				UIText.DateFormatPreferencePage_choiceGitLocaleLocal_label,
				localeLocalFormat,
				UIText.DateFormatPreferencePage_helpGitLocaleLocal_label));
		d.put(null,
				new FormatInfo(
						UIText.DateFormatPreferencePage_choiceCustom_label, "", //$NON-NLS-1$
						UIText.DateFormatPreferencePage_helpCustom_label));
		return d;
	}

	private static TimeZone getDifferentTimeZone() {
		TimeZone localTimeZone = TimeZone.getDefault();
		int offset = (localTimeZone.getRawOffset() / 3600 / 1000 - 6);
		// 6h to the West, full hour. Now get back to a sane range:
		if (offset < -12) {
			offset += 24;
		}
		String[] zoneIds = TimeZone.getAvailableIDs(offset * 3600 * 1000);
		if (zoneIds.length == 0) {
			// Huh?
			return localTimeZone;
		}
		return TimeZone.getTimeZone(zoneIds[0]);
	}

	private static final class FormatInfo {
		private final String name;

		private final String format;

		private final String explanation;

		public FormatInfo(String name, String dateFormat, String explanation) {
			this.name = name;
			this.format = dateFormat;
			this.explanation = explanation;
		}

	}
}

Back to the top