KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > ContentTypeManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.content;
12
13 import java.io.InputStream JavaDoc;
14 import java.io.Reader JavaDoc;
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 JavaDoc CONTENT_TYPE_PREF_NODE = IContentConstants.RUNTIME_NAME + IPath.SEPARATOR + "content-types"; //$NON-NLS-1$
24
private static final String JavaDoc OPTION_DEBUG_CONTENT_TYPES = "org.eclipse.core.contenttype/debug"; //$NON-NLS-1$;
25
static final boolean DEBUGGING = Activator.getDefault().getBooleanDebugOption(OPTION_DEBUG_CONTENT_TYPES, false);
26     private ContentTypeCatalog catalog;
27     private int catalogGeneration;
28
29     /**
30      * List of registered listeners (element type:
31      * <code>IContentTypeChangeListener</code>).
32      * These listeners are to be informed when
33      * something in a content type changes.
34      */

35     protected ListenerList contentTypeListeners = new ListenerList();
36
37     /**
38      * Creates and initializes the platform's content type manager. A reference to the
39      * content type manager can later be obtained by calling <code>getInstance()</code>.
40      */

41     // TODO we can remove this sometime, it is no longer needed
42
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     /**
54      * Shuts down the platform's content type manager. After this call returns,
55      * the content type manager will be closed for business.
56      */

57     public static void shutdown() {
58         // there really is nothing left to do except null the instance.
59
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     /**
70      * Obtains this platform's content type manager.
71      *
72      * @return the content type manager
73      */

74     public static ContentTypeManager getInstance() {
75         if (instance == null)
76             instance = new ContentTypeManager();
77         return instance;
78     }
79
80     /*
81      * Returns the extension for a file name (omitting the leading '.').
82      */

83     static String JavaDoc getFileExtension(String JavaDoc fileName) {
84         int dotPosition = fileName.lastIndexOf('.');
85         return (dotPosition == -1 || dotPosition == fileName.length() - 1) ? "" : fileName.substring(dotPosition + 1); //$NON-NLS-1$
86
}
87
88     protected static ILazySource readBuffer(InputStream JavaDoc contents) {
89         return new LazyInputStream(contents, BLOCK_SIZE);
90     }
91
92     protected static ILazySource readBuffer(Reader JavaDoc 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             // already has one
117
return catalog;
118         // create new catalog
119
ContentTypeCatalog newCatalog = new ContentTypeCatalog(this, catalogGeneration++);
120         // build catalog by parsing the extension registry
121
ContentTypeBuilder builder = createBuilder(newCatalog);
122         try {
123             builder.buildCatalog();
124             // only remember catalog if building it was successful
125
catalog = newCatalog;
126         } catch (InvalidRegistryObjectException e) {
127             // the registry has stale objects... just don't remember the returned (incomplete) catalog
128
}
129         newCatalog.organize();
130         return newCatalog;
131     }
132
133     public IContentType getContentType(String JavaDoc 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         // no changes related to the content type registry
153
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     /**
160      * Causes a new catalog to be built afresh next time an API call is made.
161      */

162     synchronized void invalidate() {
163         if (ContentTypeManager.DEBUGGING && catalog != null)
164             ContentMessages.message("Registry discarded"); //$NON-NLS-1$
165
catalog = null;
166     }
167
168     /* (non-Javadoc)
169      * @see IContentTypeManager#addContentTypeChangeListener(IContentTypeChangeListener)
170      */

171     public void addContentTypeChangeListener(IContentTypeChangeListener listener) {
172         contentTypeListeners.add(listener);
173     }
174
175     /* (non-Javadoc)
176      * @see IContentTypeManager#removeContentTypeChangeListener(IContentTypeChangeListener)
177      */

178     public void removeContentTypeChangeListener(IContentTypeChangeListener listener) {
179         contentTypeListeners.remove(listener);
180     }
181
182     public void fireContentTypeChangeEvent(ContentType type) {
183         Object JavaDoc[] 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 JavaDoc exception) {
190                     // already logged in SafeRunner#run()
191
}
192
193                 public void run() throws Exception JavaDoc {
194                     listener.contentTypeChanged(event);
195                 }
196             };
197             SafeRunner.run(job);
198         }
199     }
200
201     public IContentDescription getSpecificDescription(BasicDescription description) {
202         // this is the platform content type manager, no specificities
203
return description;
204     }
205 }
206
Popular Tags