ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / properties / TmfTimestampFormatPage.java
CommitLineData
f8177ba2 1/*******************************************************************************
030f8f1f 2 * Copyright (c) 2012, 2014 Ericsson
f8177ba2
FC
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:
c1cd9635
MAL
10 * Francois Chouinard - Initial API and implementation
11 * Marc-Andre Laperle - Add time zone preference
030f8f1f 12 * Patrick Tasse - Updated for fraction of seconds
f8177ba2
FC
13 *******************************************************************************/
14
15package org.eclipse.linuxtools.tmf.ui.properties;
16
c1cd9635
MAL
17import java.util.Map;
18import java.util.TimeZone;
19
20import org.eclipse.jface.preference.ComboFieldEditor;
f8177ba2
FC
21import org.eclipse.jface.preference.IPreferenceStore;
22import org.eclipse.jface.preference.PreferencePage;
23import org.eclipse.jface.preference.RadioGroupFieldEditor;
24import org.eclipse.jface.util.IPropertyChangeListener;
25import org.eclipse.jface.util.PropertyChangeEvent;
c1cd9635
MAL
26import org.eclipse.linuxtools.internal.tmf.ui.Activator;
27import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
28import org.eclipse.linuxtools.tmf.core.signal.TmfTimestampFormatUpdateSignal;
29import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimePreferencesConstants;
30import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimePreferences;
3bd46eef 31import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
f8177ba2
FC
32import org.eclipse.swt.SWT;
33import org.eclipse.swt.events.SelectionEvent;
34import org.eclipse.swt.events.SelectionListener;
35import org.eclipse.swt.layout.GridData;
36import org.eclipse.swt.layout.GridLayout;
37import org.eclipse.swt.widgets.Composite;
38import org.eclipse.swt.widgets.Control;
39import org.eclipse.swt.widgets.Label;
40import org.eclipse.swt.widgets.Text;
41import org.eclipse.ui.IWorkbench;
42import org.eclipse.ui.IWorkbenchPreferencePage;
43
44/**
c1cd9635
MAL
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.
f8177ba2
FC
48 *
49 * @version 1.0
50 * @author Francois Chouinard
e6809677 51 * @since 2.0
f8177ba2
FC
52 */
53public 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[][] {
c1cd9635
MAL
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$
f8177ba2
FC
71 };
72
73 // Sub-second formats
74 private static final String[][] fSubSecondFormats = new String[][] {
c1cd9635
MAL
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$
f8177ba2
FC
78 };
79
80 // Date and Time delimiters
81 private static final String[][] fDateTimeDelimiters = new String[][] {
c1cd9635
MAL
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$
030f8f1f 90 { "' (quote)", ITmfTimePreferencesConstants.DELIMITER_QUOTE }, //$NON-NLS-1$
c1cd9635 91 { "\" (dbl-quote)", ITmfTimePreferencesConstants.DELIMITER_DQUOT }, //$NON-NLS-1$
f8177ba2
FC
92 };
93
94 // Sub-Second delimiters
95 private static final String[][] fSubSecondDelimiters = new String[][] {
c1cd9635
MAL
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$
030f8f1f 104 { "' (quote)", ITmfTimePreferencesConstants.DELIMITER_QUOTE }, //$NON-NLS-1$
c1cd9635
MAL
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"
f8177ba2
FC
147 };
148
149 // ------------------------------------------------------------------------
150 // Attributes
151 // ------------------------------------------------------------------------
152
153 // General stuff
154 private Composite fPage;
155 private IPreferenceStore fPreferenceStore;
7bab46e6 156 private TmfTimePreferences fTimePreference;
f8177ba2
FC
157
158 // Example section
159 private Composite fExampleSection;
160 private Text fPatternDisplay;
161 private Text fExampleDisplay;
c1cd9635
MAL
162
163 // Timezone section
164 private ComboFieldEditor fCombo;
f8177ba2
FC
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
c1cd9635
MAL
179 private Map<String, String> fPreferenceMap;
180
f8177ba2
FC
181 // ------------------------------------------------------------------------
182 // Constructors
183 // ------------------------------------------------------------------------
184
185 /**
186 * The default constructor
187 */
188 public TmfTimestampFormatPage() {
c1cd9635 189 fPreferenceStore = getPreferenceStore();
7bab46e6
AM
190 fTimePreference = TmfTimePreferences.getInstance();
191 fPreferenceMap = fTimePreference.getPreferenceMap();
f8177ba2
FC
192 }
193
194 // ------------------------------------------------------------------------
195 // IWorkbenchPreferencePage
196 // ------------------------------------------------------------------------
197
c1cd9635
MAL
198 @Override
199 protected IPreferenceStore doGetPreferenceStore() {
200 return Activator.getDefault().getCorePreferenceStore();
201 }
202
f8177ba2
FC
203 @Override
204 public void init(IWorkbench workbench) {
205 }
206
207 // ------------------------------------------------------------------------
208 // PreferencePage
209 // ------------------------------------------------------------------------
210
f8177ba2
FC
211 @Override
212 protected Control createContents(Composite parent) {
213
214 // Overall preference page layout
030f8f1f
PT
215 GridLayout gl = new GridLayout();
216 gl.marginHeight = 0;
217 gl.marginWidth = 0;
218 parent.setLayout(gl);
f8177ba2
FC
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
c1cd9635
MAL
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
f8177ba2
FC
263 // Date and Time section
264 fDateTimeFields = new RadioGroupFieldEditor(
c1cd9635 265 ITmfTimePreferencesConstants.DATIME, "Date and Time format", 3, fDateTimeFormats, fPage, true); //$NON-NLS-1$
f8177ba2
FC
266 fDateTimeFields.setPreferenceStore(fPreferenceStore);
267 fDateTimeFields.load();
268 fDateTimeFields.setPropertyChangeListener(this);
269
270 // Sub-second section
271 fSSecFields = new RadioGroupFieldEditor(
c1cd9635 272 ITmfTimePreferencesConstants.SUBSEC, "Sub-second format", 3, fSubSecondFormats, fPage, true); //$NON-NLS-1$
f8177ba2
FC
273 fSSecFields.setPreferenceStore(fPreferenceStore);
274 fSSecFields.load();
275 fSSecFields.setPropertyChangeListener(this);
276
277 // Separators section
278 fDateFieldDelim = new RadioGroupFieldEditor(
c1cd9635 279 ITmfTimePreferencesConstants.DATE_DELIMITER, "Date delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$
f8177ba2
FC
280 fDateFieldDelim.setPreferenceStore(fPreferenceStore);
281 fDateFieldDelim.load();
282 fDateFieldDelim.setPropertyChangeListener(this);
283
284 fTimeFieldDelim = new RadioGroupFieldEditor(
c1cd9635 285 ITmfTimePreferencesConstants.TIME_DELIMITER, "Time delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$
f8177ba2
FC
286 fTimeFieldDelim.setPreferenceStore(fPreferenceStore);
287 fTimeFieldDelim.load();
288 fTimeFieldDelim.setPropertyChangeListener(this);
289
290 fSSecFieldDelim = new RadioGroupFieldEditor(
c1cd9635 291 ITmfTimePreferencesConstants.SSEC_DELIMITER, "Sub-Second Delimiter", 5, fSubSecondDelimiters, fPage, true); //$NON-NLS-1$
f8177ba2
FC
292 fSSecFieldDelim.setPreferenceStore(fPreferenceStore);
293 fSSecFieldDelim.load();
294 fSSecFieldDelim.setPropertyChangeListener(this);
295
f8177ba2
FC
296 refresh();
297 return fPage;
298 }
299
f8177ba2
FC
300 @Override
301 protected void performDefaults() {
302 fDateTimeFields.loadDefault();
303 fSSecFields.loadDefault();
304 fDateFieldDelim.loadDefault();
305 fTimeFieldDelim.loadDefault();
306 fSSecFieldDelim.loadDefault();
c1cd9635 307 fCombo.loadDefault();
f8177ba2 308
7bab46e6 309 fPreferenceMap = TmfTimePreferences.getInstance().getDefaultPreferenceMap();
f8177ba2
FC
310 displayExample();
311 }
312
f8177ba2
FC
313 @Override
314 protected void performApply() {
315 fDateTimeFields.store();
316 fSSecFields.store();
317 fDateFieldDelim.store();
318 fTimeFieldDelim.store();
319 fSSecFieldDelim.store();
c1cd9635 320 fCombo.store();
f8177ba2 321
c1cd9635
MAL
322 TmfTimestampFormat.updateDefaultFormats();
323 TmfSignalManager.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
f8177ba2
FC
324 displayExample();
325 }
326
d96e9054
FC
327 @Override
328 public boolean performOk() {
329 performApply();
330 return super.performOk();
331 }
332
f8177ba2
FC
333 // ------------------------------------------------------------------------
334 // SelectionListener
335 // ------------------------------------------------------------------------
336
f8177ba2
FC
337 @Override
338 public void widgetSelected(SelectionEvent e) {
339 }
340
f8177ba2
FC
341 @Override
342 public void widgetDefaultSelected(SelectionEvent e) {
343 }
344
345 // ------------------------------------------------------------------------
346 // IPropertyChangeListener
347 // ------------------------------------------------------------------------
348
f8177ba2
FC
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() {
c1cd9635
MAL
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);
f8177ba2 378 }
f8177ba2
FC
379 }
380
381 private void displayExample() {
382 long ts = 1332170682500677380L;
7bab46e6 383 String timePattern = fTimePreference.computeTimePattern(fPreferenceMap);
c1cd9635 384 fPatternDisplay.setText(timePattern);
f8177ba2
FC
385 fPatternDisplay.redraw();
386
c1cd9635 387 fExampleDisplay.setText(new TmfTimestampFormat(timePattern).format(ts));
f8177ba2
FC
388 fExampleDisplay.redraw();
389 }
390
391}
This page took 0.064792 seconds and 5 git commands to generate.