|
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 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.debug.ui.actions; |
| 13 |
|
| 14 |
import org.eclipse.debug.core.IRequest; |
| 15 |
import org.eclipse.debug.internal.ui.commands.actions.DebugCommandService; |
| 16 |
import org.eclipse.debug.internal.ui.commands.actions.ICommandParticipant; |
| 17 |
import org.eclipse.debug.internal.ui.commands.actions.IEnabledTarget; |
| 18 |
import org.eclipse.debug.ui.DebugUITools; |
| 19 |
import org.eclipse.debug.ui.contexts.DebugContextEvent; |
| 20 |
import org.eclipse.debug.ui.contexts.IDebugContextListener; |
| 21 |
import org.eclipse.debug.ui.contexts.IDebugContextService; |
| 22 |
import org.eclipse.jface.action.Action; |
| 23 |
import org.eclipse.jface.action.IAction; |
| 24 |
import org.eclipse.jface.resource.ImageDescriptor; |
| 25 |
import org.eclipse.jface.viewers.ISelection; |
| 26 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 27 |
import org.eclipse.swt.widgets.Event; |
| 28 |
import org.eclipse.ui.IWorkbenchPart; |
| 29 |
import org.eclipse.ui.IWorkbenchWindow; |
| 30 |
import org.eclipse.ui.PlatformUI; |
| 31 |
|
| 32 |
/** |
| 33 |
* Abstract base class for re-targeting actions which delegate execution to |
| 34 |
* {@link org.eclipse.debug.core.commands.IDebugCommandHandler} handlers. |
| 35 |
* The specific type of <code>IDebugCommandHandler</code> is determined by the |
| 36 |
* abstract {@link #getCommandType()} method. |
| 37 |
* <p> |
| 38 |
* This base class is an action which can be instantiated directly by views, |
| 39 |
* etc. In order to contribute an action using an extension point, a class |
| 40 |
* implementing {@link org.eclipse.ui.IActionDelegate} should be created first. |
| 41 |
* The delegate should then use the <code>DebugCommandAction</code> implement |
| 42 |
* the needed delegate functionality. <br> |
| 43 |
* Note: <code>IDebugCommandHandler</code> command typically act on the active |
| 44 |
* debug context as opposed to the active selection in view or window. The |
| 45 |
* action delegate should ignore the active window selection, and instead allow |
| 46 |
* the <code>DebugCommandAction</code> to update itself based on the active |
| 47 |
* debug context. |
| 48 |
* </p> |
| 49 |
* |
| 50 |
* @see org.eclipse.debug.core.commands.IDebugCommandHandler |
| 51 |
* |
| 52 |
* @since 3.6 This class was in the internal package |
| 53 |
* <code>org.eclipse.debug.internal.ui.commands.actions</code> since 3.3. |
| 54 |
*/ |
| 55 |
public abstract class DebugCommandAction extends Action implements IDebugContextListener { |
| 56 |
|
| 57 |
private boolean fInitialized = false; |
| 58 |
|
| 59 |
/** |
| 60 |
* The window this action is working for. |
| 61 |
*/ |
| 62 |
private IWorkbenchWindow fWindow; |
| 63 |
|
| 64 |
/** |
| 65 |
* The part this action is working for, or <code>null</code> if global to |
| 66 |
* a window. |
| 67 |
*/ |
| 68 |
private IWorkbenchPart fPart; |
| 69 |
|
| 70 |
/** |
| 71 |
* Command service. |
| 72 |
*/ |
| 73 |
private DebugCommandService fUpdateService; |
| 74 |
|
| 75 |
/** |
| 76 |
* Delegate this action is working for or <code>null</code> if none. |
| 77 |
*/ |
| 78 |
private IAction fAction; |
| 79 |
|
| 80 |
private IEnabledTarget fEnabledTarget = new IEnabledTarget() { |
| 81 |
public void setEnabled(boolean enabled) { |
| 82 |
DebugCommandAction.this.setEnabled(enabled); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
/** |
| 87 |
* Constructor |
| 88 |
*/ |
| 89 |
public DebugCommandAction() { |
| 90 |
super(); |
| 91 |
String helpContextId = getHelpContextId(); |
| 92 |
if (helpContextId != null) |
| 93 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId); |
| 94 |
setEnabled(false); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set the current delegate |
| 99 |
* @param delegate |
| 100 |
*/ |
| 101 |
public void setAction(IAction action) { |
| 102 |
fAction = action; |
| 103 |
fAction.setEnabled(isEnabled()); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Executes this action on the given target object |
| 108 |
* |
| 109 |
* @param target the target to perform the action on |
| 110 |
*/ |
| 111 |
private boolean execute(final Object[] targets) { |
| 112 |
return fUpdateService.executeCommand( |
| 113 |
getCommandType(), targets, |
| 114 |
new ICommandParticipant() { |
| 115 |
public void requestDone(org.eclipse.debug.core.IRequest request) { |
| 116 |
DebugCommandAction.this.postExecute(request, targets); |
| 117 |
} |
| 118 |
}); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* This method is called after the completion of the execution of this |
| 123 |
* command. Extending classes may override this method to perform additional |
| 124 |
* operation after command execution. |
| 125 |
* |
| 126 |
* @param targets Objects which were the targets of this action |
| 127 |
* @param request The completed request object which was given the the |
| 128 |
* debug command handler. |
| 129 |
*/ |
| 130 |
protected void postExecute(IRequest request, Object[] targets) { |
| 131 |
// do nothing by default |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Returns the {@link org.eclipse.debug.core.commands.IDebugCommandHandler} |
| 136 |
* command handler that type this action executes. |
| 137 |
* |
| 138 |
* @return command class. |
| 139 |
* |
| 140 |
* @see org.eclipse.debug.core.commands.IDebugCommandHandler |
| 141 |
*/ |
| 142 |
abstract protected Class getCommandType(); |
| 143 |
|
| 144 |
/** |
| 145 |
* @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent) |
| 146 |
*/ |
| 147 |
public void debugContextChanged(DebugContextEvent event) { |
| 148 |
fUpdateService.postUpdateCommand(getCommandType(), fEnabledTarget); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @see org.eclipse.jface.action.Action#setEnabled(boolean) |
| 153 |
*/ |
| 154 |
public void setEnabled(boolean enabled) { |
| 155 |
synchronized (this) { |
| 156 |
if (!fInitialized) { |
| 157 |
fInitialized = true; |
| 158 |
notifyAll(); |
| 159 |
} |
| 160 |
} |
| 161 |
super.setEnabled(enabled); |
| 162 |
if (fAction != null) { |
| 163 |
fAction.setEnabled(enabled); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Initializes this action for a specific part. |
| 169 |
* |
| 170 |
* @param part workbench part |
| 171 |
*/ |
| 172 |
public void init(IWorkbenchPart part) { |
| 173 |
fPart = part; |
| 174 |
fWindow = part.getSite().getWorkbenchWindow(); |
| 175 |
fUpdateService = DebugCommandService.getService(fWindow); |
| 176 |
IDebugContextService service = getDebugContextService(); |
| 177 |
String partId = part.getSite().getId(); |
| 178 |
service.addDebugContextListener(this, partId); |
| 179 |
ISelection activeContext = service.getActiveContext(partId); |
| 180 |
if (activeContext != null) { |
| 181 |
fUpdateService.updateCommand(getCommandType(), fEnabledTarget); |
| 182 |
} else { |
| 183 |
setEnabled(getInitialEnablement()); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Initializes the context action |
| 189 |
* @param window the window |
| 190 |
*/ |
| 191 |
public void init(IWorkbenchWindow window) { |
| 192 |
fWindow = window; |
| 193 |
fUpdateService = DebugCommandService.getService(fWindow); |
| 194 |
IDebugContextService contextService = getDebugContextService(); |
| 195 |
contextService.addDebugContextListener(this); |
| 196 |
ISelection activeContext = contextService.getActiveContext(); |
| 197 |
if (activeContext != null) { |
| 198 |
fUpdateService.updateCommand(getCommandType(), fEnabledTarget); |
| 199 |
} else { |
| 200 |
setEnabled(getInitialEnablement()); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns whether this action should be enabled when initialized |
| 206 |
* and there is no active debug context. |
| 207 |
* |
| 208 |
* @return false, by default |
| 209 |
*/ |
| 210 |
protected boolean getInitialEnablement() { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Returns the most recent selection |
| 216 |
* |
| 217 |
* @return structured selection |
| 218 |
*/ |
| 219 |
private ISelection getContext() { |
| 220 |
if (fPart != null) { |
| 221 |
getDebugContextService().getActiveContext(fPart.getSite().getId()); |
| 222 |
} |
| 223 |
return getDebugContextService().getActiveContext(); |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* (non-Javadoc) |
| 228 |
* @see org.eclipse.jface.action.Action#run() |
| 229 |
*/ |
| 230 |
public void run() { |
| 231 |
synchronized (this) { |
| 232 |
if (!fInitialized) { |
| 233 |
try { |
| 234 |
wait(); |
| 235 |
} catch (InterruptedException e) { |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
ISelection selection = getContext(); |
| 241 |
if (selection instanceof IStructuredSelection && isEnabled()) { |
| 242 |
IStructuredSelection ss = (IStructuredSelection) selection; |
| 243 |
boolean enabled = execute(ss.toArray()); |
| 244 |
// disable the action according to the command |
| 245 |
setEnabled(enabled); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* (non-Javadoc) |
| 251 |
* @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event) |
| 252 |
*/ |
| 253 |
public void runWithEvent(Event event) { |
| 254 |
run(); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Clean up when removing |
| 259 |
*/ |
| 260 |
public void dispose() { |
| 261 |
IDebugContextService service = getDebugContextService(); |
| 262 |
if (fPart != null) { |
| 263 |
service.removeDebugContextListener(this, fPart.getSite().getId()); |
| 264 |
} else { |
| 265 |
service.removeDebugContextListener(this); |
| 266 |
} |
| 267 |
fWindow = null; |
| 268 |
fPart = null; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Returns the context service this action linked to. |
| 273 |
* @return |
| 274 |
*/ |
| 275 |
protected IDebugContextService getDebugContextService() { |
| 276 |
return DebugUITools.getDebugContextManager().getContextService(fWindow); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* @return The help context id for this action |
| 281 |
*/ |
| 282 |
public abstract String getHelpContextId(); |
| 283 |
|
| 284 |
/* |
| 285 |
* (non-Javadoc) |
| 286 |
* @see org.eclipse.jface.action.Action#getId() |
| 287 |
*/ |
| 288 |
public abstract String getId(); |
| 289 |
|
| 290 |
/* |
| 291 |
* (non-Javadoc) |
| 292 |
* @see org.eclipse.jface.action.Action#getText() |
| 293 |
*/ |
| 294 |
public abstract String getText(); |
| 295 |
|
| 296 |
/* |
| 297 |
* (non-Javadoc) |
| 298 |
* @see org.eclipse.jface.action.Action#getToolTipText() |
| 299 |
*/ |
| 300 |
public abstract String getToolTipText(); |
| 301 |
|
| 302 |
/* |
| 303 |
* (non-Javadoc) |
| 304 |
* @see org.eclipse.jface.action.Action#getDisabledImageDescriptor() |
| 305 |
*/ |
| 306 |
public abstract ImageDescriptor getDisabledImageDescriptor(); |
| 307 |
|
| 308 |
/* |
| 309 |
* (non-Javadoc) |
| 310 |
* @see org.eclipse.jface.action.Action#getHoverImageDescriptor() |
| 311 |
*/ |
| 312 |
public abstract ImageDescriptor getHoverImageDescriptor(); |
| 313 |
|
| 314 |
/* |
| 315 |
* (non-Javadoc) |
| 316 |
* @see org.eclipse.jface.action.Action#getImageDescriptor() |
| 317 |
*/ |
| 318 |
public abstract ImageDescriptor getImageDescriptor(); |
| 319 |
|
| 320 |
/** |
| 321 |
* Returns the delegate associated with this action or <code>null</code> |
| 322 |
* if none. |
| 323 |
* |
| 324 |
* @return delegate or <code>null</code> |
| 325 |
*/ |
| 326 |
protected IAction getAction() { |
| 327 |
return fAction; |
| 328 |
} |
| 329 |
} |