|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006, 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Wind River Systems - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.debug.ui.actions; |
| 13 |
|
| 14 |
import java.util.Iterator; |
| 15 |
import java.util.Map; |
| 16 |
import java.util.WeakHashMap; |
| 17 |
|
| 18 |
import org.eclipse.core.commands.AbstractHandler; |
| 19 |
import org.eclipse.core.commands.ExecutionEvent; |
| 20 |
import org.eclipse.core.commands.ExecutionException; |
| 21 |
import org.eclipse.core.commands.HandlerEvent; |
| 22 |
import org.eclipse.core.expressions.IEvaluationContext; |
| 23 |
import org.eclipse.debug.internal.ui.commands.actions.DebugCommandService; |
| 24 |
import org.eclipse.debug.internal.ui.commands.actions.IEnabledTarget; |
| 25 |
import org.eclipse.debug.ui.DebugUITools; |
| 26 |
import org.eclipse.debug.ui.contexts.DebugContextEvent; |
| 27 |
import org.eclipse.debug.ui.contexts.IDebugContextListener; |
| 28 |
import org.eclipse.debug.ui.contexts.IDebugContextService; |
| 29 |
import org.eclipse.jface.viewers.ISelection; |
| 30 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 31 |
import org.eclipse.ui.ISources; |
| 32 |
import org.eclipse.ui.IWindowListener; |
| 33 |
import org.eclipse.ui.IWorkbenchWindow; |
| 34 |
import org.eclipse.ui.PlatformUI; |
| 35 |
import org.eclipse.ui.handlers.HandlerUtil; |
| 36 |
|
| 37 |
/** |
| 38 |
* Abstract base class for re-targeting command framework handlers, which |
| 39 |
* delegate execution to {@link org.eclipse.debug.core.commands.IDebugCommandHandler} |
| 40 |
* handlers. The specific type of <code>IDebugCommandHandler</code> is |
| 41 |
* determined by the abstract {@link #getCommandType()} method. |
| 42 |
* |
| 43 |
* <p> Note: This class is not an implementation of the <code>IDebugCommandHandler</code> |
| 44 |
* interface, which was somewhat unfortunately named. <code>IDebugCommandHandler</code> |
| 45 |
* is an interface that is internal to the debugger plugins. This class implements |
| 46 |
* {@link org.eclipse.core.commands.IHandler} interface and is to be used with the |
| 47 |
* platform commands framework. </p> |
| 48 |
* |
| 49 |
* @see org.eclipse.debug.core.commands.IDebugCommandHandler |
| 50 |
* @see org.eclipse.core.commands.IHandler |
| 51 |
* |
| 52 |
* @since 3.6 |
| 53 |
*/ |
| 54 |
public abstract class DebugCommandHandler extends AbstractHandler { |
| 55 |
|
| 56 |
private class EnabledTarget implements IEnabledTarget, IDebugContextListener, IWindowListener { |
| 57 |
boolean fEnabled = getInitialEnablement(); |
| 58 |
IWorkbenchWindow fWindow; |
| 59 |
|
| 60 |
EnabledTarget(IWorkbenchWindow window) { |
| 61 |
fWindow = window; |
| 62 |
DebugCommandService.getService(fWindow).updateCommand(getCommandType(), this); |
| 63 |
getContextService(fWindow).addDebugContextListener(this); |
| 64 |
PlatformUI.getWorkbench().addWindowListener(this); |
| 65 |
} |
| 66 |
|
| 67 |
public void setEnabled(boolean enabled) { |
| 68 |
boolean oldEnabled = fEnabled; |
| 69 |
fEnabled = enabled; |
| 70 |
if (fEnabled != oldEnabled) { |
| 71 |
fireHandlerChanged(new HandlerEvent(DebugCommandHandler.this, true, false)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public void debugContextChanged(DebugContextEvent event) { |
| 76 |
DebugCommandService.getService(fWindow).postUpdateCommand(getCommandType(), this); |
| 77 |
} |
| 78 |
|
| 79 |
public void windowOpened(IWorkbenchWindow w) { |
| 80 |
} |
| 81 |
|
| 82 |
public void windowDeactivated(IWorkbenchWindow w) { |
| 83 |
} |
| 84 |
|
| 85 |
public void windowClosed(IWorkbenchWindow w) { |
| 86 |
if (w.equals(fWindow)) { |
| 87 |
dispose(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public void windowActivated(IWorkbenchWindow w) { |
| 92 |
} |
| 93 |
|
| 94 |
void dispose() { |
| 95 |
if (isDisposed()) { |
| 96 |
return; |
| 97 |
} |
| 98 |
PlatformUI.getWorkbench().removeWindowListener(this); |
| 99 |
getContextService(fWindow).removeDebugContextListener(this); |
| 100 |
fWindow = null; |
| 101 |
} |
| 102 |
|
| 103 |
boolean isDisposed() { |
| 104 |
return fWindow == null; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
private Map fEnabledTargetsMap = new WeakHashMap(); |
| 109 |
|
| 110 |
private EnabledTarget fCurrentEnabledTarget = null; |
| 111 |
|
| 112 |
/** |
| 113 |
* Constructor |
| 114 |
*/ |
| 115 |
public DebugCommandHandler() { |
| 116 |
super(); |
| 117 |
PlatformUI.getWorkbench().addWindowListener(new IWindowListener() { |
| 118 |
|
| 119 |
public void windowOpened(IWorkbenchWindow w) { |
| 120 |
} |
| 121 |
|
| 122 |
public void windowDeactivated(IWorkbenchWindow w) { |
| 123 |
} |
| 124 |
|
| 125 |
public void windowClosed(IWorkbenchWindow w) { |
| 126 |
EnabledTarget enabledTarget = (EnabledTarget)fEnabledTargetsMap.get(w); |
| 127 |
if (enabledTarget != null) { |
| 128 |
enabledTarget.dispose(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public void windowActivated(IWorkbenchWindow w) { |
| 133 |
} |
| 134 |
}); |
| 135 |
} |
| 136 |
|
| 137 |
public void setEnabled(Object evaluationContext) { |
| 138 |
fCurrentEnabledTarget = null; |
| 139 |
|
| 140 |
if (!(evaluationContext instanceof IEvaluationContext)) { |
| 141 |
return; |
| 142 |
} |
| 143 |
IEvaluationContext context = (IEvaluationContext) evaluationContext; |
| 144 |
Object _window = context.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME); |
| 145 |
if (_window instanceof IWorkbenchWindow) { |
| 146 |
IWorkbenchWindow window = (IWorkbenchWindow)_window; |
| 147 |
fCurrentEnabledTarget = getEnabledTarget(window); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public boolean isEnabled() { |
| 152 |
if (fCurrentEnabledTarget == null) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
return fCurrentEnabledTarget.fEnabled; |
| 156 |
} |
| 157 |
|
| 158 |
private EnabledTarget getEnabledTarget(IWorkbenchWindow window) { |
| 159 |
EnabledTarget target = (EnabledTarget)fEnabledTargetsMap.get(window); |
| 160 |
if (target == null) { |
| 161 |
target = new EnabledTarget(window); |
| 162 |
DebugCommandService service = DebugCommandService.getService(window); |
| 163 |
service.updateCommand(getCommandType(), target); |
| 164 |
|
| 165 |
DebugUITools.getDebugContextManager().getContextService(window).addDebugContextListener(target); |
| 166 |
|
| 167 |
fEnabledTargetsMap.put(window, target); |
| 168 |
} |
| 169 |
return target; |
| 170 |
} |
| 171 |
|
| 172 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
| 173 |
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); |
| 174 |
if (window == null) { |
| 175 |
throw new ExecutionException("No active workbench window."); |
| 176 |
} |
| 177 |
fCurrentEnabledTarget = getEnabledTarget(window); |
| 178 |
|
| 179 |
ISelection selection = getContextService(window).getActiveContext(); |
| 180 |
if (selection instanceof IStructuredSelection && isEnabled()) { |
| 181 |
IStructuredSelection ss = (IStructuredSelection) selection; |
| 182 |
boolean enabledAfterExecute = execute(window, ss.toArray()); |
| 183 |
|
| 184 |
// enable/disable the action according to the command |
| 185 |
fCurrentEnabledTarget.setEnabled(enabledAfterExecute); |
| 186 |
} |
| 187 |
|
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
private IDebugContextService getContextService(IWorkbenchWindow window) { |
| 192 |
return DebugUITools.getDebugContextManager().getContextService(window); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Executes this action on the given target object |
| 197 |
* |
| 198 |
* @param target the target to perform the action on |
| 199 |
*/ |
| 200 |
private boolean execute(IWorkbenchWindow window, Object[] targets) { |
| 201 |
DebugCommandService service = DebugCommandService.getService(window); |
| 202 |
return service.executeCommand(getCommandType(), targets, getCommandParticipant(targets)); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Creates and returns the command participant or <code>null</code>. |
| 207 |
* |
| 208 |
* @return command participant to use on command completion |
| 209 |
*/ |
| 210 |
protected ICommandParticipant getCommandParticipant(Object[] targets) { |
| 211 |
return null; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Returns the {@link org.eclipse.debug.core.commands.IDebugCommandHandler} |
| 216 |
* command handler that type this action executes. |
| 217 |
* |
| 218 |
* @return command class. |
| 219 |
* |
| 220 |
* @see org.eclipse.debug.core.commands.IDebugCommandHandler |
| 221 |
*/ |
| 222 |
abstract protected Class getCommandType(); |
| 223 |
|
| 224 |
|
| 225 |
/** |
| 226 |
* Returns whether this action should be enabled when initialized |
| 227 |
* and there is no active debug context. |
| 228 |
* |
| 229 |
* @return false, by default |
| 230 |
*/ |
| 231 |
protected boolean getInitialEnablement() { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Clean up when removing |
| 238 |
*/ |
| 239 |
public void dispose() { |
| 240 |
for (Iterator itr = fEnabledTargetsMap.values().iterator(); itr.hasNext();) { |
| 241 |
EnabledTarget target = (EnabledTarget)itr.next(); |
| 242 |
if (!target.isDisposed()) { |
| 243 |
target.dispose(); |
| 244 |
} |
| 245 |
} |
| 246 |
fEnabledTargetsMap.clear(); |
| 247 |
fCurrentEnabledTarget = null; |
| 248 |
} |
| 249 |
} |