Commit | Line | Data |
---|---|---|
f8177ba2 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 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 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.properties; | |
14 | ||
15 | import org.eclipse.jface.preference.IPreferenceStore; | |
16 | import org.eclipse.jface.preference.PreferencePage; | |
17 | import org.eclipse.jface.preference.RadioGroupFieldEditor; | |
18 | import org.eclipse.jface.util.IPropertyChangeListener; | |
19 | import org.eclipse.jface.util.PropertyChangeEvent; | |
20 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestampFormat; | |
21 | import org.eclipse.swt.SWT; | |
22 | import org.eclipse.swt.events.SelectionEvent; | |
23 | import org.eclipse.swt.events.SelectionListener; | |
24 | import org.eclipse.swt.layout.GridData; | |
25 | import org.eclipse.swt.layout.GridLayout; | |
26 | import org.eclipse.swt.widgets.Composite; | |
27 | import org.eclipse.swt.widgets.Control; | |
28 | import org.eclipse.swt.widgets.Label; | |
29 | import org.eclipse.swt.widgets.Text; | |
30 | import org.eclipse.ui.IWorkbench; | |
31 | import org.eclipse.ui.IWorkbenchPreferencePage; | |
32 | ||
33 | /** | |
34 | * The TMF timestamp format configuration page. This page is used to select | |
35 | * the global timestamp and interval time formats (for display and parsing). | |
36 | * The user can either pick a pre-defined format or enter his/her own. | |
37 | * | |
38 | * @version 1.0 | |
39 | * @author Francois Chouinard | |
40 | */ | |
41 | public class TmfTimestampFormatPage extends PreferencePage implements IWorkbenchPreferencePage, SelectionListener, IPropertyChangeListener { | |
42 | ||
43 | // ------------------------------------------------------------------------ | |
44 | // Constants | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
47 | // Date and Time formats | |
48 | private static final String[][] fDateTimeFormats = new String[][] { | |
49 | { TmfTimePreferences.DATE_YEAR_FMT, TmfTimePreferences.DATE_YEAR_FMT }, | |
50 | { TmfTimePreferences.DATE_YEAR2_FMT, TmfTimePreferences.DATE_YEAR2_FMT }, | |
51 | { TmfTimePreferences.DATE_MONTH_FMT, TmfTimePreferences.DATE_MONTH_FMT }, | |
52 | { TmfTimePreferences.DATE_DAY_FMT, TmfTimePreferences.DATE_DAY_FMT }, | |
53 | { TmfTimePreferences.DATE_JDAY_FMT, TmfTimePreferences.DATE_JDAY_FMT }, | |
54 | { TmfTimePreferences.TIME_HOUR_FMT, TmfTimePreferences.TIME_HOUR_FMT }, | |
55 | { TmfTimePreferences.TIME_MINUTE_FMT, TmfTimePreferences.TIME_MINUTE_FMT }, | |
56 | { TmfTimePreferences.TIME_SECOND_FMT, TmfTimePreferences.TIME_SECOND_FMT }, | |
57 | { TmfTimePreferences.TIME_ELAPSED_FMT + " (secs in epoch)", TmfTimePreferences.TIME_ELAPSED_FMT }, //$NON-NLS-1$ | |
58 | { "(none)", TmfTimePreferences.TIME_NO_FMT }, //$NON-NLS-1$ | |
59 | }; | |
60 | ||
61 | // Sub-second formats | |
62 | private static final String[][] fSubSecondFormats = new String[][] { | |
63 | { TmfTimePreferences.SUBSEC_MILLI_FMT + " (ms)", TmfTimePreferences.SUBSEC_MILLI_FMT }, //$NON-NLS-1$ | |
64 | { TmfTimePreferences.SUBSEC_MICRO_FMT + " (µs)", TmfTimePreferences.SUBSEC_MICRO_FMT }, //$NON-NLS-1$ | |
65 | { TmfTimePreferences.SUBSEC_NANO_FMT + " (ns)", TmfTimePreferences.SUBSEC_NANO_FMT }, //$NON-NLS-1$ | |
66 | }; | |
67 | ||
68 | // Date and Time delimiters | |
69 | private static final String[][] fDateTimeDelimiters = new String[][] { | |
70 | { "(none)", TmfTimePreferences.DELIMITER_NONE }, //$NON-NLS-1$ | |
71 | { " (space)", TmfTimePreferences.DELIMITER_SPACE }, //$NON-NLS-1$ | |
72 | { ", (comma)", TmfTimePreferences.DELIMITER_COMMA }, //$NON-NLS-1$ | |
73 | { "- (dash)", TmfTimePreferences.DELIMITER_DASH }, //$NON-NLS-1$ | |
74 | { "_ (underline)", TmfTimePreferences.DELIMITER_UNDERLINE }, //$NON-NLS-1$ | |
75 | { ": (colon)", TmfTimePreferences.DELIMITER_COLON }, //$NON-NLS-1$ | |
76 | { "; (semicolon)", TmfTimePreferences.DELIMITER_SEMICOLON }, //$NON-NLS-1$ | |
77 | { "/ (slash)", TmfTimePreferences.DELIMITER_SLASH }, //$NON-NLS-1$ | |
78 | { "\" (dbl-quote)", TmfTimePreferences.DELIMITER_DQUOT }, //$NON-NLS-1$ | |
79 | }; | |
80 | ||
81 | // Sub-Second delimiters | |
82 | private static final String[][] fSubSecondDelimiters = new String[][] { | |
83 | { "(none)", TmfTimePreferences.DELIMITER_NONE }, //$NON-NLS-1$ | |
84 | { " (space)", TmfTimePreferences.DELIMITER_SPACE }, //$NON-NLS-1$ | |
85 | { ", (comma)", TmfTimePreferences.DELIMITER_COMMA }, //$NON-NLS-1$ | |
86 | { "- (dash)", TmfTimePreferences.DELIMITER_DASH }, //$NON-NLS-1$ | |
87 | { "_ (underline)", TmfTimePreferences.DELIMITER_UNDERLINE }, //$NON-NLS-1$ | |
88 | { ": (colon)", TmfTimePreferences.DELIMITER_COLON }, //$NON-NLS-1$ | |
89 | { "; (semicolon)", TmfTimePreferences.DELIMITER_SEMICOLON }, //$NON-NLS-1$ | |
90 | { "/ (slash)", TmfTimePreferences.DELIMITER_SLASH }, //$NON-NLS-1$ | |
91 | { "\" (dbl-quote)", TmfTimePreferences.DELIMITER_DQUOT }, //$NON-NLS-1$ | |
92 | { ". (period)", TmfTimePreferences.DELIMITER_PERIOD }, //$NON-NLS-1$ | |
93 | }; | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // Attributes | |
97 | // ------------------------------------------------------------------------ | |
98 | ||
99 | // General stuff | |
100 | private Composite fPage; | |
101 | private IPreferenceStore fPreferenceStore; | |
102 | private TmfTimePreferences fTimePreference; | |
103 | ||
104 | // Example section | |
105 | private Composite fExampleSection; | |
106 | private Text fPatternDisplay; | |
107 | private Text fExampleDisplay; | |
108 | private String fTimePattern; | |
109 | ||
110 | // Date/Time format section | |
111 | private RadioGroupFieldEditor fDateTimeFields; | |
112 | private RadioGroupFieldEditor fSSecFields; | |
113 | ||
114 | // Delimiters section | |
115 | private RadioGroupFieldEditor fDateFieldDelim; | |
116 | private RadioGroupFieldEditor fTimeFieldDelim; | |
117 | private RadioGroupFieldEditor fSSecFieldDelim; | |
118 | ||
119 | // IPropertyChangeListener data | |
120 | private String fProperty; | |
121 | private String fChangedProperty; | |
122 | ||
123 | // ------------------------------------------------------------------------ | |
124 | // Constructors | |
125 | // ------------------------------------------------------------------------ | |
126 | ||
127 | /** | |
128 | * The default constructor | |
129 | */ | |
130 | public TmfTimestampFormatPage() { | |
131 | fPreferenceStore = TmfTimePreferences.getPreferenceStore(); | |
132 | fTimePreference = TmfTimePreferences.getInstance(); | |
133 | } | |
134 | ||
135 | // ------------------------------------------------------------------------ | |
136 | // IWorkbenchPreferencePage | |
137 | // ------------------------------------------------------------------------ | |
138 | ||
139 | /* (non-Javadoc) | |
140 | * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench) | |
141 | */ | |
142 | @Override | |
143 | public void init(IWorkbench workbench) { | |
144 | } | |
145 | ||
146 | // ------------------------------------------------------------------------ | |
147 | // PreferencePage | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
150 | /* (non-Javadoc) | |
151 | * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite) | |
152 | */ | |
153 | @Override | |
154 | protected Control createContents(Composite parent) { | |
155 | ||
156 | // Overall preference page layout | |
157 | parent.setLayout(new GridLayout()); | |
158 | fPage = new Composite(parent, SWT.NONE); | |
159 | fPage.setLayout(new GridLayout()); | |
160 | fPage.setLayoutData(new GridData( | |
161 | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL)); | |
162 | ||
163 | // Example section | |
164 | fExampleSection = new Composite(fPage, SWT.NONE); | |
165 | fExampleSection.setLayout(new GridLayout(2, false)); | |
166 | fExampleSection.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
167 | ||
168 | Label patternLabel = new Label(fExampleSection, SWT.HORIZONTAL); | |
169 | patternLabel.setText("Current Format: "); //$NON-NLS-1$ | |
170 | fPatternDisplay = new Text(fExampleSection, SWT.BORDER | SWT.READ_ONLY); | |
171 | fPatternDisplay.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
172 | ||
173 | Label exampleLabel = new Label(fExampleSection, SWT.NONE); | |
174 | exampleLabel.setText("Sample Display: "); //$NON-NLS-1$ | |
175 | fExampleDisplay = new Text(fExampleSection, SWT.BORDER | SWT.READ_ONLY); | |
176 | fExampleDisplay.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
177 | ||
178 | Label separator = new Label(fPage, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.SHADOW_NONE); | |
179 | separator.setLayoutData( | |
180 | new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); | |
181 | ||
182 | // Date and Time section | |
183 | fDateTimeFields = new RadioGroupFieldEditor( | |
184 | TmfTimePreferences.DATIME, "Date and Time format", 3, fDateTimeFormats, fPage, true); //$NON-NLS-1$ | |
185 | fDateTimeFields.setPreferenceStore(fPreferenceStore); | |
186 | fDateTimeFields.load(); | |
187 | fDateTimeFields.setPropertyChangeListener(this); | |
188 | ||
189 | // Sub-second section | |
190 | fSSecFields = new RadioGroupFieldEditor( | |
191 | TmfTimePreferences.SUBSEC, "Sub-second format", 3, fSubSecondFormats, fPage, true); //$NON-NLS-1$ | |
192 | fSSecFields.setPreferenceStore(fPreferenceStore); | |
193 | fSSecFields.load(); | |
194 | fSSecFields.setPropertyChangeListener(this); | |
195 | ||
196 | // Separators section | |
197 | fDateFieldDelim = new RadioGroupFieldEditor( | |
198 | TmfTimePreferences.DATE_DELIMITER, "Date delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$ | |
199 | fDateFieldDelim.setPreferenceStore(fPreferenceStore); | |
200 | fDateFieldDelim.load(); | |
201 | fDateFieldDelim.setPropertyChangeListener(this); | |
202 | ||
203 | fTimeFieldDelim = new RadioGroupFieldEditor( | |
204 | TmfTimePreferences.TIME_DELIMITER, "Time delimiter", 5, fDateTimeDelimiters, fPage, true); //$NON-NLS-1$ | |
205 | fTimeFieldDelim.setPreferenceStore(fPreferenceStore); | |
206 | fTimeFieldDelim.load(); | |
207 | fTimeFieldDelim.setPropertyChangeListener(this); | |
208 | ||
209 | fSSecFieldDelim = new RadioGroupFieldEditor( | |
210 | TmfTimePreferences.SSEC_DELIMITER, "Sub-Second Delimiter", 5, fSubSecondDelimiters, fPage, true); //$NON-NLS-1$ | |
211 | fSSecFieldDelim.setPreferenceStore(fPreferenceStore); | |
212 | fSSecFieldDelim.load(); | |
213 | fSSecFieldDelim.setPropertyChangeListener(this); | |
214 | ||
215 | fTimePreference.initPatterns(); | |
216 | refresh(); | |
217 | return fPage; | |
218 | } | |
219 | ||
220 | /* | |
221 | * (non-Javadoc) | |
222 | * @see org.eclipse.jface.preference.PreferencePage#performDefaults() | |
223 | */ | |
224 | @Override | |
225 | protected void performDefaults() { | |
226 | fDateTimeFields.loadDefault(); | |
227 | fSSecFields.loadDefault(); | |
228 | fDateFieldDelim.loadDefault(); | |
229 | fTimeFieldDelim.loadDefault(); | |
230 | fSSecFieldDelim.loadDefault(); | |
231 | ||
232 | fTimePreference.setDefaults(); | |
233 | fTimePattern = TmfTimePreferences.getTimePattern(); | |
234 | displayExample(); | |
235 | } | |
236 | ||
237 | /* | |
238 | * (non-Javadoc) | |
239 | * @see org.eclipse.jface.preference.PreferencePage#performApply() | |
240 | */ | |
241 | @Override | |
242 | protected void performApply() { | |
243 | fDateTimeFields.store(); | |
244 | fSSecFields.store(); | |
245 | fDateFieldDelim.store(); | |
246 | fTimeFieldDelim.store(); | |
247 | fSSecFieldDelim.store(); | |
248 | ||
249 | TmfTimePreferences.setTimePattern(fTimePattern); | |
250 | displayExample(); | |
251 | } | |
252 | ||
d96e9054 FC |
253 | /* (non-Javadoc) |
254 | * @see org.eclipse.jface.preference.PreferencePage#performOk() | |
255 | */ | |
256 | @Override | |
257 | public boolean performOk() { | |
258 | performApply(); | |
259 | return super.performOk(); | |
260 | } | |
261 | ||
f8177ba2 FC |
262 | // ------------------------------------------------------------------------ |
263 | // SelectionListener | |
264 | // ------------------------------------------------------------------------ | |
265 | ||
266 | /* (non-Javadoc) | |
267 | * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) | |
268 | */ | |
269 | @Override | |
270 | public void widgetSelected(SelectionEvent e) { | |
271 | } | |
272 | ||
273 | /* (non-Javadoc) | |
274 | * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) | |
275 | */ | |
276 | @Override | |
277 | public void widgetDefaultSelected(SelectionEvent e) { | |
278 | } | |
279 | ||
280 | // ------------------------------------------------------------------------ | |
281 | // IPropertyChangeListener | |
282 | // ------------------------------------------------------------------------ | |
283 | ||
284 | /* (non-Javadoc) | |
285 | * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent) | |
286 | */ | |
287 | @Override | |
288 | public void propertyChange(PropertyChangeEvent event) { | |
289 | Object source = event.getSource(); | |
290 | Object value = event.getNewValue(); | |
291 | if (source instanceof RadioGroupFieldEditor && value instanceof String && | |
292 | !(value.equals(fChangedProperty) && source == fProperty)) | |
293 | { | |
294 | fProperty = ((RadioGroupFieldEditor) source).getPreferenceName(); | |
295 | fChangedProperty = (String) value; | |
296 | refresh(); | |
297 | } | |
298 | } | |
299 | ||
300 | // ------------------------------------------------------------------------ | |
301 | // Helper functions | |
302 | // ------------------------------------------------------------------------ | |
303 | ||
304 | private void refresh() { | |
305 | updatePatterns(); | |
306 | displayExample(); | |
307 | } | |
308 | ||
309 | void updatePatterns() { | |
310 | if (TmfTimePreferences.DATIME.equals(fProperty)) { | |
311 | fTimePreference.setDateTimeFormat(fChangedProperty); | |
312 | } else if (TmfTimePreferences.SUBSEC.equals(fProperty)) { | |
313 | fTimePreference.setSSecFormat(fChangedProperty); | |
314 | } else if (TmfTimePreferences.DATE_DELIMITER.equals(fProperty)) { | |
315 | fTimePreference.setDateFieldSep(fChangedProperty); | |
316 | } else if (TmfTimePreferences.TIME_DELIMITER.equals(fProperty)) { | |
317 | fTimePreference.setTimeFieldSep(fChangedProperty); | |
318 | } else if (TmfTimePreferences.SSEC_DELIMITER.equals(fProperty)) { | |
319 | fTimePreference.setSSecFieldSep(fChangedProperty); | |
320 | } | |
321 | fTimePreference.updatePatterns(); | |
322 | fTimePattern = TmfTimePreferences.getTimePattern(); | |
323 | } | |
324 | ||
325 | private void displayExample() { | |
326 | long ts = 1332170682500677380L; | |
327 | fPatternDisplay.setText(fTimePattern); | |
328 | fPatternDisplay.redraw(); | |
329 | ||
330 | fExampleDisplay.setText(new TmfTimestampFormat(fTimePattern).format(ts)); | |
331 | fExampleDisplay.redraw(); | |
332 | } | |
333 | ||
334 | } |