|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2023 Zsombor Gegesy and others. |
| 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 | + * Zsombor Gegesy - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.internal.debug.ui.actions; |
| 15 | + |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +import org.eclipse.debug.core.DebugException; |
| 19 | +import org.eclipse.jdt.debug.core.IJavaStackFrame; |
| 20 | +import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
| 21 | +import org.eclipse.jdt.internal.debug.ui.JavaStackFramePreferencePage; |
| 22 | +import org.eclipse.jdt.internal.ui.filtertable.Filter; |
| 23 | +import org.eclipse.jdt.internal.ui.filtertable.FilterManager; |
| 24 | +import org.eclipse.jface.action.IAction; |
| 25 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 26 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 27 | + |
| 28 | +/** |
| 29 | + * Add the current stack frame to the highlighted frames. |
| 30 | + * |
| 31 | + */ |
| 32 | +public class HighlightStackFrameAction extends ObjectActionDelegate { |
| 33 | + |
| 34 | + @Override |
| 35 | + public void run(IAction action) { |
| 36 | + // Make sure there is a current selection |
| 37 | + IStructuredSelection selection = getCurrentSelection(); |
| 38 | + if (selection == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + final var preferenceStore = JDIDebugUIPlugin.getDefault().getPreferenceStore(); |
| 43 | + final var filterManager = JavaStackFramePreferencePage.CUSTOM_STACK_FRAMES; |
| 44 | + for (Object selected : selection) { |
| 45 | + if (selected instanceof IJavaStackFrame) { |
| 46 | + IJavaStackFrame frame = (IJavaStackFrame) selected; |
| 47 | + try { |
| 48 | + addFilter(filterManager, preferenceStore, frame.getDeclaringTypeName()); |
| 49 | + // Reset all the stack frames, to be evaluated on the next step again. |
| 50 | + var thread = frame.getThread(); |
| 51 | + var frames = thread.getStackFrames(); |
| 52 | + for (var oneFrame : frames) { |
| 53 | + if (oneFrame instanceof IJavaStackFrame) { |
| 54 | + ((IJavaStackFrame) oneFrame).setCategory(null); |
| 55 | + } |
| 56 | + } |
| 57 | + } catch (DebugException e) { |
| 58 | + JDIDebugUIPlugin.log(e); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + private void addFilter(FilterManager filterManager, IPreferenceStore preferenceStore, String filterName) { |
| 66 | + var allStoredFilters = filterManager.getAllStoredFilters(preferenceStore, false); |
| 67 | + if (enableFilter(allStoredFilters, filterName)) { |
| 68 | + filterManager.save(preferenceStore, allStoredFilters); |
| 69 | + } else { |
| 70 | + var plusOne = new Filter[allStoredFilters.length + 1]; |
| 71 | + plusOne[0] = new Filter(filterName, true); |
| 72 | + System.arraycopy(allStoredFilters, 0, plusOne, 1, allStoredFilters.length); |
| 73 | + filterManager.save(preferenceStore, plusOne); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private boolean enableFilter(Filter[] allFilter, String filterName) { |
| 78 | + for (Filter filter : allFilter) { |
| 79 | + if (Objects.equals(filterName, filter.getName())) { |
| 80 | + filter.setChecked(true); |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | +} |
0 commit comments