⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spellcheckingtarget.java

📁 Eclipse高级编程3源码(书本源码)
💻 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.lang.reflect.Method;

import org.eclipse.jface.text.*;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;


public class SpellCheckingTarget implements ModifyListener {

  private static final Class[] NOPARMS = new Class[0];
  private static final Object[] NOARGS = new Object[0];
  private static final Point NOSELECTION = new Point(0, 0);

  // The target editor or null
  public IEditorPart editor;
  // The target widget or null
  public Control textArea;
  // Indicated if target is editable
  public boolean isEditable = true;
  // The document tokenizer associated with this target
  public AbstractDocumentWordTokenizer tokenizer;
  // The targets selection provider
  private ISelectionProvider selectionProvider;
  // The target document provider
  private IDocumentProvider documentProvider;
  // If we have no document provider we create an auxiliary document
  private IDocument auxDocument;
  /**
   * Constructor should not be used outside this class
   */
  private SpellCheckingTarget() {}

  /**
   * Factory method.
   * 
   * @param part - currently active workbench part or null
   * @param control - Text or StyledText control that currently has the
   *        focus or null
   */
  public static SpellCheckingTarget getInstance(IWorkbenchPart part, 
Control control) {
    SpellCheckingTarget instance = null;
    if (part instanceof ITextEditor) {
      // Special treatment for text editor
      instance = new SpellCheckingTarget();
      ITextEditor textEditor = (ITextEditor) part;
      instance.editor = textEditor;
      instance.isEditable = textEditor.isEditable();
      instance.documentProvider = textEditor.getDocumentProvider();
      instance.selectionProvider = textEditor.getSelectionProvider();
      instance.textArea = control;
    } else if (control != null) {
      // No text editor - just use the text control
      instance = new SpellCheckingTarget();
      instance.textArea = control;
      instance.isEditable = control.isEnabled();
      if (part instanceof IEditorPart) {
        // Find selection provider for generic editors
        instance.editor = (IEditorPart) part;
        if (part instanceof ISelectionProvider)
          // The editor is an selection provider itself
          instance.selectionProvider = (ISelectionProvider) part;
        else {
          // Find the editors selection provider by using reflection
          try {
            Method getSelectionProvider = part.getClass()
                    .getMethod("getSelectionProvider", NOPARMS);
            instance.selectionProvider = (ISelectionProvider) 
                           getSelectionProvider.invoke(part, NOARGS);
          } catch (Exception e1) {
          }
        }
      }
    }
    return instance;
  }
  /**
   * Get the targets text selection
   * 
   * @return - point with start and end of selection
   */
  public Point getSelection() {
    if (selectionProvider != null) {
      // use the selection provider
      ISelection sel = selectionProvider.getSelection();
      if (sel instanceof ITextSelection) {
        int pos = ((ITextSelection) sel).getOffset();
        return new Point(pos, pos
                + ((ITextSelection) sel).getLength());
      }
    }
    // otherwise retrieve selection from text control
    if (textArea instanceof Text)
      return ((Text) textArea).getSelection();
    if (textArea instanceof StyledText)
      return ((StyledText) textArea).getSelection();
    return NOSELECTION;
  }
  /**
   * Set the targets text selection
   * 
   * @param start - start of selection
   * @param end - end of selection
   */
  public void setSelection(int start, int end) {
    // Special treatment for ITextEditor
    if (editor instanceof ITextEditor)
      ((ITextEditor) editor).selectAndReveal(start, end - start);
    // if we have a text control set selection directly in control
    else if (textArea instanceof Text)
      ((Text) textArea).setSelection(start, end);
    else if (textArea instanceof StyledText)
      ((StyledText) textArea).setSelection(start, end);
    // for other editors use standard way
    else if (selectionProvider != null)
      selectionProvider.setSelection(new TextSelection(start,
              end - start));
  }
  /**
   * Get the underlying document instance
   * 
   * @return - the text document
   */
  public IDocument getDocument() {
    if (hasLiveDocument())
      return documentProvider.getDocument(getEditorInput());
    // Create auxiliary document and store it for further use
    if (textArea != null && auxDocument == null) {
      textArea.getDisplay().syncExec(new Runnable() {
        public void run() {
          if (textArea instanceof Text)
            ((Text) textArea)
                    .addModifyListener(SpellCheckingTarget.this);
          else
            ((StyledText) textArea)
                    .addModifyListener(SpellCheckingTarget.this);
          auxDocument = new Document(getWidgetText());
        }
      });
    }
    return auxDocument;
  }

  /**
   * Tests if the underlying document is not auxiliary
   * 
   * @return - true if the underlying document is not auxiliary
   */
  public boolean hasLiveDocument() {
    return (documentProvider != null);
  }

  /**
   * Get the targets editor input
   * 
   * @return - the editor input or null
   */
  public IEditorInput getEditorInput() {
    return (editor != null) ? editor.getEditorInput() : null;
  }

  /**
   * Reacts to text widget modifications
   * 
   * @param e - event object (ignored)
   */
  public void modifyText(
    ModifyEvent e) {
    auxDocument.set(getWidgetText());
  }

  /**
   * Retrieves the text from the text widget
   * 
   * @return - the text content of the focus widget
   */
  private String getWidgetText() {
    return (textArea instanceof Text) ? 
           ((Text) textArea).getText() :
           ((StyledText) textArea).getText();
  }
  /**
   * Replace text in the target
   * 
   * @param pos - the replacement position
   * @param len - the length of the text part to be replaced
   * @param replacement - the replacement string
   * @return - length change of document
   */
  public int replaceText(int pos, int len, String replacement) {
    try {
      IDocument document = getDocument();
      String oldWord = document.get(pos, len);
      if (!oldWord.equals(replacement)) {
        // True change - replace word in document
        String rawString = (tokenizer != null) ? 
               tokenizer.serializeWord(replacement) : 
               replacement;
        document.replace(pos, len, rawString);
        // In case of auxiliary document apply change to
        // Text or StyleText widget, too.
        if (!hasLiveDocument()) {
          if (textArea instanceof Text) {
            ((Text) textArea).setSelection(pos, pos + len);
            ((Text) textArea).insert(replacement);
          } else if (textArea instanceof StyledText)
            ((StyledText) textArea).replaceTextRange(pos, len,
                    replacement);
        }
        return replacement.length() - len;
      }
    } catch (BadLocationException ex) {
    }
    return 0;
  }
  /**
   * Dispose this target
   */
  public void dispose() {
    if (textArea != null) {
      // Can be called from outside the SWT-thread
      textArea.getDisplay().syncExec(new Runnable() {
        public void run() {
          if (textArea instanceof Text)
            ((Text) textArea)
                    .removeModifyListener(SpellCheckingTarget.this);
          else
            ((StyledText) textArea)
                    .removeModifyListener(SpellCheckingTarget.this);
        }
      });
    }
    if (tokenizer != null) tokenizer.dispose();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -