tmf.all: use ITmfTimestamp#toNanos when possible
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / timestamp / TmfTimePreferences.java
CommitLineData
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 14package org.eclipse.tracecompass.tmf.core.timestamp;
c1cd9635 15
202956f1
AM
16import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
c1cd9635 18import java.util.HashMap;
8765b2d4 19import java.util.Locale;
c1cd9635
MAL
20import java.util.Map;
21import java.util.TimeZone;
22
23import org.eclipse.core.runtime.Platform;
24import org.eclipse.core.runtime.preferences.DefaultScope;
25import org.eclipse.core.runtime.preferences.IEclipsePreferences;
87f20035 26import org.eclipse.core.runtime.preferences.IPreferencesService;
c1cd9635 27import org.eclipse.core.runtime.preferences.InstanceScope;
2bdf0193 28import org.eclipse.tracecompass.internal.tmf.core.Activator;
c1cd9635
MAL
29
30/**
31 * TMF Time format preferences
32 *
33 * @author Francois Chouinard
c1cd9635 34 */
7c34a4ea 35public final class TmfTimePreferences {
c1cd9635
MAL
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();
c1cd9635
MAL
47
48 // ------------------------------------------------------------------------
49 // Constructor
50 // ------------------------------------------------------------------------
51
7c34a4ea
AM
52 /**
53 * Local constructor
54 */
55 private TmfTimePreferences() {
56 }
57
c1cd9635
MAL
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
7c34a4ea 70 TmfTimestampFormat.updateDefaultFormats();
c1cd9635
MAL
71 }
72
73 // ------------------------------------------------------------------------
74 // Getters/Setters
75 // ------------------------------------------------------------------------
76
77 /**
78 * Return the timestamp pattern
79 *
80 * @return the timestamp pattern
81 */
7c34a4ea 82 public static String getTimePattern() {
c1cd9635
MAL
83 return computeTimePattern(getPreferenceMap(false));
84 }
85
86 /**
87 * Return the interval pattern
88 *
89 * @return the interval pattern
90 */
7c34a4ea 91 public static String getIntervalPattern() {
c1cd9635
MAL
92 return computeIntervalPattern(getPreferenceMap(false));
93 }
94
95 /**
96 * Get the time zone
97 *
98 * @return the time zone
99 */
7c34a4ea 100 public static TimeZone getTimeZone() {
87f20035
PT
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));
c1cd9635
MAL
107 }
108
8765b2d4
PT
109 /**
110 * Get the locale
111 *
112 * @return the locale
8765b2d4 113 */
7c34a4ea 114 public static Locale getLocale() {
87f20035
PT
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));
8765b2d4
PT
121 }
122
c1cd9635
MAL
123 /**
124 * Get the default preferences map
125 *
126 * @return a collection containing the default preferences
127 */
7c34a4ea 128 public static Map<String, String> getDefaultPreferenceMap() {
c1cd9635
MAL
129 return getPreferenceMap(true);
130 }
131
132 /**
133 * Get the current preferences map
134 *
135 * @return a collection containing the current preferences
136 */
7c34a4ea 137 public static Map<String, String> getPreferenceMap() {
c1cd9635
MAL
138 return getPreferenceMap(false);
139 }
140
141 private static Map<String, String> getPreferenceMap(boolean defaultValues) {
a4524c1b 142 Map<String, String> prefsMap = new HashMap<>();
c1cd9635
MAL
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);
9bb4b61e 149 prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.TIME_ZONE, TIME_ZONE_DEFAULT);
c1cd9635
MAL
150 return prefsMap;
151 }
152
153 // ------------------------------------------------------------------------
154 // Operations
155 // ------------------------------------------------------------------------
156
157 private static String computeIntervalPattern(Map<String, String> prefsMap) {
158 String ssecFmt = computeSubSecFormat(prefsMap);
159 return ITmfTimePreferencesConstants.TIME_ELAPSED_FMT + "." + ssecFmt; //$NON-NLS-1$
160 }
161
162 private static String computeSubSecFormat(Map<String, String> prefsMap) {
202956f1 163 String sSecFormat = checkNotNull(prefsMap.get(ITmfTimePreferencesConstants.SUBSEC));
c1cd9635
MAL
164 String sSecFieldSep = prefsMap.get(ITmfTimePreferencesConstants.SSEC_DELIMITER);
165 String ssecFmt = sSecFormat.replaceAll(" ", sSecFieldSep); //$NON-NLS-1$
166 return ssecFmt;
167 }
168
169 private static void prefToMap(IEclipsePreferences node, Map<String, String> prefsMap, String key, String defaultValue) {
170 prefsMap.put(key, node.get(key, defaultValue));
171 }
172
173 /**
174 * Compute the time pattern with the collection of preferences
175 *
176 * @param prefsMap the preferences to apply when computing the time pattern
177 * @return the time pattern resulting in applying the preferences
178 */
7c34a4ea 179 public static String computeTimePattern(Map<String, String> prefsMap) {
c1cd9635
MAL
180 String dateTimeFormat = prefsMap.get(ITmfTimePreferencesConstants.DATIME);
181 if (dateTimeFormat == null) {
182 dateTimeFormat = ITmfTimePreferencesConstants.DEFAULT_TIME_PATTERN;
183 }
184
185 String dateFormat;
186 String timeFormat;
187 int index = dateTimeFormat.indexOf(' ');
188 if (index != -1) {
189 dateFormat = dateTimeFormat.substring(0, dateTimeFormat.indexOf(' ') + 1);
190 timeFormat = dateTimeFormat.substring(dateFormat.length());
191 } else {
192 dateFormat = ""; //$NON-NLS-1$
193 timeFormat = dateTimeFormat;
194 }
195
196 String dateFieldSep = prefsMap.get(ITmfTimePreferencesConstants.DATE_DELIMITER);
197 String timeFieldSep = prefsMap.get(ITmfTimePreferencesConstants.TIME_DELIMITER);
198 String dateFmt = dateFormat.replaceAll("-", dateFieldSep); //$NON-NLS-1$
199 String timeFmt = timeFormat.replaceAll(":", timeFieldSep); //$NON-NLS-1$
200
201 String ssecFmt = computeSubSecFormat(prefsMap);
202 return dateFmt + timeFmt + (ssecFmt.equals(ITmfTimePreferencesConstants.SUBSEC_NO_FMT) ? "" : '.' + ssecFmt); //$NON-NLS-1$;
203 }
204
205}
This page took 0.111596 seconds and 5 git commands to generate.