1 11 package org.eclipse.core.internal.content; 12 13 import java.io.InputStream ; 14 import java.io.Reader ; 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.core.runtime.content.*; 17 import org.eclipse.core.runtime.preferences.*; 18 19 public class ContentTypeManager extends ContentTypeMatcher implements IContentTypeManager, IRegistryChangeListener { 20 private static ContentTypeManager instance; 21 22 public static final int BLOCK_SIZE = 0x400; 23 public static final String CONTENT_TYPE_PREF_NODE = IContentConstants.RUNTIME_NAME + IPath.SEPARATOR + "content-types"; private static final String OPTION_DEBUG_CONTENT_TYPES = "org.eclipse.core.contenttype/debug"; static final boolean DEBUGGING = Activator.getDefault().getBooleanDebugOption(OPTION_DEBUG_CONTENT_TYPES, false); 26 private ContentTypeCatalog catalog; 27 private int catalogGeneration; 28 29 35 protected ListenerList contentTypeListeners = new ListenerList(); 36 37 41 public static void startup() { 43 getInstance(); 44 } 45 46 public static void addRegistryChangeListener(IExtensionRegistry registry) { 47 if (registry == null) 48 return; 49 registry.addRegistryChangeListener(getInstance(), IContentConstants.RUNTIME_NAME); 50 registry.addRegistryChangeListener(getInstance(), IContentConstants.CONTENT_NAME); 51 } 52 53 57 public static void shutdown() { 58 instance = null; 60 } 61 62 public static void removeRegistryChangeListener(IExtensionRegistry registry) { 63 if (registry == null) 64 return; 65 getInstance().invalidate(); 66 registry.removeRegistryChangeListener(getInstance()); 67 } 68 69 74 public static ContentTypeManager getInstance() { 75 if (instance == null) 76 instance = new ContentTypeManager(); 77 return instance; 78 } 79 80 83 static String getFileExtension(String fileName) { 84 int dotPosition = fileName.lastIndexOf('.'); 85 return (dotPosition == -1 || dotPosition == fileName.length() - 1) ? "" : fileName.substring(dotPosition + 1); } 87 88 protected static ILazySource readBuffer(InputStream contents) { 89 return new LazyInputStream(contents, BLOCK_SIZE); 90 } 91 92 protected static ILazySource readBuffer(Reader contents) { 93 return new LazyReader(contents, BLOCK_SIZE); 94 } 95 96 public ContentTypeManager() { 97 super(null, new InstanceScope()); 98 } 99 100 protected ContentTypeBuilder createBuilder(ContentTypeCatalog newCatalog) { 101 return new ContentTypeBuilder(newCatalog); 102 } 103 104 public IContentType[] getAllContentTypes() { 105 ContentTypeCatalog currentCatalog = getCatalog(); 106 IContentType[] types = currentCatalog.getAllContentTypes(); 107 IContentType[] result = new IContentType[types.length]; 108 int generation = currentCatalog.getGeneration(); 109 for (int i = 0; i < result.length; i++) 110 result[i] = new ContentTypeHandler((ContentType) types[i], generation); 111 return result; 112 } 113 114 protected synchronized ContentTypeCatalog getCatalog() { 115 if (catalog != null) 116 return catalog; 118 ContentTypeCatalog newCatalog = new ContentTypeCatalog(this, catalogGeneration++); 120 ContentTypeBuilder builder = createBuilder(newCatalog); 122 try { 123 builder.buildCatalog(); 124 catalog = newCatalog; 126 } catch (InvalidRegistryObjectException e) { 127 } 129 newCatalog.organize(); 130 return newCatalog; 131 } 132 133 public IContentType getContentType(String contentTypeIdentifier) { 134 ContentTypeCatalog currentCatalog = getCatalog(); 135 ContentType type = currentCatalog.getContentType(contentTypeIdentifier); 136 return type == null ? null : new ContentTypeHandler(type, currentCatalog.getGeneration()); 137 } 138 139 public IContentTypeMatcher getMatcher(final ISelectionPolicy customPolicy, final IScopeContext context) { 140 return new ContentTypeMatcher(customPolicy, context == null ? getContext() : context); 141 } 142 143 IEclipsePreferences getPreferences() { 144 return getPreferences(getContext()); 145 } 146 147 IEclipsePreferences getPreferences(IScopeContext context) { 148 return context.getNode(CONTENT_TYPE_PREF_NODE); 149 } 150 151 public void registryChanged(IRegistryChangeEvent event) { 152 if (event.getExtensionDeltas(IContentConstants.RUNTIME_NAME, ContentTypeBuilder.PT_CONTENTTYPES).length == 0 && 154 event.getExtensionDeltas(IContentConstants.CONTENT_NAME, ContentTypeBuilder.PT_CONTENTTYPES).length == 0) 155 return; 156 invalidate(); 157 } 158 159 162 synchronized void invalidate() { 163 if (ContentTypeManager.DEBUGGING && catalog != null) 164 ContentMessages.message("Registry discarded"); catalog = null; 166 } 167 168 171 public void addContentTypeChangeListener(IContentTypeChangeListener listener) { 172 contentTypeListeners.add(listener); 173 } 174 175 178 public void removeContentTypeChangeListener(IContentTypeChangeListener listener) { 179 contentTypeListeners.remove(listener); 180 } 181 182 public void fireContentTypeChangeEvent(ContentType type) { 183 Object [] listeners = this.contentTypeListeners.getListeners(); 184 IContentType eventObject = new ContentTypeHandler(type, type.getCatalog().getGeneration()); 185 for (int i = 0; i < listeners.length; i++) { 186 final ContentTypeChangeEvent event = new ContentTypeChangeEvent(eventObject); 187 final IContentTypeChangeListener listener = (IContentTypeChangeListener) listeners[i]; 188 ISafeRunnable job = new ISafeRunnable() { 189 public void handleException(Throwable exception) { 190 } 192 193 public void run() throws Exception { 194 listener.contentTypeChanged(event); 195 } 196 }; 197 SafeRunner.run(job); 198 } 199 } 200 201 public IContentDescription getSpecificDescription(BasicDescription description) { 202 return description; 204 } 205 } 206 | Popular Tags |