1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.resources.ISaveContext; 23 import org.eclipse.core.resources.ISaveParticipant; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IAdapterFactory; 28 import org.eclipse.core.runtime.IAdapterManager; 29 import org.eclipse.core.runtime.IConfigurationElement; 30 import org.eclipse.core.runtime.IExtensionPoint; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 import org.eclipse.core.runtime.IStatus; 33 import org.eclipse.core.runtime.Platform; 34 import org.eclipse.core.runtime.Status; 35 import org.eclipse.debug.ui.DebugUITools; 36 import org.eclipse.debug.ui.IDebugModelPresentation; 37 import org.eclipse.debug.ui.ILaunchConfigurationTab; 38 import org.eclipse.jdt.core.IJavaModel; 39 import org.eclipse.jdt.core.IJavaProject; 40 import org.eclipse.jdt.core.IMember; 41 import org.eclipse.jdt.core.IPackageFragment; 42 import org.eclipse.jdt.core.JavaCore; 43 import org.eclipse.jdt.core.JavaModelException; 44 import org.eclipse.jdt.debug.core.IJavaBreakpoint; 45 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 46 import org.eclipse.jdt.debug.core.IJavaHotCodeReplaceListener; 47 import org.eclipse.jdt.debug.core.IJavaStackFrame; 48 import org.eclipse.jdt.debug.core.IJavaThread; 49 import org.eclipse.jdt.debug.core.IJavaThreadGroup; 50 import org.eclipse.jdt.debug.core.IJavaVariable; 51 import org.eclipse.jdt.debug.core.JDIDebugModel; 52 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 53 import org.eclipse.jdt.internal.debug.ui.breakpoints.ExceptionInspector; 54 import org.eclipse.jdt.internal.debug.ui.breakpoints.JavaBreakpointTypeAdapterFactory; 55 import org.eclipse.jdt.internal.debug.ui.classpath.ClasspathEntryAdapterFactory; 56 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression; 57 import org.eclipse.jdt.internal.debug.ui.monitors.JavaContendedMonitor; 58 import org.eclipse.jdt.internal.debug.ui.monitors.JavaOwnedMonitor; 59 import org.eclipse.jdt.internal.debug.ui.monitors.JavaOwningThread; 60 import org.eclipse.jdt.internal.debug.ui.monitors.JavaWaitingThread; 61 import org.eclipse.jdt.internal.debug.ui.monitors.MonitorsAdapterFactory; 62 import org.eclipse.jdt.internal.debug.ui.snippeteditor.SnippetFileDocumentProvider; 63 import org.eclipse.jdt.internal.debug.ui.sourcelookup.JavaDebugShowInAdapterFactory; 64 import org.eclipse.jdt.internal.debug.ui.threadgroups.TargetAdapterFactory; 65 import org.eclipse.jdt.internal.debug.ui.threadgroups.ThreadGroupAdapterFactory; 66 import org.eclipse.jdt.internal.debug.ui.variables.ColumnPresentationAdapterFactory; 67 import org.eclipse.jdt.internal.debug.ui.variables.JavaDebugElementAdapterFactory; 68 import org.eclipse.jdt.internal.launching.DefaultProjectClasspathEntry; 69 import org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation; 70 import org.eclipse.jdt.ui.JavaElementLabelProvider; 71 import org.eclipse.jdt.ui.PreferenceConstants; 72 import org.eclipse.jdt.ui.text.JavaTextTools; 73 import org.eclipse.jface.dialogs.ErrorDialog; 74 import org.eclipse.jface.dialogs.MessageDialog; 75 import org.eclipse.jface.operation.IRunnableWithProgress; 76 import org.eclipse.jface.preference.IPreferenceNode; 77 import org.eclipse.jface.preference.IPreferencePage; 78 import org.eclipse.jface.preference.PreferenceDialog; 79 import org.eclipse.jface.preference.PreferenceManager; 80 import org.eclipse.jface.preference.PreferenceNode; 81 import org.eclipse.jface.resource.ImageRegistry; 82 import org.eclipse.jface.window.Window; 83 import org.eclipse.swt.custom.BusyIndicator; 84 import org.eclipse.swt.widgets.Display; 85 import org.eclipse.swt.widgets.Shell; 86 import org.eclipse.ui.IWorkbenchPage; 87 import org.eclipse.ui.IWorkbenchWindow; 88 import org.eclipse.ui.PlatformUI; 89 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 90 import org.eclipse.ui.dialogs.PreferencesUtil; 91 import org.eclipse.ui.plugin.AbstractUIPlugin; 92 import org.eclipse.ui.texteditor.IDocumentProvider; 93 import org.osgi.framework.Bundle; 94 import org.osgi.framework.BundleContext; 95 96 99 public class JDIDebugUIPlugin extends AbstractUIPlugin { 100 101 105 private static final String PI_JDI_DEBUG = "org.eclipse.jdt.debug.ui"; 107 112 public static final String COMMAND_INSPECT = "org.eclipse.jdt.debug.ui.commands.Inspect"; 114 117 private static JDIDebugUIPlugin fgPlugin; 118 119 private IDocumentProvider fSnippetDocumentProvider; 120 121 private ImageDescriptorRegistry fImageDescriptorRegistry; 122 123 private ActionFilterAdapterFactory fActionFilterAdapterFactory; 124 private JavaSourceLocationWorkbenchAdapterFactory fSourceLocationAdapterFactory; 125 private JavaBreakpointWorkbenchAdapterFactory fBreakpointAdapterFactory; 126 127 private IDebugModelPresentation fUtilPresentation; 128 129 132 private IJavaHotCodeReplaceListener fHCRListener; 133 134 protected Map fVmInstallTypePageMap; 136 137 141 private boolean fShuttingDown= false; 142 143 146 private JavaTextTools fTextTools = null; 147 148 151 public JDIDebugUIPlugin() { 152 super(); 153 setDefault(this); 154 } 155 156 161 private static void setDefault(JDIDebugUIPlugin plugin) { 162 fgPlugin = plugin; 163 } 164 165 170 public static JDIDebugUIPlugin getDefault() { 171 return fgPlugin; 172 } 173 174 177 public static String getUniqueIdentifier() { 178 return PI_JDI_DEBUG; 179 } 180 181 186 public static void log(IStatus status) { 187 getDefault().getLog().log(status); 188 } 189 190 195 public static void logErrorMessage(String message) { 196 log(new Status(IStatus.ERROR, getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, message, null)); 197 } 198 199 204 public static void log(Throwable e) { 205 if (e instanceof CoreException) { 206 log(new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.ERROR, e.getMessage(), e.getCause())); 207 } else { 208 log(new Status(IStatus.ERROR, getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, "Internal Error", e)); } 210 } 211 212 217 public static IWorkbenchWindow getActiveWorkbenchWindow() { 218 return getDefault().getWorkbench().getActiveWorkbenchWindow(); 219 } 220 221 public static IWorkbenchPage getActivePage() { 222 IWorkbenchWindow w = getActiveWorkbenchWindow(); 223 if (w != null) { 224 return w.getActivePage(); 225 } 226 return null; 227 } 228 229 230 235 public static Shell getActiveWorkbenchShell() { 236 IWorkbenchWindow window = getActiveWorkbenchWindow(); 237 if (window != null) { 238 return window.getShell(); 239 } 240 return null; 241 } 242 243 246 protected ImageRegistry createImageRegistry() { 247 return JavaDebugImages.getImageRegistry(); 248 } 249 250 public IDocumentProvider getSnippetDocumentProvider() { 251 if (fSnippetDocumentProvider == null) { 252 fSnippetDocumentProvider= new SnippetFileDocumentProvider(); 253 } 254 return fSnippetDocumentProvider; 255 } 256 257 public static void statusDialog(IStatus status) { 258 switch (status.getSeverity()) { 259 case IStatus.ERROR: 260 statusDialog(DebugUIMessages.JDIDebugUIPlugin_Error_1, status); 261 break; 262 case IStatus.WARNING: 263 statusDialog(DebugUIMessages.JDIDebugUIPlugin_0, status); 264 break; 265 case IStatus.INFO: 266 statusDialog(DebugUIMessages.JDIDebugUIPlugin_4, status); 267 break; 268 } 269 } 270 public static void statusDialog(String title, IStatus status) { 271 Shell shell = getActiveWorkbenchShell(); 272 if (shell != null) { 273 switch (status.getSeverity()) { 274 case IStatus.ERROR: 275 ErrorDialog.openError(shell, title, null, status); 276 break; 277 case IStatus.WARNING: 278 MessageDialog.openWarning(shell, title, status.getMessage()); 279 break; 280 case IStatus.INFO: 281 MessageDialog.openInformation(shell, title, status.getMessage()); 282 break; 283 } 284 } 285 } 286 287 290 public static void errorDialog(String message, Throwable t) { 291 IStatus status= new Status(IStatus.ERROR, getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, message, t); 292 statusDialog(status); 293 } 294 295 304 public static Object createExtension(final IConfigurationElement element, final String classAttribute) throws CoreException { 305 Bundle bundle = Platform.getBundle(element.getContributor().getName()); 308 if (bundle.getState() == Bundle.ACTIVE) { 309 return element.createExecutableExtension(classAttribute); 310 } 311 312 final Object [] ret = new Object [1]; 313 final CoreException [] exc = new CoreException[1]; 314 BusyIndicator.showWhile(null, new Runnable () { 315 public void run() { 316 try { 317 ret[0] = element.createExecutableExtension(classAttribute); 318 } catch (CoreException e) { 319 exc[0] = e; 320 } 321 } 322 }); 323 if (exc[0] != null) { 324 throw exc[0]; 325 } 326 return ret[0]; 327 } 328 329 332 public void start(BundleContext context) throws Exception { 333 super.start(context); 334 JavaDebugOptionsManager.getDefault().startup(); 335 ResourcesPlugin.getWorkspace().addSaveParticipant(this, new ISaveParticipant() { 336 public void doneSaving(ISaveContext context) {} 337 public void prepareToSave(ISaveContext context) throws CoreException {} 338 public void rollback(ISaveContext context) {} 339 public void saving(ISaveContext context) throws CoreException { 340 savePluginPreferences(); 341 } 342 }); 343 IAdapterManager manager= Platform.getAdapterManager(); 344 fActionFilterAdapterFactory= new ActionFilterAdapterFactory(); 345 manager.registerAdapters(fActionFilterAdapterFactory, IMember.class); 346 manager.registerAdapters(fActionFilterAdapterFactory, IJavaVariable.class); 347 manager.registerAdapters(fActionFilterAdapterFactory, IJavaStackFrame.class); 348 manager.registerAdapters(fActionFilterAdapterFactory, IJavaThread.class); 349 manager.registerAdapters(fActionFilterAdapterFactory, JavaInspectExpression.class); 350 fSourceLocationAdapterFactory = new JavaSourceLocationWorkbenchAdapterFactory(); 351 manager.registerAdapters(fSourceLocationAdapterFactory, IJavaSourceLocation.class); 352 fBreakpointAdapterFactory= new JavaBreakpointWorkbenchAdapterFactory(); 353 manager.registerAdapters(fBreakpointAdapterFactory, IJavaBreakpoint.class); 354 IAdapterFactory typeFactory = new JavaBreakpointTypeAdapterFactory(); 355 manager.registerAdapters(typeFactory, IJavaBreakpoint.class); 356 357 IAdapterFactory monitorFactory = new MonitorsAdapterFactory(); 358 manager.registerAdapters(monitorFactory, IJavaThread.class); 359 manager.registerAdapters(monitorFactory, JavaContendedMonitor.class); 360 manager.registerAdapters(monitorFactory, JavaOwnedMonitor.class); 361 manager.registerAdapters(monitorFactory, JavaOwningThread.class); 362 manager.registerAdapters(monitorFactory, JavaWaitingThread.class); 363 manager.registerAdapters(monitorFactory, IJavaStackFrame.class); 364 365 IAdapterFactory targetFactory = new TargetAdapterFactory(); 366 manager.registerAdapters(targetFactory, IJavaDebugTarget.class); 367 368 IAdapterFactory groupFactory = new ThreadGroupAdapterFactory(); 369 manager.registerAdapters(groupFactory, IJavaThreadGroup.class); 370 371 IAdapterFactory showInFactory = new JavaDebugShowInAdapterFactory(); 372 manager.registerAdapters(showInFactory, IJavaStackFrame.class); 373 374 IAdapterFactory columnFactory = new ColumnPresentationAdapterFactory(); 375 manager.registerAdapters(columnFactory, IJavaVariable.class); 376 manager.registerAdapters(columnFactory, IJavaStackFrame.class); 377 378 IAdapterFactory entryFactory = new ClasspathEntryAdapterFactory(); 379 manager.registerAdapters(entryFactory, DefaultProjectClasspathEntry.class); 380 381 IAdapterFactory variableFactory = new JavaDebugElementAdapterFactory(); 382 manager.registerAdapters(variableFactory, IJavaVariable.class); 383 manager.registerAdapters(variableFactory, JavaInspectExpression.class); 384 385 fHCRListener= new JavaHotCodeReplaceListener(); 386 JDIDebugModel.addHotCodeReplaceListener(fHCRListener); 387 388 389 new ExceptionInspector(); 391 } 392 393 396 public void stop(BundleContext context) throws Exception { 397 try { 398 setShuttingDown(true); 399 JDIDebugModel.removeHotCodeReplaceListener(fHCRListener); 400 JavaDebugOptionsManager.getDefault().shutdown(); 401 if (fImageDescriptorRegistry != null) { 402 fImageDescriptorRegistry.dispose(); 403 } 404 IAdapterManager manager= Platform.getAdapterManager(); 405 manager.unregisterAdapters(fActionFilterAdapterFactory); 406 manager.unregisterAdapters(fSourceLocationAdapterFactory); 407 manager.unregisterAdapters(fBreakpointAdapterFactory); 408 if (fUtilPresentation != null) { 409 fUtilPresentation.dispose(); 410 } 411 if (fTextTools != null) { 412 fTextTools.dispose(); 413 } 414 ResourcesPlugin.getWorkspace().removeSaveParticipant(this); 415 } finally { 416 super.stop(context); 417 } 418 } 419 420 427 protected boolean isShuttingDown() { 428 return fShuttingDown; 429 } 430 431 438 private void setShuttingDown(boolean value) { 439 fShuttingDown = value; 440 } 441 442 445 public static ImageDescriptorRegistry getImageDescriptorRegistry() { 446 if (getDefault().fImageDescriptorRegistry == null) { 447 getDefault().fImageDescriptorRegistry = new ImageDescriptorRegistry(); 448 } 449 return getDefault().fImageDescriptorRegistry; 450 } 451 452 457 public static Display getStandardDisplay() { 458 Display display; 459 display= Display.getCurrent(); 460 if (display == null) 461 display= Display.getDefault(); 462 return display; 463 } 464 465 471 public static Shell getShell() { 472 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 473 if (window == null) { 474 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); 475 if (windows.length > 0) { 476 return windows[0].getShell(); 477 } 478 } 479 else { 480 return window.getShell(); 481 } 482 return null; 483 } 484 485 492 public static ElementListSelectionDialog createAllPackagesDialog(Shell shell, IJavaProject[] originals, final boolean includeDefaultPackage) throws JavaModelException{ 493 final List packageList = new ArrayList (); 494 if (originals == null) { 495 IWorkspaceRoot wsroot= ResourcesPlugin.getWorkspace().getRoot(); 496 IJavaModel model= JavaCore.create(wsroot); 497 originals= model.getJavaProjects(); 498 } 499 final IJavaProject[] projects= originals; 500 final JavaModelException[] exception= new JavaModelException[1]; 501 final boolean[] monitorCanceled = new boolean[] {false}; 502 IRunnableWithProgress r= new IRunnableWithProgress() { 503 public void run(IProgressMonitor monitor) { 504 try { 505 Set packageNameSet= new HashSet (); 506 monitor.beginTask(DebugUIMessages.JDIDebugUIPlugin_Searching_1, projects.length); 507 for (int i = 0; i < projects.length; i++) { 508 IPackageFragment[] pkgs= projects[i].getPackageFragments(); 509 for (int j = 0; j < pkgs.length; j++) { 510 if (monitor.isCanceled()) { 511 monitorCanceled[0] = true; 512 return; 513 } 514 IPackageFragment pkg = pkgs[j]; 515 if (!pkg.hasChildren() && (pkg.getNonJavaResources().length > 0)) { 516 continue; 517 } 518 String pkgName= pkg.getElementName(); 519 if (!includeDefaultPackage && pkgName.length() == 0) { 520 continue; 521 } 522 if (packageNameSet.add(pkgName)) { 523 packageList.add(pkg); 524 } 525 } 526 monitor.worked(1); 527 } 528 monitor.done(); 529 } catch (JavaModelException jme) { 530 exception[0]= jme; 531 } 532 } 533 }; 534 try { 535 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(r); 536 } catch (InvocationTargetException e) { 537 JDIDebugUIPlugin.log(e); 538 } catch (InterruptedException e) { 539 JDIDebugUIPlugin.log(e); 540 } 541 if (exception[0] != null) { 542 throw exception[0]; 543 } 544 if (monitorCanceled[0]) { 545 return null; 546 } 547 548 int flags= JavaElementLabelProvider.SHOW_DEFAULT; 549 PackageSelectionDialog dialog= new PackageSelectionDialog(shell, new JavaElementLabelProvider(flags)); 550 dialog.setIgnoreCase(false); 551 dialog.setElements(packageList.toArray()); return dialog; 553 } 554 555 559 public ILaunchConfigurationTab getVMInstallTypePage(String vmInstallTypeID) { 560 if (fVmInstallTypePageMap == null) { 561 initializeVMInstallTypePageMap(); 562 } 563 IConfigurationElement configElement = (IConfigurationElement) fVmInstallTypePageMap.get(vmInstallTypeID); 564 ILaunchConfigurationTab tab = null; 565 if (configElement != null) { 566 try { 567 tab = (ILaunchConfigurationTab) configElement.createExecutableExtension("class"); } catch(CoreException ce) { 569 log(new Status(IStatus.ERROR, getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, "An error occurred retrieving a VMInstallType page.", ce)); } 571 } 572 return tab; 573 } 574 575 protected void initializeVMInstallTypePageMap() { 576 fVmInstallTypePageMap = new HashMap (10); 577 578 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(getUniqueIdentifier(), IJavaDebugUIConstants.EXTENSION_POINT_VM_INSTALL_TYPE_PAGE); 579 IConfigurationElement[] infos= extensionPoint.getConfigurationElements(); 580 for (int i = 0; i < infos.length; i++) { 581 String id = infos[i].getAttribute("vmInstallTypeID"); fVmInstallTypePageMap.put(id, infos[i]); 583 } 584 } 585 586 592 public IDebugModelPresentation getModelPresentation() { 593 if (fUtilPresentation == null) { 594 fUtilPresentation = DebugUITools.newDebugModelPresentation(JDIDebugModel.getPluginIdentifier()); 595 } 596 return fUtilPresentation; 597 } 598 599 606 public static void showPreferencePage(String id, IPreferencePage page) { 607 final IPreferenceNode targetNode = new PreferenceNode(id, page); 608 609 PreferenceManager manager = new PreferenceManager(); 610 manager.addToRoot(targetNode); 611 final PreferenceDialog dialog = new PreferenceDialog(JDIDebugUIPlugin.getActiveWorkbenchShell(), manager); 612 final boolean [] result = new boolean[] { false }; 613 BusyIndicator.showWhile(JDIDebugUIPlugin.getStandardDisplay(), new Runnable () { 614 public void run() { 615 dialog.create(); 616 dialog.setMessage(targetNode.getLabelText()); 617 result[0]= (dialog.open() == Window.OK); 618 } 619 }); 620 } 621 622 628 public static void showPreferencePage(String pageId) { 629 PreferencesUtil.createPreferenceDialogOn(getShell(), pageId, new String [] { pageId }, null).open(); 630 } 631 632 637 public JavaTextTools getJavaTextTools() { 638 if (fTextTools == null) { 639 fTextTools = new JavaTextTools(PreferenceConstants.getPreferenceStore()); 640 } 641 return fTextTools; 642 } 643 } 644 645 | Popular Tags |