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