tmf : Use of Activator.logError() instead of System.out.println()
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / 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
2bdf0193 15package org.eclipse.tracecompass.tmf.ui.properties;
f8177ba2 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;
f8177ba2
FC
26import org.eclipse.swt.SWT;
27import org.eclipse.swt.events.SelectionEvent;
28import org.eclipse.swt.events.SelectionListener;
29import org.eclipse.swt.layout.GridData;
30import org.eclipse.swt.layout.GridLayout;
31import org.eclipse.swt.widgets.Composite;
32import org.eclipse.swt.widgets.Control;
33import org.eclipse.swt.widgets.Label;
34import org.eclipse.swt.widgets.Text;
2bdf0193
AM
35import org.eclipse.tracecompass.internal.tmf.ui.Activator;
36import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
37import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
38import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimePreferencesConstants;
39import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimePreferences;
40import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
f8177ba2
FC
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 48 *
f8177ba2
FC
49 * @author Francois Chouinard
50 */
51public class TmfTimestampFormatPage extends PreferencePage implements IWorkbenchPreferencePage, SelectionListener, IPropertyChangeListener {
52
53 // ------------------------------------------------------------------------
54 // Constants
55 // ------------------------------------------------------------------------
56
57 // Date and Time formats
58 private static final String[][] fDateTimeFormats = new String[][] {
c1cd9635
MAL
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$
f8177ba2
FC
69 };
70
71 // Sub-second formats
72 private static final String[][] fSubSecondFormats = new String[][] {
c1cd9635
MAL
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$
f8177ba2
FC
76 };
77
78 // Date and Time delimiters
79 private static final String[][] fDateTimeDelimiters = new String[][] {
c1cd9635
MAL
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$
030f8f1f 88 { "' (quote)", ITmfTimePreferencesConstants.DELIMITER_QUOTE }, //$NON-NLS-1$
c1cd9635 89 { "\" (dbl-quote)", ITmfTimePreferencesConstants.DELIMITER_DQUOT }, //$NON-NLS-1$
f8177ba2
FC
90 };
91
92 // Sub-Second delimiters
93 private static final String[][] fSubSecondDelimiters = new String[][] {
c1cd9635
MAL
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$
030f8f1f 102 { "' (quote)", ITmfTimePreferencesConstants.DELIMITER_QUOTE }, //$NON-NLS-1$
c1cd9635
MAL
103 { "\" (dbl-quote)", ITmfTimePreferencesConstants.DELIMITER_DQUOT }, //$NON-NLS-1$
104 { ". (period)", ITmfTimePreferencesConstants.DELIMITER_PERIOD }, //$NON-NLS-1$
105 };
106
107 // Time zones
108 @SuppressWarnings("nls")
109 private static final String[] timeZones = new String[] {
110 Messages.TmfTimestampFormatPage_LocalTime,
111 "GMT-12",
112 "GMT-11",
113 "GMT-10",
114 "GMT-9:30",
115 "GMT-9",
116 "GMT-7",
117 "GMT-6",
118 "GMT-5",
119 "GMT-4",
120 "GMT-3:30",
121 "GMT-3",
122 "GMT-2",
123 "GMT-1",
124 "GMT",
125 "GMT+1",
126 "GMT+2",
127 "GMT+3",
128 "GMT+3:30",
129 "GMT+4",
130 "GMT+4:30",
131 "GMT+5",
132 "GMT+5:30",
133 "GMT+6",
134 "GMT+7",
135 "GMT+8",
136 "GMT+9",
137 "GMT+9:30",
138 "GMT+10",
139 "GMT+10:30",
140 "GMT+11",
141 "GMT+11:30",
142 "GMT+12",
143 "GMT+13:00",
144 "GMT+14:00"
f8177ba2
FC
145 };
146
147 // ------------------------------------------------------------------------
148 // Attributes
149 // ------------------------------------------------------------------------
150
151 // General stuff
152 private Composite fPage;
153 private IPreferenceStore fPreferenceStore;
f8177ba2
FC
154
155 // Example section
156 private Composite fExampleSection;
157 private Text fPatternDisplay;
158 private Text fExampleDisplay;
c1cd9635
MAL
159
160 // Timezone section
161 private ComboFieldEditor fCombo;
f8177ba2
FC
162
163 // Date/Time format section
164 private RadioGroupFieldEditor fDateTimeFields;
165 private RadioGroupFieldEditor fSSecFields;
166
167 // Delimiters section
168 private RadioGroupFieldEditor fDateFieldDelim;
169 private RadioGroupFieldEditor fTimeFieldDelim;
170 private RadioGroupFieldEditor fSSecFieldDelim;
171
172 // IPropertyChangeListener data
173 private String fProperty;
174 private String fChangedProperty;
175
c1cd9635
MAL
176 private Map<String, String> fPreferenceMap;
177
f8177ba2
FC
178 // ------------------------------------------------------------------------
179 // Constructors
180 // ------------------------------------------------------------------------
181
182 /**
183 * The default constructor
184 */
185 public TmfTimestampFormatPage() {
c1cd9635 186 fPreferenceStore = getPreferenceStore();
7c34a4ea 187 fPreferenceMap = TmfTimePreferences.getPreferenceMap();
f8177ba2
FC
188 }
189
190 // ------------------------------------------------------------------------
191 // IWorkbenchPreferencePage
192 // ------------------------------------------------------------------------
193
c1cd9635
MAL
194 @Override
195 protected IPreferenceStore doGetPreferenceStore() {
196 return Activator.getDefault().getCorePreferenceStore();
197 }
198
f8177ba2
FC
199 @Override
200 public void init(IWorkbench workbench) {
201 }
202
203 // ------------------------------------------------------------------------
204 // PreferencePage
205 // ------------------------------------------------------------------------
206
f8177ba2
FC
207 @Override
208 protected Control createContents(Composite parent) {
209
210 // Overall preference page layout
030f8f1f
PT
211 GridLayout gl = new GridLayout();
212 gl.marginHeight = 0;
213 gl.marginWidth = 0;
214 parent.setLayout(gl);
f8177ba2
FC
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));
219
220 // Example section
221 fExampleSection = new Composite(fPage, SWT.NONE);
222 fExampleSection.setLayout(new GridLayout(2, false));
223 fExampleSection.setLayoutData(new GridData(GridData.FILL_BOTH));
224
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));
229
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));
234
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));
238
c1cd9635
MAL
239 // Time Zones
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++) {
244 TimeZone tz = null;
245 try {
246 tz = TimeZone.getTimeZone(timeZones[i]);
247 timeZoneIntervals[i][0] = tz.getDisplayName();
248 timeZoneIntervals[i][1] = tz.getID();
249 } catch (NullPointerException e) {
89d06813 250 Activator.getDefault().logError("TimeZone " + timeZones[i] + " does not exist.", e); //$NON-NLS-1$ //$NON-NLS-2$
c1cd9635
MAL
251 }
252 }
253
254 fCombo = new ComboFieldEditor(ITmfTimePreferencesConstants.TIME_ZONE, "Time Zone", timeZoneIntervals, fPage); //$NON-NLS-1$
255 fCombo.setPreferenceStore(fPreferenceStore);
256 fCombo.load();
257 fCombo.setPropertyChangeListener(this);
258
f8177ba2
FC
259 // Date and Time section
260 fDateTimeFields = new RadioGroupFieldEditor(
c1cd9635 261 ITmfTimePreferencesConstants.DATIME, "Date and Time format", 3, fDateTimeFormats, fPage, true); //$NON-NLS-1$
f8177ba2
FC
262 fDateTimeFields.setPreferenceStore(fPreferenceStore);
263 fDateTimeFields.load();
264 fDateTimeFields.setPropertyChangeListener(this);
265
266 // Sub-second section
267 fSSecFields = new RadioGroupFieldEditor(
c1cd9635 268 ITmfTimePreferencesConstants.SUBSEC, "Sub-second format", 3, fSubSecondFormats, fPage, true); //$NON-NLS-1$
f8177ba2
FC
269 fSSecFields.setPreferenceStore(fPreferenceStore);
270 fSSecFields.load();
271 fSSecFields.setPropertyChangeListener(this);
272
273 // Separators section
274 fDateFieldDelim = new RadioGroupFieldEditor(
c1cd9635 275 ITmfTimePreferencesConstants.DATE_DELIMITER, "Date delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$
f8177ba2
FC
276 fDateFieldDelim.setPreferenceStore(fPreferenceStore);
277 fDateFieldDelim.load();
278 fDateFieldDelim.setPropertyChangeListener(this);
279
280 fTimeFieldDelim = new RadioGroupFieldEditor(
c1cd9635 281 ITmfTimePreferencesConstants.TIME_DELIMITER, "Time delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$
f8177ba2
FC
282 fTimeFieldDelim.setPreferenceStore(fPreferenceStore);
283 fTimeFieldDelim.load();
284 fTimeFieldDelim.setPropertyChangeListener(this);
285
286 fSSecFieldDelim = new RadioGroupFieldEditor(
c1cd9635 287 ITmfTimePreferencesConstants.SSEC_DELIMITER, "Sub-Second Delimiter", 5, fSubSecondDelimiters, fPage, true); //$NON-NLS-1$
f8177ba2
FC
288 fSSecFieldDelim.setPreferenceStore(fPreferenceStore);
289 fSSecFieldDelim.load();
290 fSSecFieldDelim.setPropertyChangeListener(this);
291
f8177ba2
FC
292 refresh();
293 return fPage;
294 }
295
f8177ba2
FC
296 @Override
297 protected void performDefaults() {
298 fDateTimeFields.loadDefault();
299 fSSecFields.loadDefault();
300 fDateFieldDelim.loadDefault();
301 fTimeFieldDelim.loadDefault();
302 fSSecFieldDelim.loadDefault();
c1cd9635 303 fCombo.loadDefault();
f8177ba2 304
7c34a4ea 305 fPreferenceMap = TmfTimePreferences.getDefaultPreferenceMap();
f8177ba2
FC
306 displayExample();
307 }
308
f8177ba2
FC
309 @Override
310 protected void performApply() {
311 fDateTimeFields.store();
312 fSSecFields.store();
313 fDateFieldDelim.store();
314 fTimeFieldDelim.store();
315 fSSecFieldDelim.store();
c1cd9635 316 fCombo.store();
f8177ba2 317
c1cd9635
MAL
318 TmfTimestampFormat.updateDefaultFormats();
319 TmfSignalManager.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
f8177ba2
FC
320 displayExample();
321 }
322
d96e9054
FC
323 @Override
324 public boolean performOk() {
325 performApply();
326 return super.performOk();
327 }
328
f8177ba2
FC
329 // ------------------------------------------------------------------------
330 // SelectionListener
331 // ------------------------------------------------------------------------
332
f8177ba2
FC
333 @Override
334 public void widgetSelected(SelectionEvent e) {
335 }
336
f8177ba2
FC
337 @Override
338 public void widgetDefaultSelected(SelectionEvent e) {
339 }
340
341 // ------------------------------------------------------------------------
342 // IPropertyChangeListener
343 // ------------------------------------------------------------------------
344
f8177ba2
FC
345 @Override
346 public void propertyChange(PropertyChangeEvent event) {
347 Object source = event.getSource();
348 Object value = event.getNewValue();
349 if (source instanceof RadioGroupFieldEditor && value instanceof String &&
843f1eb2 350 !(value.equals(fChangedProperty) && source.equals(fProperty)))
f8177ba2
FC
351 {
352 fProperty = ((RadioGroupFieldEditor) source).getPreferenceName();
353 fChangedProperty = (String) value;
354 refresh();
355 }
356 }
357
358 // ------------------------------------------------------------------------
359 // Helper functions
360 // ------------------------------------------------------------------------
361
362 private void refresh() {
363 updatePatterns();
364 displayExample();
365 }
366
367 void updatePatterns() {
c1cd9635
MAL
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);
f8177ba2 374 }
f8177ba2
FC
375 }
376
377 private void displayExample() {
378 long ts = 1332170682500677380L;
7c34a4ea 379 String timePattern = TmfTimePreferences.computeTimePattern(fPreferenceMap);
c1cd9635 380 fPatternDisplay.setText(timePattern);
f8177ba2
FC
381 fPatternDisplay.redraw();
382
c1cd9635 383 fExampleDisplay.setText(new TmfTimestampFormat(timePattern).format(ts));
f8177ba2
FC
384 fExampleDisplay.redraw();
385 }
386
387}
This page took 0.09517 seconds and 5 git commands to generate.