001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.swt.widgets.Menu;
015:
016: import org.eclipse.jface.text.Position;
017: import org.eclipse.jface.text.source.Annotation;
018: import org.eclipse.jface.text.source.IVerticalRulerListener;
019: import org.eclipse.jface.text.source.IAnnotationModel;
020: import org.eclipse.jface.text.source.IVerticalRulerInfo;
021: import org.eclipse.jface.text.source.IVerticalRulerInfoExtension;
022: import org.eclipse.jface.text.source.VerticalRulerEvent;
023:
024: /**
025: * A ruler action which can select the textual range of an annotation that has a
026: * visual representation in a vertical ruler.
027: *
028: * @since 3.0
029: */
030: public class SelectAnnotationRulerAction extends TextEditorAction
031: implements IVerticalRulerListener {
032:
033: /**
034: * Creates a new action for the given ruler and editor. The action configures
035: * its visual representation from the given resource bundle.
036: *
037: * @param bundle the resource bundle
038: * @param prefix a prefix to be prepended to the various resource keys
039: * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
040: * @param editor the editor
041: *
042: * @see ResourceAction#ResourceAction(ResourceBundle, String)
043: */
044: public SelectAnnotationRulerAction(ResourceBundle bundle,
045: String prefix, ITextEditor editor) {
046: super (bundle, prefix, editor);
047: }
048:
049: /*
050: * @see org.eclipse.ui.texteditor.TextEditorAction#setEditor(org.eclipse.ui.texteditor.ITextEditor)
051: */
052: public void setEditor(ITextEditor editor) {
053: if (getTextEditor() != null) {
054: IVerticalRulerInfo service = (IVerticalRulerInfo) getTextEditor()
055: .getAdapter(IVerticalRulerInfo.class);
056: if (service instanceof IVerticalRulerInfoExtension)
057: ((IVerticalRulerInfoExtension) service)
058: .removeVerticalRulerListener(this );
059: }
060: super .setEditor(editor);
061: if (getTextEditor() != null) {
062: IVerticalRulerInfo service = (IVerticalRulerInfo) getTextEditor()
063: .getAdapter(IVerticalRulerInfo.class);
064: if (service instanceof IVerticalRulerInfoExtension)
065: ((IVerticalRulerInfoExtension) service)
066: .addVerticalRulerListener(this );
067: }
068: }
069:
070: /**
071: * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.
072: *
073: * @return the marker annotation model or <code>null</code> if there's none
074: */
075: protected IAnnotationModel getAnnotationModel() {
076: IDocumentProvider provider = getTextEditor()
077: .getDocumentProvider();
078: return provider.getAnnotationModel(getTextEditor()
079: .getEditorInput());
080: }
081:
082: /*
083: * @see org.eclipse.ui.texteditor.IVerticalRulerListener#annotationSelected(org.eclipse.ui.texteditor.VerticalRulerEvent)
084: */
085: public void annotationSelected(VerticalRulerEvent event) {
086: }
087:
088: /*
089: * @see org.eclipse.ui.texteditor.IVerticalRulerListener#annotationDefaultSelected(org.eclipse.ui.texteditor.VerticalRulerEvent)
090: */
091: public void annotationDefaultSelected(VerticalRulerEvent event) {
092: Annotation a = event.getSelectedAnnotation();
093: IAnnotationModel model = getAnnotationModel();
094: Position position = model.getPosition(a);
095: if (position == null)
096: return;
097:
098: getTextEditor().selectAndReveal(position.offset,
099: position.length);
100: }
101:
102: /*
103: * @see org.eclipse.ui.texteditor.IVerticalRulerListener#annotationContextMenuAboutToShow(org.eclipse.ui.texteditor.VerticalRulerEvent, org.eclipse.swt.widgets.Menu)
104: */
105: public void annotationContextMenuAboutToShow(
106: VerticalRulerEvent event, Menu menu) {
107: }
108: }
|