|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2005 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 |
package org.eclipse.debug.internal.ui.actions; |
| 12 |
|
| 13 |
import java.util.Iterator; |
| 14 |
|
| 15 |
import org.eclipse.core.runtime.CoreException; |
| 16 |
import org.eclipse.core.runtime.IAdaptable; |
| 17 |
import org.eclipse.core.runtime.IAdapterManager; |
| 18 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 19 |
import org.eclipse.core.runtime.IStatus; |
| 20 |
import org.eclipse.core.runtime.Platform; |
| 21 |
import org.eclipse.core.runtime.Status; |
| 22 |
import org.eclipse.core.runtime.jobs.Job; |
| 23 |
import org.eclipse.debug.core.DebugEvent; |
| 24 |
import org.eclipse.debug.core.DebugPlugin; |
| 25 |
import org.eclipse.debug.core.IDebugEventSetListener; |
| 26 |
import org.eclipse.debug.ui.IDebugUIConstants; |
| 27 |
import org.eclipse.jface.action.IAction; |
| 28 |
import org.eclipse.jface.viewers.ISelection; |
| 29 |
import org.eclipse.jface.viewers.ISelectionProvider; |
| 30 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 31 |
import org.eclipse.swt.widgets.Event; |
| 32 |
import org.eclipse.ui.IActionDelegate2; |
| 33 |
import org.eclipse.ui.IPartListener; |
| 34 |
import org.eclipse.ui.IPartService; |
| 35 |
import org.eclipse.ui.ISelectionListener; |
| 36 |
import org.eclipse.ui.IWorkbenchPart; |
| 37 |
import org.eclipse.ui.IWorkbenchPartSite; |
| 38 |
import org.eclipse.ui.IWorkbenchWindow; |
| 39 |
import org.eclipse.ui.IWorkbenchWindowActionDelegate; |
| 40 |
|
| 41 |
/** |
| 42 |
* A retargettable action that operates in conjuction with the selection in the |
| 43 |
* debug veiw. |
| 44 |
* |
| 45 |
* @since 3.1 |
| 46 |
*/ |
| 47 |
public abstract class LaunchViewRetargetAction implements IWorkbenchWindowActionDelegate, IActionDelegate2, IPartListener, ISelectionListener, IDebugEventSetListener { |
| 48 |
|
| 49 |
private IWorkbenchWindow fWindow = null; |
| 50 |
private IWorkbenchPart fActivePart = null; |
| 51 |
private ISelection fActivePartSelection = null; |
| 52 |
private IWorkbenchPart fDebugview; |
| 53 |
private ISelection fDebugViewSelection = null; |
| 54 |
private IAction fAction = null; |
| 55 |
private UpdateJob fUpdateJob = new UpdateJob(); |
| 56 |
private RunJob fRunJob = new RunJob(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Job to update action enablement. |
| 60 |
*/ |
| 61 |
class UpdateJob extends Job { |
| 62 |
public UpdateJob() { |
| 63 |
super("Update Action Enablement"); |
| 64 |
setSystem(true); |
| 65 |
} |
| 66 |
/* (non-Javadoc) |
| 67 |
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) |
| 68 |
*/ |
| 69 |
protected IStatus run(IProgressMonitor monitor) { |
| 70 |
doUpdate(); |
| 71 |
return Status.OK_STATUS; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
class RunJob extends Job { |
| 76 |
private Object[] targets; |
| 77 |
|
| 78 |
public RunJob() { |
| 79 |
super("Run Action"); |
| 80 |
setSystem(true); |
| 81 |
} |
| 82 |
|
| 83 |
protected void configure(Object[] t) { |
| 84 |
targets = t; |
| 85 |
} |
| 86 |
|
| 87 |
/* (non-Javadoc) |
| 88 |
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) |
| 89 |
*/ |
| 90 |
protected IStatus run(IProgressMonitor monitor) { |
| 91 |
for (int i = 0; i < targets.length; i++) { |
| 92 |
Object target = targets[i]; |
| 93 |
if (canPerformAction(target)) { |
| 94 |
try { |
| 95 |
performAction(target); |
| 96 |
} catch (CoreException e) { |
| 97 |
return e.getStatus(); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
return Status.OK_STATUS; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* (non-Javadoc) |
| 106 |
* @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() |
| 107 |
*/ |
| 108 |
public void dispose() { |
| 109 |
DebugPlugin.getDefault().removeDebugEventListener(this); |
| 110 |
fWindow.getPartService().removePartListener(this); |
| 111 |
fWindow.getSelectionService().removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this); |
| 112 |
fDebugview = null; |
| 113 |
fDebugViewSelection = null; |
| 114 |
fActivePart = null; |
| 115 |
fActivePartSelection = null; |
| 116 |
fWindow = null; |
| 117 |
fAction = null; |
| 118 |
fUpdateJob.cancel(); |
| 119 |
fUpdateJob = null; |
| 120 |
fRunJob.cancel(); |
| 121 |
fRunJob = null; |
| 122 |
} |
| 123 |
/* (non-Javadoc) |
| 124 |
* @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow) |
| 125 |
*/ |
| 126 |
public void init(IWorkbenchWindow window) { |
| 127 |
this.fWindow = window; |
| 128 |
IPartService partService = window.getPartService(); |
| 129 |
partService.addPartListener(this); |
| 130 |
IWorkbenchPart part = partService.getActivePart(); |
| 131 |
if (part != null) { |
| 132 |
partActivated(part); |
| 133 |
} |
| 134 |
window.getSelectionService().addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this); |
| 135 |
DebugPlugin.getDefault().addDebugEventListener(this); |
| 136 |
} |
| 137 |
/* (non-Javadoc) |
| 138 |
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
| 139 |
*/ |
| 140 |
public synchronized final void run(IAction action) { |
| 141 |
if (action.isEnabled()) { |
| 142 |
action.setEnabled(false); |
| 143 |
fRunJob.schedule(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Updates this action based on current settings. |
| 149 |
*/ |
| 150 |
protected final void update() { |
| 151 |
fUpdateJob.schedule(); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Performs the specific breakpoint toggling. |
| 156 |
* |
| 157 |
* @param target the target to operate on |
| 158 |
* @throws CoreException if an exception occurrs |
| 159 |
*/ |
| 160 |
protected abstract void performAction(Object target) throws CoreException; |
| 161 |
|
| 162 |
/* (non-Javadoc) |
| 163 |
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) |
| 164 |
*/ |
| 165 |
public void selectionChanged(IAction action, ISelection selection) { |
| 166 |
fAction.setEnabled(false); |
| 167 |
fActivePartSelection = selection; |
| 168 |
update(); |
| 169 |
} |
| 170 |
|
| 171 |
/* (non-Javadoc) |
| 172 |
* @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection) |
| 173 |
*/ |
| 174 |
public void selectionChanged(IWorkbenchPart part, ISelection selection) { |
| 175 |
fDebugview = part; |
| 176 |
fAction.setEnabled(false); |
| 177 |
fDebugViewSelection = selection; |
| 178 |
update(); |
| 179 |
} |
| 180 |
|
| 181 |
/* (non-Javadoc) |
| 182 |
* @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction) |
| 183 |
*/ |
| 184 |
public void init(IAction action) { |
| 185 |
fAction = action; |
| 186 |
} |
| 187 |
|
| 188 |
/* (non-Javadoc) |
| 189 |
* @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event) |
| 190 |
*/ |
| 191 |
public void runWithEvent(IAction action, Event event) { |
| 192 |
run(action); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Udpates the enablement of this action based on the given part and selection, |
| 197 |
* and returns whether this action is now enabled. |
| 198 |
* |
| 199 |
* @param part part in which to resolve a target adapter or <code>null</code> |
| 200 |
* @param selection selection in the given part or <code>null</code> |
| 201 |
*/ |
| 202 |
protected synchronized void update(IWorkbenchPart part, ISelection selection) { |
| 203 |
IAction action = getAction(); |
| 204 |
if (action == null) { |
| 205 |
return; |
| 206 |
} |
| 207 |
Object adapter = null; |
| 208 |
if (part != fDebugview) { |
| 209 |
adapter = getAdapter(part); |
| 210 |
} |
| 211 |
if (adapter == null && selection instanceof IStructuredSelection) { |
| 212 |
IStructuredSelection ss = (IStructuredSelection) selection; |
| 213 |
boolean enabled = false; |
| 214 |
if (!ss.isEmpty()) { |
| 215 |
enabled = true; |
| 216 |
Iterator iterator = ss.iterator(); |
| 217 |
while (iterator.hasNext() && enabled) { |
| 218 |
Object object = iterator.next(); |
| 219 |
adapter = getAdapter(object); |
| 220 |
enabled = adapter != null && canPerformAction(adapter); |
| 221 |
} |
| 222 |
} |
| 223 |
action.setEnabled(enabled); |
| 224 |
if (enabled) { |
| 225 |
fRunJob.configure(ss.toArray()); |
| 226 |
} |
| 227 |
} else { |
| 228 |
if (adapter != null) { |
| 229 |
action.setEnabled(canPerformAction(adapter)); |
| 230 |
if (action.isEnabled()) { |
| 231 |
fRunJob.configure(new Object[]{adapter}); |
| 232 |
} |
| 233 |
} else { |
| 234 |
action.setEnabled(false); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/* (non-Javadoc) |
| 240 |
* @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart) |
| 241 |
*/ |
| 242 |
public void partActivated(IWorkbenchPart part) { |
| 243 |
fActivePart = part; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the currently active part or <code>null</code> if none. |
| 248 |
* |
| 249 |
* @return the currently active part or <code>null</code> if none |
| 250 |
*/ |
| 251 |
protected IWorkbenchPart getActivePart() { |
| 252 |
return fActivePart; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Returns this delegate's action or <code>null</code> if none. |
| 257 |
* |
| 258 |
* @return this delegate's action or <code>null</code> if none |
| 259 |
*/ |
| 260 |
protected IAction getAction() { |
| 261 |
return fAction; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns the window this action is installed in or <code>null</code>. |
| 266 |
* |
| 267 |
* @return the window this action is installed in or <code>null</code> |
| 268 |
*/ |
| 269 |
protected IWorkbenchWindow getWindow() { |
| 270 |
return fWindow; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Returns the target adapter applicable to the given object or <code>null</code> |
| 275 |
* if none. |
| 276 |
* |
| 277 |
* @param object object to retrieve target adapter for |
| 278 |
* @return the target adapter applicable to the given object or <code>null</code> |
| 279 |
* if none |
| 280 |
*/ |
| 281 |
protected Object getAdapter(Object object) { |
| 282 |
Object adapter = null; |
| 283 |
if (isInstanceOfAdapterClass(object)) { |
| 284 |
adapter = object; |
| 285 |
} else if (object instanceof IAdaptable) { |
| 286 |
IAdaptable adaptable = (IAdaptable)object; |
| 287 |
adapter = adaptable.getAdapter(getAdapterClass()); |
| 288 |
if (adapter == null) { |
| 289 |
IAdapterManager adapterManager = Platform.getAdapterManager(); |
| 290 |
if (adapterManager.hasAdapter(adaptable, getAdapterClass().getName())) { //$NON-NLS-1$ |
| 291 |
adapter = adapterManager.loadAdapter(adaptable, getAdapterClass().getName()); //$NON-NLS-1$ |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
return adapter; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Returns whether the given object is an instance of the adapter this |
| 300 |
* action is looking for. |
| 301 |
* |
| 302 |
* @param object object |
| 303 |
* @return whether the given object is an instance of the adapter this |
| 304 |
* action is looking for |
| 305 |
*/ |
| 306 |
protected abstract boolean isInstanceOfAdapterClass(Object object); |
| 307 |
|
| 308 |
/** |
| 309 |
* Returns the type of adapter (target) this action works on. |
| 310 |
* |
| 311 |
* @return the type of adapter this action works on |
| 312 |
*/ |
| 313 |
protected abstract Class getAdapterClass(); |
| 314 |
|
| 315 |
/* (non-Javadoc) |
| 316 |
* @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart) |
| 317 |
*/ |
| 318 |
public void partBroughtToTop(IWorkbenchPart part) { |
| 319 |
} |
| 320 |
/* (non-Javadoc) |
| 321 |
* @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart) |
| 322 |
*/ |
| 323 |
public void partClosed(IWorkbenchPart part) { |
| 324 |
clearPart(part); |
| 325 |
if (part.equals(fDebugview)) { |
| 326 |
fDebugview = null; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Clears reference to active part and adapter when a relevant part |
| 332 |
* is closed or deactivated. |
| 333 |
* |
| 334 |
* @param part workbench part that has been closed or deactivated |
| 335 |
*/ |
| 336 |
protected void clearPart(IWorkbenchPart part) { |
| 337 |
if (part.equals(fActivePart)) { |
| 338 |
fActivePart = null; |
| 339 |
} |
| 340 |
} |
| 341 |
/* (non-Javadoc) |
| 342 |
* @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart) |
| 343 |
*/ |
| 344 |
public void partDeactivated(IWorkbenchPart part) { |
| 345 |
clearPart(part); |
| 346 |
} |
| 347 |
/* (non-Javadoc) |
| 348 |
* @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart) |
| 349 |
*/ |
| 350 |
public void partOpened(IWorkbenchPart part) { |
| 351 |
if (part.getSite().getId().equals(IDebugUIConstants.ID_DEBUG_VIEW)) { |
| 352 |
fDebugview = part; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Returns whether the specific operation is supported. |
| 358 |
* |
| 359 |
* @param target the target adapter |
| 360 |
* @return whether the operation can be performed |
| 361 |
*/ |
| 362 |
protected abstract boolean canPerformAction(Object target); |
| 363 |
|
| 364 |
/** |
| 365 |
* Returns the selection in the given part, or <code>null</code>. |
| 366 |
* |
| 367 |
* @param part workbench part |
| 368 |
* @return the selection in the given part, or <code>null</code> |
| 369 |
*/ |
| 370 |
protected ISelection getSelection(IWorkbenchPart part) { |
| 371 |
if (part != null) { |
| 372 |
IWorkbenchPartSite site = part.getSite(); |
| 373 |
if (site != null) { |
| 374 |
ISelectionProvider provider = site.getSelectionProvider(); |
| 375 |
if (provider != null) { |
| 376 |
return provider.getSelection(); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
return null; |
| 381 |
} |
| 382 |
|
| 383 |
/* (non-Javadoc) |
| 384 |
* @see org.eclipse.debug.internal.ui.actions.RetargetAction#update() |
| 385 |
*/ |
| 386 |
protected void doUpdate() { |
| 387 |
if (fDebugview == null) { |
| 388 |
// update based on active part |
| 389 |
update(fActivePart, fActivePartSelection); |
| 390 |
} else if (fDebugview.equals(getActivePart())){ |
| 391 |
// update based on debug view |
| 392 |
update(fDebugview, fDebugViewSelection); |
| 393 |
} else { |
| 394 |
// update active part, then revert to debug view if still disabled |
| 395 |
update(fActivePart, fActivePartSelection); |
| 396 |
if (!getAction().isEnabled()) { |
| 397 |
update(fDebugview, fDebugViewSelection); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/* (non-Javadoc) |
| 403 |
* @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[]) |
| 404 |
*/ |
| 405 |
public void handleDebugEvents(DebugEvent[] events) { |
| 406 |
update(); |
| 407 |
} |
| 408 |
|
| 409 |
} |