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
);
130 * Adds a listener for receiving notifications about changes in the list of
131 * registered file types.
133 * @deprecated Subscribe to #FILE_TYPES on any message bus level.
135 * @param listener The listener instance.
138 public abstract void addFileTypeListener(@NotNull FileTypeListener listener
);
141 * Removes a listener for receiving notifications about changes in the list of
142 * registered file types.
144 * @deprecated Subscribe to #FILE_TYPES on any message bus level.
146 * @param listener The listener instance.
149 public abstract void removeFileTypeListener(@NotNull FileTypeListener listener
);
152 * If fileName is already associated with any known file type returns it.
153 * Otherwise asks user to select file type and associates it with fileName extension if any selected.
155 * @param file - a file to ask for file type association
156 * @return Known file type or null. Never returns {@link FileTypes#UNKNOWN}.
159 public abstract FileType
getKnownFileTypeOrAssociate(@NotNull VirtualFile file
);
162 * Returns the semicolon-delimited list of patterns for files and folders
163 * which are excluded from the project structure though they may be present
164 * physically on the HD.
166 * @return Semicolon-delimited list of patterns.
169 public abstract String
getIgnoredFilesList();
172 * Sets new list of semicolon-delimited patterns for files and folders which
173 * are excluded from the project structure.
175 * @param list List of semicolon-delimited patterns.
177 public abstract void setIgnoredFilesList(@NotNull String list
);
180 * Adds an extension to the list of extensions associated with a file type.
182 * @param type the file type to associate the extension with.
183 * @param extension the extension to associate.
186 public final void associateExtension(@NotNull FileType type
, @NotNull @NonNls String extension
) {
187 associate(type
, new ExtensionFileNameMatcher(extension
));
190 public final void associatePattern(@NotNull FileType type
, @NotNull @NonNls String pattern
) {
191 associate(type
, parseFromString(pattern
));
194 public abstract void associate(@NotNull FileType type
, @NotNull FileNameMatcher matcher
);
197 * Removes an extension from the list of extensions associated with a file type.
199 * @param type the file type to remove the extension from.
200 * @param extension the extension to remove.
203 public final void removeAssociatedExtension(@NotNull FileType type
, @NotNull @NonNls String extension
) {
204 removeAssociation(type
, new ExtensionFileNameMatcher(extension
));
207 public abstract void removeAssociation(@NotNull FileType type
, @NotNull FileNameMatcher matcher
);
209 public static FileNameMatcher
parseFromString(String pattern
) {
210 if (pattern
.startsWith("*.") &&
211 pattern
.indexOf('*', 2) < 0 &&
212 pattern
.indexOf('.', 2) < 0 &&
213 pattern
.indexOf('?', 2) < 0) {
214 return new ExtensionFileNameMatcher(pattern
.substring(2).toLowerCase());
217 if (pattern
.contains("*") || pattern
.contains("?")) {
218 return new WildcardFileNameMatcher(pattern
);
221 return new ExactFileNameMatcher(pattern
);
225 public abstract FileType
getStdFileType(@NotNull @NonNls String fileTypeName
);