KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > spelling > DefaultSpellingEngine


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.jdt.internal.ui.text.spelling;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.content.IContentType;
20 import org.eclipse.core.runtime.content.IContentTypeManager;
21
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24
25 import org.eclipse.ui.texteditor.spelling.ISpellingEngine;
26 import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector;
27 import org.eclipse.ui.texteditor.spelling.SpellingContext;
28
29 import org.eclipse.jdt.core.JavaCore;
30
31 /**
32  * Default spelling engine.
33  * <p>
34  * Internally this spelling engine uses a different spelling engine depending on
35  * the {@linkplain IContentType content type}. Currently this engine supports
36  * the text, Java and Java properties file content types.
37  * </p>
38  *
39  * @since 3.1
40  */

41 public class DefaultSpellingEngine implements ISpellingEngine {
42
43     /** Text content type */
44     private static final IContentType TEXT_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);
45
46     /** Java source content type */
47     private static final IContentType JAVA_CONTENT_TYPE= Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE);
48
49     /** Java properties content type */
50     private static final IContentType PROPERTIES_CONTENT_TYPE= Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties"); //$NON-NLS-1$
51

52     /** Available spelling engines by content type */
53     private Map JavaDoc fEngines= new HashMap JavaDoc();
54
55     /**
56      * Initialize concrete engines.
57      */

58     public DefaultSpellingEngine() {
59         if (JAVA_CONTENT_TYPE != null)
60             fEngines.put(JAVA_CONTENT_TYPE, new JavaSpellingEngine());
61         if (PROPERTIES_CONTENT_TYPE != null)
62             fEngines.put(PROPERTIES_CONTENT_TYPE, new PropertiesFileSpellingEngine());
63         if (TEXT_CONTENT_TYPE != null)
64             fEngines.put(TEXT_CONTENT_TYPE, new TextSpellingEngine());
65     }
66
67     /*
68      * @see org.eclipse.ui.texteditor.spelling.ISpellingEngine#check(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IRegion[], org.eclipse.ui.texteditor.spelling.SpellingContext, org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector, org.eclipse.core.runtime.IProgressMonitor)
69      */

70     public void check(IDocument document, IRegion[] regions, SpellingContext context, ISpellingProblemCollector collector, IProgressMonitor monitor) {
71         ISpellingEngine engine= getEngine(context.getContentType());
72         if (engine == null)
73             engine= getEngine(TEXT_CONTENT_TYPE);
74         if (engine != null)
75             engine.check(document, regions, context, collector, monitor);
76     }
77
78     /**
79      * Returns a spelling engine for the given content type or
80      * <code>null</code> if none could be found.
81      *
82      * @param contentType the content type
83      * @return a spelling engine for the given content type or
84      * <code>null</code> if none could be found
85      */

86     private ISpellingEngine getEngine(IContentType contentType) {
87         if (contentType == null)
88             return null;
89
90         if (fEngines.containsKey(contentType))
91             return (ISpellingEngine) fEngines.get(contentType);
92
93         return getEngine(contentType.getBaseType());
94     }
95 }
96
Popular Tags