package org.eclipse.debug.internal.ui.actions; | |
/* | |
* (c) Copyright IBM Corp. 2000, 2001. | |
* All Rights Reserved. | |
*/ | |
import org.eclipse.debug.core.DebugEvent; | |
import org.eclipse.debug.core.DebugPlugin; | |
import org.eclipse.debug.core.ILaunch; | |
import org.eclipse.debug.core.ILaunchListener; | |
import org.eclipse.debug.core.ILaunchManager; | |
import org.eclipse.debug.core.model.IDebugTarget; | |
import org.eclipse.debug.core.model.IProcess; | |
import org.eclipse.debug.ui.IDebugView; | |
import org.eclipse.jface.viewers.IStructuredContentProvider; | |
import org.eclipse.jface.viewers.StructuredViewer; | |
import org.eclipse.jface.viewers.Viewer; | |
import org.eclipse.ui.IViewPart; | |
/** | |
* Removes all terminated/detached launches from the | |
* active debug view. | |
*/ | |
public class RemoveAllTerminatedAction extends AbstractRemoveAllActionDelegate implements ILaunchListener { | |
/** | |
* @see ListenerActionDelegate#doHandleDebugEvent(DebugEvent) | |
*/ | |
protected void doHandleDebugEvent(DebugEvent event) { | |
if (event.getKind() == DebugEvent.TERMINATE) { | |
Object source= event.getSource(); | |
if (source instanceof IDebugTarget) { | |
ILaunch launch= ((IDebugTarget)source).getLaunch(); | |
if (launch.isTerminated() && launchIsRegistered(launch)) { | |
getAction().setEnabled(true); | |
} | |
} else if (source instanceof IProcess) { | |
ILaunch launch= ((IProcess)source).getLaunch(); | |
if (launch.isTerminated() && launchIsRegistered(launch)) { | |
getAction().setEnabled(true); | |
} | |
} | |
} | |
} | |
private boolean launchIsRegistered(ILaunch iLaunch) { | |
ILaunch[] launches= DebugPlugin.getDefault().getLaunchManager().getLaunches(); | |
for (int i = 0; i < launches.length; i++) { | |
ILaunch launch = launches[i]; | |
if (launch.equals(iLaunch)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Updates the enabled state of this action to enabled if at | |
* least one launch is terminated and relative to the current perspective. | |
*/ | |
protected void update() { | |
Object[] elements = getElements(); | |
if (elements != null) { | |
for (int i= 0; i < elements.length; i++) { | |
if (elements[i] instanceof ILaunch) { | |
ILaunch launch= (ILaunch)elements[i]; | |
if (launch.isTerminated()) { | |
getAction().setEnabled(true); | |
return; | |
} | |
} | |
} | |
} | |
getAction().setEnabled(false); | |
} | |
/** | |
* Returns the top level elements in the active debug | |
* view, or <code>null</code> if none. | |
* | |
* @return array of object | |
*/ | |
protected Object[] getElements() { | |
IDebugView view = (IDebugView)getView().getAdapter(IDebugView.class); | |
if (view != null) { | |
Viewer viewer = view.getViewer(); | |
if (viewer instanceof StructuredViewer) { | |
IStructuredContentProvider cp = (IStructuredContentProvider)((StructuredViewer)viewer).getContentProvider(); | |
Object input = viewer.getInput(); | |
return cp.getElements(input); | |
} | |
} | |
return null; | |
} | |
protected void doAction() { | |
Object[] elements = getElements(); | |
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); | |
for (int i = 0; i < elements.length; i++) { | |
if (elements[i] instanceof ILaunch) { | |
ILaunch launch = (ILaunch)elements[i]; | |
if (launch.isTerminated()) { | |
manager.removeLaunch(launch); | |
} | |
} | |
} | |
} | |
/** | |
* @see IViewActionDelegate#init(IViewPart) | |
*/ | |
public void init(IViewPart view) { | |
super.init(view); | |
DebugPlugin.getDefault().getLaunchManager().addLaunchListener(this); | |
DebugPlugin.getDefault().addDebugEventListener(this); | |
} | |
/** | |
* @see IWorkbenchWindowActionDelegate#dispose() | |
*/ | |
public void dispose() { | |
//removes as a debug event listener | |
super.dispose(); | |
DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this); | |
} | |
/** | |
* @see ILaunchListener#launchAdded(ILaunch) | |
*/ | |
public void launchAdded(ILaunch launch) { | |
} | |
/** | |
* @see ILaunchListener#launchChanged(ILaunch) | |
*/ | |
public void launchChanged(ILaunch launch) { | |
} | |
/** | |
* @see ILaunchListener#launchRemoved(ILaunch) | |
*/ | |
public void launchRemoved(ILaunch launch) { | |
if (getAction().isEnabled()) { | |
update(); | |
} | |
} | |
} | |