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
.linuxtools
.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
.linuxtools
.internal
.tmf
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimePreferencesConstants
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimePreferences
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampFormat
;
32 import org
.eclipse
.swt
.SWT
;
33 import org
.eclipse
.swt
.events
.SelectionEvent
;
34 import org
.eclipse
.swt
.events
.SelectionListener
;
35 import org
.eclipse
.swt
.layout
.GridData
;
36 import org
.eclipse
.swt
.layout
.GridLayout
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Control
;
39 import org
.eclipse
.swt
.widgets
.Label
;
40 import org
.eclipse
.swt
.widgets
.Text
;
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
;
156 private TmfTimePreferences fTimePreference
;
159 private Composite fExampleSection
;
160 private Text fPatternDisplay
;
161 private Text fExampleDisplay
;
164 private ComboFieldEditor fCombo
;
166 // Date/Time format section
167 private RadioGroupFieldEditor fDateTimeFields
;
168 private RadioGroupFieldEditor fSSecFields
;
170 // Delimiters section
171 private RadioGroupFieldEditor fDateFieldDelim
;
172 private RadioGroupFieldEditor fTimeFieldDelim
;
173 private RadioGroupFieldEditor fSSecFieldDelim
;
175 // IPropertyChangeListener data
176 private String fProperty
;
177 private String fChangedProperty
;
179 private Map
<String
, String
> fPreferenceMap
;
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 * The default constructor
188 public TmfTimestampFormatPage() {
189 fPreferenceStore
= getPreferenceStore();
190 fTimePreference
= TmfTimePreferences
.getInstance();
191 fPreferenceMap
= fTimePreference
.getPreferenceMap();
194 // ------------------------------------------------------------------------
195 // IWorkbenchPreferencePage
196 // ------------------------------------------------------------------------
199 protected IPreferenceStore
doGetPreferenceStore() {
200 return Activator
.getDefault().getCorePreferenceStore();
204 public void init(IWorkbench workbench
) {
207 // ------------------------------------------------------------------------
209 // ------------------------------------------------------------------------
212 protected Control
createContents(Composite parent
) {
214 // Overall preference page layout
215 GridLayout gl
= new GridLayout();
218 parent
.setLayout(gl
);
219 fPage
= new Composite(parent
, SWT
.NONE
);
220 fPage
.setLayout(new GridLayout());
221 fPage
.setLayoutData(new GridData(
222 GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
| GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
));
225 fExampleSection
= new Composite(fPage
, SWT
.NONE
);
226 fExampleSection
.setLayout(new GridLayout(2, false));
227 fExampleSection
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
229 Label patternLabel
= new Label(fExampleSection
, SWT
.HORIZONTAL
);
230 patternLabel
.setText("Current Format: "); //$NON-NLS-1$
231 fPatternDisplay
= new Text(fExampleSection
, SWT
.BORDER
| SWT
.READ_ONLY
);
232 fPatternDisplay
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
234 Label exampleLabel
= new Label(fExampleSection
, SWT
.NONE
);
235 exampleLabel
.setText("Sample Display: "); //$NON-NLS-1$
236 fExampleDisplay
= new Text(fExampleSection
, SWT
.BORDER
| SWT
.READ_ONLY
);
237 fExampleDisplay
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
239 Label separator
= new Label(fPage
, SWT
.SEPARATOR
| SWT
.HORIZONTAL
| SWT
.SHADOW_NONE
);
240 separator
.setLayoutData(
241 new GridData(GridData
.HORIZONTAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.VERTICAL_ALIGN_FILL
));
244 String
[][] timeZoneIntervals
= new String
[timeZones
.length
][2];
245 timeZoneIntervals
[0][0] = timeZones
[0];
246 timeZoneIntervals
[0][1] = fPreferenceStore
.getDefaultString(ITmfTimePreferencesConstants
.TIME_ZONE
);
247 for (int i
= 1; i
< timeZones
.length
; i
++) {
250 tz
= TimeZone
.getTimeZone(timeZones
[i
]);
251 timeZoneIntervals
[i
][0] = tz
.getDisplayName();
252 timeZoneIntervals
[i
][1] = tz
.getID();
253 } catch (NullPointerException e
) {
254 System
.out
.println("TimeZone " + timeZones
[i
] + " does not exist."); //$NON-NLS-1$ //$NON-NLS-2$
258 fCombo
= new ComboFieldEditor(ITmfTimePreferencesConstants
.TIME_ZONE
, "Time Zone", timeZoneIntervals
, fPage
); //$NON-NLS-1$
259 fCombo
.setPreferenceStore(fPreferenceStore
);
261 fCombo
.setPropertyChangeListener(this);
263 // Date and Time section
264 fDateTimeFields
= new RadioGroupFieldEditor(
265 ITmfTimePreferencesConstants
.DATIME
, "Date and Time format", 3, fDateTimeFormats
, fPage
, true); //$NON-NLS-1$
266 fDateTimeFields
.setPreferenceStore(fPreferenceStore
);
267 fDateTimeFields
.load();
268 fDateTimeFields
.setPropertyChangeListener(this);
270 // Sub-second section
271 fSSecFields
= new RadioGroupFieldEditor(
272 ITmfTimePreferencesConstants
.SUBSEC
, "Sub-second format", 3, fSubSecondFormats
, fPage
, true); //$NON-NLS-1$
273 fSSecFields
.setPreferenceStore(fPreferenceStore
);
275 fSSecFields
.setPropertyChangeListener(this);
277 // Separators section
278 fDateFieldDelim
= new RadioGroupFieldEditor(
279 ITmfTimePreferencesConstants
.DATE_DELIMITER
, "Date delimiter", 5, fDateTimeDelimiters
, fPage
, true); //$NON-NLS-1$
280 fDateFieldDelim
.setPreferenceStore(fPreferenceStore
);
281 fDateFieldDelim
.load();
282 fDateFieldDelim
.setPropertyChangeListener(this);
284 fTimeFieldDelim
= new RadioGroupFieldEditor(
285 ITmfTimePreferencesConstants
.TIME_DELIMITER
, "Time delimiter", 5, fDateTimeDelimiters
, fPage
, true); //$NON-NLS-1$
286 fTimeFieldDelim
.setPreferenceStore(fPreferenceStore
);
287 fTimeFieldDelim
.load();
288 fTimeFieldDelim
.setPropertyChangeListener(this);
290 fSSecFieldDelim
= new RadioGroupFieldEditor(
291 ITmfTimePreferencesConstants
.SSEC_DELIMITER
, "Sub-Second Delimiter", 5, fSubSecondDelimiters
, fPage
, true); //$NON-NLS-1$
292 fSSecFieldDelim
.setPreferenceStore(fPreferenceStore
);
293 fSSecFieldDelim
.load();
294 fSSecFieldDelim
.setPropertyChangeListener(this);
301 protected void performDefaults() {
302 fDateTimeFields
.loadDefault();
303 fSSecFields
.loadDefault();
304 fDateFieldDelim
.loadDefault();
305 fTimeFieldDelim
.loadDefault();
306 fSSecFieldDelim
.loadDefault();
307 fCombo
.loadDefault();
309 fPreferenceMap
= TmfTimePreferences
.getInstance().getDefaultPreferenceMap();
314 protected void performApply() {
315 fDateTimeFields
.store();
317 fDateFieldDelim
.store();
318 fTimeFieldDelim
.store();
319 fSSecFieldDelim
.store();
322 TmfTimestampFormat
.updateDefaultFormats();
323 TmfSignalManager
.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
328 public boolean performOk() {
330 return super.performOk();
333 // ------------------------------------------------------------------------
335 // ------------------------------------------------------------------------
338 public void widgetSelected(SelectionEvent e
) {
342 public void widgetDefaultSelected(SelectionEvent e
) {
345 // ------------------------------------------------------------------------
346 // IPropertyChangeListener
347 // ------------------------------------------------------------------------
350 public void propertyChange(PropertyChangeEvent event
) {
351 Object source
= event
.getSource();
352 Object value
= event
.getNewValue();
353 if (source
instanceof RadioGroupFieldEditor
&& value
instanceof String
&&
354 !(value
.equals(fChangedProperty
) && source
== fProperty
))
356 fProperty
= ((RadioGroupFieldEditor
) source
).getPreferenceName();
357 fChangedProperty
= (String
) value
;
362 // ------------------------------------------------------------------------
364 // ------------------------------------------------------------------------
366 private void refresh() {
371 void updatePatterns() {
372 if (ITmfTimePreferencesConstants
.DATIME
.equals(fProperty
) ||
373 ITmfTimePreferencesConstants
.SUBSEC
.equals(fProperty
) ||
374 ITmfTimePreferencesConstants
.DATE_DELIMITER
.equals(fProperty
) ||
375 ITmfTimePreferencesConstants
.TIME_DELIMITER
.equals(fProperty
) ||
376 ITmfTimePreferencesConstants
.SSEC_DELIMITER
.equals(fProperty
)) {
377 fPreferenceMap
.put(fProperty
, fChangedProperty
);
381 private void displayExample() {
382 long ts
= 1332170682500677380L;
383 String timePattern
= fTimePreference
.computeTimePattern(fPreferenceMap
);
384 fPatternDisplay
.setText(timePattern
);
385 fPatternDisplay
.redraw();
387 fExampleDisplay
.setText(new TmfTimestampFormat(timePattern
).format(ts
));
388 fExampleDisplay
.redraw();