tmf: Make TmfTimePreferences completely static
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / properties / TmfTimestampFormatPage.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 * Patrick Tasse - Updated for fraction of seconds
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ui.properties;
16
17 import java.util.Map;
18 import java.util.TimeZone;
19
20 import org.eclipse.jface.preference.ComboFieldEditor;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.PreferencePage;
23 import org.eclipse.jface.preference.RadioGroupFieldEditor;
24 import org.eclipse.jface.util.IPropertyChangeListener;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
36 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
37 import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
38 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimePreferencesConstants;
39 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimePreferences;
40 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
41 import org.eclipse.ui.IWorkbench;
42 import org.eclipse.ui.IWorkbenchPreferencePage;
43
44 /**
45 * The TMF timestamp format configuration page. This page is used to select the
46 * global timestamp and interval time formats (for display and parsing). The
47 * user can either pick a pre-defined format or enter his/her own.
48 *
49 * @version 1.0
50 * @author Francois Chouinard
51 * @since 2.0
52 */
53 public class TmfTimestampFormatPage extends PreferencePage implements IWorkbenchPreferencePage, SelectionListener, IPropertyChangeListener {
54
55 // ------------------------------------------------------------------------
56 // Constants
57 // ------------------------------------------------------------------------
58
59 // Date and Time formats
60 private static final String[][] fDateTimeFormats = new String[][] {
61 { ITmfTimePreferencesConstants.DATE_YEAR_FMT, ITmfTimePreferencesConstants.DATE_YEAR_FMT },
62 { ITmfTimePreferencesConstants.DATE_YEAR2_FMT, ITmfTimePreferencesConstants.DATE_YEAR2_FMT },
63 { ITmfTimePreferencesConstants.DATE_MONTH_FMT, ITmfTimePreferencesConstants.DATE_MONTH_FMT },
64 { ITmfTimePreferencesConstants.DATE_DAY_FMT, ITmfTimePreferencesConstants.DATE_DAY_FMT },
65 { ITmfTimePreferencesConstants.DATE_JDAY_FMT, ITmfTimePreferencesConstants.DATE_JDAY_FMT },
66 { ITmfTimePreferencesConstants.TIME_HOUR_FMT, ITmfTimePreferencesConstants.TIME_HOUR_FMT },
67 { ITmfTimePreferencesConstants.TIME_MINUTE_FMT, ITmfTimePreferencesConstants.TIME_MINUTE_FMT },
68 { ITmfTimePreferencesConstants.TIME_SECOND_FMT, ITmfTimePreferencesConstants.TIME_SECOND_FMT },
69 { ITmfTimePreferencesConstants.TIME_ELAPSED_FMT + " (secs in epoch)", ITmfTimePreferencesConstants.TIME_ELAPSED_FMT }, //$NON-NLS-1$
70 { "(none)", ITmfTimePreferencesConstants.TIME_NO_FMT }, //$NON-NLS-1$
71 };
72
73 // Sub-second formats
74 private static final String[][] fSubSecondFormats = new String[][] {
75 { ITmfTimePreferencesConstants.SUBSEC_MILLI_FMT + " (ms)", ITmfTimePreferencesConstants.SUBSEC_MILLI_FMT }, //$NON-NLS-1$
76 { ITmfTimePreferencesConstants.SUBSEC_MICRO_FMT + " (µs)", ITmfTimePreferencesConstants.SUBSEC_MICRO_FMT }, //$NON-NLS-1$
77 { ITmfTimePreferencesConstants.SUBSEC_NANO_FMT + " (ns)", ITmfTimePreferencesConstants.SUBSEC_NANO_FMT }, //$NON-NLS-1$
78 };
79
80 // Date and Time delimiters
81 private static final String[][] fDateTimeDelimiters = new String[][] {
82 { "(none)", ITmfTimePreferencesConstants.DELIMITER_NONE }, //$NON-NLS-1$
83 { " (space)", ITmfTimePreferencesConstants.DELIMITER_SPACE }, //$NON-NLS-1$
84 { ", (comma)", ITmfTimePreferencesConstants.DELIMITER_COMMA }, //$NON-NLS-1$
85 { "- (dash)", ITmfTimePreferencesConstants.DELIMITER_DASH }, //$NON-NLS-1$
86 { "_ (underline)", ITmfTimePreferencesConstants.DELIMITER_UNDERLINE }, //$NON-NLS-1$
87 { ": (colon)", ITmfTimePreferencesConstants.DELIMITER_COLON }, //$NON-NLS-1$
88 { "; (semicolon)", ITmfTimePreferencesConstants.DELIMITER_SEMICOLON }, //$NON-NLS-1$
89 { "/ (slash)", ITmfTimePreferencesConstants.DELIMITER_SLASH }, //$NON-NLS-1$
90 { "' (quote)", ITmfTimePreferencesConstants.DELIMITER_QUOTE }, //$NON-NLS-1$
91 { "\" (dbl-quote)", ITmfTimePreferencesConstants.DELIMITER_DQUOT }, //$NON-NLS-1$
92 };
93
94 // Sub-Second delimiters
95 private static final String[][] fSubSecondDelimiters = new String[][] {
96 { "(none)", ITmfTimePreferencesConstants.DELIMITER_NONE }, //$NON-NLS-1$
97 { " (space)", ITmfTimePreferencesConstants.DELIMITER_SPACE }, //$NON-NLS-1$
98 { ", (comma)", ITmfTimePreferencesConstants.DELIMITER_COMMA }, //$NON-NLS-1$
99 { "- (dash)", ITmfTimePreferencesConstants.DELIMITER_DASH }, //$NON-NLS-1$
100 { "_ (underline)", ITmfTimePreferencesConstants.DELIMITER_UNDERLINE }, //$NON-NLS-1$
101 { ": (colon)", ITmfTimePreferencesConstants.DELIMITER_COLON }, //$NON-NLS-1$
102 { "; (semicolon)", ITmfTimePreferencesConstants.DELIMITER_SEMICOLON }, //$NON-NLS-1$
103 { "/ (slash)", ITmfTimePreferencesConstants.DELIMITER_SLASH }, //$NON-NLS-1$
104 { "' (quote)", ITmfTimePreferencesConstants.DELIMITER_QUOTE }, //$NON-NLS-1$
105 { "\" (dbl-quote)", ITmfTimePreferencesConstants.DELIMITER_DQUOT }, //$NON-NLS-1$
106 { ". (period)", ITmfTimePreferencesConstants.DELIMITER_PERIOD }, //$NON-NLS-1$
107 };
108
109 // Time zones
110 @SuppressWarnings("nls")
111 private static final String[] timeZones = new String[] {
112 Messages.TmfTimestampFormatPage_LocalTime,
113 "GMT-12",
114 "GMT-11",
115 "GMT-10",
116 "GMT-9:30",
117 "GMT-9",
118 "GMT-7",
119 "GMT-6",
120 "GMT-5",
121 "GMT-4",
122 "GMT-3:30",
123 "GMT-3",
124 "GMT-2",
125 "GMT-1",
126 "GMT",
127 "GMT+1",
128 "GMT+2",
129 "GMT+3",
130 "GMT+3:30",
131 "GMT+4",
132 "GMT+4:30",
133 "GMT+5",
134 "GMT+5:30",
135 "GMT+6",
136 "GMT+7",
137 "GMT+8",
138 "GMT+9",
139 "GMT+9:30",
140 "GMT+10",
141 "GMT+10:30",
142 "GMT+11",
143 "GMT+11:30",
144 "GMT+12",
145 "GMT+13:00",
146 "GMT+14:00"
147 };
148
149 // ------------------------------------------------------------------------
150 // Attributes
151 // ------------------------------------------------------------------------
152
153 // General stuff
154 private Composite fPage;
155 private IPreferenceStore fPreferenceStore;
156
157 // Example section
158 private Composite fExampleSection;
159 private Text fPatternDisplay;
160 private Text fExampleDisplay;
161
162 // Timezone section
163 private ComboFieldEditor fCombo;
164
165 // Date/Time format section
166 private RadioGroupFieldEditor fDateTimeFields;
167 private RadioGroupFieldEditor fSSecFields;
168
169 // Delimiters section
170 private RadioGroupFieldEditor fDateFieldDelim;
171 private RadioGroupFieldEditor fTimeFieldDelim;
172 private RadioGroupFieldEditor fSSecFieldDelim;
173
174 // IPropertyChangeListener data
175 private String fProperty;
176 private String fChangedProperty;
177
178 private Map<String, String> fPreferenceMap;
179
180 // ------------------------------------------------------------------------
181 // Constructors
182 // ------------------------------------------------------------------------
183
184 /**
185 * The default constructor
186 */
187 public TmfTimestampFormatPage() {
188 fPreferenceStore = getPreferenceStore();
189 fPreferenceMap = TmfTimePreferences.getPreferenceMap();
190 }
191
192 // ------------------------------------------------------------------------
193 // IWorkbenchPreferencePage
194 // ------------------------------------------------------------------------
195
196 @Override
197 protected IPreferenceStore doGetPreferenceStore() {
198 return Activator.getDefault().getCorePreferenceStore();
199 }
200
201 @Override
202 public void init(IWorkbench workbench) {
203 }
204
205 // ------------------------------------------------------------------------
206 // PreferencePage
207 // ------------------------------------------------------------------------
208
209 @Override
210 protected Control createContents(Composite parent) {
211
212 // Overall preference page layout
213 GridLayout gl = new GridLayout();
214 gl.marginHeight = 0;
215 gl.marginWidth = 0;
216 parent.setLayout(gl);
217 fPage = new Composite(parent, SWT.NONE);
218 fPage.setLayout(new GridLayout());
219 fPage.setLayoutData(new GridData(
220 GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL));
221
222 // Example section
223 fExampleSection = new Composite(fPage, SWT.NONE);
224 fExampleSection.setLayout(new GridLayout(2, false));
225 fExampleSection.setLayoutData(new GridData(GridData.FILL_BOTH));
226
227 Label patternLabel = new Label(fExampleSection, SWT.HORIZONTAL);
228 patternLabel.setText("Current Format: "); //$NON-NLS-1$
229 fPatternDisplay = new Text(fExampleSection, SWT.BORDER | SWT.READ_ONLY);
230 fPatternDisplay.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
231
232 Label exampleLabel = new Label(fExampleSection, SWT.NONE);
233 exampleLabel.setText("Sample Display: "); //$NON-NLS-1$
234 fExampleDisplay = new Text(fExampleSection, SWT.BORDER | SWT.READ_ONLY);
235 fExampleDisplay.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
236
237 Label separator = new Label(fPage, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.SHADOW_NONE);
238 separator.setLayoutData(
239 new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
240
241 // Time Zones
242 String[][] timeZoneIntervals = new String[timeZones.length][2];
243 timeZoneIntervals[0][0] = timeZones[0];
244 timeZoneIntervals[0][1] = fPreferenceStore.getDefaultString(ITmfTimePreferencesConstants.TIME_ZONE);
245 for (int i = 1; i < timeZones.length; i++) {
246 TimeZone tz = null;
247 try {
248 tz = TimeZone.getTimeZone(timeZones[i]);
249 timeZoneIntervals[i][0] = tz.getDisplayName();
250 timeZoneIntervals[i][1] = tz.getID();
251 } catch (NullPointerException e) {
252 System.out.println("TimeZone " + timeZones[i] + " does not exist."); //$NON-NLS-1$ //$NON-NLS-2$
253 }
254 }
255
256 fCombo = new ComboFieldEditor(ITmfTimePreferencesConstants.TIME_ZONE, "Time Zone", timeZoneIntervals, fPage); //$NON-NLS-1$
257 fCombo.setPreferenceStore(fPreferenceStore);
258 fCombo.load();
259 fCombo.setPropertyChangeListener(this);
260
261 // Date and Time section
262 fDateTimeFields = new RadioGroupFieldEditor(
263 ITmfTimePreferencesConstants.DATIME, "Date and Time format", 3, fDateTimeFormats, fPage, true); //$NON-NLS-1$
264 fDateTimeFields.setPreferenceStore(fPreferenceStore);
265 fDateTimeFields.load();
266 fDateTimeFields.setPropertyChangeListener(this);
267
268 // Sub-second section
269 fSSecFields = new RadioGroupFieldEditor(
270 ITmfTimePreferencesConstants.SUBSEC, "Sub-second format", 3, fSubSecondFormats, fPage, true); //$NON-NLS-1$
271 fSSecFields.setPreferenceStore(fPreferenceStore);
272 fSSecFields.load();
273 fSSecFields.setPropertyChangeListener(this);
274
275 // Separators section
276 fDateFieldDelim = new RadioGroupFieldEditor(
277 ITmfTimePreferencesConstants.DATE_DELIMITER, "Date delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$
278 fDateFieldDelim.setPreferenceStore(fPreferenceStore);
279 fDateFieldDelim.load();
280 fDateFieldDelim.setPropertyChangeListener(this);
281
282 fTimeFieldDelim = new RadioGroupFieldEditor(
283 ITmfTimePreferencesConstants.TIME_DELIMITER, "Time delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$
284 fTimeFieldDelim.setPreferenceStore(fPreferenceStore);
285 fTimeFieldDelim.load();
286 fTimeFieldDelim.setPropertyChangeListener(this);
287
288 fSSecFieldDelim = new RadioGroupFieldEditor(
289 ITmfTimePreferencesConstants.SSEC_DELIMITER, "Sub-Second Delimiter", 5, fSubSecondDelimiters, fPage, true); //$NON-NLS-1$
290 fSSecFieldDelim.setPreferenceStore(fPreferenceStore);
291 fSSecFieldDelim.load();
292 fSSecFieldDelim.setPropertyChangeListener(this);
293
294 refresh();
295 return fPage;
296 }
297
298 @Override
299 protected void performDefaults() {
300 fDateTimeFields.loadDefault();
301 fSSecFields.loadDefault();
302 fDateFieldDelim.loadDefault();
303 fTimeFieldDelim.loadDefault();
304 fSSecFieldDelim.loadDefault();
305 fCombo.loadDefault();
306
307 fPreferenceMap = TmfTimePreferences.getDefaultPreferenceMap();
308 displayExample();
309 }
310
311 @Override
312 protected void performApply() {
313 fDateTimeFields.store();
314 fSSecFields.store();
315 fDateFieldDelim.store();
316 fTimeFieldDelim.store();
317 fSSecFieldDelim.store();
318 fCombo.store();
319
320 TmfTimestampFormat.updateDefaultFormats();
321 TmfSignalManager.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
322 displayExample();
323 }
324
325 @Override
326 public boolean performOk() {
327 performApply();
328 return super.performOk();
329 }
330
331 // ------------------------------------------------------------------------
332 // SelectionListener
333 // ------------------------------------------------------------------------
334
335 @Override
336 public void widgetSelected(SelectionEvent e) {
337 }
338
339 @Override
340 public void widgetDefaultSelected(SelectionEvent e) {
341 }
342
343 // ------------------------------------------------------------------------
344 // IPropertyChangeListener
345 // ------------------------------------------------------------------------
346
347 @Override
348 public void propertyChange(PropertyChangeEvent event) {
349 Object source = event.getSource();
350 Object value = event.getNewValue();
351 if (source instanceof RadioGroupFieldEditor && value instanceof String &&
352 !(value.equals(fChangedProperty) && source == fProperty))
353 {
354 fProperty = ((RadioGroupFieldEditor) source).getPreferenceName();
355 fChangedProperty = (String) value;
356 refresh();
357 }
358 }
359
360 // ------------------------------------------------------------------------
361 // Helper functions
362 // ------------------------------------------------------------------------
363
364 private void refresh() {
365 updatePatterns();
366 displayExample();
367 }
368
369 void updatePatterns() {
370 if (ITmfTimePreferencesConstants.DATIME.equals(fProperty) ||
371 ITmfTimePreferencesConstants.SUBSEC.equals(fProperty) ||
372 ITmfTimePreferencesConstants.DATE_DELIMITER.equals(fProperty) ||
373 ITmfTimePreferencesConstants.TIME_DELIMITER.equals(fProperty) ||
374 ITmfTimePreferencesConstants.SSEC_DELIMITER.equals(fProperty)) {
375 fPreferenceMap.put(fProperty, fChangedProperty);
376 }
377 }
378
379 private void displayExample() {
380 long ts = 1332170682500677380L;
381 String timePattern = TmfTimePreferences.computeTimePattern(fPreferenceMap);
382 fPatternDisplay.setText(timePattern);
383 fPatternDisplay.redraw();
384
385 fExampleDisplay.setText(new TmfTimestampFormat(timePattern).format(ts));
386 fExampleDisplay.redraw();
387 }
388
389 }
This page took 0.041847 seconds and 5 git commands to generate.