Package at.bestsolution.efxclipse.text.editor.spelling

Source Code of at.bestsolution.efxclipse.text.editor.spelling.SpellingProblem

package at.bestsolution.efxclipse.text.editor.spelling;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;

import at.bestsolution.efxclipse.text.editor.IDocumentProvider;
import at.bestsolution.efxclipse.text.editor.IEditorInput;
import at.bestsolution.efxclipse.text.editor.ITextEditor;
import at.bestsolution.efxclipse.text.jface.contentassist.ICompletionProposal;
import at.bestsolution.efxclipse.text.jface.quickassist.IQuickAssistInvocationContext;
import at.bestsolution.efxclipse.text.jface.source.ISourceViewer;


/**
* A spelling problem as reported by the {@link SpellingService} service to the
* {@link ISpellingProblemCollector}.
* <p>
* This class is intended to be subclassed by clients.
* </p>
*
* @see SpellingService
* @see ISpellingProblemCollector
* @since 3.1
*/
public abstract class SpellingProblem {

  /**
   * Removes all spelling problems that are reported
   * for the given <code>word</code> in the active editor.
   * <p>
   * <em>This a workaround to fix bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=134338
   * for 3.2 at the time where spelling still resides in JDT Text.
   * Once we move the spell check engine along with its quick fixes
   * down to Platform Text we need to provide the proposals with
   * a way to access the annotation model.</em>
   * </p>
   *
   * @param editor the text editor, if <code>null</code> this method does nothing
   * @param word the word for which to remove the problems or <code>null</code> to remove all
   * @since 3.3
   * @deprecated As of 3.4, replaced by {@link #removeAll(ISourceViewer, String)}
   */
  public static void removeAllInActiveEditor(ITextEditor editor, String word) { 
    if (editor == null)
      return;

    IDocumentProvider documentProvider= editor.getDocumentProvider();
    if (documentProvider == null)
      return;

    IEditorInput editorInput= editor.getEditorInput();
    if (editorInput == null)
      return;

    IAnnotationModel model= documentProvider.getAnnotationModel(editorInput);
    if (model == null)
      return;

    IDocument document= documentProvider.getDocument(editorInput);
    if (document == null)
      return;

    boolean supportsBatchReplace= (model instanceof IAnnotationModelExtension);
    List toBeRemovedAnnotations= new ArrayList();
    Iterator iter= model.getAnnotationIterator();
    while (iter.hasNext()) {
      Annotation annotation= (Annotation) iter.next();
      if (SpellingAnnotation.TYPE.equals(annotation.getType())) {
        boolean doRemove= word == null;
        if (word == null)
          doRemove= true;
        else {
          String annotationWord= null;
          Position pos= model.getPosition(annotation);
          try {
            annotationWord= document.get(pos.getOffset(), pos.getLength());
          } catch (BadLocationException e) {
            continue;
          }
          doRemove= word.equals(annotationWord);
        }
        if (doRemove) {
          if (supportsBatchReplace)
            toBeRemovedAnnotations.add(annotation);
          else
            model.removeAnnotation(annotation);
        }
      }
    }

    if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) {
      Annotation[] annotationArray= (Annotation[])toBeRemovedAnnotations.toArray(new Annotation[toBeRemovedAnnotations.size()]);
      ((IAnnotationModelExtension)model).replaceAnnotations(annotationArray, null);
    }
  }

  /**
   * Removes all spelling problems that are reported
   * for the given <code>word</code> in the active editor.
   *
   * @param sourceViewer the source viewer
   * @param word the word for which to remove the problems or <code>null</code> to remove all
   * @since 3.4
   */
  public static void removeAll(ISourceViewer sourceViewer, String word) {
    Assert.isNotNull(sourceViewer);

    IAnnotationModel model= sourceViewer.getAnnotationModel();
    if (model == null)
      return;

    IDocument document= sourceViewer.getDocument();
    if (document == null)
      return;

    boolean supportsBatchReplace= (model instanceof IAnnotationModelExtension);
    List toBeRemovedAnnotations= new ArrayList();
    Iterator iter= model.getAnnotationIterator();
    while (iter.hasNext()) {
      Annotation annotation= (Annotation) iter.next();
      if (SpellingAnnotation.TYPE.equals(annotation.getType())) {
        boolean doRemove= word == null;
        if (word == null)
          doRemove= true;
        else {
          String annotationWord= null;
          Position pos= model.getPosition(annotation);
          try {
            annotationWord= document.get(pos.getOffset(), pos.getLength());
          } catch (BadLocationException e) {
            continue;
          }
          doRemove= word.equals(annotationWord);
        }
        if (doRemove) {
          if (supportsBatchReplace)
            toBeRemovedAnnotations.add(annotation);
          else
            model.removeAnnotation(annotation);
        }
      }
    }

    if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) {
      Annotation[] annotationArray= (Annotation[])toBeRemovedAnnotations.toArray(new Annotation[toBeRemovedAnnotations.size()]);
      ((IAnnotationModelExtension)model).replaceAnnotations(annotationArray, null);
    }
  }

  /**
   * Returns the offset of the incorrectly spelled region.
   *
   * @return the offset of the incorrectly spelled region
   */
  public abstract int getOffset();

  /**
   * Returns the length of the incorrectly spelled region.
   *
   * @return the length of the incorrectly spelled region
   */
  public abstract int getLength();

  /**
   * Returns a localized, human-readable message string which describes the spelling problem.
   *
   * @return a localized, human-readable message string which describes the spelling problem
   */
  public abstract String getMessage();

  /**
   * Returns the proposals for the incorrectly spelled region.
   *
   * @return the proposals for the incorrectly spelled region
   */
  public abstract ICompletionProposal[] getProposals();

  /**
   * Returns the proposals for the incorrectly spelled region.
   *
   * @param context the invocation context or <code>null</code> if none
   * @return the proposals for the incorrectly spelled region
   * @since 3.4
   */
  public ICompletionProposal[] getProposals(IQuickAssistInvocationContext context) {
    return getProposals();
  }
}
TOP

Related Classes of at.bestsolution.efxclipse.text.editor.spelling.SpellingProblem

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.