Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / 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.linuxtools.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.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;
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 private TmfTimePreferences fTimePreference;
157
158 // Example section
159 private Composite fExampleSection;
160 private Text fPatternDisplay;
161 private Text fExampleDisplay;
162
163 // Timezone section
164 private ComboFieldEditor fCombo;
165
166 // Date/Time format section
167 private RadioGroupFieldEditor fDateTimeFields;
168 private RadioGroupFieldEditor fSSecFields;
169
170 // Delimiters section
171 private RadioGroupFieldEditor fDateFieldDelim;
172 private RadioGroupFieldEditor fTimeFieldDelim;
173 private RadioGroupFieldEditor fSSecFieldDelim;
174
175 // IPropertyChangeListener data
176 private String fProperty;
177 private String fChangedProperty;
178
179 private Map<String, String> fPreferenceMap;
180
181 // ------------------------------------------------------------------------
182 // Constructors
183 // ------------------------------------------------------------------------
184
185 /**
186 * The default constructor
187 */
188 public TmfTimestampFormatPage() {
189 fPreferenceStore = getPreferenceStore();
190 fTimePreference = TmfTimePreferences.getInstance();
191 fPreferenceMap = fTimePreference.getPreferenceMap();
192 }
193
194 // ------------------------------------------------------------------------
195 // IWorkbenchPreferencePage
196 // ------------------------------------------------------------------------
197
198 @Override
199 protected IPreferenceStore doGetPreferenceStore() {
200 return Activator.getDefault().getCorePreferenceStore();
201 }
202
203 @Override
204 public void init(IWorkbench workbench) {
205 }
206
207 // ------------------------------------------------------------------------
208 // PreferencePage
209 // ------------------------------------------------------------------------
210
211 @Override
212 protected Control createContents(Composite parent) {
213
214 // Overall preference page layout
215 GridLayout gl = new GridLayout();
216 gl.marginHeight = 0;
217 gl.marginWidth = 0;
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));
223
224 // Example section
225 fExampleSection = new Composite(fPage, SWT.NONE);
226 fExampleSection.setLayout(new GridLayout(2, false));
227 fExampleSection.setLayoutData(new GridData(GridData.FILL_BOTH));
228
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));
233
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));
238
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));
242
243 // Time Zones
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++) {
248 TimeZone tz = null;
249 try {
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$
255 }
256 }
257
258 fCombo = new ComboFieldEditor(ITmfTimePreferencesConstants.TIME_ZONE, "Time Zone", timeZoneIntervals, fPage); //$NON-NLS-1$
259 fCombo.setPreferenceStore(fPreferenceStore);
260 fCombo.load();
261 fCombo.setPropertyChangeListener(this);
262
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);
269
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);
274 fSSecFields.load();
275 fSSecFields.setPropertyChangeListener(this);
276
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);
283
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);
289
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);
295
296 refresh();
297 return fPage;
298 }
299
300 @Override
301 protected void performDefaults() {
302 fDateTimeFields.loadDefault();
303 fSSecFields.loadDefault();
304 fDateFieldDelim.loadDefault();
305 fTimeFieldDelim.loadDefault();
306 fSSecFieldDelim.loadDefault();
307 fCombo.loadDefault();
308
309 fPreferenceMap = TmfTimePreferences.getInstance().getDefaultPreferenceMap();
310 displayExample();
311 }
312
313 @Override
314 protected void performApply() {
315 fDateTimeFields.store();
316 fSSecFields.store();
317 fDateFieldDelim.store();
318 fTimeFieldDelim.store();
319 fSSecFieldDelim.store();
320 fCombo.store();
321
322 TmfTimestampFormat.updateDefaultFormats();
323 TmfSignalManager.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
324 displayExample();
325 }
326
327 @Override
328 public boolean performOk() {
329 performApply();
330 return super.performOk();
331 }
332
333 // ------------------------------------------------------------------------
334 // SelectionListener
335 // ------------------------------------------------------------------------
336
337 @Override
338 public void widgetSelected(SelectionEvent e) {
339 }
340
341 @Override
342 public void widgetDefaultSelected(SelectionEvent e) {
343 }
344
345 // ------------------------------------------------------------------------
346 // IPropertyChangeListener
347 // ------------------------------------------------------------------------
348
349 @Override
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))
355 {
356 fProperty = ((RadioGroupFieldEditor) source).getPreferenceName();
357 fChangedProperty = (String) value;
358 refresh();
359 }
360 }
361
362 // ------------------------------------------------------------------------
363 // Helper functions
364 // ------------------------------------------------------------------------
365
366 private void refresh() {
367 updatePatterns();
368 displayExample();
369 }
370
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);
378 }
379 }
380
381 private void displayExample() {
382 long ts = 1332170682500677380L;
383 String timePattern = fTimePreference.computeTimePattern(fPreferenceMap);
384 fPatternDisplay.setText(timePattern);
385 fPatternDisplay.redraw();
386
387 fExampleDisplay.setText(new TmfTimestampFormat(timePattern).format(ts));
388 fExampleDisplay.redraw();
389 }
390
391 }
This page took 0.038452 seconds and 5 git commands to generate.