?? spellcorrectionview.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.views;import java.util.List;import org.eclipse.jface.action.*;import org.eclipse.jface.viewers.*;import org.eclipse.swt.SWT;import org.eclipse.swt.events.KeyAdapter;import org.eclipse.swt.events.KeyEvent;import org.eclipse.swt.events.ModifyEvent;import org.eclipse.swt.events.ModifyListener;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Menu;import org.eclipse.swt.widgets.Text;import org.eclipse.ui.IActionBars;import org.eclipse.ui.help.WorkbenchHelp;import org.eclipse.ui.part.ViewPart;import com.bdaum.SpellChecker.SpellCheckManager;import com.bdaum.SpellChecker.SpellCheckerImages;import com.bdaum.SpellChecker.SpellCheckerPlugin;import com.bdaum.SpellChecker.actions.CheckSpellingActionDelegate;import com.bdaum.SpellChecker.actions.CorrectionViewAction;import com.swabunga.spell.engine.Word;import com.swabunga.spell.event.SpellCheckEvent;import com.bdaum.SpellChecker.Messages;public class SpellCorrectionView extends ViewPart { // Text constant for cases where we have no correction proposals private static final String[] NOPROPOSALS = new String[] { Messages .getString("SpellCorrectionView.No_suggestions") }; /* Widgets */ // The current Display instance private Display display; // The Text widget for displaying the bad word private Text badWord; // The TableViewer of this view private TableViewer viewer; /* View actions */ // Toolbar and menu actions private IAction ignoreAction; private IAction ignoreAllAction; private IAction cancelAction; private IAction replaceAction; private IAction replaceAllAction; private IAction addToDictionaryAction; // The double click action private Action doubleClickAction; /* The manager */ private SpellCheckManager spellCheckManager; /* The current spelling error event */ private SpellCheckEvent currentEvent; /* Indicator if replacements are allowed */ private boolean documentIsEditable; /** * The ViewContentProvider creates the table content from the spelling error * event. */ class ViewContentProvider implements IStructuredContentProvider { // Current spelling error event private Object spEvent; /** * This method is called by the TableViewer after setInput() was called. */ public void inputChanged(Viewer v, Object oldInput, Object newInput) { spEvent = newInput; } /** * This method is called when the table is refreshed */ public Object[] getElements(Object parent) { // Fetch correction proposals from the spelling error event if (spEvent instanceof SpellCheckEvent) { List suggestions = ((SpellCheckEvent) spEvent).getSuggestions(); int s = suggestions.size(); // Check if we have proposals if (s > 0) { // Return correction proposals as an array to the // TableViewer Word[] sugArray = new Word[s]; suggestions.toArray(sugArray); return sugArray; } } return NOPROPOSALS; } public void dispose() { } } /** * The ViewLabelProvider creates the individual table entries */ class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { /* * Process text for table entry */ public String getColumnText(Object obj, int index) { return getText(obj); } public Image getColumnImage(Object obj, int index) { return null; } public Image getImage(Object obj) { return null; } } public void createPartControl(Composite parent) { // Fetch Display instance for later usage display = parent.getDisplay(); // Set GridLayout parent.setLayout(new GridLayout()); // Create Text widget for display of bad word badWord = new Text(parent, SWT.BORDER); badWord.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Disable/Enable actions when content of text field changes badWord.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { updateActionEnablement(); } }); // Create table viewer viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH)); // Set ContentProvider and LabelProvider viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); // Listener for selection of table elements // Selected elements are copied to text field badWord. viewer.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { ISelection sel = event.getSelection(); if (sel instanceof IStructuredSelection) { Object obj = ((IStructuredSelection) sel).getFirstElement(); // Check for Word type to exclude NOPROPOSALS // from selection if (obj instanceof Word) badWord.setText(obj.toString()); } } }); // Add KeyListener to support keyboard shortcuts viewer.getControl().addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.character == '+') addToDictionaryAction.run(); else switch (e.keyCode) { case 13: if ((e.stateMask & SWT.CTRL) != 0) { if ((e.stateMask & SWT.SHIFT) != 0) replaceAllAction.run(); else replaceAction.run(); } else { if ((e.stateMask & SWT.SHIFT) != 0) ignoreAllAction.run(); else ignoreAction.run(); } break; case SWT.ESC: cancelAction.run(); break; } } }); // Create actions makeActions(); // Add the context menu hookContextMenu(); // Add the double click action hookDoubleClickAction(); // Create the toolbar contributeToActionBars(); // Initialize the actions updateActionEnablement(); // Create help context WorkbenchHelp.setHelp(parent, "com.bdaum.SpellChecker.correctionView_context"); } /** * Set focus to TableViewer */ public void setFocus() { viewer.getControl().setFocus(); } // Add double click action private void hookDoubleClickAction() { viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { doubleClickAction.run(); } }); } // Add context menu private void hookContextMenu() { // Create new menu manager MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$ // Remove all menu items before building the menu menuMgr.setRemoveAllWhenShown(true); // Event processing for context menu menuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { SpellCorrectionView.this.fillContribution(manager); } }); // Create menu Menu menu = menuMgr.createContextMenu(viewer.getControl()); viewer.getControl().setMenu(menu); // Register context menu with workbench site getSite().registerContextMenu(menuMgr, viewer); } private void contributeToActionBars() { // Fetch action bar from workbench site IActionBars bars = getViewSite().getActionBars(); // Create the drop-down menu fillContribution(bars.getMenuManager()); // Create the toolbar fillContribution(bars.getToolBarManager()); } // Fill menus or toolbar with actions private void fillContribution(IContributionManager manager) { manager.add(replaceAction); manager.add(replaceAllAction); manager.add(new Separator()); manager.add(addToDictionaryAction); manager.add(new Separator()); manager.add(ignoreAction); manager.add(ignoreAllAction); manager.add(cancelAction); // Other plug-ins can insert new actions here manager.add(new Separator("Additions")); } // Create actions private void makeActions() { replaceAction = createAction(CorrectionViewAction.REPLACE, Messages .getString("SpellCorrectionView.Replace"), Messages .getString("SpellCorrectionView.Replace_occurrence"), SpellCheckerImages.IMG_REPLACE); replaceAllAction = createAction( CorrectionViewAction.REPLACEALL, Messages.getString("SpellCorrectionView.Replace_all"), Messages .getString("SpellCorrectionView.Replace_all_occurrences"), SpellCheckerImages.IMG_REPLACEALL); addToDictionaryAction = createAction(CorrectionViewAction.ADD, Messages .getString("SpellCorrectionView.Add_to_dictionary"), Messages .getString("SpellCorrectionView.Add_word_to_dictionary"), SpellCheckerImages.IMG_ADDTODICTIONARY); ignoreAction = createAction(CorrectionViewAction.IGNORE, Messages .getString("SpellCorrectionView.Ignore"), Messages .getString("SpellCorrectionView.Ignore_spelling_problem"), SpellCheckerImages.IMG_IGNORE); ignoreAllAction = createAction(CorrectionViewAction.IGNOREALL, Messages .getString("SpellCorrectionView.Ignore_all"), Messages .getString("SpellCorrectionView.Ignore_for_all_occurrences"), SpellCheckerImages.IMG_IGNOREALL); cancelAction = createAction(CorrectionViewAction.CANCEL, Messages .getString("SpellCorrectionView.StartCancel"), Messages .getString("SpellCorrectionView.StartCancel_spell_checking"), SpellCheckerImages.IMG_CANCEL); doubleClickAction = new CorrectionViewAction(this, CorrectionViewAction.DOUBLECLICK); } // Create a single action private IAction createAction(int operation, String label, String toolTip, String imageID) { IAction action = new CorrectionViewAction(this, operation); action.setText(label); action.setToolTipText(toolTip); SpellCheckerImages.setImageDescriptors(action, "lcl16", imageID); return action; } /** * Perform operation for an action * * @param operation - * the operation code */ public void performOperation(int operation) { if (currentEvent == null) { if (operation == CorrectionViewAction.CANCEL) // Start spell checking via action delegate SpellCheckerPlugin.getSpellCheckingActionDelegate().run(null); return; } switch (operation) { case CorrectionViewAction.DOUBLECLICK: if (!documentIsEditable) return; ISelection selection = viewer.getSelection(); Object obj = ((IStructuredSelection) selection).getFirstElement(); if (!(obj instanceof Word)) return; currentEvent.replaceWord(obj.toString(), false); break; case CorrectionViewAction.REPLACE: currentEvent.replaceWord(badWord.getText(), false); break; case CorrectionViewAction.REPLACEALL: if (!documentIsEditable) return; currentEvent.replaceWord(badWord.getText(), true); break; case CorrectionViewAction.ADD: String newWord = badWord.getText(); String originalBadWord = currentEvent.getInvalidWord(); if (!documentIsEditable && !originalBadWord.equals(newWord)) return; currentEvent.addToDictionary(newWord); break; case CorrectionViewAction.IGNORE: currentEvent.ignoreWord(false); break; case CorrectionViewAction.IGNOREALL: currentEvent.ignoreWord(true); break; case CorrectionViewAction.CANCEL: currentEvent.cancel(); break; } signalEventProcessed(); } // Signal end of event processing private void signalEventProcessed() { // Release waiting manager spellCheckManager.continueSpellChecking(); // Reset current event currentEvent = null; // Update viewer updateView(); } /** * Update actions. Most of the actions are disabled when no more events are * pending. The cancel action, however, now acts as a start action. */ public void updateActionEnablement() { boolean pendingEvent = (currentEvent != null); // Enable or disable actions if (pendingEvent & documentIsEditable) { boolean modified = !currentEvent.getInvalidWord().equals( badWord.getText()); replaceAction.setEnabled(modified); replaceAllAction.setEnabled(modified); } else { replaceAction.setEnabled(false); replaceAllAction.setEnabled(false); } ignoreAction.setEnabled(pendingEvent); ignoreAllAction.setEnabled(pendingEvent); addToDictionaryAction.setEnabled(pendingEvent); // Update cancel action if (pendingEvent) { SpellCheckerImages.setImageDescriptors(cancelAction, "lcl16", SpellCheckerImages.IMG_CANCEL); cancelAction.setChecked(true); cancelAction.setToolTipText(Messages .getString("SpellCorrectionView.Cancel_spell_checking")); cancelAction.setEnabled(true); } else { SpellCheckerImages.setImageDescriptors(cancelAction, "lcl16", SpellCheckerImages.IMG_CHECK); cancelAction.setChecked(false); cancelAction.setToolTipText(Messages .getString("SpellCorrectionView.Start_spell_checking")); CheckSpellingActionDelegate delegate = SpellCheckerPlugin .getSpellCheckingActionDelegate(); cancelAction.setEnabled(delegate != null && delegate.isEnabled()); } } // Update view private void updateView() { // Execute via syncExec as we are called from other thread display.syncExec(new Runnable() { public void run() { // Update TableViewer viewer.setInput(currentEvent); // Update Text field and title if (currentEvent == null) { badWord.setText(""); setContentDescription(spellCheckManager.getCurrentName() + Messages.getString("SpellCorrectionView.done")); } else { badWord.setText(currentEvent.getInvalidWord()); setContentDescription(spellCheckManager.getCurrentName() + Messages .getString("SpellCorrectionView.in_progress")); } // Update actions updateActionEnablement(); } }); } /** * Indicate that we are loading a dictionary */ public void indicateLoading(final String name) { display.syncExec(new Runnable() { public void run() { setContentDescription(name + Messages.getString("SpellCorrectionView.loading")); } }); } /** * Supplies the Spell Correction View with a new spelling error event * * @param event - * the spelling error event * @param manager - * the manager to be notified when finished * @param documentIsEditable - * true, if document may be modified */ public void setInput(SpellCheckEvent event, SpellCheckManager manager, boolean documentIsEditable) { // Accept event, manager, and flag this.currentEvent = event; this.spellCheckManager = manager; this.documentIsEditable = documentIsEditable; // Update the view updateView(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -