1 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.ISchedulingRule; 20 import org.eclipse.core.runtime.jobs.Job; 21 import org.eclipse.debug.core.IRequest; 22 import org.eclipse.debug.core.commands.IDebugCommandHandler; 23 import org.eclipse.debug.core.commands.IDebugCommandRequest; 24 import org.eclipse.debug.core.commands.IEnabledStateRequest; 25 import org.eclipse.debug.internal.core.DebugOptions; 26 27 33 public abstract class DebugCommand implements IDebugCommandHandler { 34 35 38 class SerialPerObjectRule implements ISchedulingRule { 39 40 private Object fObject = null; 41 42 public SerialPerObjectRule(Object lock) { 43 fObject = lock; 44 } 45 46 51 public boolean contains(ISchedulingRule rule) { 52 return rule == this; 53 } 54 55 60 public boolean isConflicting(ISchedulingRule rule) { 61 if (rule instanceof SerialPerObjectRule) { 62 SerialPerObjectRule vup = (SerialPerObjectRule) rule; 63 return fObject == vup.fObject; 64 } 65 return false; 66 } 67 68 } 69 70 public boolean execute(final IDebugCommandRequest request) { 71 Job job = new Job(getExecuteTaskName()) { 72 protected IStatus run(IProgressMonitor monitor) { 73 if (DebugOptions.DEBUG_COMMANDS) { 74 System.out.println("execute: " + DebugCommand.this); } 76 Object [] elements = request.getElements(); 77 Object [] targets = new Object [elements.length]; 78 for (int i = 0; i < elements.length; i++) { 79 targets[i]= getTarget(elements[i]); 80 } 81 targets = coalesce(targets); 82 monitor.beginTask(getExecuteTaskName(), targets.length); 83 try { 84 doExecute(targets, monitor, request); 85 } catch (CoreException e) { 86 request.setStatus(e.getStatus()); 87 if (DebugOptions.DEBUG_COMMANDS) { 88 System.out.println("\t" + e.getStatus().getMessage()); } 90 } 91 request.done(); 92 monitor.setCanceled(request.isCanceled()); 93 monitor.done(); 94 return Status.OK_STATUS; 95 } 96 }; 97 job.setSystem(true); 98 job.schedule(); 99 return isRemainEnabled(); 100 } 101 102 107 protected boolean isRemainEnabled() { 108 return false; 109 } 110 111 public void canExecute(final IEnabledStateRequest request) { 112 Job job = new Job(getEnablementTaskName()) { 113 protected IStatus run(IProgressMonitor monitor) { 114 if (DebugOptions.DEBUG_COMMANDS) { 115 System.out.print("can execute command: " + DebugCommand.this); } 117 Object [] elements = request.getElements(); 118 Object [] targets = new Object [elements.length]; 119 for (int i = 0; i < elements.length; i++) { 120 targets[i] = getTarget(elements[i]); 121 if (targets[i] == null) { 122 request.setEnabled(false); 123 request.cancel(); 124 if (DebugOptions.DEBUG_COMMANDS) { 125 System.out.println(" >> false (no adapter)"); } 127 } 128 } 129 if (!request.isCanceled()) { 130 targets = coalesce(targets); 131 monitor.beginTask(getEnablementTaskName(), targets.length); 132 try { 133 boolean executable = isExecutable(targets, monitor, request); 134 if (DebugOptions.DEBUG_COMMANDS) { 135 System.out.println(" >> " + executable); } 137 request.setEnabled(executable); 138 } catch (CoreException e) { 139 request.setStatus(e.getStatus()); 140 request.setEnabled(false); 141 if (DebugOptions.DEBUG_COMMANDS) { 142 System.out.println(" >> ABORTED"); System.out.println("\t" + e.getStatus().getMessage()); } 145 } 146 } 147 monitor.setCanceled(request.isCanceled()); 148 request.done(); 149 monitor.done(); 150 return Status.OK_STATUS; 151 } 152 }; 153 job.setSystem(true); 154 job.setRule(createUpdateSchedulingRule(request)); 155 job.schedule(); 156 157 } 158 159 165 protected String getEnablementTaskName() { 166 return "Check Debug Command"; } 169 170 176 protected String getExecuteTaskName() { 177 return "Execute Debug Command"; } 180 181 187 protected abstract void doExecute(Object [] targets, IProgressMonitor monitor, IRequest request) throws CoreException; 188 189 197 protected abstract boolean isExecutable(Object [] targets, IProgressMonitor monitor, IEnabledStateRequest request) throws CoreException; 198 199 205 protected abstract Object getTarget(Object element); 206 207 212 protected ISchedulingRule createUpdateSchedulingRule(IDebugCommandRequest request) { 213 return new SerialPerObjectRule(request.getElements()[0]); 214 } 215 216 private Object [] coalesce(Object [] objects) { 217 if (objects.length == 1) { 218 return objects; 219 } else { 220 LinkedHashSet set = new LinkedHashSet (objects.length); 221 for (int i = 0; i < objects.length; i++) { 222 set.add(objects[i]); 223 } 224 return set.toArray(); 225 } 226 } 227 } 228 | Popular Tags |