|
| 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.debug.internal.ui.viewers.model.provisional.TreeModelViewer; |
| 21 | +import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView; |
| 22 | +import org.eclipse.debug.ui.IDebugUIConstants; |
| 23 | +import org.eclipse.jface.action.IAction; |
| 24 | +import org.eclipse.jface.viewers.ISelection; |
| 25 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 26 | +import org.eclipse.jface.viewers.TreeSelection; |
| 27 | +import org.eclipse.swt.SWT; |
| 28 | +import org.eclipse.swt.events.KeyAdapter; |
| 29 | +import org.eclipse.swt.events.KeyEvent; |
| 30 | +import org.eclipse.swt.graphics.Font; |
| 31 | +import org.eclipse.swt.graphics.GC; |
| 32 | +import org.eclipse.swt.graphics.Point; |
| 33 | +import org.eclipse.swt.graphics.Rectangle; |
| 34 | +import org.eclipse.swt.widgets.Label; |
| 35 | +import org.eclipse.swt.widgets.Text; |
| 36 | +import org.eclipse.swt.widgets.TreeItem; |
| 37 | +import org.eclipse.swt.widgets.Widget; |
| 38 | +import org.eclipse.ui.IViewActionDelegate; |
| 39 | +import org.eclipse.ui.IViewPart; |
| 40 | +import org.eclipse.ui.IWorkbenchPage; |
| 41 | +import org.eclipse.ui.PlatformUI; |
| 42 | + |
| 43 | +public class BreakpointLabelAction implements IViewActionDelegate { |
| 44 | + |
| 45 | + private IViewPart fView; |
| 46 | + protected IViewPart getView() { |
| 47 | + return fView; |
| 48 | + } |
| 49 | + |
| 50 | + protected void setView(IViewPart view) { |
| 51 | + fView = view; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void run(IAction action) { |
| 56 | + String emptyString = ""; //$NON-NLS-1$ |
| 57 | + IStructuredSelection selection = getSelection(); |
| 58 | + |
| 59 | + if (selection instanceof TreeSelection treeSelect && selection.getFirstElement() instanceof Breakpoint breakpoint) { |
| 60 | + if (treeSelect.size() == 1) { |
| 61 | + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 62 | + IViewPart viewPart = page.findView(IDebugUIConstants.ID_BREAKPOINT_VIEW); |
| 63 | + if (viewPart instanceof BreakpointsView breakpointView) { |
| 64 | + TreeModelViewer treeViewer = breakpointView.getTreeModelViewer(); |
| 65 | + Widget item = treeViewer.findItem(treeSelect.getPaths()[0]); |
| 66 | + if (item instanceof TreeItem tree) { |
| 67 | + String current = tree.getText(); |
| 68 | + Rectangle bounds; |
| 69 | + try { |
| 70 | + bounds = tree.getBounds(); |
| 71 | + } catch (ArrayIndexOutOfBoundsException e) { // TreeItem having FontData [Breakpoints having |
| 72 | + // custom label] |
| 73 | + tree.setFont(null); |
| 74 | + GC gc = new GC(tree.getParent()); |
| 75 | + Font currentFont = gc.getFont(); |
| 76 | + gc.setFont(currentFont); |
| 77 | + Point textWidth = gc.textExtent(tree.getText()); |
| 78 | + gc.dispose(); |
| 79 | + bounds = tree.getBounds(0); |
| 80 | + bounds.x = bounds.x + 10; |
| 81 | + bounds.width = textWidth.x + 20; |
| 82 | + |
| 83 | + } |
| 84 | + Label label = new Label(tree.getParent(), SWT.WRAP); |
| 85 | + label.setText(ActionMessages.BreakpointLabelDialog); |
| 86 | + label.setBounds(bounds.x, bounds.y - 20, label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, |
| 87 | + label.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); |
| 88 | + |
| 89 | + Text inlineEditor = new Text(tree.getParent(), SWT.BORDER); |
| 90 | + inlineEditor.setBounds(bounds.x, bounds.y, bounds.width, bounds.height); |
| 91 | + inlineEditor.setText(current); |
| 92 | + inlineEditor.setFocus(); |
| 93 | + |
| 94 | + inlineEditor.addListener(SWT.FocusOut, event -> { |
| 95 | + tree.setText(current); |
| 96 | + label.dispose(); |
| 97 | + inlineEditor.dispose(); |
| 98 | + |
| 99 | + }); |
| 100 | + inlineEditor.addKeyListener(new KeyAdapter() { |
| 101 | + @Override |
| 102 | + public void keyPressed(KeyEvent e) { |
| 103 | + if (e.keyCode == SWT.ESC) { |
| 104 | + tree.setText(current); |
| 105 | + inlineEditor.dispose(); |
| 106 | + label.dispose(); |
| 107 | + } else if (e.keyCode == SWT.CR) { |
| 108 | + String newLabel = inlineEditor.getText(); |
| 109 | + if (!newLabel.isEmpty() && !newLabel.equals(current)) { |
| 110 | + try { |
| 111 | + breakpoint.setBreakpointLabel(newLabel); |
| 112 | + } catch (CoreException e1) { |
| 113 | + DebugUIPlugin.log(e1); |
| 114 | + } |
| 115 | + } else if (newLabel.isEmpty()) { |
| 116 | + try { |
| 117 | + breakpoint.setBreakpointLabel(null); // Set to default |
| 118 | + } catch (CoreException e2) { |
| 119 | + DebugUIPlugin.log(e2); |
| 120 | + } |
| 121 | + } |
| 122 | + inlineEditor.dispose(); |
| 123 | + label.dispose(); |
| 124 | + } |
| 125 | + } |
| 126 | + }); |
| 127 | + tree.setText(emptyString); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + protected IStructuredSelection getSelection() { |
| 138 | + return (IStructuredSelection) getView().getViewSite().getSelectionProvider().getSelection(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void selectionChanged(IAction action, ISelection selection) { |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void init(IViewPart view) { |
| 147 | + setView(view); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments