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
;
18 import java
.util
.TimeZone
;
20 import org
.eclipse
.core
.runtime
.Platform
;
21 import org
.eclipse
.core
.runtime
.preferences
.DefaultScope
;
22 import org
.eclipse
.core
.runtime
.preferences
.IEclipsePreferences
;
23 import org
.eclipse
.core
.runtime
.preferences
.InstanceScope
;
24 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
27 * TMF Time format preferences
29 * @author Francois Chouinard
33 public class TmfTimePreferences
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
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 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 private static TmfTimePreferences fPreferences
;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * Initialize the default preferences and the singleton
58 public static void init() {
59 IEclipsePreferences defaultPreferences
= DefaultScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
60 defaultPreferences
.put(ITmfTimePreferencesConstants
.DATIME
, DATIME_DEFAULT
);
61 defaultPreferences
.put(ITmfTimePreferencesConstants
.SUBSEC
, SUBSEC_DEFAULT
);
62 defaultPreferences
.put(ITmfTimePreferencesConstants
.DATE_DELIMITER
, DATE_DELIMITER_DEFAULT
);
63 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_DELIMITER
, TIME_DELIMITER_DEFAULT
);
64 defaultPreferences
.put(ITmfTimePreferencesConstants
.SSEC_DELIMITER
, SSEC_DELIMITER_DEFAULT
);
65 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, TIME_ZONE_DEFAULT
);
67 // Create the singleton and update default formats
72 * Get the TmfTimePreferences singleton
74 * @return The TmfTimePreferences instance
76 public static synchronized TmfTimePreferences
getInstance() {
77 if (fPreferences
== null) {
78 fPreferences
= new TmfTimePreferences();
79 TmfTimestampFormat
.updateDefaultFormats();
87 private TmfTimePreferences() {
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 * Return the timestamp pattern
97 * @return the timestamp pattern
99 public String
getTimePattern() {
100 return computeTimePattern(getPreferenceMap(false));
104 * Return the interval pattern
106 * @return the interval pattern
108 public String
getIntervalPattern() {
109 return computeIntervalPattern(getPreferenceMap(false));
115 * @return the time zone
117 public TimeZone
getTimeZone() {
118 return TimeZone
.getTimeZone(Platform
.getPreferencesService().getString(Activator
.PLUGIN_ID
, ITmfTimePreferencesConstants
.TIME_ZONE
, TimeZone
.getDefault().getID(), null));
122 * Get the default preferences map
124 * @return a collection containing the default preferences
126 public Map
<String
, String
> getDefaultPreferenceMap() {
127 return getPreferenceMap(true);
131 * Get the current preferences map
133 * @return a collection containing the current preferences
135 public Map
<String
, String
> getPreferenceMap() {
136 return getPreferenceMap(false);
139 private static Map
<String
, String
> getPreferenceMap(boolean defaultValues
) {
140 Map
<String
, String
> prefsMap
= new HashMap
<String
, String
>();
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
);
150 // ------------------------------------------------------------------------
152 // ------------------------------------------------------------------------
154 private static String
computeIntervalPattern(Map
<String
, String
> prefsMap
) {
155 String ssecFmt
= computeSubSecFormat(prefsMap
);
156 return ITmfTimePreferencesConstants
.TIME_ELAPSED_FMT
+ "." + ssecFmt
; //$NON-NLS-1$
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$
166 private static void prefToMap(IEclipsePreferences node
, Map
<String
, String
> prefsMap
, String key
, String defaultValue
) {
167 prefsMap
.put(key
, node
.get(key
, defaultValue
));
171 * Compute the time pattern with the collection of preferences
173 * @param prefsMap the preferences to apply when computing the time pattern
174 * @return the time pattern resulting in applying the preferences
176 public String
computeTimePattern(Map
<String
, String
> prefsMap
) {
177 String dateTimeFormat
= prefsMap
.get(ITmfTimePreferencesConstants
.DATIME
);
178 if (dateTimeFormat
== null) {
179 dateTimeFormat
= ITmfTimePreferencesConstants
.DEFAULT_TIME_PATTERN
;
184 int index
= dateTimeFormat
.indexOf(' ');
186 dateFormat
= dateTimeFormat
.substring(0, dateTimeFormat
.indexOf(' ') + 1);
187 timeFormat
= dateTimeFormat
.substring(dateFormat
.length());
189 dateFormat
= ""; //$NON-NLS-1$
190 timeFormat
= dateTimeFormat
;
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$
198 String ssecFmt
= computeSubSecFormat(prefsMap
);
199 return dateFmt
+ timeFmt
+ (ssecFmt
.equals(ITmfTimePreferencesConstants
.SUBSEC_NO_FMT
) ?
"" : '.' + ssecFmt
); //$NON-NLS-1$;