|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024, 2025 IBM Corporation 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 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.debug.internal.ui.actions; |
| 15 | + |
| 16 | +import org.eclipse.core.runtime.CoreException; |
| 17 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 18 | +import org.eclipse.core.runtime.IStatus; |
| 19 | +import org.eclipse.core.runtime.Status; |
| 20 | +import org.eclipse.core.runtime.jobs.Job; |
| 21 | +import org.eclipse.debug.core.DebugPlugin; |
| 22 | +import org.eclipse.debug.core.IBreakpointManager; |
| 23 | +import org.eclipse.debug.core.model.IBreakpoint; |
| 24 | +import org.eclipse.debug.internal.ui.DebugUIPlugin; |
| 25 | +import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants; |
| 26 | +import org.eclipse.jface.action.IAction; |
| 27 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 28 | +import org.eclipse.jface.dialogs.MessageDialogWithToggle; |
| 29 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 30 | +import org.eclipse.jface.viewers.ISelection; |
| 31 | +import org.eclipse.swt.widgets.Event; |
| 32 | +import org.eclipse.ui.IActionDelegate2; |
| 33 | +import org.eclipse.ui.IViewActionDelegate; |
| 34 | +import org.eclipse.ui.IViewPart; |
| 35 | +import org.eclipse.ui.IWorkbenchWindow; |
| 36 | +import org.eclipse.ui.IWorkbenchWindowActionDelegate; |
| 37 | + |
| 38 | +public abstract class AbstractAllBreakpointEnablement |
| 39 | + implements IViewActionDelegate, IActionDelegate2, IWorkbenchWindowActionDelegate { |
| 40 | + |
| 41 | + private IAction fAction; |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Needed for reflective creation |
| 46 | + */ |
| 47 | + |
| 48 | + @Override |
| 49 | + public void dispose() { |
| 50 | + fAction = null; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void init(IAction action) { |
| 55 | + fAction = action; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns this delegate's action. |
| 60 | + * |
| 61 | + * @return the underlying <code>IAction</code> |
| 62 | + */ |
| 63 | + protected IAction getAction() { |
| 64 | + return fAction; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void runWithEvent(IAction action, Event event) { |
| 69 | + run(action); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void init(IViewPart view) { |
| 74 | + initialize(); |
| 75 | + update(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void init(IWorkbenchWindow window) { |
| 80 | + initialize(); |
| 81 | + update(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Initializes any listeners, etc. |
| 86 | + */ |
| 87 | + protected abstract void initialize(); |
| 88 | + |
| 89 | + /** |
| 90 | + * Update enablement. |
| 91 | + */ |
| 92 | + protected void update() { |
| 93 | + IAction action = getAction(); |
| 94 | + if (action != null) { |
| 95 | + action.setEnabled(isEnabled()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void selectionChanged(IAction action, ISelection s) { |
| 101 | + // do nothing |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void run(IAction action) { |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + protected abstract boolean isEnabled(); |
| 110 | + |
| 111 | + /** |
| 112 | + * Schedules the enablement operation for breakpoints |
| 113 | + * |
| 114 | + * @param jobName Name of the job |
| 115 | + * @param enablementStatus <code>true</code> for enabling all breakpoints; |
| 116 | + * <code>false</code> for disabling all breakpoints; |
| 117 | + */ |
| 118 | + protected void scheduleEnablement(String jobName, boolean enablementStatus) { |
| 119 | + final IBreakpoint[] breakpoints = getBreakpoints(); |
| 120 | + new Job(jobName) { |
| 121 | + @Override |
| 122 | + protected IStatus run(IProgressMonitor monitor) { |
| 123 | + try { |
| 124 | + for (IBreakpoint breakpoint : breakpoints) { |
| 125 | + breakpoint.setEnabled(enablementStatus); |
| 126 | + } |
| 127 | + } catch (CoreException e) { |
| 128 | + DebugUIPlugin.log(e); |
| 129 | + return Status.CANCEL_STATUS; |
| 130 | + } |
| 131 | + return Status.OK_STATUS; |
| 132 | + } |
| 133 | + }.schedule(); |
| 134 | + } |
| 135 | + protected IBreakpoint[] getBreakpoints() { |
| 136 | + IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager(); |
| 137 | + return breakpointManager.getBreakpoints(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Update the enablement status of every breakpoints |
| 142 | + * |
| 143 | + * @param enablementStatus <code>true</code> for enabling all breakpoints; |
| 144 | + * <code>false</code> for disabling all breakpoints; |
| 145 | + */ |
| 146 | + protected void updateBreakpoints(boolean enablementStatus) { |
| 147 | + IBreakpoint[] breakpoints = getBreakpoints(); |
| 148 | + if (breakpoints.length < 1) { |
| 149 | + return; |
| 150 | + } |
| 151 | + IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow(); |
| 152 | + if (window == null) { |
| 153 | + return; |
| 154 | + } |
| 155 | + IPreferenceStore store = DebugUIPlugin.getDefault().getPreferenceStore(); |
| 156 | + boolean prompt = store |
| 157 | + .getBoolean(enablementStatus ? IDebugPreferenceConstants.PREF_PROMPT_ENABLE_ALL_BREAKPOINTS |
| 158 | + : IDebugPreferenceConstants.PREF_PROMPT_DISABLE_ALL_BREAKPOINTS); |
| 159 | + boolean proceed = true; |
| 160 | + if (prompt) { |
| 161 | + MessageDialogWithToggle mdwt = MessageDialogWithToggle.openYesNoQuestion(window.getShell(), |
| 162 | + enablementStatus ? ActionMessages.EnableAllBreakpointsAction_0 |
| 163 | + : ActionMessages.DisableAllBreakPointsAction_0, |
| 164 | + enablementStatus ? ActionMessages.EnableAllBreakpointsAction_1 |
| 165 | + : ActionMessages.DisableAllBreakPointsAction_1, |
| 166 | + enablementStatus ? ActionMessages.EnableAllBreakpointsAction_3 |
| 167 | + : ActionMessages.DisableAllBreakPointsAction_2, |
| 168 | + !prompt, null, null); |
| 169 | + |
| 170 | + if (mdwt.getReturnCode() != IDialogConstants.YES_ID) { |
| 171 | + proceed = false; |
| 172 | + } else { |
| 173 | + store.setValue( |
| 174 | + enablementStatus ? IDebugPreferenceConstants.PREF_PROMPT_ENABLE_ALL_BREAKPOINTS |
| 175 | + : IDebugPreferenceConstants.PREF_PROMPT_DISABLE_ALL_BREAKPOINTS, |
| 176 | + !mdwt.getToggleState()); |
| 177 | + } |
| 178 | + } |
| 179 | + if (proceed) { |
| 180 | + if (enablementStatus) { |
| 181 | + scheduleEnablement(ActionMessages.EnableAllBreakpointsAction_0, true); |
| 182 | + } else { |
| 183 | + scheduleEnablement(ActionMessages.DisableAllBreakPointsAction_0, false); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments