Delete unneeded import that is not compatible with OpenJDK
[fedora-idea.git] / plugins / spellchecker / src / com / intellij / spellchecker / SpellCheckerManager.java
blob7e21685ac21cba3dd081b8ec60dd1b57ab72722d
1 /*
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.spellchecker;
18 import com.intellij.codeHighlighting.HighlightDisplayLevel;
19 import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
20 import com.intellij.openapi.application.ApplicationManager;
21 import com.intellij.openapi.components.ServiceManager;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.extensions.Extensions;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.project.ProjectManager;
26 import com.intellij.spellchecker.dictionary.Dictionary;
27 import com.intellij.spellchecker.dictionary.Loader;
28 import com.intellij.spellchecker.engine.SpellCheckerEngine;
29 import com.intellij.spellchecker.engine.SpellCheckerFactory;
30 import com.intellij.spellchecker.engine.SuggestionProvider;
31 import com.intellij.spellchecker.settings.SpellCheckerSettings;
32 import com.intellij.spellchecker.state.StateLoader;
33 import com.intellij.spellchecker.util.SPFileUtil;
34 import com.intellij.spellchecker.util.Strings;
35 import com.intellij.util.Consumer;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
39 import java.io.InputStream;
40 import java.util.*;
42 public class SpellCheckerManager {
44 private static final Logger LOG = Logger.getInstance("#com.intellij.spellchecker.SpellCheckerManager");
46 private static final int MAX_SUGGESTIONS_THRESHOLD = 5;
47 private static final int MAX_METRICS = 1;
49 private Project project;
51 private SpellCheckerEngine spellChecker;
53 private Dictionary userDictionary;
57 @NotNull
58 private final SuggestionProvider suggestionProvider = new BaseSuggestionProvider(this);
60 private final SpellCheckerSettings settings;
62 public static SpellCheckerManager getInstance(Project project) {
63 return ServiceManager.getService(project, SpellCheckerManager.class);
66 public SpellCheckerManager(Project project, SpellCheckerSettings settings) {
67 this.project = project;
68 this.settings = settings;
69 reloadConfiguration();
73 public Project getProject() {
74 return project;
77 public Dictionary getUserDictionary() {
78 return userDictionary;
81 public void reloadConfiguration() {
82 spellChecker = SpellCheckerFactory.create();
83 fillEngineDictionary();
86 private void fillEngineDictionary() {
87 spellChecker.reset();
88 final StateLoader stateLoader = new StateLoader(project);
89 final List<Loader> loaders = new ArrayList<Loader>();
90 // Load bundled dictionaries from corresponding jars
91 for (BundledDictionaryProvider provider : Extensions.getExtensions(BundledDictionaryProvider.EP_NAME)) {
92 for (String dictionary : provider.getBundledDictionaries()) {
93 if (this.settings == null || !this.settings.getBundledDisabledDictionariesPaths().contains(dictionary)) {
94 final Class<? extends BundledDictionaryProvider> loaderClass = provider.getClass();
95 final InputStream stream = loaderClass.getResourceAsStream(dictionary);
96 if (stream != null){
97 loaders.add(new StreamLoader(stream));
98 } else {
99 LOG.warn("Couldn't load dictionary '" + dictionary + "' for loader '" + loaderClass + "'");
104 if (this.settings != null && this.settings.getDictionaryFoldersPaths() != null) {
105 final Set<String> disabledDictionaries = settings.getDisabledDictionariesPaths();
106 for (String folder : this.settings.getDictionaryFoldersPaths()) {
107 SPFileUtil.processFilesRecursively(folder, new Consumer<String>() {
108 public void consume(final String s) {
109 if (!disabledDictionaries.contains(s)) {
110 loaders.add(new FileLoader(s));
117 loaders.add(stateLoader);
118 for (Loader loader : loaders) {
119 spellChecker.loadDictionary(loader);
121 userDictionary = stateLoader.getDictionary();
125 public boolean hasProblem(@NotNull String word) {
126 return !spellChecker.isCorrect(word);
129 public void acceptWordAsCorrect(@NotNull String word) {
130 final String transformed = spellChecker.getTransformation().transform(word);
131 if (transformed != null) {
132 userDictionary.addToDictionary(transformed);
133 spellChecker.addToDictionary(transformed);
137 public void update(@Nullable Collection<String> words, SpellCheckerSettings allDictionaries) {
138 userDictionary.replaceAll(words);
139 reloadConfiguration();
140 restartInspections();
144 @NotNull
145 public List<String> getBundledDictionaries() {
146 final ArrayList<String> dictionaries = new ArrayList<String>();
147 for (BundledDictionaryProvider provider : Extensions.getExtensions(BundledDictionaryProvider.EP_NAME)) {
148 dictionaries.addAll(Arrays.asList(provider.getBundledDictionaries()));
150 return dictionaries;
153 @NotNull
154 public static HighlightDisplayLevel getHighlightDisplayLevel() {
155 return HighlightDisplayLevel.find(SpellCheckerSeveritiesProvider.TYPO);
158 @NotNull
159 public List<String> getSuggestions(@NotNull String text) {
160 return suggestionProvider.getSuggestions(text);
165 @NotNull
166 protected List<String> getRawSuggestions(@NotNull String word) {
167 if (!spellChecker.isCorrect(word)) {
168 List<String> suggestions = spellChecker.getSuggestions(word, MAX_SUGGESTIONS_THRESHOLD, MAX_METRICS);
169 if (suggestions.size() != 0) {
170 boolean capitalized = Strings.isCapitalized(word);
171 boolean upperCases = Strings.isUpperCase(word);
172 if (capitalized) {
173 Strings.capitalize(suggestions);
175 else if (upperCases) {
176 Strings.upperCase(suggestions);
179 List<String> result = new ArrayList<String>();
180 for (String s : suggestions) {
181 if (!result.contains(s)) {
182 result.add(s);
185 return result;
187 return Collections.emptyList();
190 @NotNull
191 public List<String> getVariants(@NotNull String prefix) {
193 return Collections.emptyList();
197 public void restartInspections() {
198 ApplicationManager.getApplication().invokeLater(new Runnable() {
199 public void run() {
200 Project[] projects = ProjectManager.getInstance().getOpenProjects();
201 for (Project project : projects) {
202 if (project.isInitialized() && project.isOpen() && !project.isDefault()) {
203 DaemonCodeAnalyzer.getInstance(project).restart();