1 11 package org.eclipse.ui.internal.registry; 12 13 import java.io.BufferedReader ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStreamReader ; 17 import java.io.Reader ; 18 import java.io.StringReader ; 19 import java.io.StringWriter ; 20 import java.io.Writer ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 32 import org.eclipse.core.commands.common.EventManager; 33 import org.eclipse.core.runtime.IConfigurationElement; 34 import org.eclipse.core.runtime.IExtension; 35 import org.eclipse.core.runtime.IExtensionPoint; 36 import org.eclipse.core.runtime.IPath; 37 import org.eclipse.core.runtime.Platform; 38 import org.eclipse.core.runtime.content.IContentType; 39 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; 40 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 41 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 42 import org.eclipse.jface.dialogs.ErrorDialog; 43 import org.eclipse.jface.dialogs.MessageDialog; 44 import org.eclipse.jface.preference.IPreferenceStore; 45 import org.eclipse.jface.resource.ImageDescriptor; 46 import org.eclipse.jface.util.SafeRunnable; 47 import org.eclipse.swt.program.Program; 48 import org.eclipse.swt.widgets.Shell; 49 import org.eclipse.ui.IEditorDescriptor; 50 import org.eclipse.ui.IEditorRegistry; 51 import org.eclipse.ui.IFileEditorMapping; 52 import org.eclipse.ui.IMemento; 53 import org.eclipse.ui.IPropertyListener; 54 import org.eclipse.ui.ISharedImages; 55 import org.eclipse.ui.PlatformUI; 56 import org.eclipse.ui.WorkbenchException; 57 import org.eclipse.ui.XMLMemento; 58 import org.eclipse.ui.activities.WorkbenchActivityHelper; 59 import org.eclipse.ui.internal.IPreferenceConstants; 60 import org.eclipse.ui.internal.IWorkbenchConstants; 61 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 62 import org.eclipse.ui.internal.WorkbenchImages; 63 import org.eclipse.ui.internal.WorkbenchMessages; 64 import org.eclipse.ui.internal.WorkbenchPlugin; 65 import org.eclipse.ui.internal.editorsupport.ComponentSupport; 66 import org.eclipse.ui.internal.misc.ExternalProgramImageDescriptor; 67 import org.eclipse.ui.internal.misc.ProgramImageDescriptor; 68 import org.eclipse.ui.internal.util.Util; 69 70 import com.ibm.icu.text.Collator; 71 72 75 public class EditorRegistry extends EventManager implements IEditorRegistry, 76 IExtensionChangeHandler { 77 78 private final static IEditorDescriptor [] EMPTY = new IEditorDescriptor[0]; 79 80 class RelatedRegistry { 81 82 88 public IEditorDescriptor[] getRelatedObjects(IContentType type) { 89 IEditorDescriptor[] relatedObjects = (IEditorDescriptor[]) contentTypeToEditorMappings.get(type); 90 if (relatedObjects == null) { 91 return EMPTY; 92 } 93 return relatedObjects; 94 } 95 96 101 public IEditorDescriptor[] getRelatedObjects(String fileName) { 102 IFileEditorMapping mapping = getMappingFor(fileName); 103 if (mapping == null) { 104 return EMPTY; 105 } 106 107 return mapping.getEditors(); 108 } 109 110 } 111 112 private Map contentTypeToEditorMappings = new HashMap (); 113 114 120 private Map extensionImages = new HashMap (); 121 122 130 private List sortedEditorsFromPlugins = new ArrayList (); 131 132 private Map mapIDtoEditor = initialIdToEditorMap(10); 134 135 private EditorMap typeEditorMappings; 137 138 141 private static final Comparator comparer = new Comparator () { 142 private Collator collator = Collator.getInstance(); 143 144 public int compare(Object arg0, Object arg1) { 145 String s1 = ((IEditorDescriptor) arg0).getLabel(); 146 String s2 = ((IEditorDescriptor) arg1).getLabel(); 147 return collator.compare(s1, s2); 148 } 149 }; 150 151 private RelatedRegistry relatedRegistry; 152 153 public static final String EMPTY_EDITOR_ID = "org.eclipse.ui.internal.emptyEditorTab"; 155 159 public EditorRegistry() { 160 super(); 161 initializeFromStorage(); 162 IExtensionTracker tracker = PlatformUI.getWorkbench().getExtensionTracker(); 163 tracker.registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter())); 164 relatedRegistry = new RelatedRegistry(); 165 } 166 167 186 public void addEditorFromPlugin(EditorDescriptor editor, List extensions, 187 List filenames, List contentTypeVector, boolean bDefault) { 188 189 PlatformUI.getWorkbench().getExtensionTracker().registerObject( 190 editor.getConfigurationElement().getDeclaringExtension(), 191 editor, IExtensionTracker.REF_WEAK); 192 sortedEditorsFromPlugins.add(editor); 194 195 Iterator itr = extensions.iterator(); 197 while (itr.hasNext()) { 198 String fileExtension = (String ) itr.next(); 199 200 if (fileExtension != null && fileExtension.length() > 0) { 201 FileEditorMapping mapping = getMappingFor("*." + fileExtension); if (mapping == null) { mapping = new FileEditorMapping(fileExtension); 204 typeEditorMappings.putDefault(mappingKeyFor(mapping), 205 mapping); 206 } 207 mapping.addEditor(editor); 208 if (bDefault) { 209 mapping.setDefaultEditor(editor); 210 } 211 } 212 } 213 214 itr = filenames.iterator(); 216 while (itr.hasNext()) { 217 String filename = (String ) itr.next(); 218 219 if (filename != null && filename.length() > 0) { 220 FileEditorMapping mapping = getMappingFor(filename); 221 if (mapping == null) { String name; 223 String extension; 224 int index = filename.indexOf('.'); 225 if (index < 0) { 226 name = filename; 227 extension = ""; } else { 229 name = filename.substring(0, index); 230 extension = filename.substring(index + 1); 231 } 232 mapping = new FileEditorMapping(name, extension); 233 typeEditorMappings.putDefault(mappingKeyFor(mapping), 234 mapping); 235 } 236 mapping.addEditor(editor); 237 if (bDefault) { 238 mapping.setDefaultEditor(editor); 239 } 240 } 241 } 242 243 244 itr = contentTypeVector.iterator(); 245 while(itr.hasNext()) { 246 String contentTypeId = (String ) itr.next(); 247 if (contentTypeId != null && contentTypeId.length() > 0) { 248 IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId); 249 if (contentType != null) { 250 IEditorDescriptor [] editorArray = (IEditorDescriptor[]) contentTypeToEditorMappings.get(contentType); 251 if (editorArray == null) { 252 editorArray = new IEditorDescriptor[] {editor}; 253 contentTypeToEditorMappings.put(contentType, editorArray); 254 } 255 else { 256 IEditorDescriptor [] newArray = new IEditorDescriptor[editorArray.length + 1]; 257 if (bDefault) { newArray[0] = editor; 259 System.arraycopy(editorArray, 0, newArray, 1, editorArray.length); 260 } 261 else { 262 newArray[editorArray.length] = editor; 263 System.arraycopy(editorArray, 0, newArray, 0, editorArray.length); 264 } 265 contentTypeToEditorMappings.put(contentType, newArray); 266 } 267 } 268 } 269 } 270 271 mapIDtoEditor.put(editor.getId(), editor); 273 } 274 275 278 private void addExternalEditorsToEditorMap() { 279 IEditorDescriptor desc = null; 280 281 FileEditorMapping maps[] = typeEditorMappings.allMappings(); 283 for (int i = 0; i < maps.length; i++) { 284 FileEditorMapping map = maps[i]; 285 IEditorDescriptor[] descArray = map.getEditors(); 286 for (int n = 0; n < descArray.length; n++) { 287 desc = descArray[n]; 288 mapIDtoEditor.put(desc.getId(), desc); 289 } 290 } 291 } 292 293 296 public void addPropertyListener(IPropertyListener l) { 297 addListenerObject(l); 298 } 299 300 303 public IEditorDescriptor findEditor(String id) { 304 return (IEditorDescriptor) mapIDtoEditor.get(id); 305 } 306 307 313 private void firePropertyChange(final int type) { 314 Object [] array = getListeners(); 315 for (int nX = 0; nX < array.length; nX++) { 316 final IPropertyListener l = (IPropertyListener) array[nX]; 317 Platform.run(new SafeRunnable() { 318 public void run() { 319 l.propertyChanged(EditorRegistry.this, type); 320 } 321 }); 322 } 323 } 324 325 330 public IEditorDescriptor getDefaultEditor() { 331 return findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); 334 } 335 336 339 public IEditorDescriptor getDefaultEditor(String filename) { 340 return getDefaultEditor(filename, guessAtContentType(filename)); 341 } 342 343 350 private IContentType guessAtContentType(String filename) { 351 return Platform.getContentTypeManager().findContentTypeFor(filename); 352 } 353 354 359 private ImageDescriptor getDefaultImage() { 360 return WorkbenchImages.getImageDescriptor(ISharedImages.IMG_OBJ_FILE); 362 } 363 364 367 public IEditorDescriptor[] getEditors(String filename) { 368 return getEditors(filename, guessAtContentType(filename)); 369 } 370 371 374 public IFileEditorMapping[] getFileEditorMappings() { 375 FileEditorMapping[] array = typeEditorMappings.allMappings(); 376 final Collator collator = Collator.getInstance(); 377 Arrays.sort(array, new Comparator () { 378 379 382 public int compare(Object o1, Object o2) { 383 String s1 = ((FileEditorMapping) o1).getLabel(); 384 String s2 = ((FileEditorMapping) o2).getLabel(); 385 return collator.compare(s1, s2); 386 } 387 }); 388 return array; 389 } 390 391 394 public ImageDescriptor getImageDescriptor(String filename) { 395 return getImageDescriptor(filename, guessAtContentType(filename)); 396 } 397 398 406 private FileEditorMapping getMappingFor(String ext) { 407 if (ext == null) { 408 return null; 409 } 410 String key = mappingKeyFor(ext); 411 return typeEditorMappings.get(key); 412 } 413 414 425 private FileEditorMapping[] getMappingForFilename(String filename) { 426 FileEditorMapping[] mapping = new FileEditorMapping[2]; 427 428 mapping[0] = getMappingFor(filename); 430 431 int index = filename.lastIndexOf('.'); 433 if (index > -1) { 434 String extension = filename.substring(index); 435 mapping[1] = getMappingFor("*" + extension); } 437 438 return mapping; 439 } 440 441 451 public IEditorDescriptor[] getSortedEditorsFromOS() { 452 List externalEditors = new ArrayList (); 453 Program[] programs = Program.getPrograms(); 454 455 for (int i = 0; i < programs.length; i++) { 456 463 464 EditorDescriptor editor = new EditorDescriptor(); 465 editor.setOpenMode(EditorDescriptor.OPEN_EXTERNAL); 466 editor.setProgram(programs[i]); 467 468 ImageDescriptor desc = new ExternalProgramImageDescriptor( 471 programs[i]); 472 editor.setImageDescriptor(desc); 473 externalEditors.add(editor); 474 } 475 476 Object [] tempArray = sortEditors(externalEditors); 477 IEditorDescriptor[] array = new IEditorDescriptor[externalEditors 478 .size()]; 479 for (int i = 0; i < tempArray.length; i++) { 480 array[i] = (IEditorDescriptor) tempArray[i]; 481 } 482 return array; 483 } 484 485 491 public IEditorDescriptor[] getSortedEditorsFromPlugins() { 492 IEditorDescriptor[] array = new IEditorDescriptor[sortedEditorsFromPlugins 493 .size()]; 494 sortedEditorsFromPlugins.toArray(array); 495 return array; 496 } 497 498 506 private HashMap initialIdToEditorMap(int initialSize) { 507 HashMap map = new HashMap (initialSize); 508 addSystemEditors(map); 509 return map; 510 } 511 512 520 private void addSystemEditors(HashMap map) { 521 EditorDescriptor editor = new EditorDescriptor(); 523 editor.setID(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); 524 editor.setName(WorkbenchMessages.SystemEditorDescription_name); 525 editor.setOpenMode(EditorDescriptor.OPEN_EXTERNAL); 526 map.put(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID, editor); 528 529 if (ComponentSupport.inPlaceEditorSupported()) { 531 editor = new EditorDescriptor(); 532 editor.setID(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); 533 editor.setName(WorkbenchMessages.SystemInPlaceDescription_name); 534 editor.setOpenMode(EditorDescriptor.OPEN_INPLACE); 535 map.put(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID, editor); 537 } 538 539 EditorDescriptor emptyEditorDescriptor = new EditorDescriptor(); 540 emptyEditorDescriptor.setID(EMPTY_EDITOR_ID); 541 emptyEditorDescriptor.setName("(Empty)"); emptyEditorDescriptor 543 .setImageDescriptor(WorkbenchImages 544 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_ELEMENT)); 545 map.put(EMPTY_EDITOR_ID, emptyEditorDescriptor); 546 } 547 548 552 private void initializeFromStorage() { 553 typeEditorMappings = new EditorMap(); 554 extensionImages = new HashMap (); 555 556 EditorRegistryReader registryReader = new EditorRegistryReader(); 558 registryReader.addEditors(this); 559 sortInternalEditors(); 560 rebuildInternalEditorMap(); 561 562 IPreferenceStore store = PlatformUI.getPreferenceStore(); 563 String defaultEditors = store 564 .getString(IPreferenceConstants.DEFAULT_EDITORS); 565 String chachedDefaultEditors = store 566 .getString(IPreferenceConstants.DEFAULT_EDITORS_CACHE); 567 568 if (defaultEditors == null 571 || defaultEditors.equals(chachedDefaultEditors)) { 572 setProductDefaults(defaultEditors); 573 loadAssociations(); } else { 575 loadAssociations(); setProductDefaults(defaultEditors); 577 store.putValue(IPreferenceConstants.DEFAULT_EDITORS_CACHE, 578 defaultEditors); 579 } 580 addExternalEditorsToEditorMap(); 581 } 582 583 592 private void setProductDefaults(String defaultEditors) { 593 if (defaultEditors == null || defaultEditors.length() == 0) { 594 return; 595 } 596 597 StringTokenizer extEditors = new StringTokenizer (defaultEditors, 598 new Character (IPreferenceConstants.SEPARATOR).toString()); 599 while (extEditors.hasMoreTokens()) { 600 String extEditor = extEditors.nextToken().trim(); 601 int index = extEditor.indexOf(':'); 602 if (extEditor.length() < 3 || index <= 0 603 || index >= (extEditor.length() - 1)) { 604 WorkbenchPlugin 606 .log("Error setting default editor. Could not parse '" + extEditor + "'. Default editors should be specified as '*.ext1:editorId1;*.ext2:editorId2'"); return; 608 } 609 String ext = extEditor.substring(0, index).trim(); 610 String editorId = extEditor.substring(index + 1).trim(); 611 FileEditorMapping mapping = getMappingFor(ext); 612 if (mapping == null) { 613 WorkbenchPlugin 614 .log("Error setting default editor. Could not find mapping for '" + ext + "'."); continue; 616 } 617 EditorDescriptor editor = (EditorDescriptor) findEditor(editorId); 618 if (editor == null) { 619 WorkbenchPlugin 620 .log("Error setting default editor. Could not find editor: '" + editorId + "'."); continue; 622 } 623 mapping.setDefaultEditor(editor); 624 } 625 } 626 627 634 private boolean readEditors(Map editorTable) { 635 IPath workbenchStatePath = WorkbenchPlugin.getDefault().getDataLocation(); 637 if(workbenchStatePath == null) { 638 return false; 639 } 640 IPreferenceStore store = WorkbenchPlugin.getDefault() 641 .getPreferenceStore(); 642 Reader reader = null; 643 try { 644 String xmlString = store.getString(IPreferenceConstants.EDITORS); 646 if (xmlString == null || xmlString.length() == 0) { 647 FileInputStream stream = new FileInputStream (workbenchStatePath 648 .append(IWorkbenchConstants.EDITOR_FILE_NAME) 649 .toOSString()); 650 reader = new BufferedReader (new InputStreamReader (stream, 651 "utf-8")); } else { 653 reader = new StringReader (xmlString); 654 } 655 XMLMemento memento = XMLMemento.createReadRoot(reader); 656 EditorDescriptor editor; 657 IMemento[] edMementos = memento 658 .getChildren(IWorkbenchConstants.TAG_DESCRIPTOR); 659 for (int i = 0; i < edMementos.length; i++) { 661 editor = new EditorDescriptor(); 662 boolean valid = editor.loadValues(edMementos[i]); 663 if (!valid) { 664 continue; 665 } 666 if (editor.getPluginID() != null) { 667 EditorDescriptor validEditorDescritor = (EditorDescriptor) mapIDtoEditor 675 .get(editor.getId()); 676 if (validEditorDescritor != null) { 677 editorTable.put(validEditorDescritor.getId(), 678 validEditorDescritor); 679 } 680 } else { ImageDescriptor descriptor; 683 if (editor.getProgram() == null) { 684 descriptor = new ProgramImageDescriptor(editor 685 .getFileName(), 0); 686 } else { 687 descriptor = new ExternalProgramImageDescriptor(editor 688 .getProgram()); 689 } 690 editor.setImageDescriptor(descriptor); 691 editorTable.put(editor.getId(), editor); 692 } 693 } 694 } catch (IOException e) { 695 try { 696 if (reader != null) { 697 reader.close(); 698 } 699 } catch (IOException ex) { 700 e.printStackTrace(); 701 } 702 return false; 704 } catch (WorkbenchException e) { 705 ErrorDialog.openError((Shell) null, WorkbenchMessages.EditorRegistry_errorTitle, 706 WorkbenchMessages.EditorRegistry_errorMessage, 707 e.getStatus()); 708 return false; 709 } 710 711 return true; 712 } 713 714 724 public void readResources(Map editorTable, Reader reader) 725 throws WorkbenchException { 726 XMLMemento memento = XMLMemento.createReadRoot(reader); 727 String versionString = memento.getString(IWorkbenchConstants.TAG_VERSION); 728 boolean versionIs31 = "3.1".equals(versionString); 730 IMemento[] extMementos = memento 731 .getChildren(IWorkbenchConstants.TAG_INFO); 732 for (int i = 0; i < extMementos.length; i++) { 733 String name = extMementos[i] 734 .getString(IWorkbenchConstants.TAG_NAME); 735 if (name == null) { 736 name = "*"; } 738 String extension = extMementos[i] 739 .getString(IWorkbenchConstants.TAG_EXTENSION); 740 IMemento[] idMementos = extMementos[i] 741 .getChildren(IWorkbenchConstants.TAG_EDITOR); 742 String [] editorIDs = new String [idMementos.length]; 743 for (int j = 0; j < idMementos.length; j++) { 744 editorIDs[j] = idMementos[j] 745 .getString(IWorkbenchConstants.TAG_ID); 746 } 747 idMementos = extMementos[i] 748 .getChildren(IWorkbenchConstants.TAG_DELETED_EDITOR); 749 String [] deletedEditorIDs = new String [idMementos.length]; 750 for (int j = 0; j < idMementos.length; j++) { 751 deletedEditorIDs[j] = idMementos[j] 752 .getString(IWorkbenchConstants.TAG_ID); 753 } 754 FileEditorMapping mapping = getMappingFor(name + "." + extension); if (mapping == null) { 756 mapping = new FileEditorMapping(name, extension); 757 } 758 List editors = new ArrayList (); 759 for (int j = 0; j < editorIDs.length; j++) { 760 if (editorIDs[j] != null) { 761 EditorDescriptor editor = (EditorDescriptor) editorTable 762 .get(editorIDs[j]); 763 if (editor != null) { 764 editors.add(editor); 765 } 766 } 767 } 768 List deletedEditors = new ArrayList (); 769 for (int j = 0; j < deletedEditorIDs.length; j++) { 770 if (deletedEditorIDs[j] != null) { 771 EditorDescriptor editor = (EditorDescriptor) editorTable 772 .get(deletedEditorIDs[j]); 773 if (editor != null) { 774 deletedEditors.add(editor); 775 } 776 } 777 } 778 779 List defaultEditors = new ArrayList (); 780 781 if (versionIs31) { idMementos = extMementos[i] 783 .getChildren(IWorkbenchConstants.TAG_DEFAULT_EDITOR); 784 String [] defaultEditorIds = new String [idMementos.length]; 785 for (int j = 0; j < idMementos.length; j++) { 786 defaultEditorIds[j] = idMementos[j] 787 .getString(IWorkbenchConstants.TAG_ID); 788 } 789 for (int j = 0; j < defaultEditorIds.length; j++) { 790 if (defaultEditorIds[j] != null) { 791 EditorDescriptor editor = (EditorDescriptor) editorTable 792 .get(defaultEditorIds[j]); 793 if (editor != null) { 794 defaultEditors.add(editor); 795 } 796 } 797 } 798 } 799 else { if (!editors.isEmpty()) { 801 EditorDescriptor editor = (EditorDescriptor) editors.get(0); 802 if (editor != null) { 803 defaultEditors.add(editor); 804 } 805 } 806 defaultEditors.addAll(Arrays.asList(mapping.getDeclaredDefaultEditors())); 807 } 808 809 IEditorDescriptor[] editorsArray = mapping.getEditors(); 812 for (int j = 0; j < editorsArray.length; j++) { 813 if (!contains(editors, editorsArray[j]) 814 && !deletedEditors.contains(editorsArray[j])) { 815 editors.add(editorsArray[j]); 816 } 817 } 818 mapping.setEditorsList(editors); 820 mapping.setDeletedEditorsList(deletedEditors); 821 mapping.setDefaultEditors(defaultEditors); 822 typeEditorMappings.put(mappingKeyFor(mapping), mapping); 823 } 824 } 825 826 835 private boolean contains(List editorsArray, 836 IEditorDescriptor editorDescriptor) { 837 IEditorDescriptor currentEditorDescriptor = null; 838 Iterator i = editorsArray.iterator(); 839 while (i.hasNext()) { 840 currentEditorDescriptor = (IEditorDescriptor) i.next(); 841 if (currentEditorDescriptor.getId() 842 .equals(editorDescriptor.getId())) { 843 return true; 844 } 845 } 846 return false; 847 848 } 849 850 858 private boolean readResources(Map editorTable) { 859 IPath workbenchStatePath = WorkbenchPlugin.getDefault().getDataLocation(); 861 if(workbenchStatePath == null) { 863 return false; 864 } 865 IPreferenceStore store = WorkbenchPlugin.getDefault() 866 .getPreferenceStore(); 867 Reader reader = null; 868 try { 869 String xmlString = store.getString(IPreferenceConstants.RESOURCES); 871 if (xmlString == null || xmlString.length() == 0) { 872 FileInputStream stream = new FileInputStream (workbenchStatePath 873 .append(IWorkbenchConstants.RESOURCE_TYPE_FILE_NAME) 874 .toOSString()); 875 reader = new BufferedReader (new InputStreamReader (stream, 876 "utf-8")); } else { 878 reader = new StringReader (xmlString); 879 } 880 readResources(editorTable, reader); 882 } catch (IOException e) { 883 try { 884 if (reader != null) { 885 reader.close(); 886 } 887 } catch (IOException ex) { 888 ex.printStackTrace(); 889 } 890 MessageDialog.openError((Shell) null, WorkbenchMessages.EditorRegistry_errorTitle, 891 WorkbenchMessages.EditorRegistry_errorMessage); 892 return false; 893 } catch (WorkbenchException e) { 894 ErrorDialog.openError((Shell) null, WorkbenchMessages.EditorRegistry_errorTitle, 895 WorkbenchMessages.EditorRegistry_errorMessage, 896 e.getStatus()); 897 return false; 898 } 899 return true; 900 901 } 902 903 907 private boolean loadAssociations() { 908 Map editorTable = new HashMap (); 909 if (!readEditors(editorTable)) { 910 return false; 911 } 912 return readResources(editorTable); 913 } 914 915 919 private String mappingKeyFor(String type) { 920 return type.toLowerCase(); 922 } 923 924 930 private String mappingKeyFor(FileEditorMapping mapping) { 931 return mappingKeyFor(mapping.getName() 932 + (mapping.getExtension().length() == 0 ? "" : "." + mapping.getExtension())); } 934 935 938 private void rebuildEditorMap() { 939 rebuildInternalEditorMap(); 940 addExternalEditorsToEditorMap(); 941 } 942 943 946 private void rebuildInternalEditorMap() { 947 Iterator itr = null; 948 IEditorDescriptor desc = null; 949 950 mapIDtoEditor = initialIdToEditorMap(mapIDtoEditor.size()); 952 953 itr = sortedEditorsFromPlugins.iterator(); 955 while (itr.hasNext()) { 956 desc = (IEditorDescriptor) itr.next(); 957 mapIDtoEditor.put(desc.getId(), desc); 958 } 959 } 960 961 964 public void removePropertyListener(IPropertyListener l) { 965 removeListenerObject(l); 966 } 967 968 972 public void saveAssociations() { 973 List editors = new ArrayList (); 975 IPreferenceStore store = WorkbenchPlugin.getDefault() 976 .getPreferenceStore(); 977 978 XMLMemento memento = XMLMemento 979 .createWriteRoot(IWorkbenchConstants.TAG_EDITORS); 980 memento.putString(IWorkbenchConstants.TAG_VERSION, "3.1"); FileEditorMapping maps[] = typeEditorMappings.userMappings(); 982 for (int mapsIndex = 0; mapsIndex < maps.length; mapsIndex++) { 983 FileEditorMapping type = maps[mapsIndex]; 984 IMemento editorMemento = memento 985 .createChild(IWorkbenchConstants.TAG_INFO); 986 editorMemento.putString(IWorkbenchConstants.TAG_NAME, type 987 .getName()); 988 editorMemento.putString(IWorkbenchConstants.TAG_EXTENSION, type 989 .getExtension()); 990 IEditorDescriptor[] editorArray = type.getEditors(); 991 for (int i = 0; i < editorArray.length; i++) { 992 EditorDescriptor editor = (EditorDescriptor) editorArray[i]; 993 if (!editors.contains(editor)) { 994 editors.add(editor); 995 } 996 IMemento idMemento = editorMemento 997 .createChild(IWorkbenchConstants.TAG_EDITOR); 998 idMemento.putString(IWorkbenchConstants.TAG_ID, editorArray[i] 999 .getId()); 1000 } 1001 editorArray = type.getDeletedEditors(); 1002 for (int i = 0; i < editorArray.length; i++) { 1003 EditorDescriptor editor = (EditorDescriptor) editorArray[i]; 1004 if (!editors.contains(editor)) { 1005 editors.add(editor); 1006 } 1007 IMemento idMemento = editorMemento 1008 .createChild(IWorkbenchConstants.TAG_DELETED_EDITOR); 1009 idMemento.putString(IWorkbenchConstants.TAG_ID, editorArray[i] 1010 .getId()); 1011 } 1012 editorArray = type.getDeclaredDefaultEditors(); 1013 for (int i = 0; i < editorArray.length; i++) { 1014 EditorDescriptor editor = (EditorDescriptor) editorArray[i]; 1015 if (!editors.contains(editor)) { 1016 editors.add(editor); 1017 } 1018 IMemento idMemento = editorMemento 1019 .createChild(IWorkbenchConstants.TAG_DEFAULT_EDITOR); 1020 idMemento.putString(IWorkbenchConstants.TAG_ID, editorArray[i] 1021 .getId()); 1022 } 1023 } 1024 Writer writer = null; 1025 try { 1026 writer = new StringWriter (); 1027 memento.save(writer); 1028 writer.close(); 1029 store.setValue(IPreferenceConstants.RESOURCES, writer.toString()); 1030 } catch (IOException e) { 1031 try { 1032 if (writer != null) { 1033 writer.close(); 1034 } 1035 } catch (IOException ex) { 1036 ex.printStackTrace(); 1037 } 1038 MessageDialog.openError((Shell) null, "Saving Problems", "Unable to save resource associations."); return; 1041 } 1042 1043 memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_EDITORS); 1044 Iterator itr = editors.iterator(); 1045 while (itr.hasNext()) { 1046 EditorDescriptor editor = (EditorDescriptor) itr.next(); 1047 IMemento editorMemento = memento 1048 .createChild(IWorkbenchConstants.TAG_DESCRIPTOR); 1049 editor.saveValues(editorMemento); 1050 } 1051 writer = null; 1052 try { 1053 writer = new StringWriter (); 1054 memento.save(writer); 1055 writer.close(); 1056 store.setValue(IPreferenceConstants.EDITORS, writer.toString()); 1057 } catch (IOException e) { 1058 try { 1059 if (writer != null) { 1060 writer.close(); 1061 } 1062 } catch (IOException ex) { 1063 ex.printStackTrace(); 1064 } 1065 MessageDialog.openError((Shell) null, 1066 "Error", "Unable to save resource associations."); return; 1068 } 1069 } 1070 1071 1080 public void setFileEditorMappings(FileEditorMapping[] newResourceTypes) { 1081 typeEditorMappings = new EditorMap(); 1082 for (int i = 0; i < newResourceTypes.length; i++) { 1083 FileEditorMapping mapping = newResourceTypes[i]; 1084 typeEditorMappings.put(mappingKeyFor(mapping), mapping); 1085 } 1086 extensionImages = new HashMap (); 1087 rebuildEditorMap(); 1088 firePropertyChange(PROP_CONTENTS); 1089 } 1090 1091 1094 public void setDefaultEditor(String fileName, String editorId) { 1095 EditorDescriptor desc = (EditorDescriptor) findEditor(editorId); 1096 FileEditorMapping[] mapping = getMappingForFilename(fileName); 1097 if (mapping[0] != null) { 1098 mapping[0].setDefaultEditor(desc); 1099 } 1100 if (mapping[1] != null) { 1101 mapping[1].setDefaultEditor(desc); 1102 } 1103 } 1104 1105 1110 private Object [] sortEditors(List unsortedList) { 1111 Object [] array = new Object [unsortedList.size()]; 1112 unsortedList.toArray(array); 1113 1114 Collections.sort(Arrays.asList(array), comparer); 1115 return array; 1116 } 1117 1118 1123 private void sortInternalEditors() { 1124 Object [] array = sortEditors(sortedEditorsFromPlugins); 1125 sortedEditorsFromPlugins = new ArrayList (); 1126 for (int i = 0; i < array.length; i++) { 1127 sortedEditorsFromPlugins.add(array[i]); 1128 } 1129 } 1130 1131 1136 private static class EditorMap { 1137 HashMap defaultMap = new HashMap (); 1138 1139 HashMap map = new HashMap (); 1140 1141 1147 public void putDefault(String key, FileEditorMapping value) { 1148 defaultMap.put(key, value); 1149 } 1150 1151 1157 public void put(String key, FileEditorMapping value) { 1158 Object result = defaultMap.get(key); 1159 if (value.equals(result)) { 1160 map.remove(key); 1161 } else { 1162 map.put(key, value); 1163 } 1164 } 1165 1166 1175 public FileEditorMapping get(String key) { 1176 Object result = map.get(key); 1177 if (result == null) { 1178 result = defaultMap.get(key); 1179 } 1180 return (FileEditorMapping) result; 1181 } 1182 1183 1189 public FileEditorMapping[] allMappings() { 1190 HashMap merge = (HashMap ) defaultMap.clone(); 1191 merge.putAll(map); 1192 Collection values = merge.values(); 1193 FileEditorMapping result[] = new FileEditorMapping[values.size()]; 1194 return (FileEditorMapping[]) values.toArray(result); 1195 } 1196 1197 1202 public FileEditorMapping[] userMappings() { 1203 Collection values = map.values(); 1204 FileEditorMapping result[] = new FileEditorMapping[values.size()]; 1205 return (FileEditorMapping[]) values.toArray(result); 1206 } 1207 } 1208 1209 1214 public boolean isSystemInPlaceEditorAvailable(String filename) { 1215 return ComponentSupport.inPlaceEditorAvailable(filename); 1216 } 1217 1218 1223 public boolean isSystemExternalEditorAvailable(String filename) { 1224 int nDot = filename.lastIndexOf('.'); 1225 if (nDot >= 0) { 1226 String strName = filename.substring(nDot); 1227 return Program.findProgram(strName) != null; 1228 } 1229 return false; 1230 } 1231 1232 1237 public ImageDescriptor getSystemExternalEditorImageDescriptor( 1238 String filename) { 1239 Program externalProgram = null; 1240 int extensionIndex = filename.lastIndexOf('.'); 1241 if (extensionIndex >= 0) { 1242 externalProgram = Program.findProgram(filename 1243 .substring(extensionIndex)); 1244 } 1245 if (externalProgram == null) { 1246 return null; 1247 } 1248 1249 return new ExternalProgramImageDescriptor(externalProgram); 1250 } 1251 1252 1262 private void removeEditorFromMapping(HashMap map, IEditorDescriptor desc) { 1263 Iterator iter = map.values().iterator(); 1264 FileEditorMapping mapping; 1265 IEditorDescriptor[] editors; 1266 while (iter.hasNext()) { 1267 mapping = (FileEditorMapping) iter.next(); 1268 editors = mapping.getEditors(); 1269 for (int i = 0; i < editors.length; i++) { 1270 if (editors[i] == desc) { 1271 mapping.removeEditor((EditorDescriptor) editors[i]); 1272 break; 1273 } 1274 } 1275 if (editors.length <= 0) { 1276 map.remove(mapping); 1277 break; 1278 } 1279 } 1280 } 1281 1282 1283 1286 public void removeExtension(IExtension source, Object [] objects) { 1287 for (int i = 0; i < objects.length; i++) { 1288 if (objects[i] instanceof EditorDescriptor) { 1289 EditorDescriptor desc = (EditorDescriptor) objects[i]; 1290 1291 sortedEditorsFromPlugins.remove(desc); 1292 mapIDtoEditor.values().remove(desc); 1293 removeEditorFromMapping(typeEditorMappings.defaultMap, desc); 1294 removeEditorFromMapping(typeEditorMappings.map, desc); 1295 } 1297 1298 } 1299 } 1300 1301 1304 public void addExtension(IExtensionTracker tracker, IExtension extension) { 1305 EditorRegistryReader eReader = new EditorRegistryReader(); 1306 IConfigurationElement[] elements = extension.getConfigurationElements(); 1307 for (int i = 0; i < elements.length; i++) { 1308 String id = elements[i].getAttribute(IWorkbenchConstants.TAG_ID); 1309 if (id != null && findEditor(id) != null) { 1310 continue; 1311 } 1312 eReader.readElement(this, elements[i]); 1313 } 1314 } 1315 1316 private IExtensionPoint getExtensionPointFilter() { 1317 return Platform.getExtensionRegistry().getExtensionPoint(PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_EDITOR); 1318 } 1319 1320 1323 public IEditorDescriptor getDefaultEditor(String fileName, IContentType contentType) { 1324 return getEditorForContentType(fileName, contentType); 1325 } 1326 1327 1335 private IEditorDescriptor getEditorForContentType(String filename, 1336 IContentType contentType) { 1337 IEditorDescriptor desc = null; 1338 Object [] contentTypeResults = findRelatedObjects(contentType, filename, relatedRegistry); 1339 if (contentTypeResults != null && contentTypeResults.length > 0) { 1340 desc = (IEditorDescriptor) contentTypeResults[0]; 1341 } 1342 return desc; 1343 } 1344 1345 1348 public IEditorDescriptor[] getEditors(String fileName, IContentType contentType) { 1349 return findRelatedObjects(contentType, fileName, relatedRegistry); 1350 } 1351 1352 1355 public ImageDescriptor getImageDescriptor(String filename, IContentType contentType) { 1356 if (filename == null) { 1357 return getDefaultImage(); 1358 } 1359 1360 if (contentType != null) { 1361 IEditorDescriptor desc = getEditorForContentType(filename, contentType); 1362 if (desc != null) { 1363 ImageDescriptor anImage = (ImageDescriptor) extensionImages.get(desc); 1364 if (anImage != null) { 1365 return anImage; 1366 } 1367 anImage = desc.getImageDescriptor(); 1368 extensionImages.put(desc, anImage); 1369 return anImage; 1370 } 1371 } 1372 String key = mappingKeyFor(filename); 1374 ImageDescriptor anImage = (ImageDescriptor) extensionImages.get(key); 1375 if (anImage != null) { 1376 return anImage; 1377 } 1378 1379 FileEditorMapping[] mapping = getMappingForFilename(filename); 1381 for (int i = 0; i < 2; i++) { 1382 if (mapping[i] != null) { 1383 String mappingKey = mappingKeyFor(mapping[i]); 1385 ImageDescriptor mappingImage = (ImageDescriptor) extensionImages 1386 .get(key); 1387 if (mappingImage != null) { 1388 return mappingImage; 1389 } 1390 IEditorDescriptor editor = mapping[i].getDefaultEditor(); 1392 if (editor != null) { 1393 mappingImage = editor.getImageDescriptor(); 1394 extensionImages.put(mappingKey, mappingImage); 1395 return mappingImage; 1396 } 1397 } 1398 } 1399 1400 anImage = getSystemExternalEditorImageDescriptor(filename); 1402 if (anImage == null) { 1403 anImage = getDefaultImage(); 1404 } 1405 return anImage; 1408 1409 } 1410 1411 1422 private IEditorDescriptor [] findRelatedObjects(IContentType type, String fileName, 1423 RelatedRegistry registry) { 1424 List allRelated = new ArrayList (); 1425 List nonDefaultFileEditors = new ArrayList (); 1426 IEditorDescriptor [] related; 1427 1428 if (fileName != null) { 1429 FileEditorMapping mapping = getMappingFor(fileName); 1430 if (mapping != null) { 1431 related = mapping.getDeclaredDefaultEditors(); 1433 for (int i = 0; i < related.length; i++) { 1434 if (!allRelated.contains(related[i])) { 1436 if (!WorkbenchActivityHelper.filterItem(related[i])) { 1438 allRelated.add(related[i]); 1439 } 1440 } 1441 } 1442 1443 nonDefaultFileEditors.addAll(Arrays.asList(mapping.getEditors())); 1447 } 1448 1449 int index = fileName.lastIndexOf('.'); 1450 if (index > -1) { 1451 String extension = "*" + fileName.substring(index); mapping = getMappingFor(extension); 1453 if (mapping != null) { 1454 related = mapping.getDeclaredDefaultEditors(); 1455 for (int i = 0; i < related.length; i++) { 1456 if (!allRelated.contains(related[i])) { 1458 if (!WorkbenchActivityHelper.filterItem(related[i])) { 1460 allRelated.add(related[i]); 1461 } 1462 } 1463 } 1464 nonDefaultFileEditors.addAll(Arrays.asList(mapping.getEditors())); 1465 } 1466 } 1467 } 1468 1469 if (type != null) { 1470 related = registry.getRelatedObjects(type); 1472 for (int i = 0; i < related.length; i++) { 1473 if (!allRelated.contains(related[i])) { 1475 if (!WorkbenchActivityHelper.filterItem(related[i])) { 1477 allRelated.add(related[i]); 1478 } 1479 } 1480 } 1481 1482 } 1483 1484 if (type != null) { 1485 while ((type = type.getBaseType()) != null) { 1487 related = registry.getRelatedObjects(type); 1488 for (int i = 0; i < related.length; i++) { 1489 if (!allRelated.contains(related[i])) { 1491 if (!WorkbenchActivityHelper.filterItem(related[i])) { 1493 allRelated.add(related[i]); 1494 } 1495 } 1496 } 1497 } 1498 } 1499 1500 for (Iterator i = nonDefaultFileEditors.iterator(); i.hasNext();) { 1502 IEditorDescriptor editor = (IEditorDescriptor) i.next(); 1503 if (!allRelated.contains(editor) && !WorkbenchActivityHelper.filterItem(editor)) { 1504 allRelated.add(editor); 1505 } 1506 } 1507 1508 return (IEditorDescriptor []) allRelated.toArray(new IEditorDescriptor [allRelated 1509 .size()]); 1510 } 1511 1512 1521 public IEditorDescriptor [] getEditorsForContentType(IContentType type) { 1522 ArrayList allRelated = new ArrayList (); 1523 if (type == null) { 1524 return new IEditorDescriptor [0]; 1525 } 1526 1527 Object [] related = relatedRegistry.getRelatedObjects(type); 1528 for (int i = 0; i < related.length; i++) { 1529 if (!allRelated.contains(related[i])) { 1531 if (!WorkbenchActivityHelper.filterItem(related[i])) { 1533 allRelated.add(related[i]); 1534 } 1535 1536 } 1537 } 1538 1539 while ((type = type.getBaseType()) != null) { 1541 related = relatedRegistry.getRelatedObjects(type); 1542 for (int i = 0; i < related.length; i++) { 1543 if (!allRelated.contains(related[i])) { 1545 if (!WorkbenchActivityHelper.filterItem(related[i])) { 1547 allRelated.add(related[i]); 1548 } 1549 } 1550 } 1551 } 1552 1553 return (IEditorDescriptor[]) allRelated.toArray(new IEditorDescriptor[allRelated.size()]); 1554 } 1555 1556 1562 public IFileEditorMapping [] getUnifiedMappings() { 1563 IFileEditorMapping[] standardMappings = PlatformUI.getWorkbench() 1564 .getEditorRegistry().getFileEditorMappings(); 1565 1566 List allMappings = new ArrayList (Arrays.asList(standardMappings)); 1567 IContentType [] contentTypes = Platform.getContentTypeManager().getAllContentTypes(); 1569 for (int i = 0; i < contentTypes.length; i++) { 1570 IContentType type = contentTypes[i]; 1571 String [] extensions = type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); 1572 for (int j = 0; j < extensions.length; j++) { 1573 String extension = extensions[j]; 1574 boolean found = false; 1575 for (Iterator k = allMappings.iterator(); k.hasNext();) { 1576 IFileEditorMapping mapping = (IFileEditorMapping) k.next(); 1577 if ("*".equals(mapping.getName()) && extension.equals(mapping.getExtension())) { found = true; 1579 break; 1580 } 1581 } 1582 if (!found) { 1583 MockMapping mockMapping = new MockMapping(type, "*", extension); allMappings.add(mockMapping); 1585 } 1586 } 1587 1588 String [] filenames = type.getFileSpecs(IContentType.FILE_NAME_SPEC); 1589 for (int j = 0; j < filenames.length; j++) { 1590 String wholename = filenames[j]; 1591 int idx = wholename.indexOf('.'); 1592 String name = idx == -1 ? wholename : wholename.substring(0, idx); 1593 String extension = idx == -1 ? "" : wholename.substring(idx + 1); 1595 boolean found = false; 1596 for (Iterator k = allMappings.iterator(); k.hasNext();) { 1597 IFileEditorMapping mapping = (IFileEditorMapping) k.next(); 1598 if (name.equals(mapping.getName()) && extension.equals(mapping.getExtension())) { 1599 found = true; 1600 break; 1601 } 1602 } 1603 if (!found) { 1604 MockMapping mockMapping = new MockMapping(type, name, extension); 1605 allMappings.add(mockMapping); 1606 } 1607 } 1608 } 1609 1610 return (IFileEditorMapping []) allMappings 1611 .toArray(new IFileEditorMapping [allMappings.size()]); 1612 } 1613 1614} 1615 1616 1617class MockMapping implements IFileEditorMapping { 1618 1619 private IContentType contentType; 1620 private String extension; 1621 private String filename; 1622 1623 MockMapping(IContentType type, String name, String ext) { 1624 this.contentType = type; 1625 this.filename = name; 1626 this.extension = ext; 1627 } 1628 1629 public IEditorDescriptor getDefaultEditor() { 1630 IEditorDescriptor[] candidates = ((EditorRegistry) PlatformUI 1631 .getWorkbench().getEditorRegistry()) 1632 .getEditorsForContentType(contentType); 1633 if (candidates.length == 0) { 1634 return null; 1635 } 1636 return candidates[0]; 1637 } 1638 1639 public IEditorDescriptor[] getEditors() { 1640 return ((EditorRegistry) PlatformUI.getWorkbench().getEditorRegistry()) 1641 .getEditorsForContentType(contentType); 1642 } 1643 1644 public IEditorDescriptor[] getDeletedEditors() { 1645 return new IEditorDescriptor[0]; 1646 } 1647 1648 public String getExtension() { 1649 return extension; 1650 } 1651 1652 public ImageDescriptor getImageDescriptor() { 1653 IEditorDescriptor editor = getDefaultEditor(); 1654 if (editor == null) { 1655 return WorkbenchImages 1656 .getImageDescriptor(ISharedImages.IMG_OBJ_FILE); 1657 } 1658 1659 return editor.getImageDescriptor(); 1660 } 1661 1662 public String getLabel() { 1663 return filename + '.' + extension; 1664 } 1665 1666 public String getName() { 1667 return filename; 1668 } 1669 1670 1673 public boolean equals(Object obj) { 1674 if (this == obj) { 1675 return true; 1676 } 1677 1678 if (!(obj instanceof MockMapping)) { 1679 return false; 1680 } 1681 1682 MockMapping mapping = (MockMapping) obj; 1683 if (!this.filename.equals(mapping.filename)) { 1684 return false; 1685 } 1686 1687 if (!this.extension.equals(mapping.extension)) { 1688 return false; 1689 } 1690 1691 if (!Util.equals(this.getEditors(), mapping.getEditors())) { 1692 return false; 1693 } 1694 return Util.equals(this.getDeletedEditors(), mapping 1695 .getDeletedEditors()); 1696 } 1697} 1698 1699 | Popular Tags |