1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Marc-Andre Laperle - Add time zone preference
12 * Patrick Tasse - Updated for fraction of seconds
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ui
.properties
;
18 import java
.util
.TimeZone
;
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
;
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.
50 * @author Francois Chouinard
53 public class TmfTimestampFormatPage
extends PreferencePage
implements IWorkbenchPreferencePage
, SelectionListener
, IPropertyChangeListener
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
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$
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$
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$
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$
110 @SuppressWarnings("nls")
111 private static final String
[] timeZones
= new String
[] {
112 Messages
.TmfTimestampFormatPage_LocalTime
,
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
154 private Composite fPage
;
155 private IPreferenceStore fPreferenceStore
;
158 private Composite fExampleSection
;
159 private Text fPatternDisplay
;
160 private Text fExampleDisplay
;
163 private ComboFieldEditor fCombo
;
165 // Date/Time format section
166 private RadioGroupFieldEditor fDateTimeFields
;
167 private RadioGroupFieldEditor fSSecFields
;
169 // Delimiters section
170 private RadioGroupFieldEditor fDateFieldDelim
;
171 private RadioGroupFieldEditor fTimeFieldDelim
;
172 private RadioGroupFieldEditor fSSecFieldDelim
;
174 // IPropertyChangeListener data
175 private String fProperty
;
176 private String fChangedProperty
;
178 private Map
<String
, String
> fPreferenceMap
;
180 // ------------------------------------------------------------------------
182 // ------------------------------------------------------------------------
185 * The default constructor
187 public TmfTimestampFormatPage() {
188 fPreferenceStore
= getPreferenceStore();
189 fPreferenceMap
= TmfTimePreferences
.getPreferenceMap();
192 // ------------------------------------------------------------------------
193 // IWorkbenchPreferencePage
194 // ------------------------------------------------------------------------
197 protected IPreferenceStore
doGetPreferenceStore() {
198 return Activator
.getDefault().getCorePreferenceStore();
202 public void init(IWorkbench workbench
) {
205 // ------------------------------------------------------------------------
207 // ------------------------------------------------------------------------
210 protected Control
createContents(Composite parent
) {
212 // Overall preference page layout
213 GridLayout gl
= new GridLayout();
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
));
223 fExampleSection
= new Composite(fPage
, SWT
.NONE
);
224 fExampleSection
.setLayout(new GridLayout(2, false));
225 fExampleSection
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
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
));
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
));
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
));
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
++) {
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$
256 fCombo
= new ComboFieldEditor(ITmfTimePreferencesConstants
.TIME_ZONE
, "Time Zone", timeZoneIntervals
, fPage
); //$NON-NLS-1$
257 fCombo
.setPreferenceStore(fPreferenceStore
);
259 fCombo
.setPropertyChangeListener(this);
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);
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
);
273 fSSecFields
.setPropertyChangeListener(this);
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);
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);
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);
299 protected void performDefaults() {
300 fDateTimeFields
.loadDefault();
301 fSSecFields
.loadDefault();
302 fDateFieldDelim
.loadDefault();
303 fTimeFieldDelim
.loadDefault();
304 fSSecFieldDelim
.loadDefault();
305 fCombo
.loadDefault();
307 fPreferenceMap
= TmfTimePreferences
.getDefaultPreferenceMap();
312 protected void performApply() {
313 fDateTimeFields
.store();
315 fDateFieldDelim
.store();
316 fTimeFieldDelim
.store();
317 fSSecFieldDelim
.store();
320 TmfTimestampFormat
.updateDefaultFormats();
321 TmfSignalManager
.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
326 public boolean performOk() {
328 return super.performOk();
331 // ------------------------------------------------------------------------
333 // ------------------------------------------------------------------------
336 public void widgetSelected(SelectionEvent e
) {
340 public void widgetDefaultSelected(SelectionEvent e
) {
343 // ------------------------------------------------------------------------
344 // IPropertyChangeListener
345 // ------------------------------------------------------------------------
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
))
354 fProperty
= ((RadioGroupFieldEditor
) source
).getPreferenceName();
355 fChangedProperty
= (String
) value
;
360 // ------------------------------------------------------------------------
362 // ------------------------------------------------------------------------
364 private void refresh() {
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
);
379 private void displayExample() {
380 long ts
= 1332170682500677380L;
381 String timePattern
= TmfTimePreferences
.computeTimePattern(fPreferenceMap
);
382 fPatternDisplay
.setText(timePattern
);
383 fPatternDisplay
.redraw();
385 fExampleDisplay
.setText(new TmfTimestampFormat(timePattern
).format(ts
));
386 fExampleDisplay
.redraw();