/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * 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: * IBM Corporation - initial API and implementation *******************************************************************************/ package net.sourceforge.phpeclipse.wiki.editor; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText; import org.eclipse.jface.viewers.IPostSelectionProvider; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; 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.ISourceViewer; class WikiOccurrencesUpdater implements ISelectionChangedListener { /** * Annotation fType for recipe occurrences. */ private static final String ANNOTATION_TYPE= "net.sourceforge.phpeclipse.wiki.editor.highlightannotation"; /** * The editor we operate on. */ private final WikiEditor fEditor; /** * The set of annotations added in the previous run, always replaced by a * new run. */ private final List fOldAnnotations= new LinkedList(); /** * Creates a new instance on editor editor. * * @param editor the editor to mark occurrences on. */ public WikiOccurrencesUpdater(WikiEditor editor) { ((IPostSelectionProvider) editor.getSelectionProvider()).addPostSelectionChangedListener(this); fEditor= editor; } /* * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent) */ public void selectionChanged(SelectionChangedEvent event) { update((ISourceViewer) event.getSource()); } /** * Updates the drawn annotations. * * @param viewer the viewer to get the document and annotation model from */ public void update(ISourceViewer viewer) { if (viewer==null) { return; } // try { IDocument document= viewer.getDocument(); IAnnotationModel model= viewer.getAnnotationModel(); if (document == null || model == null) return; removeOldAnnotations(model); // String word= getWordAtSelection(fEditor.getSelectionProvider().getSelection(), document); // if (isIngredient(word)) { // createNewAnnotations(word, document, model); // } // } catch (BadLocationException e) { // // ignore // } } /** * Removes the previous set of annotations from the annotation model. * * @param model the annotation model */ private void removeOldAnnotations(IAnnotationModel model) { for (Iterator it= fOldAnnotations.iterator(); it.hasNext();) { Annotation annotation= (Annotation) it.next(); model.removeAnnotation(annotation); } fOldAnnotations.clear(); } /** * Returns the word at the current selection / caret position. * * @param selection the selection * @param document the document * @return the currently selected text, or the word at the caret if the * selection has length 0 * @throws BadLocationException if accessing the document fails */ private String getWordAtSelection(ISelection selection, IDocument document) throws BadLocationException { if (selection instanceof ITextSelection) { ITextSelection ts= (ITextSelection) selection; int offset= ts.getOffset(); int end= offset + ts.getLength(); // non-empty selections if (end != offset) return ts.getText(); while (offset > 0 && isIngredientChar(document.getChar(offset - 1))) offset--; while (end < document.getLength() && isIngredientChar(document.getChar(end))) end++; return document.get(offset, end - offset); } return ""; } private boolean isIngredientChar(char c) { return !Character.isWhitespace(c) && c != ':' && c != ',' && c != '.'; } /** * Adds an annotation for every occurrence of * ingredient in the document. Also stores the created * annotations in fOldAnnotations. * * @param ingredient the word to look for * @param document the document * @param model the annotation model */ private void createNewAnnotations(String ingredient, IDocument document, IAnnotationModel model) { String content= document.get(); int idx= content.indexOf(ingredient); while (idx != -1) { Annotation annotation= new Annotation(ANNOTATION_TYPE, false, ingredient); Position position= new Position(idx, ingredient.length()); model.addAnnotation(annotation, position); fOldAnnotations.add(annotation); idx= content.indexOf(ingredient, idx + 1); } } public void dispose() { ((IPostSelectionProvider) fEditor.getSelectionProvider()).removePostSelectionChangedListener(this); } }