|
| 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.expressions; |
| 15 | + |
| 16 | +import java.util.Iterator; |
| 17 | + |
| 18 | +import org.eclipse.core.runtime.IAdaptable; |
| 19 | +import org.eclipse.debug.core.DebugEvent; |
| 20 | +import org.eclipse.debug.core.DebugPlugin; |
| 21 | +import org.eclipse.debug.core.IDebugEventSetListener; |
| 22 | +import org.eclipse.debug.core.ILaunch; |
| 23 | +import org.eclipse.debug.core.model.IDebugElement; |
| 24 | +import org.eclipse.debug.core.model.IExpression; |
| 25 | +import org.eclipse.debug.core.model.IStackFrame; |
| 26 | +import org.eclipse.debug.core.model.IThread; |
| 27 | +import org.eclipse.debug.core.model.IWatchExpression; |
| 28 | +import org.eclipse.debug.internal.ui.DebugUIPlugin; |
| 29 | +import org.eclipse.debug.internal.ui.actions.ActionMessages; |
| 30 | +import org.eclipse.debug.ui.DebugUITools; |
| 31 | +import org.eclipse.jface.action.IAction; |
| 32 | +import org.eclipse.jface.viewers.ISelection; |
| 33 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 34 | +import org.eclipse.swt.widgets.Event; |
| 35 | +import org.eclipse.ui.IActionDelegate2; |
| 36 | +import org.eclipse.ui.IViewActionDelegate; |
| 37 | +import org.eclipse.ui.IViewPart; |
| 38 | +import org.eclipse.ui.IWorkbenchPage; |
| 39 | + |
| 40 | +/* |
| 41 | + * Associates the current stack frame with this expression, allowing its evaluation |
| 42 | + * result to remain accessible across different debug contexts. |
| 43 | + */ |
| 44 | +public class PinWatchContextAction implements IViewActionDelegate, IDebugEventSetListener, IActionDelegate2 { |
| 45 | + |
| 46 | + /** |
| 47 | + * Finds the currently selected context in the UI. |
| 48 | + * |
| 49 | + * @return the current debug context |
| 50 | + */ |
| 51 | + protected IDebugElement getContext() { |
| 52 | + IAdaptable object = DebugUITools.getDebugContext(); |
| 53 | + IDebugElement context = null; |
| 54 | + if (object instanceof IDebugElement iDebugElement) { |
| 55 | + context = iDebugElement; |
| 56 | + } else if (object instanceof ILaunch iLaunch) { |
| 57 | + context = iLaunch.getDebugTarget(); |
| 58 | + } |
| 59 | + return context; |
| 60 | + } |
| 61 | + |
| 62 | + protected IStructuredSelection getCurrentSelection() { |
| 63 | + IWorkbenchPage page = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage(); |
| 64 | + if (page != null) { |
| 65 | + ISelection selection = page.getSelection(); |
| 66 | + if (selection instanceof IStructuredSelection sel) { |
| 67 | + return sel; |
| 68 | + } |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
| 75 | + */ |
| 76 | + @Override |
| 77 | + public void run(IAction action) { |
| 78 | + IDebugElement context = getContext(); |
| 79 | + for (Iterator<?> iter = getCurrentSelection().iterator(); iter.hasNext();) { |
| 80 | + if (iter.next() instanceof IWatchExpression expression) { |
| 81 | + if (expression.getPinnedContext() != null) { |
| 82 | + expression.removePinnedContext(); |
| 83 | + expression.setExpressionContext(context); |
| 84 | + action.setText(ActionMessages.ExpressionsPinContext); |
| 85 | + } else { |
| 86 | + expression.setPinnedContext(context); |
| 87 | + action.setText(ActionMessages.ExpressionsRemovePin); |
| 88 | + } |
| 89 | + if (expression.isEnabled()) { |
| 90 | + expression.evaluate(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void selectionChanged(IAction action, ISelection selection) { |
| 98 | + |
| 99 | + IDebugElement debugElement = getContext(); |
| 100 | + if (debugElement == null) { |
| 101 | + action.setEnabled(false); |
| 102 | + return; |
| 103 | + } else { |
| 104 | + action.setEnabled(true); |
| 105 | + } |
| 106 | + if (getCurrentSelection() == null) { |
| 107 | + return; |
| 108 | + } |
| 109 | + for (Object select : getCurrentSelection()) { |
| 110 | + if (select instanceof IWatchExpression expression) { |
| 111 | + if (expression.getPinnedContext() != null) { |
| 112 | + action.setText(ActionMessages.ExpressionsRemovePin); |
| 113 | + } else { |
| 114 | + action.setText(ActionMessages.ExpressionsPinContext); |
| 115 | + } |
| 116 | + } else { |
| 117 | + action.setEnabled(false); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void handleDebugEvents(DebugEvent[] events) { |
| 126 | + for (DebugEvent event : events) { |
| 127 | + if (event.getSource() instanceof IThread thread && thread.isTerminated()) { |
| 128 | + for (IExpression exp : DebugPlugin.getDefault().getExpressionManager().getExpressions()) { |
| 129 | + if (exp instanceof IWatchExpression expression && expression.getPinnedContext() != null) { |
| 130 | + if (expression.getPinnedContext() instanceof IStackFrame frame && frame.isTerminated()) { |
| 131 | + expression.removePinnedContext(); |
| 132 | + expression.setExpressionContext(getContext()); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void init(IViewPart view) { |
| 144 | + DebugPlugin.getDefault().addDebugEventListener(this); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void init(IAction action) { |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void dispose() { |
| 154 | + DebugPlugin.getDefault().removeDebugEventListener(this); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void runWithEvent(IAction action, Event event) { |
| 159 | + run(action); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments