| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2015 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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Locale; |
| 20 | import java.util.Map; |
| 21 | import java.util.TimeZone; |
| 22 | |
| 23 | import org.eclipse.core.runtime.Platform; |
| 24 | import org.eclipse.core.runtime.preferences.DefaultScope; |
| 25 | import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 26 | import org.eclipse.core.runtime.preferences.IPreferencesService; |
| 27 | import org.eclipse.core.runtime.preferences.InstanceScope; |
| 28 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
| 29 | |
| 30 | /** |
| 31 | * TMF Time format preferences |
| 32 | * |
| 33 | * @author Francois Chouinard |
| 34 | */ |
| 35 | public final class TmfTimePreferences { |
| 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(); |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Constructor |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | /** |
| 53 | * Local constructor |
| 54 | */ |
| 55 | private TmfTimePreferences() { |
| 56 | } |
| 57 | |
| 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 | |
| 70 | TmfTimestampFormat.updateDefaultFormats(); |
| 71 | } |
| 72 | |
| 73 | // ------------------------------------------------------------------------ |
| 74 | // Getters/Setters |
| 75 | // ------------------------------------------------------------------------ |
| 76 | |
| 77 | /** |
| 78 | * Return the timestamp pattern |
| 79 | * |
| 80 | * @return the timestamp pattern |
| 81 | */ |
| 82 | public static String getTimePattern() { |
| 83 | return computeTimePattern(getPreferenceMap(false)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return the interval pattern |
| 88 | * |
| 89 | * @return the interval pattern |
| 90 | */ |
| 91 | public static String getIntervalPattern() { |
| 92 | return computeIntervalPattern(getPreferenceMap(false)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the time zone |
| 97 | * |
| 98 | * @return the time zone |
| 99 | */ |
| 100 | public static TimeZone getTimeZone() { |
| 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)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get the locale |
| 111 | * |
| 112 | * @return the locale |
| 113 | */ |
| 114 | public static Locale getLocale() { |
| 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)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the default preferences map |
| 125 | * |
| 126 | * @return a collection containing the default preferences |
| 127 | */ |
| 128 | public static Map<String, String> getDefaultPreferenceMap() { |
| 129 | return getPreferenceMap(true); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the current preferences map |
| 134 | * |
| 135 | * @return a collection containing the current preferences |
| 136 | */ |
| 137 | public static Map<String, String> getPreferenceMap() { |
| 138 | return getPreferenceMap(false); |
| 139 | } |
| 140 | |
| 141 | private static Map<String, String> getPreferenceMap(boolean defaultValues) { |
| 142 | Map<String, String> prefsMap = new HashMap<>(); |
| 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); |
| 149 | prefToMap(prefs, prefsMap, ITmfTimePreferencesConstants.TIME_ZONE, TIME_ZONE_DEFAULT); |
| 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) { |
| 163 | String sSecFormat = checkNotNull(prefsMap.get(ITmfTimePreferencesConstants.SUBSEC)); |
| 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 | */ |
| 179 | public static String computeTimePattern(Map<String, String> prefsMap) { |
| 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 | } |