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