Commit | Line | Data |
---|---|---|
c1cd9635 | 1 | /******************************************************************************* |
87f20035 | 2 | * Copyright (c) 2012, 2015 Ericsson |
c1cd9635 MAL |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | * Marc-Andre Laperle - Add time zone preference | |
12 | *******************************************************************************/ | |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.core.timestamp; |
c1cd9635 | 15 | |
202956f1 AM |
16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
17 | ||
c1cd9635 | 18 | import java.util.HashMap; |
8765b2d4 | 19 | import java.util.Locale; |
c1cd9635 MAL |
20 | import java.util.Map; |
21 | import java.util.TimeZone; | |
22 | ||
23 | import org.eclipse.core.runtime.Platform; | |
24 | import org.eclipse.core.runtime.preferences.DefaultScope; | |
25 | import org.eclipse.core.runtime.preferences.IEclipsePreferences; | |
87f20035 | 26 | import org.eclipse.core.runtime.preferences.IPreferencesService; |
c1cd9635 | 27 | import org.eclipse.core.runtime.preferences.InstanceScope; |
197ef97d | 28 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 | 29 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
c1cd9635 MAL |
30 | |
31 | /** | |
32 | * TMF Time format preferences | |
33 | * | |
34 | * @author Francois Chouinard | |
c1cd9635 | 35 | */ |
7c34a4ea | 36 | public final class TmfTimePreferences { |
c1cd9635 MAL |
37 | |
38 | // ------------------------------------------------------------------------ | |
39 | // Constants | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
42 | private static final String DATIME_DEFAULT = ITmfTimePreferencesConstants.TIME_HOUR_FMT; | |
43 | private static final String SUBSEC_DEFAULT = ITmfTimePreferencesConstants.SUBSEC_NANO_FMT; | |
44 | private static final String DATE_DELIMITER_DEFAULT = ITmfTimePreferencesConstants.DELIMITER_DASH; | |
45 | private static final String TIME_DELIMITER_DEFAULT = ITmfTimePreferencesConstants.DELIMITER_COLON; | |
46 | private static final String SSEC_DELIMITER_DEFAULT = ITmfTimePreferencesConstants.DELIMITER_SPACE; | |
47 | private static final String TIME_ZONE_DEFAULT = TimeZone.getDefault().getID(); | |
c1cd9635 MAL |
48 | |
49 | // ------------------------------------------------------------------------ | |
50 | // Constructor | |
51 | // ------------------------------------------------------------------------ | |
52 | ||
7c34a4ea AM |
53 | /** |
54 | * Local constructor | |
55 | */ | |
56 | private TmfTimePreferences() { | |
57 | } | |
58 | ||
c1cd9635 MAL |
59 | /** |
60 | * Initialize the default preferences and the singleton | |
61 | */ | |
62 | public static void init() { | |
63 | IEclipsePreferences defaultPreferences = DefaultScope.INSTANCE.getNode(Activator.PLUGIN_ID); | |
64 | defaultPreferences.put(ITmfTimePreferencesConstants.DATIME, DATIME_DEFAULT); | |
65 | defaultPreferences.put(ITmfTimePreferencesConstants.SUBSEC, SUBSEC_DEFAULT); | |
66 | defaultPreferences.put(ITmfTimePreferencesConstants.DATE_DELIMITER, DATE_DELIMITER_DEFAULT); | |
67 | defaultPreferences.put(ITmfTimePreferencesConstants.TIME_DELIMITER, TIME_DELIMITER_DEFAULT); | |
68 | defaultPreferences.put(ITmfTimePreferencesConstants.SSEC_DELIMITER, SSEC_DELIMITER_DEFAULT); | |
69 | defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, TIME_ZONE_DEFAULT); | |
70 | ||
7c34a4ea | 71 | TmfTimestampFormat.updateDefaultFormats(); |
c1cd9635 MAL |
72 | } |
73 | ||
74 | // ------------------------------------------------------------------------ | |
75 | // Getters/Setters | |
76 | // ------------------------------------------------------------------------ | |
77 | ||
78 | /** | |
79 | * Return the timestamp pattern | |
80 | * | |
81 | * @return the timestamp pattern | |
82 | */ | |
197ef97d | 83 | public static @NonNull String getTimePattern() { |
c1cd9635 MAL |
84 | return computeTimePattern(getPreferenceMap(false)); |
85 | } | |
86 | ||
87 | /** | |
88 | * Return the interval pattern | |
89 | * | |
90 | * @return the interval pattern | |
91 | */ | |
197ef97d | 92 | public static @NonNull String getIntervalPattern() { |
c1cd9635 MAL |
93 | return computeIntervalPattern(getPreferenceMap(false)); |
94 | } | |
95 | ||
96 | /** | |
97 | * Get the time zone | |
98 | * | |
99 | * @return the time zone | |
100 | */ | |
7c34a4ea | 101 | public static TimeZone getTimeZone() { |
87f20035 PT |
102 | String defaultId = TimeZone.getDefault().getID(); |
103 | IPreferencesService preferencesService = Platform.getPreferencesService(); | |
104 | if (preferencesService == null) { | |
105 | return TimeZone.getTimeZone(defaultId); | |
106 | } | |
107 | return TimeZone.getTimeZone(preferencesService.getString(Activator.PLUGIN_ID, ITmfTimePreferencesConstants.TIME_ZONE, defaultId, null)); | |
c1cd9635 MAL |
108 | } |
109 | ||
8765b2d4 PT |
110 | /** |
111 | * Get the locale | |
112 | * | |
113 | * @return the locale | |
8765b2d4 | 114 | */ |
7c34a4ea | 115 | public static Locale getLocale() { |
87f20035 PT |
116 | String defaultLanguageTag = Locale.getDefault().toLanguageTag(); |
117 | IPreferencesService preferencesService = Platform.getPreferencesService(); | |
118 | if (preferencesService == null) { | |
119 | return Locale.forLanguageTag(defaultLanguageTag); | |
120 | } | |
121 | return Locale.forLanguageTag(preferencesService.getString(Activator.PLUGIN_ID, ITmfTimePreferencesConstants.LOCALE, defaultLanguageTag, null)); | |
8765b2d4 PT |
122 | } |
123 | ||
c1cd9635 MAL |
124 | /** |
125 | * Get the default preferences map | |
126 | * | |
127 | * @return a collection containing the default preferences | |
128 | */ | |
7c34a4ea | 129 | public static Map<String, String> getDefaultPreferenceMap() { |
c1cd9635 MAL |
130 | return getPreferenceMap(true); |
131 | } | |
132 | ||
133 | /** | |
134 | * Get the current preferences map | |
135 | * | |
136 | * @return a collection containing the current preferences | |
137 | */ | |
7c34a4ea | 138 | public static Map<String, String> getPreferenceMap() { |
c1cd9635 MAL |
139 | return getPreferenceMap(false); |
140 | } | |
141 | ||
142 | private static Map<String, String> getPreferenceMap(boolean defaultValues) { | |
a4524c1b | 143 | Map<String, String> prefsMap = new HashMap<>(); |
c1cd9635 MAL |
144 | IEclipsePreferences prefs = defaultValues ? DefaultScope.INSTANCE.getNode(Activator.PLUGIN_ID) : InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID); |
145 | prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.SUBSEC, SUBSEC_DEFAULT); | |
146 | prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.TIME_DELIMITER, TIME_DELIMITER_DEFAULT); | |
147 | prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.SSEC_DELIMITER, SSEC_DELIMITER_DEFAULT); | |
148 | prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.DATIME, DATIME_DEFAULT); | |
149 | prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.DATE_DELIMITER, DATE_DELIMITER_DEFAULT); | |
9bb4b61e | 150 | prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.TIME_ZONE, TIME_ZONE_DEFAULT); |
c1cd9635 MAL |
151 | return prefsMap; |
152 | } | |
153 | ||
154 | // ------------------------------------------------------------------------ | |
155 | // Operations | |
156 | // ------------------------------------------------------------------------ | |
157 | ||
197ef97d | 158 | private static @NonNull String computeIntervalPattern(Map<String, String> prefsMap) { |
c1cd9635 MAL |
159 | String ssecFmt = computeSubSecFormat(prefsMap); |
160 | return ITmfTimePreferencesConstants.TIME_ELAPSED_FMT + "." + ssecFmt; //$NON-NLS-1$ | |
161 | } | |
162 | ||
163 | private static String computeSubSecFormat(Map<String, String> prefsMap) { | |
202956f1 | 164 | String sSecFormat = checkNotNull(prefsMap.get(ITmfTimePreferencesConstants.SUBSEC)); |
c1cd9635 MAL |
165 | String sSecFieldSep = prefsMap.get(ITmfTimePreferencesConstants.SSEC_DELIMITER); |
166 | String ssecFmt = sSecFormat.replaceAll(" ", sSecFieldSep); //$NON-NLS-1$ | |
167 | return ssecFmt; | |
168 | } | |
169 | ||
170 | private static void prefToMap(IEclipsePreferences node, Map<String, String> prefsMap, String key, String defaultValue) { | |
171 | prefsMap.put(key, node.get(key, defaultValue)); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Compute the time pattern with the collection of preferences | |
176 | * | |
177 | * @param prefsMap the preferences to apply when computing the time pattern | |
178 | * @return the time pattern resulting in applying the preferences | |
179 | */ | |
197ef97d | 180 | public static @NonNull String computeTimePattern(Map<String, String> prefsMap) { |
c1cd9635 MAL |
181 | String dateTimeFormat = prefsMap.get(ITmfTimePreferencesConstants.DATIME); |
182 | if (dateTimeFormat == null) { | |
183 | dateTimeFormat = ITmfTimePreferencesConstants.DEFAULT_TIME_PATTERN; | |
184 | } | |
185 | ||
186 | String dateFormat; | |
187 | String timeFormat; | |
188 | int index = dateTimeFormat.indexOf(' '); | |
189 | if (index != -1) { | |
190 | dateFormat = dateTimeFormat.substring(0, dateTimeFormat.indexOf(' ') + 1); | |
191 | timeFormat = dateTimeFormat.substring(dateFormat.length()); | |
192 | } else { | |
193 | dateFormat = ""; //$NON-NLS-1$ | |
194 | timeFormat = dateTimeFormat; | |
195 | } | |
196 | ||
197 | String dateFieldSep = prefsMap.get(ITmfTimePreferencesConstants.DATE_DELIMITER); | |
198 | String timeFieldSep = prefsMap.get(ITmfTimePreferencesConstants.TIME_DELIMITER); | |
199 | String dateFmt = dateFormat.replaceAll("-", dateFieldSep); //$NON-NLS-1$ | |
200 | String timeFmt = timeFormat.replaceAll(":", timeFieldSep); //$NON-NLS-1$ | |
201 | ||
202 | String ssecFmt = computeSubSecFormat(prefsMap); | |
203 | return dateFmt + timeFmt + (ssecFmt.equals(ITmfTimePreferencesConstants.SUBSEC_NO_FMT) ? "" : '.' + ssecFmt); //$NON-NLS-1$; | |
204 | } | |
205 | ||
206 | } |