KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > PDESelectAnnotationRulerAction


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.text;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.ITextOperationTarget;
18 import org.eclipse.jface.text.Position;
19 import org.eclipse.jface.text.source.Annotation;
20 import org.eclipse.jface.text.source.IAnnotationAccessExtension;
21 import org.eclipse.jface.text.source.ISourceViewer;
22 import org.eclipse.jface.text.source.IVerticalRulerInfo;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
25 import org.eclipse.ui.texteditor.ITextEditor;
26 import org.eclipse.ui.texteditor.ITextEditorExtension;
27 import org.eclipse.ui.texteditor.SelectMarkerRulerAction;
28
29 public class PDESelectAnnotationRulerAction extends SelectMarkerRulerAction {
30
31     private boolean fIsEditable;
32     private ITextEditor fTextEditor;
33     private Position fPosition;
34     private ResourceBundle JavaDoc fBundle;
35     private String JavaDoc fPrefix;
36
37     public PDESelectAnnotationRulerAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, IVerticalRulerInfo ruler) {
38         super(bundle, prefix, editor, ruler);
39         fTextEditor = editor;
40         fBundle = bundle;
41         fPrefix = prefix;
42     }
43     
44     public void run() {
45         runWithEvent(null);
46     }
47     
48     /*
49      * @see org.eclipse.jface.action.IAction#runWithEvent(org.eclipse.swt.widgets.Event)
50      * @since 3.2
51      */

52     public void runWithEvent(Event event) {
53         if (fIsEditable) {
54             ITextOperationTarget operation = (ITextOperationTarget) fTextEditor.getAdapter(ITextOperationTarget.class);
55             final int opCode= ISourceViewer.QUICK_ASSIST;
56             if (operation != null && operation.canDoOperation(opCode)) {
57                 fTextEditor.selectAndReveal(fPosition.getOffset(), fPosition.getLength());
58                 operation.doOperation(opCode);
59             }
60             return;
61         }
62
63         super.run();
64     }
65
66     public void update() {
67         checkReadOnly();
68         
69         if (fIsEditable) {
70             initialize(fBundle, fPrefix + "QuickFix."); //$NON-NLS-1$
71
}
72
73         super.update();
74     }
75
76     private void checkReadOnly() {
77         fPosition = null;
78         fIsEditable = false;
79
80         AbstractMarkerAnnotationModel model = getAnnotationModel();
81         IAnnotationAccessExtension annotationAccess = getAnnotationAccessExtension();
82
83         IDocument document= getDocument();
84         if (model == null)
85             return;
86
87         Iterator JavaDoc iter = model.getAnnotationIterator();
88         int layer = Integer.MIN_VALUE;
89
90         while (iter.hasNext()) {
91             Annotation annotation= (Annotation) iter.next();
92             if (annotation.isMarkedDeleted())
93                 continue;
94
95             int annotationLayer = annotationAccess.getLayer(annotation);
96             if (annotationAccess != null)
97                 if (annotationLayer < layer)
98                     continue;
99
100             Position position = model.getPosition(annotation);
101             if (!includesRulerLine(position, document))
102                 continue;
103
104             boolean isReadOnly = fTextEditor instanceof ITextEditorExtension && ((ITextEditorExtension)fTextEditor).isEditorInputReadOnly();
105             if (!isReadOnly) {
106                 fPosition = position;
107                 fIsEditable = true;
108                 layer = annotationLayer;
109                 continue;
110             }
111         }
112     }
113 }
114
Popular Tags