KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filebuffers > ResourceExtensionRegistry


1 /*******************************************************************************
2  * Copyright (c) 2007 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.filebuffers;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.content.IContentDescription;
20 import org.eclipse.core.runtime.content.IContentType;
21
22 import org.eclipse.core.resources.IFile;
23
24 import org.eclipse.core.filebuffers.FileBuffers;
25 import org.eclipse.core.filebuffers.IAnnotationModelFactory;
26 import org.eclipse.core.filebuffers.IDocumentFactory;
27 import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
28 import org.eclipse.core.filebuffers.LocationKind;
29
30
31 /**
32  * This is a special {@link ExtensionsRegistry} that is
33  * optimized for <code>IFile<code>s..
34  *
35  * @since 3.3
36  */

37 public class ResourceExtensionRegistry extends ExtensionsRegistry {
38
39     /**
40      * Returns the set of content types for the given location.
41      *
42      * @param location the location for which to look up the content types
43      * @param locationKind the kind of the given location
44      * @return the set of content types for the location
45      */

46     protected IContentType[] findContentTypes(IPath location, LocationKind locationKind) {
47         if (locationKind != LocationKind.LOCATION) {
48             IFile file= FileBuffers.getWorkspaceFileAtLocation(location);
49             if (file != null)
50                 return findContentTypes(file);
51         }
52         return fContentTypeManager.findContentTypesFor(location.lastSegment());
53     }
54
55     /**
56      * Returns the sharable document factory for the given file.
57      *
58      * @param file the file for which to looked up the factory
59      * @return the sharable document factory
60      */

61     IDocumentFactory getDocumentFactory(IFile file) {
62         IDocumentFactory factory= getDocumentFactory(findContentTypes(file));
63         if (factory == null) {
64             factory= getDocumentFactory(file.getFullPath().lastSegment());
65         }
66         if (factory == null)
67             factory= getDocumentFactory(file.getFileExtension());
68         if (factory == null)
69             factory= getDocumentFactory(WILDCARD);
70         return factory;
71     }
72     
73     /**
74      * Returns the sharable annotation model factory for the given file.
75      *
76      * @param file the file for which to look up the factory
77      * @return the sharable annotation model factory
78      */

79     IAnnotationModelFactory getAnnotationModelFactory(IFile file) {
80         IAnnotationModelFactory factory= getAnnotationModelFactory(findContentTypes(file));
81         if (factory == null)
82             factory= getAnnotationModelFactory(file.getFullPath().lastSegment());
83         if (factory == null)
84             factory= getAnnotationModelFactory(file.getFileExtension());
85         if (factory == null)
86             factory= getAnnotationModelFactory(WILDCARD);
87         return factory;
88     }
89
90     /**
91      * Returns the set of content types for the given location.
92      *
93      * @param file the file for which to look up the content types
94      * @return the set of content types for the location
95      */

96     private IContentType[] findContentTypes(IFile file) {
97         try {
98             IContentDescription contentDescription= file.getContentDescription();
99             if (contentDescription != null) {
100                 IContentType contentType= contentDescription.getContentType();
101                 if (contentType != null)
102                     return new IContentType[] {contentType};
103             }
104         } catch (CoreException x) {
105             // go for the default
106
}
107         return fContentTypeManager.findContentTypesFor(file.getFullPath().lastSegment());
108     }
109
110     /**
111      * Returns the sharable set of document setup participants for the given file.
112      *
113      * @param file the file for which to look up the setup participants
114      * @return the sharable set of document setup participants
115      */

116     IDocumentSetupParticipant[] getDocumentSetupParticipants(IFile file) {
117         Set JavaDoc participants= new HashSet JavaDoc();
118         
119         List JavaDoc p= getDocumentSetupParticipants(findContentTypes(file));
120         if (p != null)
121             participants.addAll(p);
122         
123         p= getDocumentSetupParticipants(file.getFullPath().lastSegment());
124         if (p != null)
125             participants.addAll(p);
126         
127         p= getDocumentSetupParticipants(file.getFileExtension());
128         if (p != null)
129             participants.addAll(p);
130         
131         p= getDocumentSetupParticipants(WILDCARD);
132         if (p != null)
133             participants.addAll(p);
134         
135         IDocumentSetupParticipant[] result= new IDocumentSetupParticipant[participants.size()];
136         participants.toArray(result);
137         return result;
138     }
139
140 }
141
Popular Tags