Commit | Line | Data |
---|---|---|
c1cd9635 MAL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012, 2013 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.linuxtools.tmf.core.timestamp; | |
15 | ||
16 | import java.util.HashMap; | |
17 | import java.util.Map; | |
18 | import java.util.TimeZone; | |
19 | ||
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; | |
25 | ||
26 | /** | |
27 | * TMF Time format preferences | |
28 | * | |
29 | * @author Francois Chouinard | |
30 | * @version 1.0 | |
31 | * @since 2.1 | |
32 | */ | |
33 | public 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 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
49 | private static TmfTimePreferences fPreferences; | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Constructor | |
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 | // Create the singleton and update default formats | |
68 | getInstance(); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Get the TmfTimePreferences singleton | |
73 | * | |
74 | * @return The TmfTimePreferences instance | |
75 | */ | |
76 | public static synchronized TmfTimePreferences getInstance() { | |
77 | if (fPreferences == null) { | |
78 | fPreferences = new TmfTimePreferences(); | |
79 | TmfTimestampFormat.updateDefaultFormats(); | |
80 | } | |
81 | return fPreferences; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Local constructor | |
86 | */ | |
87 | private TmfTimePreferences() { | |
88 | } | |
89 | ||
90 | // ------------------------------------------------------------------------ | |
91 | // Getters/Setters | |
92 | // ------------------------------------------------------------------------ | |
93 | ||
94 | /** | |
95 | * Return the timestamp pattern | |
96 | * | |
97 | * @return the timestamp pattern | |
98 | */ | |
99 | public String getTimePattern() { | |
100 | return computeTimePattern(getPreferenceMap(false)); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Return the interval pattern | |
105 | * | |
106 | * @return the interval pattern | |
107 | */ | |
108 | public String getIntervalPattern() { | |
109 | return computeIntervalPattern(getPreferenceMap(false)); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Get the time zone | |
114 | * | |
115 | * @return the time zone | |
116 | */ | |
117 | public TimeZone getTimeZone() { | |
118 | return TimeZone.getTimeZone(Platform.getPreferencesService().getString(Activator.PLUGIN_ID, ITmfTimePreferencesConstants.TIME_ZONE, TimeZone.getDefault().getID(), null)); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Get the default preferences map | |
123 | * | |
124 | * @return a collection containing the default preferences | |
125 | */ | |
126 | public 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 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<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); | |
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 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 | } |