2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.openapi
.fileTypes
;
18 import com
.intellij
.openapi
.application
.Application
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.application
.CachedSingletonsRegistry
;
21 import com
.intellij
.openapi
.vfs
.VirtualFile
;
22 import org
.jetbrains
.annotations
.NonNls
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
26 import java
.util
.ArrayList
;
27 import java
.util
.List
;
30 * Manages the relationship between filenames and {@link FileType} instances.
33 public abstract class FileTypeManager
{
34 private static FileTypeManager ourInstance
= CachedSingletonsRegistry
.markCachedField(FileTypeManager
.class);
36 * Returns the singleton instance of the FileTypeManager component.
38 * @return the instace of FileTypeManager
40 public static FileTypeManager
getInstance() {
41 if (ourInstance
== null) {
42 Application app
= ApplicationManager
.getApplication();
43 ourInstance
= app
!= null ? app
.getComponent(FileTypeManager
.class) : new MockFileTypeManager();
48 public abstract void registerFileType(@NotNull FileType type
, @NotNull List
<FileNameMatcher
> defaultAssociations
);
51 * Registers a file type.
53 * @param type The file type to register.
54 * @param defaultAssociatedExtensions The list of extensions which cause the file to be
55 * treated as the specified file type. The extensions should not start with '.'.
57 public final void registerFileType(@NotNull FileType type
, @NonNls @Nullable String
... defaultAssociatedExtensions
) {
58 List
<FileNameMatcher
> matchers
= new ArrayList
<FileNameMatcher
>();
59 if (defaultAssociatedExtensions
!= null) {
60 for (String extension
: defaultAssociatedExtensions
) {
61 matchers
.add(new ExtensionFileNameMatcher(extension
));
64 registerFileType(type
, matchers
);
68 * Returns the file type for the specified file name.
70 * @param fileName The file name for which the type is requested.
71 * @return The file type instance, or {@link FileTypes#UNKNOWN} if not found.
75 FileType
getFileTypeByFileName(@NotNull @NonNls String fileName
);
78 * Returns the file type for the specified file.
80 * @param file The file for which the type is requested.
81 * @return The file type instance.
85 FileType
getFileTypeByFile(@NotNull VirtualFile file
);
88 * Returns the file type for the specified extension.
89 * Note that a more general way of obtaining file type is with {@link #getFileTypeByFile(VirtualFile)}
90 * @param extension The extension for which the file type is requested, not including the leading '.'.
91 * @return The file type instance.
95 FileType
getFileTypeByExtension(@NonNls @NotNull String extension
);
98 * Returns the list of all registered file types.
100 * @return The list of file types.
103 public abstract FileType
[] getRegisteredFileTypes();
106 * Checks if the specified file is ignored by IDEA. Ignored files are not visible in
107 * different project views and cannot be opened in the editor. They will neither be parsed nor compiled.
109 * @param name The name of the file to check.
110 * @return true if the file is ignored, false otherwise.
113 public abstract boolean isFileIgnored(@NonNls @NotNull String name
);
116 * Returns the list of extensions associated with the specified file type.
118 * @param type The file type for which the extensions are requested.
119 * @return The array of extensions associated with the file type.
120 * @deprecated since more generic way of associations by means of whildcards exist not every associations matches extension paradigm
123 public abstract String
[] getAssociatedExtensions(@NotNull FileType type
);
127 public abstract List
<FileNameMatcher
> getAssociations(@NotNull FileType type
);
129 public abstract boolean isFileOfType(VirtualFile file
, FileType type
);
132 * Adds a listener for receiving notifications about changes in the list of
133 * registered file types.
135 * @deprecated Subscribe to #FILE_TYPES on any message bus level.
137 * @param listener The listener instance.
140 public abstract void addFileTypeListener(@NotNull FileTypeListener listener
);
143 * Removes a listener for receiving notifications about changes in the list of
144 * registered file types.
146 * @deprecated Subscribe to #FILE_TYPES on any message bus level.
148 * @param listener The listener instance.
151 public abstract void removeFileTypeListener(@NotNull FileTypeListener listener
);
154 * If fileName is already associated with any known file type returns it.
155 * Otherwise asks user to select file type and associates it with fileName extension if any selected.
157 * @param file - a file to ask for file type association
158 * @return Known file type or null. Never returns {@link FileTypes#UNKNOWN}.
161 public abstract FileType
getKnownFileTypeOrAssociate(@NotNull VirtualFile file
);
164 * Returns the semicolon-delimited list of patterns for files and folders
165 * which are excluded from the project structure though they may be present
166 * physically on the HD.
168 * @return Semicolon-delimited list of patterns.
171 public abstract String
getIgnoredFilesList();
174 * Sets new list of semicolon-delimited patterns for files and folders which
175 * are excluded from the project structure.
177 * @param list List of semicolon-delimited patterns.
179 public abstract void setIgnoredFilesList(@NotNull String list
);
182 * Adds an extension to the list of extensions associated with a file type.
184 * @param type the file type to associate the extension with.
185 * @param extension the extension to associate.
188 public final void associateExtension(@NotNull FileType type
, @NotNull @NonNls String extension
) {
189 associate(type
, new ExtensionFileNameMatcher(extension
));
192 public final void associatePattern(@NotNull FileType type
, @NotNull @NonNls String pattern
) {
193 associate(type
, parseFromString(pattern
));
196 public abstract void associate(@NotNull FileType type
, @NotNull FileNameMatcher matcher
);
199 * Removes an extension from the list of extensions associated with a file type.
201 * @param type the file type to remove the extension from.
202 * @param extension the extension to remove.
205 public final void removeAssociatedExtension(@NotNull FileType type
, @NotNull @NonNls String extension
) {
206 removeAssociation(type
, new ExtensionFileNameMatcher(extension
));
209 public abstract void removeAssociation(@NotNull FileType type
, @NotNull FileNameMatcher matcher
);
211 public static FileNameMatcher
parseFromString(String pattern
) {
212 if (pattern
.startsWith("*.") &&
213 pattern
.indexOf('*', 2) < 0 &&
214 pattern
.indexOf('.', 2) < 0 &&
215 pattern
.indexOf('?', 2) < 0) {
216 return new ExtensionFileNameMatcher(pattern
.substring(2).toLowerCase());
219 if (pattern
.contains("*") || pattern
.contains("?")) {
220 return new WildcardFileNameMatcher(pattern
);
223 return new ExactFileNameMatcher(pattern
);
227 public abstract FileType
getStdFileType(@NotNull @NonNls String fileTypeName
);