1 11 package org.eclipse.debug.core.model; 12 13 import com.ibm.icu.text.MessageFormat; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.resources.IMarker; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IWorkspace; 24 import org.eclipse.core.resources.IncrementalProjectBuilder; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.Status; 30 import org.eclipse.debug.core.DebugPlugin; 31 import org.eclipse.debug.core.IBreakpointManager; 32 import org.eclipse.debug.core.ILaunch; 33 import org.eclipse.debug.core.ILaunchConfiguration; 34 import org.eclipse.debug.core.ILaunchManager; 35 import org.eclipse.debug.core.IStatusHandler; 36 import org.eclipse.debug.internal.core.DebugCoreMessages; 37 38 50 public abstract class LaunchConfigurationDelegate implements ILaunchConfigurationDelegate2 { 51 52 57 private static final String DEBUG_CORE = "org.eclipse.debug.core"; 59 64 private static final String DEBUG_UI = "org.eclipse.debug.ui"; 66 71 private static final String EMPTY_STRING = ""; 73 76 protected static final IStatus promptStatus = new Status(IStatus.INFO, DEBUG_UI, 200, EMPTY_STRING, null); 77 78 82 protected static final IStatus switchToDebugPromptStatus = new Status(IStatus.INFO, DEBUG_CORE, 201, EMPTY_STRING, null); 83 84 88 protected static final IStatus complileErrorPromptStatus = new Status(IStatus.INFO, DEBUG_CORE, 202, EMPTY_STRING, null); 89 90 96 protected static final IStatus saveScopedDirtyEditors = new Status(IStatus.INFO, DEBUG_CORE, 222, EMPTY_STRING, null); 97 98 106 protected static final IStatus complileErrorProjectPromptStatus = new Status(IStatus.INFO, DEBUG_CORE, 203, EMPTY_STRING, null); 107 108 111 public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException { 112 return null; 113 } 114 115 118 public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { 119 IProject[] projects = getBuildOrder(configuration, mode); 120 if (projects == null) { 121 return true; 122 } 123 buildProjects(projects, monitor); 124 return false; 125 } 126 127 137 protected IProject[] getBuildOrder(ILaunchConfiguration configuration, String mode) throws CoreException { 138 return null; 139 } 140 141 150 protected IProject[] getProjectsForProblemSearch(ILaunchConfiguration configuration, String mode) throws CoreException { 151 return null; 152 } 153 154 157 public boolean finalLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { 158 IProject[] projects = getProjectsForProblemSearch(configuration, mode); 159 if (projects == null) { 160 return true; } 162 boolean continueLaunch = true; 163 164 monitor.subTask(DebugCoreMessages.LaunchConfigurationDelegate_6); 165 List errors = new ArrayList (); 166 for (int i = 0; i < projects.length; i++) { 167 monitor.subTask(MessageFormat.format(DebugCoreMessages.LaunchConfigurationDelegate_7, new String []{projects[i].getName()})); 168 if (existsProblems(projects[i])) { 169 errors.add(projects[i]); 170 } 171 } 172 if (!errors.isEmpty()) { 173 errors.add(0, configuration); 174 IStatusHandler prompter = DebugPlugin.getDefault().getStatusHandler(promptStatus); 175 if (prompter != null) { 176 continueLaunch = ((Boolean ) prompter.handleStatus(complileErrorProjectPromptStatus, errors)).booleanValue(); 177 } 178 } 179 180 return continueLaunch; 181 } 182 183 194 public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { 195 if (!saveBeforeLaunch(configuration, mode, monitor)) { 196 return false; 197 } 198 if (mode.equals(ILaunchManager.RUN_MODE) && configuration.supportsMode(ILaunchManager.DEBUG_MODE)) { 199 IBreakpoint[] breakpoints= getBreakpoints(configuration); 200 if (breakpoints == null) { 201 return true; 202 } 203 for (int i = 0; i < breakpoints.length; i++) { 204 if (breakpoints[i].isEnabled()) { 205 IStatusHandler prompter = DebugPlugin.getDefault().getStatusHandler(promptStatus); 206 if (prompter != null) { 207 boolean launchInDebugModeInstead = ((Boolean )prompter.handleStatus(switchToDebugPromptStatus, configuration)).booleanValue(); 208 if (launchInDebugModeInstead) { 209 return false; } 211 } 212 return true; 214 } 215 } 216 } 217 return true; 219 } 220 221 236 protected boolean saveBeforeLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { 237 IStatusHandler prompter = DebugPlugin.getDefault().getStatusHandler(promptStatus); 238 if(prompter != null) { 239 IProject[] buildOrder = getBuildOrder(configuration, mode); 241 if(!((Boolean )prompter.handleStatus(saveScopedDirtyEditors, new Object []{configuration, buildOrder})).booleanValue()) { 242 return false; 243 } 244 } 245 return true; 246 } 247 248 255 protected IBreakpoint[] getBreakpoints(ILaunchConfiguration configuration) { 256 IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager(); 257 if (!breakpointManager.isEnabled()) { 258 return null; 260 } 261 return breakpointManager.getBreakpoints(); 262 } 263 264 275 protected IProject[] computeReferencedBuildOrder(IProject[] baseProjects) throws CoreException { 276 HashSet unorderedProjects = new HashSet (); 277 for(int i = 0; i< baseProjects.length; i++) { 278 unorderedProjects.add(baseProjects[i]); 279 addReferencedProjects(baseProjects[i], unorderedProjects); 280 } 281 IProject[] projectSet = (IProject[]) unorderedProjects.toArray(new IProject[unorderedProjects.size()]); 282 return computeBuildOrder(projectSet); 283 } 284 285 286 295 protected void addReferencedProjects(IProject project, Set references) throws CoreException{ 296 if (project.isOpen()) { 297 IProject[] projects = project.getReferencedProjects(); 298 for (int i = 0; i < projects.length; i++) { 299 IProject refProject= projects[i]; 300 if (refProject.exists() && !references.contains(refProject)) { 301 references.add(refProject); 302 addReferencedProjects(refProject, references); 303 } 304 } 305 } 306 } 307 308 316 protected IProject[] computeBuildOrder(IProject[] projects) { 317 String [] orderedNames = ResourcesPlugin.getWorkspace().getDescription().getBuildOrder(); 318 if (orderedNames != null) { 319 List orderedProjects = new ArrayList (projects.length); 320 List unorderedProjects = new ArrayList (projects.length); 322 for(int i = 0; i < projects.length; ++i) { 323 unorderedProjects.add(projects[i]); 324 } 325 326 for (int i = 0; i < orderedNames.length; i++) { 327 String projectName = orderedNames[i]; 328 for (Iterator iterator = unorderedProjects.iterator(); iterator.hasNext(); ) { 329 IProject project = (IProject)iterator.next(); 330 if (project.getName().equals(projectName)) { 331 orderedProjects.add(project); 332 iterator.remove(); 333 break; 334 } 335 } 336 } 337 orderedProjects.addAll(unorderedProjects); 339 return (IProject[]) orderedProjects.toArray(new IProject[orderedProjects.size()]); 340 } 341 342 IWorkspace.ProjectOrder po = ResourcesPlugin.getWorkspace().computeProjectOrder(projects); 344 return po.projects; 345 } 346 347 357 protected boolean existsProblems(IProject proj) throws CoreException { 358 IMarker[] markers = proj.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); 359 if (markers.length > 0) { 360 for (int i = 0; i < markers.length; i++) { 361 if (isLaunchProblem(markers[i])) { 362 return true; 363 } 364 } 365 } 366 return false; 367 } 368 369 379 protected boolean isLaunchProblem(IMarker problemMarker) throws CoreException { 380 Integer severity = (Integer )problemMarker.getAttribute(IMarker.SEVERITY); 381 if (severity != null) { 382 return severity.intValue() >= IMarker.SEVERITY_ERROR; 383 } 384 385 return false; 386 } 387 388 395 protected void buildProjects(IProject[] projects, IProgressMonitor monitor) throws CoreException { 396 for (int i = 0; i < projects.length; i++ ) { 397 projects[i].build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor); 398 } 399 } 400 } 401 | Popular Tags |