|
| 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.Dialog; |
| 22 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 23 | +import org.eclipse.jface.viewers.ISelection; |
| 24 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 25 | +import org.eclipse.jface.window.Window; |
| 26 | +import org.eclipse.swt.SWT; |
| 27 | +import org.eclipse.swt.events.SelectionAdapter; |
| 28 | +import org.eclipse.swt.events.SelectionEvent; |
| 29 | +import org.eclipse.swt.layout.GridData; |
| 30 | +import org.eclipse.swt.widgets.Button; |
| 31 | +import org.eclipse.swt.widgets.Composite; |
| 32 | +import org.eclipse.swt.widgets.Control; |
| 33 | +import org.eclipse.swt.widgets.Label; |
| 34 | +import org.eclipse.swt.widgets.Shell; |
| 35 | +import org.eclipse.swt.widgets.Text; |
| 36 | +import org.eclipse.ui.IViewActionDelegate; |
| 37 | +import org.eclipse.ui.IViewPart; |
| 38 | + |
| 39 | +public class BreakpointLabelAction implements IViewActionDelegate { |
| 40 | + |
| 41 | + private IViewPart fView; |
| 42 | + |
| 43 | + protected IViewPart getView() { |
| 44 | + return fView; |
| 45 | + } |
| 46 | + |
| 47 | + protected void setView(IViewPart view) { |
| 48 | + fView = view; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void run(IAction action) { |
| 53 | + IStructuredSelection selection = getSelection(); |
| 54 | + if (selection.getFirstElement() instanceof Breakpoint bp) { |
| 55 | + String label = askForLabel(bp); |
| 56 | + if (label == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + if (!label.isEmpty()) { |
| 60 | + try { |
| 61 | + bp.setBreakpointLabel(label); |
| 62 | + } catch (CoreException e) { |
| 63 | + DebugUIPlugin.log(e); |
| 64 | + } |
| 65 | + } else { |
| 66 | + try { |
| 67 | + bp.setBreakpointLabel(""); //$NON-NLS-1$ |
| 68 | + } catch (CoreException e) { |
| 69 | + DebugUIPlugin.log(e); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + protected IStructuredSelection getSelection() { |
| 76 | + return (IStructuredSelection) getView().getViewSite().getSelectionProvider().getSelection(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void selectionChanged(IAction action, ISelection selection) { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void init(IViewPart view) { |
| 85 | + setView(view); |
| 86 | + } |
| 87 | + |
| 88 | + private String askForLabel(Breakpoint breakpoint) { |
| 89 | + String current = breakpoint.getBreakpointLabel(); |
| 90 | + |
| 91 | + class BreakpointLabelBox extends Dialog { |
| 92 | + private Text textInput; |
| 93 | + private String value; |
| 94 | + private Button clearButton; |
| 95 | + private String initialLabel; |
| 96 | + |
| 97 | + protected BreakpointLabelBox(Shell parentShell, String intialValue) { |
| 98 | + super(parentShell); |
| 99 | + initialLabel = intialValue; |
| 100 | + } |
| 101 | + @Override |
| 102 | + protected Control createDialogArea(Composite parent) { |
| 103 | + Composite composite = (Composite) super.createDialogArea(parent); |
| 104 | + Label label = new Label(composite, SWT.WRAP); |
| 105 | + label.setText(ActionMessages.BreakpointLabelDialog); |
| 106 | + GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL |
| 107 | + | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER); |
| 108 | + data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); |
| 109 | + label.setLayoutData(data); |
| 110 | + label.setFont(parent.getFont()); |
| 111 | + textInput = new Text(composite, SWT.SINGLE | SWT.BORDER); |
| 112 | + textInput.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); |
| 113 | + textInput.setText(initialLabel); |
| 114 | + applyDialogFont(composite); |
| 115 | + return composite; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected void configureShell(Shell newShell) { |
| 120 | + super.configureShell(newShell); |
| 121 | + newShell.setText(ActionMessages.BreakpointLabelTitle); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + protected void createButtonsForButtonBar(Composite parent) { |
| 127 | + super.createButtonsForButtonBar(parent); |
| 128 | + clearButton = createButton(parent, IDialogConstants.BACK_ID, ActionMessages.BreakpointLabelClear, |
| 129 | + false); |
| 130 | + clearButton.addSelectionListener(new SelectionAdapter() { |
| 131 | + @Override |
| 132 | + public void widgetSelected(SelectionEvent e) { |
| 133 | + textInput.setText(""); //$NON-NLS-1$ |
| 134 | + |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected void buttonPressed(int buttonId) { |
| 141 | + if (buttonId == IDialogConstants.OK_ID) { |
| 142 | + value = textInput.getText(); |
| 143 | + } else { |
| 144 | + value = null; |
| 145 | + } |
| 146 | + super.buttonPressed(buttonId); |
| 147 | + } |
| 148 | + private String getInput() { |
| 149 | + return value; |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + BreakpointLabelBox dialog = new BreakpointLabelBox(fView.getSite().getShell(), current); |
| 154 | + |
| 155 | + if (dialog.open() != Window.OK) { |
| 156 | + return null; |
| 157 | + } |
| 158 | + String data = dialog.getInput(); |
| 159 | + return data.trim(); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments