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.
49 * @author Francois Chouinard
51 public class TmfTimestampFormatPage
extends PreferencePage
implements IWorkbenchPreferencePage
, SelectionListener
, IPropertyChangeListener
{
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 // Date and Time formats
58 private static final String
[][] fDateTimeFormats
= new String
[][] {
59 { ITmfTimePreferencesConstants
.DATE_YEAR_FMT
, ITmfTimePreferencesConstants
.DATE_YEAR_FMT
},
60 { ITmfTimePreferencesConstants
.DATE_YEAR2_FMT
, ITmfTimePreferencesConstants
.DATE_YEAR2_FMT
},
61 { ITmfTimePreferencesConstants
.DATE_MONTH_FMT
, ITmfTimePreferencesConstants
.DATE_MONTH_FMT
},
62 { ITmfTimePreferencesConstants
.DATE_DAY_FMT
, ITmfTimePreferencesConstants
.DATE_DAY_FMT
},
63 { ITmfTimePreferencesConstants
.DATE_JDAY_FMT
, ITmfTimePreferencesConstants
.DATE_JDAY_FMT
},
64 { ITmfTimePreferencesConstants
.TIME_HOUR_FMT
, ITmfTimePreferencesConstants
.TIME_HOUR_FMT
},
65 { ITmfTimePreferencesConstants
.TIME_MINUTE_FMT
, ITmfTimePreferencesConstants
.TIME_MINUTE_FMT
},
66 { ITmfTimePreferencesConstants
.TIME_SECOND_FMT
, ITmfTimePreferencesConstants
.TIME_SECOND_FMT
},
67 { ITmfTimePreferencesConstants
.TIME_ELAPSED_FMT
+ " (secs in epoch)", ITmfTimePreferencesConstants
.TIME_ELAPSED_FMT
}, //$NON-NLS-1$
68 { "(none)", ITmfTimePreferencesConstants
.TIME_NO_FMT
}, //$NON-NLS-1$
72 private static final String
[][] fSubSecondFormats
= new String
[][] {
73 { ITmfTimePreferencesConstants
.SUBSEC_MILLI_FMT
+ " (ms)", ITmfTimePreferencesConstants
.SUBSEC_MILLI_FMT
}, //$NON-NLS-1$
74 { ITmfTimePreferencesConstants
.SUBSEC_MICRO_FMT
+ " (µs)", ITmfTimePreferencesConstants
.SUBSEC_MICRO_FMT
}, //$NON-NLS-1$
75 { ITmfTimePreferencesConstants
.SUBSEC_NANO_FMT
+ " (ns)", ITmfTimePreferencesConstants
.SUBSEC_NANO_FMT
}, //$NON-NLS-1$
78 // Date and Time delimiters
79 private static final String
[][] fDateTimeDelimiters
= new String
[][] {
80 { "(none)", ITmfTimePreferencesConstants
.DELIMITER_NONE
}, //$NON-NLS-1$
81 { " (space)", ITmfTimePreferencesConstants
.DELIMITER_SPACE
}, //$NON-NLS-1$
82 { ", (comma)", ITmfTimePreferencesConstants
.DELIMITER_COMMA
}, //$NON-NLS-1$
83 { "- (dash)", ITmfTimePreferencesConstants
.DELIMITER_DASH
}, //$NON-NLS-1$
84 { "_ (underline)", ITmfTimePreferencesConstants
.DELIMITER_UNDERLINE
}, //$NON-NLS-1$
85 { ": (colon)", ITmfTimePreferencesConstants
.DELIMITER_COLON
}, //$NON-NLS-1$
86 { "; (semicolon)", ITmfTimePreferencesConstants
.DELIMITER_SEMICOLON
}, //$NON-NLS-1$
87 { "/ (slash)", ITmfTimePreferencesConstants
.DELIMITER_SLASH
}, //$NON-NLS-1$
88 { "' (quote)", ITmfTimePreferencesConstants
.DELIMITER_QUOTE
}, //$NON-NLS-1$
89 { "\" (dbl-quote)", ITmfTimePreferencesConstants
.DELIMITER_DQUOT
}, //$NON-NLS-1$
92 // Sub-Second delimiters
93 private static final String
[][] fSubSecondDelimiters
= new String
[][] {
94 { "(none)", ITmfTimePreferencesConstants
.DELIMITER_NONE
}, //$NON-NLS-1$
95 { " (space)", ITmfTimePreferencesConstants
.DELIMITER_SPACE
}, //$NON-NLS-1$
96 { ", (comma)", ITmfTimePreferencesConstants
.DELIMITER_COMMA
}, //$NON-NLS-1$
97 { "- (dash)", ITmfTimePreferencesConstants
.DELIMITER_DASH
}, //$NON-NLS-1$
98 { "_ (underline)", ITmfTimePreferencesConstants
.DELIMITER_UNDERLINE
}, //$NON-NLS-1$
99 { ": (colon)", ITmfTimePreferencesConstants
.DELIMITER_COLON
}, //$NON-NLS-1$
100 { "; (semicolon)", ITmfTimePreferencesConstants
.DELIMITER_SEMICOLON
}, //$NON-NLS-1$
101 { "/ (slash)", ITmfTimePreferencesConstants
.DELIMITER_SLASH
}, //$NON-NLS-1$
102 { "' (quote)", ITmfTimePreferencesConstants
.DELIMITER_QUOTE
}, //$NON-NLS-1$
103 { "\" (dbl-quote)", ITmfTimePreferencesConstants
.DELIMITER_DQUOT
}, //$NON-NLS-1$
104 { ". (period)", ITmfTimePreferencesConstants
.DELIMITER_PERIOD
}, //$NON-NLS-1$
108 @SuppressWarnings("nls")
109 private static final String
[] timeZones
= new String
[] {
110 Messages
.TmfTimestampFormatPage_LocalTime
,
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
152 private Composite fPage
;
153 private IPreferenceStore fPreferenceStore
;
156 private Composite fExampleSection
;
157 private Text fPatternDisplay
;
158 private Text fExampleDisplay
;
161 private ComboFieldEditor fCombo
;
163 // Date/Time format section
164 private RadioGroupFieldEditor fDateTimeFields
;
165 private RadioGroupFieldEditor fSSecFields
;
167 // Delimiters section
168 private RadioGroupFieldEditor fDateFieldDelim
;
169 private RadioGroupFieldEditor fTimeFieldDelim
;
170 private RadioGroupFieldEditor fSSecFieldDelim
;
172 // IPropertyChangeListener data
173 private String fProperty
;
174 private String fChangedProperty
;
176 private Map
<String
, String
> fPreferenceMap
;
178 // ------------------------------------------------------------------------
180 // ------------------------------------------------------------------------
183 * The default constructor
185 public TmfTimestampFormatPage() {
186 fPreferenceStore
= getPreferenceStore();
187 fPreferenceMap
= TmfTimePreferences
.getPreferenceMap();
190 // ------------------------------------------------------------------------
191 // IWorkbenchPreferencePage
192 // ------------------------------------------------------------------------
195 protected IPreferenceStore
doGetPreferenceStore() {
196 return Activator
.getDefault().getCorePreferenceStore();
200 public void init(IWorkbench workbench
) {
203 // ------------------------------------------------------------------------
205 // ------------------------------------------------------------------------
208 protected Control
createContents(Composite parent
) {
210 // Overall preference page layout
211 GridLayout gl
= new GridLayout();
214 parent
.setLayout(gl
);
215 fPage
= new Composite(parent
, SWT
.NONE
);
216 fPage
.setLayout(new GridLayout());
217 fPage
.setLayoutData(new GridData(
218 GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
| GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
));
221 fExampleSection
= new Composite(fPage
, SWT
.NONE
);
222 fExampleSection
.setLayout(new GridLayout(2, false));
223 fExampleSection
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
225 Label patternLabel
= new Label(fExampleSection
, SWT
.HORIZONTAL
);
226 patternLabel
.setText("Current Format: "); //$NON-NLS-1$
227 fPatternDisplay
= new Text(fExampleSection
, SWT
.BORDER
| SWT
.READ_ONLY
);
228 fPatternDisplay
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
230 Label exampleLabel
= new Label(fExampleSection
, SWT
.NONE
);
231 exampleLabel
.setText("Sample Display: "); //$NON-NLS-1$
232 fExampleDisplay
= new Text(fExampleSection
, SWT
.BORDER
| SWT
.READ_ONLY
);
233 fExampleDisplay
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
235 Label separator
= new Label(fPage
, SWT
.SEPARATOR
| SWT
.HORIZONTAL
| SWT
.SHADOW_NONE
);
236 separator
.setLayoutData(
237 new GridData(GridData
.HORIZONTAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.VERTICAL_ALIGN_FILL
));
240 String
[][] timeZoneIntervals
= new String
[timeZones
.length
][2];
241 timeZoneIntervals
[0][0] = timeZones
[0];
242 timeZoneIntervals
[0][1] = fPreferenceStore
.getDefaultString(ITmfTimePreferencesConstants
.TIME_ZONE
);
243 for (int i
= 1; i
< timeZones
.length
; i
++) {
246 tz
= TimeZone
.getTimeZone(timeZones
[i
]);
247 timeZoneIntervals
[i
][0] = tz
.getDisplayName();
248 timeZoneIntervals
[i
][1] = tz
.getID();
249 } catch (NullPointerException e
) {
250 System
.out
.println("TimeZone " + timeZones
[i
] + " does not exist."); //$NON-NLS-1$ //$NON-NLS-2$
254 fCombo
= new ComboFieldEditor(ITmfTimePreferencesConstants
.TIME_ZONE
, "Time Zone", timeZoneIntervals
, fPage
); //$NON-NLS-1$
255 fCombo
.setPreferenceStore(fPreferenceStore
);
257 fCombo
.setPropertyChangeListener(this);
259 // Date and Time section
260 fDateTimeFields
= new RadioGroupFieldEditor(
261 ITmfTimePreferencesConstants
.DATIME
, "Date and Time format", 3, fDateTimeFormats
, fPage
, true); //$NON-NLS-1$
262 fDateTimeFields
.setPreferenceStore(fPreferenceStore
);
263 fDateTimeFields
.load();
264 fDateTimeFields
.setPropertyChangeListener(this);
266 // Sub-second section
267 fSSecFields
= new RadioGroupFieldEditor(
268 ITmfTimePreferencesConstants
.SUBSEC
, "Sub-second format", 3, fSubSecondFormats
, fPage
, true); //$NON-NLS-1$
269 fSSecFields
.setPreferenceStore(fPreferenceStore
);
271 fSSecFields
.setPropertyChangeListener(this);
273 // Separators section
274 fDateFieldDelim
= new RadioGroupFieldEditor(
275 ITmfTimePreferencesConstants
.DATE_DELIMITER
, "Date delimiter", 5, fDateTimeDelimiters
, fPage
, true); //$NON-NLS-1$
276 fDateFieldDelim
.setPreferenceStore(fPreferenceStore
);
277 fDateFieldDelim
.load();
278 fDateFieldDelim
.setPropertyChangeListener(this);
280 fTimeFieldDelim
= new RadioGroupFieldEditor(
281 ITmfTimePreferencesConstants
.TIME_DELIMITER
, "Time delimiter", 5, fDateTimeDelimiters
, fPage
, true); //$NON-NLS-1$
282 fTimeFieldDelim
.setPreferenceStore(fPreferenceStore
);
283 fTimeFieldDelim
.load();
284 fTimeFieldDelim
.setPropertyChangeListener(this);
286 fSSecFieldDelim
= new RadioGroupFieldEditor(
287 ITmfTimePreferencesConstants
.SSEC_DELIMITER
, "Sub-Second Delimiter", 5, fSubSecondDelimiters
, fPage
, true); //$NON-NLS-1$
288 fSSecFieldDelim
.setPreferenceStore(fPreferenceStore
);
289 fSSecFieldDelim
.load();
290 fSSecFieldDelim
.setPropertyChangeListener(this);
297 protected void performDefaults() {
298 fDateTimeFields
.loadDefault();
299 fSSecFields
.loadDefault();
300 fDateFieldDelim
.loadDefault();
301 fTimeFieldDelim
.loadDefault();
302 fSSecFieldDelim
.loadDefault();
303 fCombo
.loadDefault();
305 fPreferenceMap
= TmfTimePreferences
.getDefaultPreferenceMap();
310 protected void performApply() {
311 fDateTimeFields
.store();
313 fDateFieldDelim
.store();
314 fTimeFieldDelim
.store();
315 fSSecFieldDelim
.store();
318 TmfTimestampFormat
.updateDefaultFormats();
319 TmfSignalManager
.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
324 public boolean performOk() {
326 return super.performOk();
329 // ------------------------------------------------------------------------
331 // ------------------------------------------------------------------------
334 public void widgetSelected(SelectionEvent e
) {
338 public void widgetDefaultSelected(SelectionEvent e
) {
341 // ------------------------------------------------------------------------
342 // IPropertyChangeListener
343 // ------------------------------------------------------------------------
346 public void propertyChange(PropertyChangeEvent event
) {
347 Object source
= event
.getSource();
348 Object value
= event
.getNewValue();
349 if (source
instanceof RadioGroupFieldEditor
&& value
instanceof String
&&
350 !(value
.equals(fChangedProperty
) && source
.equals(fProperty
)))
352 fProperty
= ((RadioGroupFieldEditor
) source
).getPreferenceName();
353 fChangedProperty
= (String
) value
;
358 // ------------------------------------------------------------------------
360 // ------------------------------------------------------------------------
362 private void refresh() {
367 void updatePatterns() {
368 if (ITmfTimePreferencesConstants
.DATIME
.equals(fProperty
) ||
369 ITmfTimePreferencesConstants
.SUBSEC
.equals(fProperty
) ||
370 ITmfTimePreferencesConstants
.DATE_DELIMITER
.equals(fProperty
) ||
371 ITmfTimePreferencesConstants
.TIME_DELIMITER
.equals(fProperty
) ||
372 ITmfTimePreferencesConstants
.SSEC_DELIMITER
.equals(fProperty
)) {
373 fPreferenceMap
.put(fProperty
, fChangedProperty
);
377 private void displayExample() {
378 long ts
= 1332170682500677380L;
379 String timePattern
= TmfTimePreferences
.computeTimePattern(fPreferenceMap
);
380 fPatternDisplay
.setText(timePattern
);
381 fPatternDisplay
.redraw();
383 fExampleDisplay
.setText(new TmfTimestampFormat(timePattern
).format(ts
));
384 fExampleDisplay
.redraw();