1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Marc-Andre Laperle - Add time zone preference
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.timestamp
;
16 import java
.util
.HashMap
;
17 import java
.util
.Locale
;
19 import java
.util
.TimeZone
;
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
;
28 * TMF Time format preferences
30 * @author Francois Chouinard
34 public class TmfTimePreferences
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
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();
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 private static TmfTimePreferences fPreferences
;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * Initialize the default preferences and the singleton
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
);
68 // Create the singleton and update default formats
73 * Get the TmfTimePreferences singleton
75 * @return The TmfTimePreferences instance
77 public static synchronized TmfTimePreferences
getInstance() {
78 if (fPreferences
== null) {
79 fPreferences
= new TmfTimePreferences();
80 TmfTimestampFormat
.updateDefaultFormats();
88 private TmfTimePreferences() {
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
96 * Return the timestamp pattern
98 * @return the timestamp pattern
100 public String
getTimePattern() {
101 return computeTimePattern(getPreferenceMap(false));
105 * Return the interval pattern
107 * @return the interval pattern
109 public String
getIntervalPattern() {
110 return computeIntervalPattern(getPreferenceMap(false));
116 * @return the time zone
118 public TimeZone
getTimeZone() {
119 return TimeZone
.getTimeZone(Platform
.getPreferencesService().getString(Activator
.PLUGIN_ID
, ITmfTimePreferencesConstants
.TIME_ZONE
, TimeZone
.getDefault().getID(), null));
128 public Locale
getLocale() {
129 return Locale
.forLanguageTag(Platform
.getPreferencesService().getString(Activator
.PLUGIN_ID
, ITmfTimePreferencesConstants
.LOCALE
, Locale
.getDefault().toLanguageTag(), null));
133 * Get the default preferences map
135 * @return a collection containing the default preferences
137 public Map
<String
, String
> getDefaultPreferenceMap() {
138 return getPreferenceMap(true);
142 * Get the current preferences map
144 * @return a collection containing the current preferences
146 public Map
<String
, String
> getPreferenceMap() {
147 return getPreferenceMap(false);
150 private static Map
<String
, String
> getPreferenceMap(boolean defaultValues
) {
151 Map
<String
, String
> prefsMap
= new HashMap
<>();
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
);
161 // ------------------------------------------------------------------------
163 // ------------------------------------------------------------------------
165 private static String
computeIntervalPattern(Map
<String
, String
> prefsMap
) {
166 String ssecFmt
= computeSubSecFormat(prefsMap
);
167 return ITmfTimePreferencesConstants
.TIME_ELAPSED_FMT
+ "." + ssecFmt
; //$NON-NLS-1$
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$
177 private static void prefToMap(IEclipsePreferences node
, Map
<String
, String
> prefsMap
, String key
, String defaultValue
) {
178 prefsMap
.put(key
, node
.get(key
, defaultValue
));
182 * Compute the time pattern with the collection of preferences
184 * @param prefsMap the preferences to apply when computing the time pattern
185 * @return the time pattern resulting in applying the preferences
187 public String
computeTimePattern(Map
<String
, String
> prefsMap
) {
188 String dateTimeFormat
= prefsMap
.get(ITmfTimePreferencesConstants
.DATIME
);
189 if (dateTimeFormat
== null) {
190 dateTimeFormat
= ITmfTimePreferencesConstants
.DEFAULT_TIME_PATTERN
;
195 int index
= dateTimeFormat
.indexOf(' ');
197 dateFormat
= dateTimeFormat
.substring(0, dateTimeFormat
.indexOf(' ') + 1);
198 timeFormat
= dateTimeFormat
.substring(dateFormat
.length());
200 dateFormat
= ""; //$NON-NLS-1$
201 timeFormat
= dateTimeFormat
;
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$
209 String ssecFmt
= computeSubSecFormat(prefsMap
);
210 return dateFmt
+ timeFmt
+ (ssecFmt
.equals(ITmfTimePreferencesConstants
.SUBSEC_NO_FMT
) ?
"" : '.' + ssecFmt
); //$NON-NLS-1$;