📄 checkspellingactiondelegate.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.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.MultiEditor;
import com.bdaum.SpellChecker.Messages;
import com.bdaum.SpellChecker.SpellCheckManager;
import com.bdaum.SpellChecker.SpellCheckerPlugin;
import com.bdaum.SpellChecker.SpellCheckingTarget;
import com.bdaum.SpellChecker.views.SpellCorrectionView;
/**
* This class implements the workbench action "Check Spelling".
* It also starts a tracking mechanism that keeps it informed
* about the focus control and the currently activated
* workbench part.
*
* @see IWorkbenchWindowActionDelegate
*/
public class CheckSpellingActionDelegate
implements IWorkbenchWindowActionDelegate,
IEditorActionDelegate, FocusListener, IPartListener {
private IWorkbenchWindow window;
/** The current IAction instance **/
private IAction action;
/** The current focus control element **/
private Control currentFocusControl;
/** The most recent spell checking target under the focus **/
private Control recentFocusControl;
/** Cache for part service **/
private IPartService partService;
/** The most recent active workbench part **/
private IWorkbenchPart activePart;
/**
* Initialize this action delegate
* @see IWorkbenchWindowActionDelegate#init
*/
public void init(IWorkbenchWindow window) {
this.window = window;
// Register with the PartService as a listener
partService = window.getPartService();
activePart = partService.getActivePart();
partService.addPartListener(this);
// Start focus tracking
setCurrentFocusControl();
// Register with the Plugin class.
SpellCheckerPlugin.setSpellCheckingActionDelegate(this);
}
/** * IPartListener methods ** */
public void partActivated(IWorkbenchPart part) {
if (!(part instanceof SpellCorrectionView))
activePart = part;
updateActionEnablement(action);
}
public void partBroughtToTop(
IWorkbenchPart part) {}
public void partClosed(
IWorkbenchPart part) {}
public void partDeactivated(
IWorkbenchPart part) {}
public void partOpened(
IWorkbenchPart part) {}
// Tracking the focus
private void setCurrentFocusControl() {
Shell shell = window.getShell();
if (shell == null || shell.isDisposed()) return;
Display display = shell.getDisplay();
Control newFocusControl = display.getFocusControl();
// Remove and set listeners if focus control has changed
if (newFocusControl != currentFocusControl) {
if (currentFocusControl != null)
currentFocusControl.removeFocusListener(this);
if (newFocusControl != null)
newFocusControl.addFocusListener(this);
currentFocusControl = newFocusControl;
}
if (currentFocusControl != null) {
// Filter events by removing events from SpellCorrectionView.
// This avoids that the SpellChecker action disables
// when the focus moves to the SpellCorrectionView
if (!(partService.getActivePart() instanceof
SpellCorrectionView))
recentFocusControl = currentFocusControl;
return;
}
// Lost track - retry later
recentFocusControl = null;
display.timerExec(100, new Runnable() {
public void run() {
setCurrentFocusControl();
}
});
}
/* FocusListener methods */
public void focusGained(FocusEvent e) {
updateActionEnablement(action);
}
public void focusLost(FocusEvent e) {
setCurrentFocusControl();
}
/**
* Enable or disable action
*
* @param action - the action to be enabled or disabled
*/
private void updateActionEnablement(IAction action) {
// The action is only enabled if there is a valid spell
// checking target
updateActionEnablement(action, getSpellCheckingTarget() != null);
}
/**
* Enable or disable action
*
* @param action - the action to be enabled or disabled
* @param enabled - new enablement state
*/
private void updateActionEnablement(IAction action,
boolean enabled) {
if (action != null) {
action.setEnabled(enabled);
IWorkbenchPage activePage = window.getActivePage();
// Update correction view actions
if (activePage != null) {
SpellCorrectionView view = (SpellCorrectionView) activePage
.findView(
"com.bdaum.SpellChecker.views.SpellCorrectionView");
if (view != null) view.updateActionEnablement();
}
}
}
/**
* Disables the action when process is busy
*
* @param busy - true if process is busy
*/
public void indicateBusy(
final boolean busy) {
window.getShell().getDisplay().syncExec(new Runnable() {
public void run() {
if (busy)
updateActionEnablement(action, false);
else
updateActionEnablement(action);
}
});
}
/**
* Retrieves the enablement state of the action
*
* @return - true if enabled
*/
public boolean isEnabled() {
return (action != null && action.isEnabled());
}
/**
* The current workbench selection has changed.
*
* @see IWorkbenchWindowActionDelegate#selectionChanged
*/
public void selectionChanged(IAction action, ISelection selection) {
// Remember action
this.action = action;
// Update action and view
updateActionEnablement(action);
}
/**
* Execute the spell checking action.
*
* @see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
// skip if not enabled
if (this.action == null || !this.action.isEnabled())
return;
// Get spell checking target
final SpellCheckingTarget target = getSpellCheckingTarget();
if (target != null) {
// Get current Display instance
final Display display = window.getShell().getDisplay();
if (display == null) return;
try {
// Now find the SpellCorrectionView and open it
final SpellCorrectionView view = showCorrectionView();
if (view == null) return;
// Disable action when busy
indicateBusy(true);
// Get the SpellCheckManager
final SpellCheckManager manager = SpellCheckerPlugin
.getManager();
// First cancel any pending spell checking processes.
if (SpellCheckerPlugin.isPending())
manager.abortSpellChecking();
// Start spell checking process in new thread
SpellCheckerPlugin.startThread(new Thread("SpellCheckThread") {
public void run() {
manager.checkDocument(target, display, view);
// Enable action when done
indicateBusy(false);
}
});
} catch (PartInitException e) {
SpellCheckerPlugin.logError(6,
Messages.getString(
"CheckSpellingActionDelegate.Cannot_initialize_SpellCorrectionView"), e);
}
}
}
/**
* Finds and shows the spell checker correction view
*
* @return - the view part
* @throws PartInitException
*/
public SpellCorrectionView showCorrectionView()
throws PartInitException {
// get active page
IWorkbenchPage activePage = window.getActivePage();
// show view
return (activePage == null) ? null
: (SpellCorrectionView) activePage.showView(
"com.bdaum.SpellChecker.views.SpellCorrectionView");
}
/**
* Returns SpellCheckingTarget with active editor and
* a Text or StyledText instance that has the focus
*
* @return - a new SpellCheckingTarget.
*/
private SpellCheckingTarget getSpellCheckingTarget() {
// First determine if the control that has the focus is a
// valid text control
Control validControl =
( ( (recentFocusControl instanceof Text &&
((Text) recentFocusControl).getEditable())
|| (recentFocusControl instanceof StyledText &&
((StyledText) recentFocusControl).getEditable()))
&& !recentFocusControl.isDisposed())
? recentFocusControl : null;
// Check if focus is in workbench window
if (validControl == null || validControl.isDisposed()
|| validControl.getShell() == window.getShell()) {
// Get workbench component
IWorkbenchPart part = window.getPartService().getActivePart();
// Update active part just to make sure we have the
// current one
if (!(part instanceof SpellCorrectionView))
activePart = part;
// Is it a MultiEditor?
// If yes, get the active inner editor.
while (activePart instanceof MultiEditor)
activePart = ((MultiEditor) activePart).getActiveEditor();
// Now create a SpellCheckingTarget
return SpellCheckingTarget.getInstance(activePart,
validControl);
}
// Must be in a dialog box, use text field only.
return SpellCheckingTarget.getInstance(null, validControl);
}
/**
* Dispose action
*/
public void dispose() {
// Deregister as focus listener
if (currentFocusControl != null
&& !currentFocusControl.isDisposed())
currentFocusControl.removeFocusListener(this);
currentFocusControl = null;
// Deregister from the part service
partService.removePartListener(this);
// also deregister from the Plugin class
SpellCheckerPlugin.setSpellCheckingActionDelegate(null);
}
/**
* Action called from editor context menu
*
* @param action - the action
* @param targetEditor - the target editor
*/
public void setActiveEditor(
IAction action, IEditorPart targetEditor) {
this.action = action;
if (targetEditor != null)
init(targetEditor.getSite().getWorkbenchWindow());
else
init(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -