001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.jface.text.source.IAnnotationModel;
015:
016: /**
017: * Action for jumping to a particular annotation in the editor's text viewer.
018: * <p>
019: * This action only runs if <code>getTextEditor()</code>
020: * implements {@link org.eclipse.ui.texteditor.ITextEditorExtension4}.</p>
021: * <p>
022: * This class may be instantiated; it is not intended to be subclassed.
023: * </p>
024: *
025: * @since 3.2
026: */
027: public class GotoAnnotationAction extends TextEditorAction {
028:
029: /**
030: * The navigation direction.
031: * <code>true</code> to go to next and <code>false</code> to go to previous annotation.
032: */
033: private boolean fForward;
034:
035: /**
036: * Creates a new action for the given text editor. The action configures its
037: * visual representation from the given resource bundle.
038: *
039: * @param bundle the resource bundle
040: * @param prefix a prefix to be prepended to the various resource keys
041: * (described in <code>ResourceAction</code> constructor), or
042: * <code>null</code> if none
043: * @param editor the text editor
044: * @param forward <code>true</code> to go to next and <code>false</code> to go to previous annotation
045: * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
046: */
047: public GotoAnnotationAction(ResourceBundle bundle, String prefix,
048: ITextEditor editor, boolean forward) {
049: super (bundle, prefix, editor);
050: fForward = forward;
051: setHelpContextId(fForward ? IAbstractTextEditorHelpContextIds.GOTO_NEXT_ANNOTATION_ACTION
052: : IAbstractTextEditorHelpContextIds.GOTO_PREVIOUS_ANNOTATION_ACTION);
053: }
054:
055: /**
056: * Creates a new action for the given text editor. The action configures its
057: * visual representation from the given resource bundle.
058: *
059: * @param editor the text editor
060: * @param forward <code>true</code> to go to next and <code>false</code> to go to previous annotation
061: * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
062: */
063: public GotoAnnotationAction(ITextEditor editor, boolean forward) {
064: this (
065: EditorMessages.getBundleForConstructedKeys(),
066: forward ? "Editor.GotoNextAnnotation." : "Editor.GotoPreviousAnnotation.", editor, forward); //$NON-NLS-1$ //$NON-NLS-2$
067: }
068:
069: /*
070: * @see org.eclipse.jface.action.IAction#run()
071: * @since 3.2
072: */
073: public void run() {
074: ITextEditor editor = getTextEditor();
075: if (editor instanceof ITextEditorExtension4)
076: ((ITextEditorExtension4) editor).gotoAnnotation(fForward);
077: }
078:
079: /*
080: * @see org.eclipse.ui.texteditor.TextEditorAction#setEditor(org.eclipse.ui.texteditor.ITextEditor)
081: * @since 3.2
082: */
083: public void setEditor(ITextEditor editor) {
084: super .setEditor(editor);
085: update();
086: }
087:
088: /*
089: * @see org.eclipse.ui.texteditor.IUpdate#update()
090: * @since 3.2
091: */
092: public void update() {
093: ITextEditor editor = getTextEditor();
094: if (!(editor instanceof AbstractTextEditor)) {
095: setEnabled(false);
096: return;
097: }
098:
099: IAnnotationModel model = editor.getDocumentProvider()
100: .getAnnotationModel(editor.getEditorInput());
101: setEnabled(model != null);
102: }
103: }
|