|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 IBM Corporation |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.debug.internal.ui.actions.breakpoints; |
| 15 | + |
| 16 | +import org.eclipse.core.runtime.CoreException; |
| 17 | +import org.eclipse.debug.core.model.Breakpoint; |
| 18 | +import org.eclipse.debug.internal.ui.DebugUIPlugin; |
| 19 | +import org.eclipse.debug.internal.ui.actions.ActionMessages; |
| 20 | +import org.eclipse.jface.action.IAction; |
| 21 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 22 | +import org.eclipse.jface.dialogs.IInputValidator; |
| 23 | +import org.eclipse.jface.dialogs.InputDialog; |
| 24 | +import org.eclipse.jface.viewers.ISelection; |
| 25 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 26 | +import org.eclipse.jface.window.Window; |
| 27 | +import org.eclipse.swt.events.SelectionAdapter; |
| 28 | +import org.eclipse.swt.events.SelectionEvent; |
| 29 | +import org.eclipse.swt.widgets.Button; |
| 30 | +import org.eclipse.swt.widgets.Composite; |
| 31 | +import org.eclipse.swt.widgets.Shell; |
| 32 | +import org.eclipse.ui.IViewActionDelegate; |
| 33 | +import org.eclipse.ui.IViewPart; |
| 34 | + |
| 35 | +public class BreakpointLabelAction implements IViewActionDelegate { |
| 36 | + |
| 37 | + private IViewPart fView; |
| 38 | + |
| 39 | + protected IViewPart getView() { |
| 40 | + return fView; |
| 41 | + } |
| 42 | + |
| 43 | + protected void setView(IViewPart view) { |
| 44 | + fView = view; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void run(IAction action) { |
| 49 | + IStructuredSelection selection = getSelection(); |
| 50 | + if (selection.getFirstElement() instanceof Breakpoint bp) { |
| 51 | + String label = askForLabel(bp); |
| 52 | + if (!label.isEmpty()) { |
| 53 | + try { |
| 54 | + bp.setBreakpointLabel(label); |
| 55 | + } catch (CoreException e) { |
| 56 | + DebugUIPlugin.log(e); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + protected IStructuredSelection getSelection() { |
| 63 | + return (IStructuredSelection) getView().getViewSite().getSelectionProvider().getSelection(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void selectionChanged(IAction action, ISelection selection) { |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void init(IViewPart view) { |
| 72 | + setView(view); |
| 73 | + } |
| 74 | + |
| 75 | + private String askForLabel(Breakpoint breakpoint) { |
| 76 | + String current = breakpoint.getBreakpointLabel(); |
| 77 | + class CustomInputDialog extends InputDialog { |
| 78 | + public CustomInputDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, |
| 79 | + IInputValidator validator) { |
| 80 | + super(parentShell, dialogTitle, dialogMessage, initialValue, validator); |
| 81 | + } |
| 82 | + @Override |
| 83 | + protected void createButtonsForButtonBar(Composite parent) { |
| 84 | + super.createButtonsForButtonBar(parent); |
| 85 | + Button removeButton = createButton(parent, IDialogConstants.CANCEL_ID, |
| 86 | + ActionMessages.BreakpointLabelRemove, false); |
| 87 | + boolean buttonEnable = false; |
| 88 | + if (!breakpoint.getBreakpointLabel().isEmpty()) { |
| 89 | + buttonEnable = true; |
| 90 | + } |
| 91 | + removeButton.setEnabled(buttonEnable); |
| 92 | + removeButton.addSelectionListener(new SelectionAdapter() { |
| 93 | + @Override |
| 94 | + public void widgetSelected(SelectionEvent e) { |
| 95 | + try { |
| 96 | + breakpoint.setBreakpointLabel("");//$NON-NLS-1$ |
| 97 | + buttonPressed(IDialogConstants.CANCEL_ID); |
| 98 | + } catch (CoreException e1) { |
| 99 | + DebugUIPlugin.log(e1); |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + IInputValidator validator = value -> { |
| 106 | + if (value.isBlank() || value.isEmpty()) { |
| 107 | + return ActionMessages.BreakpointLabelValid; |
| 108 | + } |
| 109 | + return null; |
| 110 | + }; |
| 111 | + CustomInputDialog dialog = new CustomInputDialog(fView.getSite().getShell(), |
| 112 | + ActionMessages.BreakpointLabelTitle, ActionMessages.BreakpointLabelDialog, |
| 113 | + current, validator); |
| 114 | + |
| 115 | + if (dialog.open() != Window.OK) { |
| 116 | + return ""; //$NON-NLS-1$ |
| 117 | + } |
| 118 | + String data = dialog.getValue(); |
| 119 | + return data.trim(); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments