1 11 package org.eclipse.debug.internal.ui.launchConfigurations; 12 13 import java.io.BufferedInputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Comparator ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import javax.xml.transform.TransformerException ; 34 35 import org.eclipse.core.expressions.EvaluationContext; 36 import org.eclipse.core.expressions.IEvaluationContext; 37 import org.eclipse.core.resources.IContainer; 38 import org.eclipse.core.resources.IFile; 39 import org.eclipse.core.resources.IResource; 40 import org.eclipse.core.resources.ISaveContext; 41 import org.eclipse.core.resources.ISaveParticipant; 42 import org.eclipse.core.runtime.CoreException; 43 import org.eclipse.core.runtime.IConfigurationElement; 44 import org.eclipse.core.runtime.IExtensionPoint; 45 import org.eclipse.core.runtime.IPath; 46 import org.eclipse.core.runtime.IStatus; 47 import org.eclipse.core.runtime.Platform; 48 import org.eclipse.core.runtime.Status; 49 import org.eclipse.debug.core.DebugPlugin; 50 import org.eclipse.debug.core.ILaunch; 51 import org.eclipse.debug.core.ILaunchConfiguration; 52 import org.eclipse.debug.core.ILaunchConfigurationType; 53 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 54 import org.eclipse.debug.core.ILaunchDelegate; 55 import org.eclipse.debug.core.ILaunchListener; 56 import org.eclipse.debug.core.ILaunchManager; 57 import org.eclipse.debug.core.ILaunchMode; 58 import org.eclipse.debug.internal.core.IConfigurationElementConstants; 59 import org.eclipse.debug.internal.core.LaunchManager; 60 import org.eclipse.debug.internal.ui.DebugPluginImages; 61 import org.eclipse.debug.internal.ui.DebugUIPlugin; 62 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 63 import org.eclipse.debug.internal.ui.ILaunchHistoryChangedListener; 64 import org.eclipse.debug.ui.DebugUITools; 65 import org.eclipse.debug.ui.IDebugUIConstants; 66 import org.eclipse.debug.ui.ILaunchConfigurationTab; 67 import org.eclipse.debug.ui.ILaunchGroup; 68 import org.eclipse.jface.resource.ImageRegistry; 69 import org.eclipse.swt.SWT; 70 import org.eclipse.swt.graphics.Image; 71 import org.eclipse.swt.widgets.Display; 72 import org.eclipse.ui.IFileEditorInput; 73 import org.eclipse.ui.PlatformUI; 74 import org.eclipse.ui.activities.IWorkbenchActivitySupport; 75 import org.eclipse.ui.activities.WorkbenchActivityHelper; 76 import org.w3c.dom.Document ; 77 import org.w3c.dom.Element ; 78 import org.w3c.dom.Node ; 79 import org.w3c.dom.NodeList ; 80 import org.xml.sax.InputSource ; 81 import org.xml.sax.SAXException ; 82 import org.xml.sax.helpers.DefaultHandler ; 83 84 98 public class LaunchConfigurationManager implements ILaunchListener, ISaveParticipant { 99 102 protected Map fLaunchGroups; 103 104 107 protected Map fLaunchHistories; 108 109 112 protected List fLaunchHistoryChangedListeners = new ArrayList (3); 113 114 117 private List fLaunchShortcuts = null; 118 119 122 private Map fLaunchShortcutsByPerspective = null; 123 124 127 protected ImageRegistry fErrorImages = null; 128 129 132 protected boolean fRestoring = false; 133 134 138 private Set fLoadedModes = null; 139 140 143 private static final String LAUNCH_CONFIGURATION_HISTORY_FILENAME = "launchConfigurationHistory.xml"; 145 148 public void startup() { 149 ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); 150 launchManager.addLaunchListener(this); 151 DebugUIPlugin.getDefault().addSaveParticipant(this); 152 ILaunch[] launches = launchManager.getLaunches(); 154 for (int i = 0; i < launches.length; i++) { 155 launchAdded(launches[i]); 156 } 157 } 158 159 165 public boolean launchModeAvailable(String mode) { 166 if (fLoadedModes == null) { 167 ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); 168 ILaunchConfigurationType[] types = launchManager.getLaunchConfigurationTypes(); 169 ILaunchMode[] modes = launchManager.getLaunchModes(); 170 fLoadedModes = new HashSet (3); 171 for (int i = 0; i < types.length; i++) { 172 for (int j = 0; j < modes.length; j++) { 173 if (types[i].supportsMode(modes[j].getIdentifier())) { 174 fLoadedModes.add(modes[j].getIdentifier()); 175 } 176 } 177 } 178 } 179 return fLoadedModes.contains(mode); 180 } 181 182 191 public static boolean isVisible(ILaunchConfiguration launchConfiguration) { 192 try { 193 return !(launchConfiguration.getAttribute(IDebugUIConstants.ATTR_PRIVATE, false)); 194 } catch (CoreException e) { 195 } 196 return false; 197 } 198 199 206 public static ILaunchConfiguration[] filterConfigs(ILaunchConfiguration[] configurations) { 207 IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench().getActivitySupport(); 208 if (activitySupport == null) { 209 return configurations; 210 } 211 List filteredConfigs = new ArrayList (); 212 ILaunchConfigurationType type = null; 213 LaunchConfigurationTypeContribution contribution = null; 214 ILaunchConfiguration configuration = null; 215 for (int i = 0; i < configurations.length; i++) { 216 configuration = configurations[i]; 217 try { 218 type = configuration.getType(); 219 contribution = new LaunchConfigurationTypeContribution(type); 220 if (DebugUIPlugin.doLaunchConfigurationFiltering(configuration) & !WorkbenchActivityHelper.filterItem(contribution)) { 221 filteredConfigs.add(configuration); 222 } 223 } 224 catch (CoreException e) {DebugUIPlugin.log(e.getStatus());} 225 } 226 return (ILaunchConfiguration[]) filteredConfigs.toArray(new ILaunchConfiguration[filteredConfigs.size()]); 227 } 228 229 235 public static ILaunchDelegate[] filterLaunchDelegates(ILaunchConfigurationType type, Set modes) throws CoreException { 236 IWorkbenchActivitySupport as = PlatformUI.getWorkbench().getActivitySupport(); 237 ILaunchDelegate[] delegates = type.getDelegates(modes); 238 if(as == null) { 239 return delegates; 240 } 241 HashSet set = new HashSet (); 242 for(int i = 0; i < delegates.length; i++) { 243 if(!WorkbenchActivityHelper.filterItem(new LaunchDelegateContribution(delegates[i]))) { 245 set.add(delegates[i]); 246 } 247 } 248 return (ILaunchDelegate[]) set.toArray(new ILaunchDelegate[set.size()]); 249 } 250 251 254 public void shutdown() { 255 ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); 256 launchManager.removeLaunchListener(this); 257 if (fLaunchHistories != null) { 258 Iterator histories = fLaunchHistories.values().iterator(); 259 while (histories.hasNext()) { 260 LaunchHistory history = (LaunchHistory)histories.next(); 261 history.dispose(); 262 } 263 } 264 DebugUIPlugin.getDefault().removeSaveParticipant(this); 265 } 266 267 270 public void launchRemoved(ILaunch launch) {} 271 272 275 public void launchChanged(ILaunch launch) {} 276 277 282 public void launchAdded(final ILaunch launch) { 283 removeTerminatedLaunches(launch); 284 } 285 286 290 protected void removeTerminatedLaunches(ILaunch newLaunch) { 291 if (DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugUIConstants.PREF_AUTO_REMOVE_OLD_LAUNCHES)) { 292 ILaunchManager lManager= DebugPlugin.getDefault().getLaunchManager(); 293 Object [] launches= lManager.getLaunches(); 294 for (int i= 0; i < launches.length; i++) { 295 ILaunch launch= (ILaunch)launches[i]; 296 if (launch != newLaunch && launch.isTerminated()) { 297 lManager.removeLaunch(launch); 298 } 299 } 300 } 301 } 302 303 311 public ILaunchConfiguration getLastLaunch(String groupId) { 312 LaunchHistory history = getLaunchHistory(groupId); 313 if (history != null) { 314 return history.getRecentLaunch(); 315 } 316 return null; 317 } 318 319 326 public ILaunchConfiguration getFilteredLastLaunch(String groupId) { 327 LaunchHistory history = getLaunchHistory(groupId); 328 if (history != null) { 329 ILaunchConfiguration[] filterConfigs = history.getCompleteLaunchHistory(); 330 if (filterConfigs.length > 0) { 331 return filterConfigs[0]; 332 } 333 } 334 return null; 335 } 336 337 341 public void addLaunchHistoryListener(ILaunchHistoryChangedListener listener) { 342 if (!fLaunchHistoryChangedListeners.contains(listener)) { 343 fLaunchHistoryChangedListeners.add(listener); 344 } 345 } 346 347 351 public void removeLaunchHistoryListener(ILaunchHistoryChangedListener listener) { 352 fLaunchHistoryChangedListeners.remove(listener); 353 } 354 355 358 protected void fireLaunchHistoryChanged() { 359 Iterator iterator = fLaunchHistoryChangedListeners.iterator(); 360 ILaunchHistoryChangedListener listener = null; 361 while (iterator.hasNext()) { 362 listener = (ILaunchHistoryChangedListener) iterator.next(); 363 listener.launchHistoryChanged(); 364 } 365 } 366 367 375 protected String getHistoryAsXML() throws CoreException, ParserConfigurationException , TransformerException , IOException { 376 Document doc = DebugUIPlugin.getDocument(); 377 Element historyRootElement = doc.createElement(IConfigurationElementConstants.LAUNCH_HISTORY); 378 doc.appendChild(historyRootElement); 379 380 Iterator histories = fLaunchHistories.values().iterator(); 381 LaunchHistory history = null; 382 while (histories.hasNext()) { 383 history = (LaunchHistory)histories.next(); 384 Element groupElement = doc.createElement(IConfigurationElementConstants.LAUNCH_GROUP); 385 groupElement.setAttribute(IConfigurationElementConstants.ID, history.getLaunchGroup().getIdentifier()); 386 historyRootElement.appendChild(groupElement); 387 Element historyElement = doc.createElement(IConfigurationElementConstants.MRU_HISTORY); 388 groupElement.appendChild(historyElement); 389 createEntry(doc, historyElement, history.getCompleteLaunchHistory()); 390 Element favs = doc.createElement(IConfigurationElementConstants.FAVORITES); 391 groupElement.appendChild(favs); 392 createEntry(doc, favs, history.getFavorites()); 393 history.setSaved(true); 394 } 395 return DebugUIPlugin.serializeDocument(doc); 396 } 397 398 406 protected void createEntry(Document doc, Element historyRootElement, ILaunchConfiguration[] configurations) throws CoreException { 407 for (int i = 0; i < configurations.length; i++) { 408 ILaunchConfiguration configuration = configurations[i]; 409 if (configuration.exists()) { 410 Element launch = doc.createElement(IConfigurationElementConstants.LAUNCH); 411 launch.setAttribute(IConfigurationElementConstants.MEMENTO, configuration.getMemento()); 412 historyRootElement.appendChild(launch); 413 } 414 } 415 } 416 417 421 protected IPath getHistoryFilePath() { 422 return DebugUIPlugin.getDefault().getStateLocation().append(LAUNCH_CONFIGURATION_HISTORY_FILENAME); 423 } 424 425 429 protected void persistLaunchHistory() throws IOException , CoreException, TransformerException , ParserConfigurationException { 430 synchronized (this) { 431 if (fLaunchHistories == null || fRestoring) { 432 return; 433 } 434 } 435 boolean shouldsave = false; 436 for(Iterator iter = fLaunchHistories.values().iterator(); iter.hasNext();) { 437 shouldsave |= ((LaunchHistory)iter.next()).needsSaving(); 438 } 439 if(shouldsave) { 440 IPath historyPath = getHistoryFilePath(); 441 String osHistoryPath = historyPath.toOSString(); 442 String xml = getHistoryAsXML(); 443 File file = new File (osHistoryPath); 444 file.createNewFile(); 445 446 FileOutputStream stream = new FileOutputStream (file); 447 stream.write(xml.getBytes("UTF8")); stream.close(); 449 } 450 } 451 452 456 private void restoreLaunchHistory() { 457 IPath historyPath = getHistoryFilePath(); 459 String osHistoryPath = historyPath.toOSString(); 460 File file = new File (osHistoryPath); 461 if (!file.exists()) { 463 return; 464 } 465 InputStream stream= null; 466 Element rootHistoryElement= null; 467 try { 468 stream = new BufferedInputStream (new FileInputStream (file)); 470 try { 471 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 472 parser.setErrorHandler(new DefaultHandler ()); 473 rootHistoryElement = parser.parse(new InputSource (stream)).getDocumentElement(); 474 } catch (SAXException e) { 475 DebugUIPlugin.log(e); 476 return; 477 } catch (ParserConfigurationException e) { 478 DebugUIPlugin.log(e); 479 return; 480 } finally { 481 stream.close(); 482 } 483 } catch (IOException exception) { 484 DebugUIPlugin.log(exception); 485 return; 486 } 487 if (!rootHistoryElement.getNodeName().equalsIgnoreCase(IConfigurationElementConstants.LAUNCH_HISTORY)) { 489 return; 490 } 491 Collection l = fLaunchHistories.values(); 494 LaunchHistory[] histories = (LaunchHistory[])l.toArray(new LaunchHistory[l.size()]); 495 NodeList list = rootHistoryElement.getChildNodes(); 496 int length = list.getLength(); 497 Node node = null; 498 Element entry = null; 499 for (int i = 0; i < length; ++i) { 500 node = list.item(i); 501 short type = node.getNodeType(); 502 if (type == Node.ELEMENT_NODE) { 503 entry = (Element) node; 504 if (entry.getNodeName().equalsIgnoreCase(IConfigurationElementConstants.LAUNCH)) { 505 createHistoryElement(entry, histories, false); 506 } else if (entry.getNodeName().equalsIgnoreCase(IConfigurationElementConstants.LAST_LAUNCH)) { 507 createHistoryElement(entry, histories, true); 508 } else if (entry.getNodeName().equals(IConfigurationElementConstants.LAUNCH_GROUP)) { 509 String id = entry.getAttribute(IConfigurationElementConstants.ID); 510 if (id != null) { 511 LaunchHistory history = getLaunchHistory(id); 512 if (history != null) { 513 restoreHistory(entry, history); 514 } 515 } 516 } 517 } 518 } 519 } 520 521 527 private void restoreHistory(Element groupElement, LaunchHistory history) { 528 NodeList nodes = groupElement.getChildNodes(); 529 int length = nodes.getLength(); 530 for (int i = 0; i < length; i++) { 531 Node node = nodes.item(i); 532 if (node.getNodeType() == Node.ELEMENT_NODE) { 533 Element element = (Element)node; 534 if (element.getNodeName().equals(IConfigurationElementConstants.MRU_HISTORY)) { 535 ILaunchConfiguration[] configs = getLaunchConfigurations(element); 536 for (int j = 0; j < configs.length; j++) { 537 history.addHistory(configs[j], false); 538 } 539 } else if (element.getNodeName().equals(IConfigurationElementConstants.FAVORITES)) { 540 ILaunchConfiguration[] favs = getLaunchConfigurations(element); 541 history.setFavorites(favs); 542 } 543 } 544 } 545 } 546 547 552 private ILaunchConfiguration[] getLaunchConfigurations(Element root) { 553 List configs = new ArrayList (); 554 NodeList nodes = root.getChildNodes(); 555 int length = nodes.getLength(); 556 for (int i = 0; i < length; i++) { 557 Node node = nodes.item(i); 558 if (node.getNodeType() == Node.ELEMENT_NODE) { 559 Element element = (Element) node; 560 if (element.getNodeName().equals(IConfigurationElementConstants.LAUNCH)) { 561 String memento = element.getAttribute(IConfigurationElementConstants.MEMENTO); 562 if (memento != null) { 563 try { 564 ILaunchConfiguration configuration = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(memento); 565 if (configuration.exists()) { 566 configs.add(configuration); 567 } 568 } catch (CoreException e) { 569 } 570 } 571 } 572 } 573 } 574 return (ILaunchConfiguration[]) configs.toArray(new ILaunchConfiguration[configs.size()]); 575 } 576 577 581 private void createHistoryElement(Element entry, LaunchHistory[] histories, boolean prepend) { 582 String memento = entry.getAttribute(IConfigurationElementConstants.MEMENTO); 583 String mode = entry.getAttribute(IConfigurationElementConstants.MODE); 584 try { 585 ILaunchConfiguration launchConfig = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(memento); 586 if (launchConfig.exists()) { 587 LaunchHistory history = null; 588 for (int i = 0; i < histories.length; i++) { 589 history = histories[i]; 590 if (history.accepts(launchConfig) && history.getLaunchGroup().getMode().equals(mode)) { 591 history.addHistory(launchConfig, prepend); 592 } 593 } 594 } 595 } catch (CoreException e) { 596 } 598 } 599 600 603 private void loadLaunchShortcuts() { 604 if(fLaunchShortcuts == null) { 605 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_LAUNCH_SHORTCUTS); 607 IConfigurationElement[] infos= extensionPoint.getConfigurationElements(); 608 609 fLaunchShortcuts = new ArrayList (infos.length); 611 for (int i = 0; i < infos.length; i++) { 612 fLaunchShortcuts.add(new LaunchShortcutExtension(infos[i])); 613 } 614 Collections.sort(fLaunchShortcuts, new ShortcutComparator()); 615 } 616 } 617 618 621 private void loadLaunchGroups() { 622 if (fLaunchGroups == null) { 623 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_LAUNCH_GROUPS); 625 IConfigurationElement[] infos= extensionPoint.getConfigurationElements(); 626 627 fLaunchGroups = new HashMap (infos.length); 629 LaunchGroupExtension ext = null; 630 for (int i = 0; i < infos.length; i++) { 631 ext = new LaunchGroupExtension(infos[i]); 632 fLaunchGroups.put(ext.getIdentifier(), ext); 633 } 634 } 635 } 636 637 642 public List getLaunchShortcuts() { 643 if (fLaunchShortcuts == null) { 644 loadLaunchShortcuts(); 645 } 646 return fLaunchShortcuts; 647 } 648 649 655 public List getLaunchShortcuts(IResource resource) { 656 List list = new ArrayList (); 657 List sc = getLaunchShortcuts(); 658 List ctxt = new ArrayList (); 659 ctxt.add(resource); 660 IEvaluationContext context = new EvaluationContext(null, ctxt); 661 context.addVariable("selection", ctxt); LaunchShortcutExtension ext = null; 663 for(Iterator iter = sc.iterator(); iter.hasNext();) { 664 ext = (LaunchShortcutExtension) iter.next(); 665 try { 666 if(ext.evalEnablementExpression(context, ext.getContextualLaunchEnablementExpression()) && !WorkbenchActivityHelper.filterItem(ext)) { 667 if(!list.contains(ext)) { 668 list.add(ext); 669 } 670 } 671 } 672 catch(CoreException ce) {} 673 } 674 return list; 675 } 676 677 686 public List getShortcutsSupportingMode(List shortctus, String mode) { 687 ArrayList supporting = new ArrayList (shortctus); 688 Iterator iterator = supporting.listIterator(); 689 while (iterator.hasNext()) { 690 LaunchShortcutExtension ext = (LaunchShortcutExtension) iterator.next(); 691 if (!ext.getModes().contains(mode)) { 692 iterator.remove(); 693 } 694 } 695 return supporting; 696 } 697 698 707 public List getApplicableConfigurationTypes(IResource resource) { 708 List types = new ArrayList (); 709 List exts = getLaunchShortcuts(); 710 LaunchShortcutExtension ext = null; 711 List list = new ArrayList (); 712 list.add(resource); 713 IEvaluationContext context = new EvaluationContext(null, list); 714 context.setAllowPluginActivation(true); 715 context.addVariable("selection", list); HashSet set = new HashSet (); 717 for(Iterator iter = exts.listIterator(); iter.hasNext();) { 718 ext = (LaunchShortcutExtension) iter.next(); 719 try { 720 if(ext.evalEnablementExpression(context, ext.getContextualLaunchEnablementExpression())) { 721 set.addAll(ext.getAssociatedConfigurationTypes()); 722 } 723 } 724 catch(CoreException ce) { 725 IStatus status = new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), "Launch shortcut '" + ext.getId() + "' enablement expression caused exception. Shortcut was removed.", ce); DebugUIPlugin.log(status); 727 iter.remove(); 728 } 729 } 730 LaunchManager lm = (LaunchManager) DebugPlugin.getDefault().getLaunchManager(); 731 ILaunchConfigurationType type = null; 732 for(Iterator iter = set.iterator(); iter.hasNext();) { 733 type = lm.getLaunchConfigurationType((String )iter.next()); 734 if(type != null) { 735 if(!types.contains(type) && type.isPublic() && !"org.eclipse.ui.externaltools.builder".equals(type.getCategory())) { types.add(type); 737 } 738 } 739 } 740 return types; 741 } 742 743 750 public List getApplicableLaunchConfigurations(IResource resource) { 751 ArrayList list = new ArrayList (); 752 try { 753 List types = getApplicableConfigurationTypes(resource); 754 ILaunchConfiguration[] configurations = filterConfigs(getLaunchManager().getLaunchConfigurations()); 755 ILaunchConfiguration configuration = null; 756 IResource[] resources = null; 757 for(int i = 0; i < configurations.length; i++) { 758 configuration = configurations[i]; 759 if(types.contains(configuration.getType()) && acceptConfiguration(configuration)) { 760 resources = configuration.getMappedResources(); 761 if (resources != null) { 762 for (int j = 0; j < resources.length; j++) { 763 if (resource.equals(resources[j]) || resource.getFullPath().isPrefixOf(resources[j].getFullPath())) { 764 list.add(configuration); 765 break; 766 } 767 } 768 } 769 else { 770 list.add(configuration); 772 } 773 } 774 } 775 } catch (CoreException e) { 776 list.clear(); 777 DebugPlugin.log(e); 778 } 779 return list; 780 } 781 782 788 private boolean acceptConfiguration(ILaunchConfiguration config) throws CoreException { 789 if(config != null && !DebugUITools.isPrivate(config)) { 790 if(!"org.eclipse.ui.externaltools".equals(config.getType().getCategory())) { return true; 792 } 793 else { 794 IResource[] res = config.getMappedResources(); 795 if(res != null) { 796 return true; 797 } 798 } 799 } 800 return false; 801 } 802 803 808 public List getLaunchShortcuts(String category) { 809 return filterShortcuts(getLaunchShortcuts(), category); 810 } 811 812 819 protected List filterShortcuts(List unfiltered, String category) { 820 List filtered = new ArrayList (unfiltered.size()); 821 Iterator iter = unfiltered.iterator(); 822 LaunchShortcutExtension extension = null; 823 while (iter.hasNext()){ 824 extension = (LaunchShortcutExtension)iter.next(); 825 if (category == null) { 826 if (extension.getCategory() == null) { 827 filtered.add(extension); 828 } 829 } else if (category.equals(extension.getCategory())){ 830 filtered.add(extension); 831 } 832 } 833 return filtered; 834 } 835 836 847 public List getLaunchShortcuts(String perpsective, String category) { 848 if (fLaunchShortcutsByPerspective == null) { 849 Iterator shortcuts = getLaunchShortcuts().iterator(); 850 fLaunchShortcutsByPerspective = new HashMap (10); 851 LaunchShortcutExtension ext = null; 852 Iterator perspectives = null; 853 while (shortcuts.hasNext()) { 854 ext = (LaunchShortcutExtension)shortcuts.next(); 855 perspectives = ext.getPerspectives().iterator(); 856 while (perspectives.hasNext()) { 857 String id = (String )perspectives.next(); 858 List list = (List )fLaunchShortcutsByPerspective.get(id); 859 if (list == null) { 860 list = new ArrayList (4); 861 fLaunchShortcutsByPerspective.put(id, list); 862 } 863 list.add(ext); 864 } 865 } 866 } 867 List list = (List )fLaunchShortcutsByPerspective.get(perpsective); 868 if (list == null) { 869 return new ArrayList (); 870 } 871 return filterShortcuts(list, category); 872 } 873 874 883 public ILaunchConfiguration getMRUConfiguration(List configurations, ILaunchGroup group, IResource resource) { 884 if(group != null) { 885 ArrayList candidates = new ArrayList (); 886 LaunchHistory history = getLaunchHistory(group.getIdentifier()); 887 if(history != null) { 888 ILaunchConfiguration[] configs = history.getCompleteLaunchHistory(); 889 for(int i = 0; i < configs.length; i++) { 890 if(configurations.contains(configs[i])) { 891 if(resource instanceof IContainer) { 892 return configs[i]; 893 } 894 else { 895 candidates.add(configs[i]); 896 } 897 } 898 } 899 } 900 ILaunchConfiguration config = null; 902 IResource[] res = null; 903 for(Iterator iter = candidates.iterator(); iter.hasNext();) { 904 config = (ILaunchConfiguration) iter.next(); 905 try { 906 res = config.getMappedResources(); 907 if(res != null) { 908 for(int i = 0; i < res.length; i++) { 909 if(res[i].equals(resource)) { 910 return config; 911 } 912 } 913 } 914 } 915 catch(CoreException ce) {} 916 } 917 config = getLastLaunch(group.getIdentifier()); 918 if(candidates.contains(config)) { 919 return config; 920 } 921 } 922 return null; 923 } 924 925 934 public LaunchShortcutExtension getLaunchShortcut(String id) { 935 loadLaunchShortcuts(); 936 LaunchShortcutExtension ext = null; 937 for(int i = 0; i < fLaunchShortcuts.size(); i++) { 938 ext = (LaunchShortcutExtension) fLaunchShortcuts.get(i); 939 if(ext.getId().equals(id)) { 940 return ext; 941 } 942 } 943 return null; 944 } 945 946 952 public ILaunchConfiguration isSharedConfig(Object receiver) { 953 if(receiver instanceof IFile) { 954 IFile file = (IFile) receiver; 955 String ext = file.getFileExtension(); 956 if(ext == null) { 957 return null; 958 } 959 if(ext.equals("launch")) { ILaunchConfiguration config = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(file); 961 if(config != null && config.exists()) { 962 return config; 963 } 964 } 965 } 966 else if(receiver instanceof IFileEditorInput) { 967 IFileEditorInput input = (IFileEditorInput) receiver; 968 return isSharedConfig(input.getFile()); 969 } 970 return null; 971 } 972 973 976 public Image getErrorTabImage(ILaunchConfigurationTab tab) { 977 if (fErrorImages == null) { 978 fErrorImages = new ImageRegistry(); 979 } 980 String key = tab.getClass().getName(); 981 Image image = fErrorImages.get(key); 982 if (image == null) { 983 Image base = tab.getImage(); 985 if (base == null) { 986 base = DebugPluginImages.getImage(IInternalDebugUIConstants.IMG_OVR_TRANSPARENT); 987 } 988 base = new Image(Display.getCurrent(), base, SWT.IMAGE_COPY); 989 LaunchConfigurationTabImageDescriptor desc = new LaunchConfigurationTabImageDescriptor(base, LaunchConfigurationTabImageDescriptor.ERROR); 990 image = desc.createImage(); 991 fErrorImages.put(key, image); 992 } 993 return image; 994 } 995 996 1001 public LaunchGroupExtension getLaunchGroup(String id) { 1002 if (fLaunchGroups == null) { 1003 loadLaunchGroups(); 1004 } 1005 return (LaunchGroupExtension)fLaunchGroups.get(id); 1006 } 1007 1008 1013 public ILaunchGroup[] getLaunchGroups() { 1014 if (fLaunchGroups == null) { 1015 loadLaunchGroups(); 1016 } 1017 Collection groups = fLaunchGroups.values(); 1018 return (ILaunchGroup[])groups.toArray(new ILaunchGroup[groups.size()]); 1019 } 1020 1021 1026 public LaunchHistory getLaunchHistory(String id) { 1027 loadLaunchHistories(); 1028 return (LaunchHistory)fLaunchHistories.get(id); 1029 } 1030 1031 1036 private LaunchManager getLaunchManager() { 1037 return (LaunchManager) DebugPlugin.getDefault().getLaunchManager(); 1038 } 1039 1040 1043 private synchronized void loadLaunchHistories() { 1044 if (fLaunchHistories == null) { 1045 fRestoring = true; 1046 ILaunchGroup[] groups = getLaunchGroups(); 1047 fLaunchHistories = new HashMap (groups.length); 1048 ILaunchGroup extension = null; 1049 for (int i = 0; i < groups.length; i++) { 1050 extension = groups[i]; 1051 if (extension.isPublic()) { 1052 fLaunchHistories.put(extension.getIdentifier(), new LaunchHistory(extension)); 1053 } 1054 } 1055 restoreLaunchHistory(); 1056 fRestoring = false; 1057 } 1058 } 1059 1060 1066 public LaunchGroupExtension getDefaultLaunchGroup(String mode) { 1067 if (mode.equals(ILaunchManager.DEBUG_MODE)) { 1068 return getLaunchGroup(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP); 1069 } 1070 return getLaunchGroup(IDebugUIConstants.ID_RUN_LAUNCH_GROUP); 1071 } 1072 1073 1082 public ILaunchGroup getLaunchGroup(ILaunchConfiguration configuration, String mode) { 1083 try { 1084 String category = configuration.getCategory(); 1085 ILaunchGroup[] groups = getLaunchGroups(); 1086 ILaunchGroup extension = null; 1087 for (int i = 0; i < groups.length; i++) { 1088 extension = groups[i]; 1089 if (category == null) { 1090 if (extension.getCategory() == null && extension.getMode().equals(mode)) { 1091 return extension; 1092 } 1093 } else if (category.equals(extension.getCategory())) { 1094 if (extension.getMode().equals(mode)) { 1095 return extension; 1096 } 1097 } 1098 } 1099 } 1100 catch (CoreException e) {DebugUIPlugin.log(e);} 1101 return null; 1102 } 1103 1104 1112 public static ILaunchConfiguration getSharedTypeConfig(ILaunchConfigurationType type) throws CoreException { 1113 String id = type.getIdentifier(); 1114 String name = id + ".SHARED_INFO"; ILaunchConfiguration shared = null; 1116 ILaunchConfiguration[] configurations = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(type); 1117 ILaunchConfiguration configuration = null; 1118 for (int i = 0; i < configurations.length; i++) { 1119 configuration = configurations[i]; 1120 if (configuration.getName().equals(name)) { 1121 shared = configuration; 1122 break; 1123 } 1124 } 1125 1126 if (shared == null) { 1127 ILaunchConfigurationWorkingCopy workingCopy; 1129 workingCopy = type.newInstance(null, name); 1130 workingCopy.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true); 1131 shared = workingCopy.doSave(); 1134 } 1135 return shared; 1136 } 1137 1138 1139 1142 public void doneSaving(ISaveContext context) {} 1143 1144 1147 public void prepareToSave(ISaveContext context) throws CoreException {} 1148 1149 1152 public void rollback(ISaveContext context) {} 1153 1154 1157 public void saving(ISaveContext context) throws CoreException { 1158 try { 1159 persistLaunchHistory(); 1160 } 1161 catch (CoreException e) {DebugUIPlugin.log(e);} 1162 catch (IOException e) {DebugUIPlugin.log(e);} 1163 catch (ParserConfigurationException e) {DebugUIPlugin.log(e);} 1164 catch (TransformerException e) {DebugUIPlugin.log(e);} 1165 } 1166 1167 1174 public void setRecentLaunch(ILaunch launch) { 1175 ILaunchGroup[] groups = DebugUITools.getLaunchGroups(); 1176 int size = groups.length; 1177 for (int i = 0; i < size; i++) { 1178 String id = groups[i].getIdentifier(); 1179 LaunchHistory history = getLaunchHistory(id); 1180 if (history != null) 1181 history.launchAdded(launch); 1182 } 1183 } 1184 1185} 1186 1187 1191class ShortcutComparator implements Comparator { 1192 1195 public int compare(Object a, Object b) { 1196 LaunchShortcutExtension shorcutA = (LaunchShortcutExtension)a; 1197 String labelA = shorcutA.getLabel(); 1198 String pathA = shorcutA.getMenuPath(); 1199 LaunchShortcutExtension shortcutB = (LaunchShortcutExtension)b; 1200 String labelB = shortcutB.getLabel(); 1201 String pathB = shortcutB.getMenuPath(); 1202 1203 if (nullOrEqual(pathA, pathB)) { 1206 if (labelA == labelB) { 1208 return 0; 1209 } 1210 if (labelA == null) { 1211 return 1; 1212 } 1213 if (labelB == null) { 1214 return -1; 1215 } 1216 return labelA.compareToIgnoreCase(labelB); 1217 } 1218 if (pathA == null) { 1220 return 1; 1221 } 1222 if (pathB == null) { 1223 return -1; 1224 } 1225 return pathA.compareToIgnoreCase(pathB); 1226 } 1227 1228 private boolean nullOrEqual(String a, String b) { 1229 if (a == null) { 1230 return b == null; 1231 } 1232 return a.equals(b); 1233 } 1234 1235} 1236 | Popular Tags |