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