From 39e042242d94a67ca0b74182c13e00acf28c86df Mon Sep 17 00:00:00 2001 From: John Cortell Date: Thu, 5 Apr 2007 14:08:48 +0000 Subject: [PATCH] Reviewed and applied patch for 180673. "Remove all Global Variables" action in the Variables view was enabled even when there were no global variables in the view. --- .../RemoveAllGlobalsActionDelegate.java | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/RemoveAllGlobalsActionDelegate.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/RemoveAllGlobalsActionDelegate.java index 233c1146393..b4aa4fe7410 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/RemoveAllGlobalsActionDelegate.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/RemoveAllGlobalsActionDelegate.java @@ -11,11 +11,16 @@ package org.eclipse.cdt.debug.internal.ui.actions; import org.eclipse.cdt.debug.core.ICGlobalVariableManager; +import org.eclipse.cdt.debug.core.model.IGlobalVariableDescriptor; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.debug.core.DebugEvent; import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.IDebugEventSetListener; +import org.eclipse.debug.core.model.IDebugTarget; import org.eclipse.debug.core.model.IDebugElement; import org.eclipse.debug.ui.DebugUITools; import org.eclipse.jface.action.IAction; +import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IViewActionDelegate; import org.eclipse.ui.IViewPart; import org.eclipse.ui.actions.ActionDelegate; @@ -23,12 +28,41 @@ import org.eclipse.ui.actions.ActionDelegate; /** * A delegate for the "Remove All Globals" action. */ -public class RemoveAllGlobalsActionDelegate extends ActionDelegate implements IViewActionDelegate { +public class RemoveAllGlobalsActionDelegate extends ActionDelegate implements IViewActionDelegate, IDebugEventSetListener { + + private IAction fAction; /* (non-Javadoc) * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart) */ public void init( IViewPart view ) { + DebugPlugin.getDefault().addDebugEventListener(this); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.actions.ActionDelegate#init(org.eclipse.jface.action.IAction) + */ + public void init( IAction action ) { + DebugPlugin.getDefault().addDebugEventListener(this); + fAction = action; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) + */ + public void selectionChanged(IAction action, ISelection selection) { + update(); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.actions.ActionDelegate#dispose() + */ + public void dispose() { + DebugPlugin.getDefault().removeDebugEventListener(this); + fAction = null; } /* (non-Javadoc) @@ -48,4 +82,49 @@ public class RemoveAllGlobalsActionDelegate extends ActionDelegate implements IV } } } + + /** + * Enables/disables the action based on whether there are any globals in the + * variables view. + */ + private void update() { + final IAction action = fAction; + if (action != null) { + final IAdaptable context = DebugUITools.getDebugContext(); + boolean enabled = false; + if (context instanceof IDebugElement) { + final ICGlobalVariableManager gvm = (ICGlobalVariableManager) ((IDebugElement) context) + .getDebugTarget().getAdapter( + ICGlobalVariableManager.class); + if (gvm != null) { + final IGlobalVariableDescriptor[] globals = gvm + .getDescriptors(); + enabled = globals != null && globals.length > 0; + } + } + action.setEnabled(enabled); + } + } + + /* + * (non-Javadoc) + * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[]) + */ + public void handleDebugEvents( DebugEvent[] events ) { + // The ICGlobalVariableManager will fire a target content-changed + // event when a global is added or removed. Update the enable/disable + // state of this action accordingly + + if (fAction != null) { + for (int i = 0; i < events.length; i++) { + final DebugEvent event = events[i]; + if (event.getSource() instanceof IDebugTarget + && event.getKind() == DebugEvent.CHANGE + && event.getDetail() == DebugEvent.CONTENT ) { + update(); + break; + } + } + } + } }