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