KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.jdt.internal.ui.text.spelling;
13
14 import org.eclipse.core.runtime.IProgressMonitor;
15
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18
19 import org.eclipse.ui.texteditor.spelling.ISpellingEngine;
20 import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector;
21 import org.eclipse.ui.texteditor.spelling.SpellingContext;
22
23 import org.eclipse.jdt.ui.PreferenceConstants;
24
25 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine;
26 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
27 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent;
28 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEventListener;
29
30 /**
31  * Internal abstract spelling engine, subclasses provide a content-type specific implementation.
32  *
33  * @since 3.1
34  */

35 public abstract class SpellingEngine implements ISpellingEngine {
36
37     /**
38      * A named preference that controls the maximum number of problems reported during spell checking.
39      * <p>
40      * Value is of type <code>Integer</code>.
41      * </p>
42      * XXX: will become API in 3.4
43      *
44      * @since 3.3.1
45      */

46     public final static String JavaDoc SPELLING_PROBLEMS_THRESHOLD= "spelling_problems_threshold"; //$NON-NLS-1$
47

48     
49     /**
50      * {@link ISpellEvent}listener that forwards events as
51      * {@link org.eclipse.ui.texteditor.spelling.SpellingProblem}.
52      */

53     protected static class SpellEventListener implements ISpellEventListener {
54
55         /** Spelling problem collector */
56         private ISpellingProblemCollector fCollector;
57         
58         /**
59          * The document.
60          * @since 3.3
61          */

62         private IDocument fDocument;
63
64         private int fProblemsThreshold;
65         private int fProblemCount;
66
67         /**
68          * Initialize with the given spelling problem collector.
69          *
70          * @param collector the spelling problem collector
71          * @param document the document
72          */

73         public SpellEventListener(ISpellingProblemCollector collector, IDocument document) {
74             fCollector= collector;
75             fDocument= document;
76             fProblemsThreshold= PreferenceConstants.getPreferenceStore().getInt(SpellingEngine.SPELLING_PROBLEMS_THRESHOLD);
77         }
78
79         /*
80          * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEventListener#handle(org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent)
81          */

82         public void handle(ISpellEvent event) {
83             if (isProblemsThresholdReached())
84                 return;
85             fProblemCount++;
86             fCollector.accept(new JavaSpellingProblem(event, fDocument));
87         }
88         
89         boolean isProblemsThresholdReached() {
90             return fProblemCount >= fProblemsThreshold;
91         }
92     }
93
94     /*
95      * @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)
96      */

97     public void check(IDocument document, IRegion[] regions, SpellingContext context, ISpellingProblemCollector collector, IProgressMonitor monitor) {
98         if (collector != null) {
99             final ISpellCheckEngine spellingEngine= SpellCheckEngine.getInstance();
100             ISpellChecker checker= spellingEngine.getSpellChecker();
101             if (checker != null)
102                 check(document, regions, checker, collector, monitor);
103         }
104     }
105
106     /**
107      * Spell checks the given document regions with the given arguments.
108      *
109      * @param document the document
110      * @param regions the regions
111      * @param checker the spell checker
112      * @param collector the spelling problem collector
113      * @param monitor the progress monitor, can be <code>null</code>
114      */

115     protected abstract void check(IDocument document, IRegion[] regions, ISpellChecker checker, ISpellingProblemCollector collector, IProgressMonitor monitor);
116
117 }
118
Popular Tags