Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f53211a132d647bf400e27784043fd152a3a08bc (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
/*******************************************************************************
 * 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;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.eclipse.core.runtime.Assert;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.util.GitDateFormatter;

/**
 * A date formatter that formats dates according to the user's preferences.
 *
 * @see org.eclipse.egit.ui.internal.preferences.DateFormatPreferencePage
 */
public class PreferenceBasedDateFormatter extends GitDateFormatter {

	private final SimpleDateFormat customFormat;

	private final GitDateFormatter.Format gitFormat;

	/**
	 * Creates a new {@link PreferenceBasedDateFormatter} that will format dates
	 * according to the date format preferences set at the time it was created.
	 *
	 * @return a new {@link PreferenceBasedDateFormatter}
	 */
	public static @NonNull PreferenceBasedDateFormatter create() {
		String choice = Activator.getDefault().getPreferenceStore()
				.getString(UIPreferences.DATE_FORMAT_CHOICE);
		GitDateFormatter.Format format = null;
		try {
			format = GitDateFormatter.Format.valueOf(choice);
		} catch (IllegalArgumentException | NullPointerException e) {
			// Custom format: ignore and leave at null
		}
		return new PreferenceBasedDateFormatter(format);
	}

	private PreferenceBasedDateFormatter(GitDateFormatter.Format gitFormat) {
		super(gitFormat != null ? gitFormat : GitDateFormatter.Format.DEFAULT);
		this.gitFormat = gitFormat;
		SimpleDateFormat format = null;
		if (gitFormat == null) {
			String pattern = Activator.getDefault().getPreferenceStore()
					.getString(UIPreferences.DATE_FORMAT);
			try {
				format = new SimpleDateFormat(pattern);
			} catch (IllegalArgumentException | NullPointerException e1) {
				// Corrupted preferences?
				Activator.logError("org.eclipse.egit.ui preference " //$NON-NLS-1$
						+ UIPreferences.DATE_FORMAT + " is invalid; ignoring", //$NON-NLS-1$
						e1);
				format = new SimpleDateFormat(
						Activator.getDefault().getPreferenceStore()
								.getDefaultString(UIPreferences.DATE_FORMAT));
			}
		}
		this.customFormat = format;
	}

	/**
	 * Formats a {@link Date} using the default {@link TimeZone} according to
	 * the date format preferences set when this instance was created.
	 *
	 * @param date
	 *            to format
	 * @return the string representation of the date, or the empty string if the
	 *         date is {@code null}.
	 */
	public String formatDate(Date date) {
		if (date == null) {
			return ""; //$NON-NLS-1$
		}
		TimeZone timeZone = TimeZone.getDefault();
		Assert.isNotNull(timeZone);
		return formatDate(date, timeZone);
	}

	/**
	 * Formats a {@link Date} using the given {@link TimeZone} according to the
	 * date format preferences set when this instance was created.
	 *
	 * @param date
	 *            to format
	 * @param timeZone
	 *            to use
	 * @return the string representation of the date
	 */
	public String formatDate(@NonNull Date date, @NonNull TimeZone timeZone) {
		return formatDate(new PersonIdent("", "", date, timeZone)); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * Formats the date of a {@link PersonIdent} according to the date format
	 * preferences set when this instance was created.
	 *
	 * @param ident
	 *            to format the date of
	 * @return the string representation of the date, or the empty string if
	 *         {@code ident} is {@code null}.
	 */
	@Override
	public String formatDate(PersonIdent ident) {
		if (ident == null) {
			return ""; //$NON-NLS-1$
		}
		if (customFormat == null) {
			return super.formatDate(ident);
		}
		return customFormat.format(ident.getWhen());
	}

	/**
	 * Retrieves the {@link org.eclipse.jgit.util.GitDateFormatter.Format
	 * GitDateFormatter.Format} this formatter uses.
	 *
	 * @return the format, or {@code null} if a user-defined date format pattern
	 *         is used
	 */
	public @Nullable GitDateFormatter.Format getFormat() {
		return gitFormat;
	}
}

Back to the top