Implement TmfTimestampFormat
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / properties / TmfTimePreferences.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.properties;
14
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
17 import org.eclipse.linuxtools.tmf.core.event.TmfTimestampFormat;
18
19 /**
20 * TMF Time format preferences
21 *
22 * @version 1.0
23 * @author Francois Chouinard
24 */
25 @SuppressWarnings("javadoc")
26 public class TmfTimePreferences {
27
28 // ------------------------------------------------------------------------
29 // Constants
30 // ------------------------------------------------------------------------
31
32 public static final String DEFAULT_TIME_PATTERN = "HH:mm:ss.SSS_CCC_NNN"; //$NON-NLS-1$
33
34 static final String TIME_FORMAT_PREF = "org.eclipse.linuxtools.tmf.ui.prefs.time.format"; //$NON-NLS-1$
35 static final String DATIME = TIME_FORMAT_PREF + ".datime"; //$NON-NLS-1$
36 static final String SUBSEC = TIME_FORMAT_PREF + ".subsec"; //$NON-NLS-1$
37
38 static final String DATE_DELIMITER = TIME_FORMAT_PREF + ".date.delimiter"; //$NON-NLS-1$
39 static final String TIME_DELIMITER = TIME_FORMAT_PREF + ".time.delimiter"; //$NON-NLS-1$
40 static final String SSEC_DELIMITER = TIME_FORMAT_PREF + ".ssec.delimiter"; //$NON-NLS-1$
41
42 static final String DATE_YEAR_FMT = "yyyy-MM-dd HH:mm:ss"; //$NON-NLS-1$
43 static final String DATE_YEAR2_FMT = "yy-MM-dd HH:mm:ss"; //$NON-NLS-1$
44 static final String DATE_MONTH_FMT = "MM-dd HH:mm:ss"; //$NON-NLS-1$
45 static final String DATE_DAY_FMT = "dd HH:mm:ss"; //$NON-NLS-1$
46 static final String DATE_JDAY_FMT = "DDD HH:mm:ss"; //$NON-NLS-1$
47 static final String DATE_NO_FMT = "HH:mm:ss"; //$NON-NLS-1$
48
49 static final String TIME_HOUR_FMT = "HH:mm:ss"; //$NON-NLS-1$
50 static final String TIME_MINUTE_FMT = "mm:ss"; //$NON-NLS-1$
51 static final String TIME_SECOND_FMT = "ss"; //$NON-NLS-1$
52 static final String TIME_ELAPSED_FMT = "TTT"; //$NON-NLS-1$
53 static final String TIME_NO_FMT = ""; //$NON-NLS-1$
54
55 static final String SUBSEC_MILLI_FMT = "SSS"; //$NON-NLS-1$
56 static final String SUBSEC_MICRO_FMT = "SSS_CCC"; //$NON-NLS-1$
57 static final String SUBSEC_NANO_FMT = "SSS_CCC_NNN"; //$NON-NLS-1$
58 static final String SUBSEC_NO_FMT = ""; //$NON-NLS-1$
59
60 static final String DELIMITER_NONE = ""; //$NON-NLS-1$
61 static final String DELIMITER_SPACE = " "; //$NON-NLS-1$
62 static final String DELIMITER_PERIOD = "."; //$NON-NLS-1$
63 static final String DELIMITER_COMMA = ","; //$NON-NLS-1$
64 static final String DELIMITER_DASH = "-"; //$NON-NLS-1$
65 static final String DELIMITER_UNDERLINE = "_"; //$NON-NLS-1$
66 static final String DELIMITER_COLON = ":"; //$NON-NLS-1$
67 static final String DELIMITER_SEMICOLON = ";"; //$NON-NLS-1$
68 static final String DELIMITER_SLASH = "/"; //$NON-NLS-1$
69 static final String DELIMITER_DQUOT = "\""; //$NON-NLS-1$
70
71 // ------------------------------------------------------------------------
72 // Attributes
73 // ------------------------------------------------------------------------
74
75 private static TmfTimePreferences fPreferences;
76
77 private static IPreferenceStore fPreferenceStore;
78 private static String fTimestampPattern;
79 private static String fIntervalPattern;
80
81 private String fDatimeFormat;
82 private String fDateFormat;
83 private String fTimeFormat;
84 private String fSSecFormat;
85
86 private String fDateFieldSep = "-"; //$NON-NLS-1$
87 private String fTimeFieldSep = ":"; //$NON-NLS-1$
88 private String fSSecFieldSep = " "; //$NON-NLS-1$
89
90 // ------------------------------------------------------------------------
91 // Constructor
92 // ------------------------------------------------------------------------
93
94 public static void init() {
95 fPreferenceStore = Activator.getDefault().getPreferenceStore();
96 fPreferenceStore.setDefault(TmfTimePreferences.DATIME, TIME_HOUR_FMT);
97 fPreferenceStore.setDefault(TmfTimePreferences.SUBSEC, SUBSEC_NANO_FMT);
98 fPreferenceStore.setDefault(TmfTimePreferences.DATE_DELIMITER, DELIMITER_DASH);
99 fPreferenceStore.setDefault(TmfTimePreferences.TIME_DELIMITER, DELIMITER_COLON);
100 fPreferenceStore.setDefault(TmfTimePreferences.SSEC_DELIMITER, DELIMITER_UNDERLINE);
101
102 // Create the singleton and initialize format preferences
103 getInstance();
104 }
105
106 public static synchronized IPreferenceStore getPreferenceStore() {
107 if (fPreferenceStore == null) {
108 init();
109 }
110 return fPreferenceStore;
111 }
112
113 public static synchronized TmfTimePreferences getInstance() {
114 if (fPreferences == null) {
115 fPreferences = new TmfTimePreferences();
116 }
117 return fPreferences;
118 }
119
120 /**
121 * Local constructor
122 */
123 private TmfTimePreferences() {
124 initPatterns();
125 setTimePattern(fTimestampPattern);
126 }
127
128 // ------------------------------------------------------------------------
129 // Getters/Setters
130 // ------------------------------------------------------------------------
131
132 /**
133 * @return the timestamp pattern
134 */
135 public static String getTimePattern() {
136 return fTimestampPattern;
137 }
138
139 /**
140 * Sets the timestamp pattern and updates TmfTimestampFormat
141 *
142 * @param timePattern the new timestamp pattern
143 */
144 static void setTimePattern(String timePattern) {
145 fTimestampPattern = timePattern;
146 TmfTimestampFormat.setDefaultTimeFormat(fTimestampPattern);
147 TmfTimestampFormat.setDefaultIntervalFormat(fIntervalPattern);
148 }
149
150 /**
151 * Update the Date field separator
152 * @param pattern the Date field separator
153 */
154 void setDateFieldSep(String pattern) {
155 fDateFieldSep = pattern;
156 }
157
158 /**
159 * Update the Time field separator
160 * @param pattern the Time field separator
161 */
162 void setTimeFieldSep(String pattern) {
163 fTimeFieldSep = pattern;
164 }
165
166 /**
167 * Update the Subseconds field separator
168 * @param pattern the Subseconds field separator
169 */
170 void setSSecFieldSep(String pattern) {
171 fSSecFieldSep = pattern;
172 }
173
174 /**
175 * Update the Date/Time format
176 * @param pattern the Date/Time format
177 */
178 void setDateTimeFormat(String pattern) {
179 fDatimeFormat = pattern;
180 if (fDatimeFormat == null) {
181 fDatimeFormat = DEFAULT_TIME_PATTERN;
182 }
183 int index = fDatimeFormat.indexOf(' ');
184 if (index != -1) {
185 fDateFormat = fDatimeFormat.substring(0, fDatimeFormat.indexOf(' ') + 1);
186 fTimeFormat = fDatimeFormat.substring(fDateFormat.length());
187 } else {
188 fDateFormat = ""; //$NON-NLS-1$
189 fTimeFormat = fDatimeFormat;
190 }
191 }
192
193 /**
194 * Update the Subseconds format
195 * @param pattern the Subseconds format
196 */
197 void setSSecFormat(String pattern) {
198 fSSecFormat = pattern;
199 }
200
201 // ------------------------------------------------------------------------
202 // Operations
203 // ------------------------------------------------------------------------
204
205 void initPatterns() {
206 setDateTimeFormat(fPreferenceStore.getString(DATIME));
207 fSSecFormat = fPreferenceStore.getString(SUBSEC);
208 fDateFieldSep = fPreferenceStore.getString(DATE_DELIMITER);
209 fTimeFieldSep = fPreferenceStore.getString(TIME_DELIMITER);
210 fSSecFieldSep = fPreferenceStore.getString(SSEC_DELIMITER);
211 updatePatterns();
212 }
213
214 void updatePatterns() {
215 String dateFmt = fDateFormat.replaceAll("-", fDateFieldSep); //$NON-NLS-1$
216 String timeFmt = fTimeFormat.replaceAll(":", fTimeFieldSep); //$NON-NLS-1$
217 String ssecFmt = fSSecFormat.replaceAll("_", fSSecFieldSep); //$NON-NLS-1$
218
219 fTimestampPattern = dateFmt + timeFmt + "." + ssecFmt; //$NON-NLS-1$
220 fIntervalPattern = "TTT." + ssecFmt; //$NON-NLS-1$
221 }
222
223 void setDefaults() {
224 setDateTimeFormat(TmfTimePreferences.TIME_HOUR_FMT);
225 setSSecFormat(TmfTimePreferences.SUBSEC_NANO_FMT);
226 setDateFieldSep(TmfTimePreferences.DELIMITER_DASH);
227 setTimeFieldSep(TmfTimePreferences.DELIMITER_COLON);
228 setSSecFieldSep(TmfTimePreferences.DELIMITER_UNDERLINE);
229 updatePatterns();
230 }
231
232 }
This page took 0.035563 seconds and 6 git commands to generate.