|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006, 2009 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.core.commands; |
| 12 |
|
| 13 |
import java.util.LinkedHashSet; |
| 14 |
|
| 15 |
import org.eclipse.core.runtime.CoreException; |
| 16 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 17 |
import org.eclipse.core.runtime.IStatus; |
| 18 |
import org.eclipse.core.runtime.Status; |
| 19 |
import org.eclipse.core.runtime.jobs.IJobChangeEvent; |
| 20 |
import org.eclipse.core.runtime.jobs.IJobChangeListener; |
| 21 |
import org.eclipse.core.runtime.jobs.ISchedulingRule; |
| 22 |
import org.eclipse.core.runtime.jobs.Job; |
| 23 |
import org.eclipse.debug.core.DebugPlugin; |
| 24 |
import org.eclipse.debug.core.IRequest; |
| 25 |
import org.eclipse.debug.internal.core.DebugOptions; |
| 26 |
|
| 27 |
/** |
| 28 |
* Abstract implementation of a debug command handler. Handles {@link IDebugCommandRequest} |
| 29 |
* and {@link IEnabledStateRequest} updates asynchronously using jobs. |
| 30 |
* <p> |
| 31 |
* Clients may subclass this class. |
| 32 |
* </p> |
| 33 |
* @since 3.6 |
| 34 |
*/ |
| 35 |
public abstract class AbstractDebugCommand implements IDebugCommandHandler { |
| 36 |
|
| 37 |
/** |
| 38 |
* Job to update enabled state of action. |
| 39 |
*/ |
| 40 |
private class UpdateJob extends Job implements IJobChangeListener { |
| 41 |
|
| 42 |
/** |
| 43 |
* The request to update |
| 44 |
*/ |
| 45 |
private IEnabledStateRequest request; |
| 46 |
|
| 47 |
/** |
| 48 |
* Whether this job has been run |
| 49 |
*/ |
| 50 |
private boolean run = false; |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates a new job to update the specified request |
| 54 |
* |
| 55 |
* @param stateRequest |
| 56 |
*/ |
| 57 |
UpdateJob(IEnabledStateRequest stateRequest) { |
| 58 |
super(getEnabledStateTaskName()); |
| 59 |
request = stateRequest; |
| 60 |
setSystem(true); |
| 61 |
setRule(getEnabledStateSchedulingRule(request)); |
| 62 |
getJobManager().addJobChangeListener(this); |
| 63 |
} |
| 64 |
|
| 65 |
/* (non-Javadoc) |
| 66 |
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) |
| 67 |
*/ |
| 68 |
protected IStatus run(IProgressMonitor monitor) { |
| 69 |
run = true; |
| 70 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 71 |
System.out.print("can execute command: " + AbstractDebugCommand.this); //$NON-NLS-1$ |
| 72 |
} |
| 73 |
if (monitor.isCanceled()) { |
| 74 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 75 |
System.out.println(" >> *CANCELED* <<"); //$NON-NLS-1$ |
| 76 |
} |
| 77 |
request.cancel(); |
| 78 |
} |
| 79 |
Object[] elements = request.getElements(); |
| 80 |
Object[] targets = new Object[elements.length]; |
| 81 |
if (!request.isCanceled()) { |
| 82 |
for (int i = 0; i < elements.length; i++) { |
| 83 |
targets[i] = getTarget(elements[i]); |
| 84 |
if (targets[i] == null) { |
| 85 |
request.setEnabled(false); |
| 86 |
request.cancel(); |
| 87 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 88 |
System.out.println(" >> false (no adapter)"); //$NON-NLS-1$ |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
if (monitor.isCanceled()) { |
| 93 |
request.cancel(); |
| 94 |
} |
| 95 |
} |
| 96 |
if (!request.isCanceled()) { |
| 97 |
targets = coalesce(targets); |
| 98 |
monitor.beginTask(getEnabledStateTaskName(), targets.length); |
| 99 |
try { |
| 100 |
boolean executable = isExecutable(targets, monitor, request); |
| 101 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 102 |
System.out.println(" >> " + executable); //$NON-NLS-1$ |
| 103 |
} |
| 104 |
request.setEnabled(executable); |
| 105 |
} catch (CoreException e) { |
| 106 |
request.setStatus(e.getStatus()); |
| 107 |
request.setEnabled(false); |
| 108 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 109 |
System.out.println(" >> ABORTED"); //$NON-NLS-1$ |
| 110 |
System.out.println("\t" + e.getStatus().getMessage()); //$NON-NLS-1$ |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
monitor.setCanceled(request.isCanceled()); |
| 115 |
request.done(); |
| 116 |
monitor.done(); |
| 117 |
return Status.OK_STATUS; |
| 118 |
} |
| 119 |
|
| 120 |
/* (non-Javadoc) |
| 121 |
* @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object) |
| 122 |
*/ |
| 123 |
public boolean belongsTo(Object family) { |
| 124 |
Object myFamily = getEnabledStateJobFamily(request); |
| 125 |
if (myFamily != null) { |
| 126 |
return myFamily.equals(family); |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
public void aboutToRun(IJobChangeEvent event) { |
| 132 |
} |
| 133 |
|
| 134 |
public void awake(IJobChangeEvent event) { |
| 135 |
} |
| 136 |
|
| 137 |
public void done(IJobChangeEvent event) { |
| 138 |
if (event.getJob() == this) { |
| 139 |
if (!run) { |
| 140 |
request.cancel(); |
| 141 |
request.done(); |
| 142 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 143 |
System.out.println(" >> *CANCELED* <<" + AbstractDebugCommand.this); //$NON-NLS-1$ |
| 144 |
} |
| 145 |
} |
| 146 |
getJobManager().removeJobChangeListener(this); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public void running(IJobChangeEvent event) { |
| 151 |
} |
| 152 |
|
| 153 |
public void scheduled(IJobChangeEvent event) { |
| 154 |
} |
| 155 |
|
| 156 |
public void sleeping(IJobChangeEvent event) { |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Scheduling rule to serialize commands on an object |
| 163 |
*/ |
| 164 |
private class SerialPerObjectRule implements ISchedulingRule { |
| 165 |
|
| 166 |
private Object fObject = null; |
| 167 |
|
| 168 |
public SerialPerObjectRule(Object lock) { |
| 169 |
fObject = lock; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* (non-Javadoc) |
| 174 |
* |
| 175 |
* @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule) |
| 176 |
*/ |
| 177 |
public boolean contains(ISchedulingRule rule) { |
| 178 |
return rule == this; |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* (non-Javadoc) |
| 183 |
* |
| 184 |
* @see org.eclipse.core.runtime.jobs.ISchedulingRule#isConflicting(org.eclipse.core.runtime.jobs.ISchedulingRule) |
| 185 |
*/ |
| 186 |
public boolean isConflicting(ISchedulingRule rule) { |
| 187 |
if (rule instanceof SerialPerObjectRule) { |
| 188 |
SerialPerObjectRule vup = (SerialPerObjectRule) rule; |
| 189 |
return fObject == vup.fObject; |
| 190 |
} |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/* (non-Javadoc) |
| 197 |
* @see org.eclipse.debug.core.commands.IDebugCommandHandler#execute(org.eclipse.debug.core.commands.IDebugCommandRequest) |
| 198 |
*/ |
| 199 |
public boolean execute(final IDebugCommandRequest request) { |
| 200 |
Job job = new Job(getExecuteTaskName()) { |
| 201 |
protected IStatus run(IProgressMonitor monitor) { |
| 202 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 203 |
System.out.println("execute: " + AbstractDebugCommand.this); //$NON-NLS-1$ |
| 204 |
} |
| 205 |
Object[] elements = request.getElements(); |
| 206 |
Object[] targets = new Object[elements.length]; |
| 207 |
for (int i = 0; i < elements.length; i++) { |
| 208 |
targets[i]= getTarget(elements[i]); |
| 209 |
} |
| 210 |
targets = coalesce(targets); |
| 211 |
monitor.beginTask(getExecuteTaskName(), targets.length); |
| 212 |
try { |
| 213 |
doExecute(targets, monitor, request); |
| 214 |
} catch (CoreException e) { |
| 215 |
request.setStatus(e.getStatus()); |
| 216 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 217 |
System.out.println("\t" + e.getStatus().getMessage()); //$NON-NLS-1$ |
| 218 |
} |
| 219 |
} |
| 220 |
request.done(); |
| 221 |
monitor.setCanceled(request.isCanceled()); |
| 222 |
monitor.done(); |
| 223 |
return Status.OK_STATUS; |
| 224 |
} |
| 225 |
public boolean belongsTo(Object family) { |
| 226 |
Object jobFamily = getExecuteJobFamily(request); |
| 227 |
if (jobFamily != null) { |
| 228 |
return jobFamily.equals(family); |
| 229 |
} |
| 230 |
return false; |
| 231 |
} |
| 232 |
}; |
| 233 |
job.setSystem(true); |
| 234 |
job.setRule(getExecuteSchedulingRule(request)); |
| 235 |
job.schedule(); |
| 236 |
return isRemainEnabled(request); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Returns whether this command should remain enabled after starting execution of the specified request. |
| 241 |
* |
| 242 |
* @param request the request being executed |
| 243 |
* @return whether to remain enabled while executing the request |
| 244 |
*/ |
| 245 |
protected boolean isRemainEnabled(IDebugCommandRequest request) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
/* (non-Javadoc) |
| 250 |
* @see org.eclipse.debug.core.commands.IDebugCommandHandler#canExecute(org.eclipse.debug.core.commands.IEnabledStateRequest) |
| 251 |
*/ |
| 252 |
public void canExecute(final IEnabledStateRequest request) { |
| 253 |
Job job = new UpdateJob(request); |
| 254 |
job.schedule(); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Returns the name to use for a job and progress monitor task names when performing |
| 259 |
* an {@link IEnabledStateRequest}. |
| 260 |
* |
| 261 |
* @return task name |
| 262 |
*/ |
| 263 |
protected String getEnabledStateTaskName() { |
| 264 |
// this is a system job name and does not need to be NLS'd |
| 265 |
return "Check Debug Command"; //$NON-NLS-1$ |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Returns the name to use for jobs and progress monitor task names when executing |
| 270 |
* an {@link IDebugCommandRequest}. |
| 271 |
* |
| 272 |
* @return task name |
| 273 |
*/ |
| 274 |
protected String getExecuteTaskName() { |
| 275 |
// this is a system job name and does not need to be NLS'd |
| 276 |
return "Execute Debug Command"; //$NON-NLS-1$ |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Executes this command synchronously on the specified targets, reporting progress. This method |
| 281 |
* is called by a job. If an exception is thrown, the calling job will set the associated status |
| 282 |
* on the request object. The calling job also calls #done() on the request object after this method |
| 283 |
* is called, and sets a cancel status on the progress monitor if the request is canceled. |
| 284 |
* <p> |
| 285 |
* Handlers must override this method. |
| 286 |
* </p> |
| 287 |
* @param targets objects to perform this command on |
| 288 |
* @param monitor progress monitor |
| 289 |
* @param request can be used to cancel this command |
| 290 |
* @exception CoreException if this handler fails to perform the request |
| 291 |
*/ |
| 292 |
protected abstract void doExecute(Object[] targets, IProgressMonitor monitor, IRequest request) throws CoreException; |
| 293 |
|
| 294 |
/** |
| 295 |
* Returns whether this command is executable on the specified targets, reporting progress. This method |
| 296 |
* is called by a job. If an exception is thrown, the calling job will set the associated status |
| 297 |
* on the request object and report that this command is not enabled. The calling job also calls #done() |
| 298 |
* on the request object after this method is called, and sets a cancel status on the progress monitor if |
| 299 |
* the request is canceled. Enabled state is set to <code>false</code> if the request is canceled. |
| 300 |
* <p> |
| 301 |
* Handlers must override this method. |
| 302 |
* </p> |
| 303 |
* @param targets objects to check command enabled state for |
| 304 |
* @param monitor progress monitor |
| 305 |
* @param request can be used to cancel this update request |
| 306 |
* @return whether this command can be executed for the given targets |
| 307 |
*/ |
| 308 |
protected abstract boolean isExecutable(Object[] targets, IProgressMonitor monitor, IEnabledStateRequest request) throws CoreException; |
| 309 |
|
| 310 |
/** |
| 311 |
* Returns the appropriate target for this command handler for the given object. |
| 312 |
* This method is called to map each element in a command request to the target |
| 313 |
* object that is used in {@link #doExecute(Object[], IProgressMonitor, IRequest)} |
| 314 |
* and {@link #isExecutable(Object[], IProgressMonitor, IEnabledStateRequest)}. |
| 315 |
* The target may be the element itself, or some other object. Allows for redirection. |
| 316 |
* <p> |
| 317 |
* Clients must override this method. |
| 318 |
* </p> |
| 319 |
* @param element element from a {@link IDebugCommandRequest} |
| 320 |
* @return associated target object for execution or enabled state update. Cannot return <code>null</code>. |
| 321 |
*/ |
| 322 |
protected abstract Object getTarget(Object element); |
| 323 |
|
| 324 |
/** |
| 325 |
* Convenience method to return an adapter of the specified type for the given object or <code>null</code> |
| 326 |
* if none. |
| 327 |
* |
| 328 |
* @param element element to retrieve adapter for |
| 329 |
* @param type adapter type |
| 330 |
* @return adapter or <code>null</code> |
| 331 |
* @see {@link DebugPlugin#getAdapter(Object, Class)} |
| 332 |
*/ |
| 333 |
protected Object getAdapter(Object element, Class type) { |
| 334 |
return DebugPlugin.getAdapter(element, type); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Returns a scheduling rule for this command's {@link IEnabledStateRequest} update job |
| 339 |
* or <code>null</code> if none. By default a rule is created to serialize |
| 340 |
* jobs on the first element in the request. |
| 341 |
* <p> |
| 342 |
* Clients may override this method as required. |
| 343 |
* </p> |
| 344 |
* @param request request that a scheduling rule is required for |
| 345 |
* @return scheduling rule or <code>null</code> |
| 346 |
*/ |
| 347 |
protected ISchedulingRule getEnabledStateSchedulingRule(IDebugCommandRequest request) { |
| 348 |
return new SerialPerObjectRule(request.getElements()[0]); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Returns a scheduling rule for this command's {@link IDebugCommandRequest} execute job |
| 353 |
* or <code>null</code> if none. By default, execution jobs have no scheduling rule. |
| 354 |
* <p> |
| 355 |
* Clients may override this method as required. |
| 356 |
* </p> |
| 357 |
* @param request request that a scheduling rule is required for |
| 358 |
* @return scheduling rule or <code>null</code> |
| 359 |
*/ |
| 360 |
protected ISchedulingRule getExecuteSchedulingRule(IDebugCommandRequest request) { |
| 361 |
return null; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Returns the job family for the this command's {@link IEnabledStateRequest} update job |
| 366 |
* or <code>null</code> if none. The default implementation returns <code>null</code>. |
| 367 |
* <p> |
| 368 |
* Clients may override this method as required. |
| 369 |
* </p> |
| 370 |
* @param request request the job family is required for |
| 371 |
* @return job family object or <code>null</code> if none |
| 372 |
*/ |
| 373 |
protected Object getEnabledStateJobFamily(IDebugCommandRequest request) { |
| 374 |
return null; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Returns the job family for the this command's {@link IDebugCommandRequest} execute job |
| 379 |
* or <code>null</code> if none. The default implementation returns <code>null</code>. |
| 380 |
* <p> |
| 381 |
* Clients may override this method as required. |
| 382 |
* </p> |
| 383 |
* @param request request the job family is required for |
| 384 |
* @return job family object or <code>null</code> if none |
| 385 |
*/ |
| 386 |
protected Object getExecuteJobFamily(IDebugCommandRequest request) { |
| 387 |
return null; |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Returns an array of objects with duplicates removed, if any. |
| 392 |
* |
| 393 |
* @param objects array of objects |
| 394 |
* @return array of object in same order with duplicates removed, if any. |
| 395 |
*/ |
| 396 |
private Object[] coalesce(Object[] objects) { |
| 397 |
if (objects.length == 1) { |
| 398 |
return objects; |
| 399 |
} else { |
| 400 |
LinkedHashSet set = new LinkedHashSet(objects.length); |
| 401 |
for (int i = 0; i < objects.length; i++) { |
| 402 |
set.add(objects[i]); |
| 403 |
} |
| 404 |
return set.toArray(); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
} |