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