|
Removed
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.internal.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.core.commands.IDebugCommandHandler; |
| 26 |
import org.eclipse.debug.core.commands.IDebugCommandRequest; |
| 27 |
import org.eclipse.debug.core.commands.IEnabledStateRequest; |
| 28 |
import org.eclipse.debug.internal.core.DebugOptions; |
| 29 |
|
| 30 |
/** |
| 31 |
* Common function for standard debug commands. |
| 32 |
* |
| 33 |
* @since 3.3 |
| 34 |
* |
| 35 |
*/ |
| 36 |
public abstract class DebugCommand implements IDebugCommandHandler { |
| 37 |
|
| 38 |
/** |
| 39 |
* Job to update enabled state of action. |
| 40 |
*/ |
| 41 |
class UpdateJob extends Job implements IJobChangeListener { |
| 42 |
|
| 43 |
/** |
| 44 |
* The request to update |
| 45 |
*/ |
| 46 |
private IEnabledStateRequest request; |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether this job has been run |
| 50 |
*/ |
| 51 |
private boolean run = false; |
| 52 |
|
| 53 |
/** |
| 54 |
* Creates a new job to update the specified request |
| 55 |
* |
| 56 |
* @param stateRequest |
| 57 |
*/ |
| 58 |
UpdateJob(IEnabledStateRequest stateRequest) { |
| 59 |
super(getEnablementTaskName()); |
| 60 |
request = stateRequest; |
| 61 |
setSystem(true); |
| 62 |
setRule(createUpdateSchedulingRule(request)); |
| 63 |
getJobManager().addJobChangeListener(this); |
| 64 |
} |
| 65 |
|
| 66 |
protected IStatus run(IProgressMonitor monitor) { |
| 67 |
run = true; |
| 68 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 69 |
System.out.print("can execute command: " + DebugCommand.this); //$NON-NLS-1$ |
| 70 |
} |
| 71 |
if (monitor.isCanceled()) { |
| 72 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 73 |
System.out.println(" >> *CANCELED* <<"); //$NON-NLS-1$ |
| 74 |
} |
| 75 |
request.cancel(); |
| 76 |
} |
| 77 |
Object[] elements = request.getElements(); |
| 78 |
Object[] targets = new Object[elements.length]; |
| 79 |
if (!request.isCanceled()) { |
| 80 |
for (int i = 0; i < elements.length; i++) { |
| 81 |
targets[i] = getTarget(elements[i]); |
| 82 |
if (targets[i] == null) { |
| 83 |
request.setEnabled(false); |
| 84 |
request.cancel(); |
| 85 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 86 |
System.out.println(" >> false (no adapter)"); //$NON-NLS-1$ |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
if (monitor.isCanceled()) { |
| 91 |
request.cancel(); |
| 92 |
} |
| 93 |
} |
| 94 |
if (!request.isCanceled()) { |
| 95 |
targets = coalesce(targets); |
| 96 |
monitor.beginTask(getEnablementTaskName(), targets.length); |
| 97 |
try { |
| 98 |
boolean executable = isExecutable(targets, monitor, request); |
| 99 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 100 |
System.out.println(" >> " + executable); //$NON-NLS-1$ |
| 101 |
} |
| 102 |
request.setEnabled(executable); |
| 103 |
} catch (CoreException e) { |
| 104 |
request.setStatus(e.getStatus()); |
| 105 |
request.setEnabled(false); |
| 106 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 107 |
System.out.println(" >> ABORTED"); //$NON-NLS-1$ |
| 108 |
System.out.println("\t" + e.getStatus().getMessage()); //$NON-NLS-1$ |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
monitor.setCanceled(request.isCanceled()); |
| 113 |
request.done(); |
| 114 |
monitor.done(); |
| 115 |
return Status.OK_STATUS; |
| 116 |
} |
| 117 |
|
| 118 |
/* (non-Javadoc) |
| 119 |
* @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object) |
| 120 |
*/ |
| 121 |
public boolean belongsTo(Object family) { |
| 122 |
return getUpdateJobFamily().equals(family); |
| 123 |
} |
| 124 |
|
| 125 |
public void aboutToRun(IJobChangeEvent event) { |
| 126 |
} |
| 127 |
|
| 128 |
public void awake(IJobChangeEvent event) { |
| 129 |
} |
| 130 |
|
| 131 |
public void done(IJobChangeEvent event) { |
| 132 |
if (event.getJob() == this) { |
| 133 |
if (!run) { |
| 134 |
request.cancel(); |
| 135 |
request.done(); |
| 136 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 137 |
System.out.println(" >> *CANCELED* <<" + DebugCommand.this); //$NON-NLS-1$ |
| 138 |
} |
| 139 |
} |
| 140 |
getJobManager().removeJobChangeListener(this); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public void running(IJobChangeEvent event) { |
| 145 |
} |
| 146 |
|
| 147 |
public void scheduled(IJobChangeEvent event) { |
| 148 |
} |
| 149 |
|
| 150 |
public void sleeping(IJobChangeEvent event) { |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Scheduling rule to serialize commands on an object |
| 157 |
*/ |
| 158 |
class SerialPerObjectRule implements ISchedulingRule { |
| 159 |
|
| 160 |
private Object fObject = null; |
| 161 |
|
| 162 |
public SerialPerObjectRule(Object lock) { |
| 163 |
fObject = lock; |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* (non-Javadoc) |
| 168 |
* |
| 169 |
* @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule) |
| 170 |
*/ |
| 171 |
public boolean contains(ISchedulingRule rule) { |
| 172 |
return rule == this; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* (non-Javadoc) |
| 177 |
* |
| 178 |
* @see org.eclipse.core.runtime.jobs.ISchedulingRule#isConflicting(org.eclipse.core.runtime.jobs.ISchedulingRule) |
| 179 |
*/ |
| 180 |
public boolean isConflicting(ISchedulingRule rule) { |
| 181 |
if (rule instanceof SerialPerObjectRule) { |
| 182 |
SerialPerObjectRule vup = (SerialPerObjectRule) rule; |
| 183 |
return fObject == vup.fObject; |
| 184 |
} |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
public boolean execute(final IDebugCommandRequest request) { |
| 191 |
Job job = new Job(getExecuteTaskName()) { |
| 192 |
protected IStatus run(IProgressMonitor monitor) { |
| 193 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 194 |
System.out.println("execute: " + DebugCommand.this); //$NON-NLS-1$ |
| 195 |
} |
| 196 |
Object[] elements = request.getElements(); |
| 197 |
Object[] targets = new Object[elements.length]; |
| 198 |
for (int i = 0; i < elements.length; i++) { |
| 199 |
targets[i]= getTarget(elements[i]); |
| 200 |
} |
| 201 |
targets = coalesce(targets); |
| 202 |
monitor.beginTask(getExecuteTaskName(), targets.length); |
| 203 |
try { |
| 204 |
doExecute(targets, monitor, request); |
| 205 |
} catch (CoreException e) { |
| 206 |
request.setStatus(e.getStatus()); |
| 207 |
if (DebugOptions.DEBUG_COMMANDS) { |
| 208 |
System.out.println("\t" + e.getStatus().getMessage()); //$NON-NLS-1$ |
| 209 |
} |
| 210 |
} |
| 211 |
request.done(); |
| 212 |
monitor.setCanceled(request.isCanceled()); |
| 213 |
monitor.done(); |
| 214 |
return Status.OK_STATUS; |
| 215 |
} |
| 216 |
}; |
| 217 |
job.setSystem(true); |
| 218 |
job.schedule(); |
| 219 |
return isRemainEnabled(); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns whether this command should remain enabled after execution is invoked. |
| 224 |
* |
| 225 |
* @return whether to remain enabled |
| 226 |
*/ |
| 227 |
protected boolean isRemainEnabled() { |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
public void canExecute(final IEnabledStateRequest request) { |
| 232 |
Job job = new UpdateJob(request); |
| 233 |
job.schedule(); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns the name to use for jobs and progress monitor task names when checking |
| 238 |
* enabled state. |
| 239 |
* |
| 240 |
* @return task name |
| 241 |
*/ |
| 242 |
protected String getEnablementTaskName() { |
| 243 |
// this is a system job name and does not need to be NLS'd |
| 244 |
return "Check Debug Command"; //$NON-NLS-1$ |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns the name to use for jobs and progress monitor task names when executing |
| 249 |
* a debug command |
| 250 |
* |
| 251 |
* @return task name |
| 252 |
*/ |
| 253 |
protected String getExecuteTaskName() { |
| 254 |
// this is a system job name and does not need to be NLS'd |
| 255 |
return "Execute Debug Command"; //$NON-NLS-1$ |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Executes the actual operation. |
| 260 |
* |
| 261 |
* @param targets objects to perform on |
| 262 |
* @param request request |
| 263 |
*/ |
| 264 |
protected abstract void doExecute(Object[] targets, IProgressMonitor monitor, IRequest request) throws CoreException; |
| 265 |
|
| 266 |
/** |
| 267 |
* Returns whether this command is executable. |
| 268 |
* |
| 269 |
* @param targets objects to check command for |
| 270 |
* @param monitor progress monitor |
| 271 |
* @param request request |
| 272 |
* @return whether this command can be executed |
| 273 |
*/ |
| 274 |
protected abstract boolean isExecutable(Object[] targets, IProgressMonitor monitor, IEnabledStateRequest request) throws CoreException; |
| 275 |
|
| 276 |
/** |
| 277 |
* Returns the appropriate command adapter from the given object. |
| 278 |
* |
| 279 |
* @param element object to obtain adapter from |
| 280 |
* @return adapter |
| 281 |
*/ |
| 282 |
protected abstract Object getTarget(Object element); |
| 283 |
|
| 284 |
/** |
| 285 |
* Returns an adapter of the specified type for the given object or <code>null</code> |
| 286 |
* if none. The object itself is returned if it is an instance of the specified type. |
| 287 |
* |
| 288 |
* @param element element to retrieve adapter for |
| 289 |
* @param type adapter type |
| 290 |
* @return adapter or <code>null</code> |
| 291 |
*/ |
| 292 |
protected Object getAdapter(Object element, Class type) { |
| 293 |
return DebugPlugin.getAdapter(element, type); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Scheduling rule for updating command enabled state. |
| 298 |
* |
| 299 |
* @return scheduling rule or <code>null</code> |
| 300 |
*/ |
| 301 |
protected ISchedulingRule createUpdateSchedulingRule(IDebugCommandRequest request) { |
| 302 |
return new SerialPerObjectRule(request.getElements()[0]); |
| 303 |
} |
| 304 |
|
| 305 |
private Object[] coalesce(Object[] objects) { |
| 306 |
if (objects.length == 1) { |
| 307 |
return objects; |
| 308 |
} else { |
| 309 |
LinkedHashSet set = new LinkedHashSet(objects.length); |
| 310 |
for (int i = 0; i < objects.length; i++) { |
| 311 |
set.add(objects[i]); |
| 312 |
} |
| 313 |
return set.toArray(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Returns the job family for this command's "can execute" job. |
| 319 |
* |
| 320 |
* @return the job family for this command's "can execute" job |
| 321 |
*/ |
| 322 |
protected abstract Object getUpdateJobFamily(); |
| 323 |
|
| 324 |
} |