|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2000, 2020 IBM Corporation and others. |
| 2 | + * Copyright (c) 2000, 2025 IBM Corporation and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
15 | 15 |
|
16 | 16 |
|
17 | 17 | import java.text.MessageFormat; |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.regex.Pattern; |
18 | 21 |
|
19 | 22 | import org.eclipse.debug.internal.ui.DebugUIPlugin; |
20 | 23 | import org.eclipse.debug.internal.ui.IDebugHelpContextIds; |
| 24 | +import org.eclipse.debug.internal.ui.SWTFactory; |
| 25 | +import org.eclipse.debug.internal.ui.views.console.ProcessConsole; |
21 | 26 | import org.eclipse.jface.preference.BooleanFieldEditor; |
22 | 27 | import org.eclipse.jface.preference.ColorFieldEditor; |
23 | 28 | import org.eclipse.jface.preference.FieldEditor; |
24 | 29 | import org.eclipse.jface.preference.FieldEditorPreferencePage; |
25 | 30 | import org.eclipse.jface.preference.IPreferenceStore; |
26 | 31 | import org.eclipse.jface.preference.IntegerFieldEditor; |
27 | 32 | import org.eclipse.jface.util.PropertyChangeEvent; |
| 33 | +import org.eclipse.jface.viewers.ArrayContentProvider; |
| 34 | +import org.eclipse.jface.viewers.ComboViewer; |
| 35 | +import org.eclipse.jface.viewers.IStructuredSelection; |
28 | 36 | import org.eclipse.swt.SWT; |
29 | 37 | import org.eclipse.swt.events.SelectionAdapter; |
30 | 38 | import org.eclipse.swt.events.SelectionEvent; |
31 | 39 | import org.eclipse.swt.layout.GridData; |
32 | 40 | import org.eclipse.swt.widgets.Button; |
| 41 | +import org.eclipse.swt.widgets.Combo; |
33 | 42 | import org.eclipse.swt.widgets.Composite; |
| 43 | +import org.eclipse.swt.widgets.Label; |
34 | 44 | import org.eclipse.ui.IWorkbench; |
35 | 45 | import org.eclipse.ui.IWorkbenchPreferencePage; |
36 | 46 | import org.eclipse.ui.PlatformUI; |
@@ -87,6 +97,14 @@ protected void clearErrorMessage() { |
87 | 97 | private BooleanFieldEditor2 fInterpretControlCharactersEditor; |
88 | 98 | private BooleanFieldEditor2 fInterpretCrAsControlCharacterEditor; |
89 | 99 |
|
| 100 | + private ComboViewer fElapsedFormat; |
| 101 | + |
| 102 | + private Label fElapsedFormatPreviewLabel; |
| 103 | + |
| 104 | + @SuppressWarnings("nls") |
| 105 | + private static final String[] ELAPSED_FORMATS = new String[] { "H:MM:SS", "HH:MM:SS", "HH:MM:SS.mmm", "MM:SS.mmm", |
| 106 | + "HHh MMm SSs", DebugPreferencesMessages.ConsoleDisableElapsedTime.toString() }; |
| 107 | + |
90 | 108 | /** |
91 | 109 | * Create the console page. |
92 | 110 | */ |
@@ -159,6 +177,40 @@ public void widgetSelected(SelectionEvent e) { |
159 | 177 | addField(new BooleanFieldEditor(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT, DebugPreferencesMessages.ConsolePreferencePage_Show__Console_View_when_there_is_program_output_3, SWT.NONE, getFieldEditorParent())); |
160 | 178 | addField(new BooleanFieldEditor(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR, DebugPreferencesMessages.ConsolePreferencePage_Show__Console_View_when_there_is_program_error_3, SWT.NONE, getFieldEditorParent())); |
161 | 179 |
|
| 180 | + Label comboLabel = new Label(getFieldEditorParent(), SWT.NONE); |
| 181 | + comboLabel.setText(DebugPreferencesMessages.ConsoleElapsedTimeLabel); |
| 182 | + fElapsedFormat = new ComboViewer(getFieldEditorParent(), SWT.DROP_DOWN | SWT.BORDER); |
| 183 | + Combo combo = fElapsedFormat.getCombo(); |
| 184 | + combo.setToolTipText(DebugPreferencesMessages.ConsoleElapsedTimeToolTip); |
| 185 | + |
| 186 | + combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 187 | + |
| 188 | + fElapsedFormat.setContentProvider(ArrayContentProvider.getInstance()); |
| 189 | + fElapsedFormat.setInput(ELAPSED_FORMATS); |
| 190 | + combo.select(selectPreferredElapsedTime()); |
| 191 | + |
| 192 | + fElapsedFormat.addSelectionChangedListener(event -> { |
| 193 | + if (event.getSelection() instanceof IStructuredSelection selection) { |
| 194 | + String selectedFormat = selection.getFirstElement() != null ? selection.getFirstElement().toString() |
| 195 | + : null; |
| 196 | + if (selectedFormat != null) { |
| 197 | + fElapsedFormatPreviewLabel.setText("Preview : " + processElapsedTimeFormat(selectedFormat.trim())); //$NON-NLS-1$ |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + }); |
| 202 | + combo.addModifyListener(e -> { |
| 203 | + if (validateElapsedTimeFormat(combo.getText().trim())) { |
| 204 | + fElapsedFormatPreviewLabel.setText("Preview : " + processElapsedTimeFormat(combo.getText().trim())); //$NON-NLS-1$ |
| 205 | + } else { |
| 206 | + fElapsedFormatPreviewLabel.setText("Invalid format"); //$NON-NLS-1$ |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + SWTFactory.createLabel(getFieldEditorParent(), "", 1); //$NON-NLS-1$ |
| 211 | + fElapsedFormatPreviewLabel = SWTFactory.createLabel(getFieldEditorParent(), |
| 212 | + "Preview : " + processElapsedTimeFormat(combo.getText().trim()), 1); //$NON-NLS-1$ |
| 213 | + |
162 | 214 | ColorFieldEditor sysout= new ColorFieldEditor(IDebugPreferenceConstants.CONSOLE_SYS_OUT_COLOR, DebugPreferencesMessages.ConsolePreferencePage_Standard_Out__2, getFieldEditorParent()); |
163 | 215 | ColorFieldEditor syserr= new ColorFieldEditor(IDebugPreferenceConstants.CONSOLE_SYS_ERR_COLOR, DebugPreferencesMessages.ConsolePreferencePage_Standard_Error__3, getFieldEditorParent()); |
164 | 216 | ColorFieldEditor sysin= new ColorFieldEditor(IDebugPreferenceConstants.CONSOLE_SYS_IN_COLOR, DebugPreferencesMessages.ConsolePreferencePage_Standard_In__4, getFieldEditorParent()); |
@@ -197,6 +249,11 @@ public boolean performOk() { |
197 | 249 | int low = store.getInt(IDebugPreferenceConstants.CONSOLE_LOW_WATER_MARK); |
198 | 250 | int high = low + 8000; |
199 | 251 | store.setValue(IDebugPreferenceConstants.CONSOLE_HIGH_WATER_MARK, high); |
| 252 | + String elapsedTimeInput = fElapsedFormat.getCombo().getText().trim(); |
| 253 | + if (validateElapsedTimeFormat(elapsedTimeInput)) { |
| 254 | + store.setValue(IDebugPreferenceConstants.CONSOLE_ELAPSED_FORMAT, |
| 255 | + elapsedTimeInput); |
| 256 | + } |
200 | 257 | return ok; |
201 | 258 | } |
202 | 259 |
|
@@ -266,6 +323,7 @@ protected void performDefaults() { |
266 | 323 | updateWidthEditor(); |
267 | 324 | updateBufferSizeEditor(); |
268 | 325 | updateInterpretCrAsControlCharacterEditor(); |
| 326 | + updateElapsedTimePreferences(); |
269 | 327 | } |
270 | 328 |
|
271 | 329 | protected boolean canClearErrorMessage() { |
@@ -301,4 +359,46 @@ public void propertyChange(PropertyChangeEvent event) { |
301 | 359 | super.propertyChange(event); |
302 | 360 | } |
303 | 361 | } |
| 362 | + |
| 363 | + protected void updateElapsedTimePreferences() { |
| 364 | + fElapsedFormat.setInput(ELAPSED_FORMATS); |
| 365 | + fElapsedFormat.getCombo().select(0); |
| 366 | + } |
| 367 | + |
| 368 | + private int selectPreferredElapsedTime() { |
| 369 | + IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore(); |
| 370 | + String prefElapsed = store.getString(IDebugPreferenceConstants.CONSOLE_ELAPSED_FORMAT); |
| 371 | + int selectionIndex = Arrays.asList(ELAPSED_FORMATS).indexOf(prefElapsed); |
| 372 | + if (selectionIndex > -1) { |
| 373 | + return selectionIndex; |
| 374 | + } |
| 375 | + fElapsedFormat.getCombo().add(prefElapsed); |
| 376 | + return fElapsedFormat.getCombo().getItemCount() - 1; |
| 377 | + } |
| 378 | + |
| 379 | + private boolean validateElapsedTimeFormat(String format) { |
| 380 | + if (format.equals(DebugPreferencesMessages.ConsoleDisableElapsedTime)) { |
| 381 | + return true; |
| 382 | + } |
| 383 | + if (format.equals("")) { //$NON-NLS-1$ |
| 384 | + return false; |
| 385 | + } |
| 386 | + String matcherFormat = "^((H{1,2}:)?MM(:SS)?(\\.mmm)?|(HHh )?(MMm )?(SSs)?)$"; //$NON-NLS-1$ |
| 387 | + Pattern pattern = Pattern.compile(matcherFormat); |
| 388 | + if (pattern.matcher(format).matches()) { |
| 389 | + return true; |
| 390 | + } |
| 391 | + return false; |
| 392 | + } |
| 393 | + |
| 394 | + private String processElapsedTimeFormat(String format) { |
| 395 | + if (format.equals(DebugPreferencesMessages.ConsoleDisableElapsedTime)) { |
| 396 | + return "Not Available"; //$NON-NLS-1$ |
| 397 | + } |
| 398 | + String dateTimeFormated = ProcessConsole.convertElapsedFormat(format); |
| 399 | + Duration elapsedTime = Duration.ofHours(1).plusMinutes(2).plusSeconds(3).plusMillis(456); |
| 400 | + String elapsedString = String.format(dateTimeFormated, elapsedTime.toHours(), elapsedTime.toMinutesPart(), |
| 401 | + elapsedTime.toSecondsPart(), elapsedTime.toMillisPart()); |
| 402 | + return elapsedString; |
| 403 | + } |
304 | 404 | } |
0 commit comments