?? spellcheckmanager.java
字號:
/*******************************************************************************
* Copyright (c) 2003 Berthold Daum.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Berthold Daum
*******************************************************************************/
package com.bdaum.SpellChecker;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import com.bdaum.SpellChecker.actions.CheckSpellingActionDelegate;
import com.bdaum.SpellChecker.preferences.SpellCheckerPreferences;
import com.bdaum.SpellChecker.views.SpellCorrectionView;
import com.swabunga.spell.engine.GenericSpellDictionary;
import com.swabunga.spell.engine.SpellDictionary;
import com.swabunga.spell.engine.SpellDictionaryDichoDisk;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
import com.swabunga.spell.event.SpellChecker;
/**
* This class organizes the interaction between the SpellChecker, the user
* interface, and the spell checker configuration.
*/
public class SpellCheckManager implements SpellCheckListener {
// File extension for phonetic dictionaries
private static final String PHONETICEXTENSION = ".phon";
// File extension for user dictionary
private static final String USEREXTENSION = ".user";
// Tuple representing an empty text selection
private static final Point NOSELECTION = new Point(0, 0);
/* The engines for the spell checker */
private Map engineMap = new HashMap(10);
private SpellChecker currentEngine;
/* The spell checking view */
private SpellCorrectionView correctionViewer;
/* Currently active preferences */
private SpellCheckerPreferences currentPreferences;
/* The configuration */
private SpellCheckConfiguration config = new SpellCheckConfiguration();
/* The curent spell checking target */
private SpellCheckingTarget currentTarget;
/* The current selection */
private Point currentSelection = NOSELECTION;
/* current Display */
private Display display;
/* Indicator for aborting the current spell checking process */
private boolean abort = false;
/* Current tokenizer name */
private String currentName;
/**
* Checks the document content. This method is thread safe.
*
* @param target -
* the spell checking target
* @param display -
* the current Display instance
* @param correctionViewer -
* the spell checking view
*/
public synchronized void checkDocument(SpellCheckingTarget target,
Display display, SpellCorrectionView correctionViewer) {
this.display = display;
// First reset the current preferences to the default preferences
currentPreferences = SpellCheckerPlugin.getDefault().getPreferences();
// Save parameters
this.correctionViewer = correctionViewer;
this.currentTarget = target;
// Reset tokenizer name and selection
currentName = Messages
.getString("SpellCheckManager.Default_Spell_Checker");
currentSelection = NOSELECTION;
// This following must be done in the SWT thread to avoid
// thread conflicts
display.syncExec(new Runnable() {
public void run() {
// Retrieve current text selection
currentSelection = currentTarget.getSelection();
}
});
// Get preferences and find tokenizer
IEditorInput input = target.getEditorInput();
if (input != null) {
// We deal with a editor input object and retrieve an input
// specific tokenizer
currentTarget.tokenizer = getDocumentWordTokenizer(input);
if (currentTarget.tokenizer != null)
performCheck();
} else {
// We cannot determine the text type
// and use the default preferences
currentPreferences = SpellCheckerPlugin.getDefault()
.getPreferences();
currentTarget.tokenizer = new DocumentWordTokenizer();
performCheck();
}
}
/**
* Returns the current spell check target
*
* @return - Target object
*/
public SpellCheckingTarget getCurrentTarget() {
return currentTarget;
}
/**
* Returns the current tokenizer name
*
* @return - tokenizer name
*/
public String getCurrentName() {
return currentName;
}
/**
* Retrieves a suitable tokenizer for a given input
*
* @param input -
* the current editor input
* @return - the tokenizer configured for this input type
*/
private AbstractDocumentWordTokenizer getDocumentWordTokenizer(
IEditorInput input) {
// Get file extension form editor input
String doctype = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input)
.getFile().getFullPath().getFileExtension()
: "*";
// Search for extensions to extension point "documentTokenizer"
// First get the plug-in registry
IExtensionRegistry reg = Platform.getExtensionRegistry();
// Now get the extension point
IExtensionPoint exPoint = reg.getExtensionPoint(SpellCheckerPlugin
.getId(), "documentTokenizer");
// Fetch all installed extensions for this extension point.
// This can be more than one if several plug-ins were installed.
IExtension[] tokenizers = exPoint.getExtensions();
for (int i = 0; i < tokenizers.length; i++) {
IExtension extension = tokenizers[i];
// Now fetch all tokenizer specifications
// Each extension can define several of these specifications
IConfigurationElement[] configurations = extension
.getConfigurationElements();
for (int j = 0; j < configurations.length; j++) {
IConfigurationElement element = configurations[j];
// For each tokenizer we step through the list
// of declared file extensions
StringTokenizer st = new StringTokenizer(element
.getAttribute("extensions"));
while (st.hasMoreElements()) {
String ext = st.nextToken();
if (ext.equalsIgnoreCase(doctype)) {
// Positive
try {
// Now fetch the plug-in specific preferences
currentPreferences = (SpellCheckerPreferences) element
.createExecutableExtension("preferences");
} catch (CoreException e) {
// No luck, we use the default preferences
}
currentName = element.getAttribute("name");
try {
// Try to create a tokenizer instance
return (AbstractDocumentWordTokenizer) element
.createExecutableExtension("class");
} catch (CoreException e) {
SpellCheckerPlugin
.logError(
1,
Messages
.getString("SpellCheckManager.Could_not_create_tokenizer"),
e);
}
}
}
}
}
// No matching extension found. Use the default tokenizer.
return new DocumentWordTokenizer();
}
/**
* Returns the current SpellCheckerPreference.
*
* @return - current SpellCheckerPreferences
*/
public SpellCheckerPreferences getPreferences() {
return currentPreferences;
}
/**
* Runs the jazzy engine
*/
private void performCheck() {
// Initialize the tokenizer
IDocument document = currentTarget.getDocument();
currentTarget.tokenizer.init(document, currentSelection.x,
currentSelection.y - currentSelection.x, config);
// Reset the abort flag
abort = false;
// Fetch the engine
SpellChecker engine = getEngine();
if (engine != null) {
// Run the engine
engine.checkSpelling(currentTarget.tokenizer);
// Reset the spell checking view
correctionViewer.setInput(null, this, currentTarget.isEditable);
// Restore original selection
setSelection(currentSelection.x, currentSelection.y);
// Done
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -