KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > JavaSelectAnnotationRulerAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.javaeditor;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15
16 import org.eclipse.swt.widgets.Event;
17
18 import org.eclipse.jface.preference.IPreferenceStore;
19
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextOperationTarget;
22 import org.eclipse.jface.text.Position;
23 import org.eclipse.jface.text.source.Annotation;
24 import org.eclipse.jface.text.source.IAnnotationAccessExtension;
25 import org.eclipse.jface.text.source.ISourceViewer;
26 import org.eclipse.jface.text.source.IVerticalRulerInfo;
27
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
30 import org.eclipse.ui.texteditor.AnnotationPreference;
31 import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
32 import org.eclipse.ui.texteditor.ITextEditor;
33 import org.eclipse.ui.texteditor.ITextEditorExtension;
34 import org.eclipse.ui.texteditor.SelectMarkerRulerAction;
35
36 import org.eclipse.ui.editors.text.EditorsUI;
37
38 import org.eclipse.jdt.ui.PreferenceConstants;
39
40 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
43 import org.eclipse.jdt.internal.ui.text.correction.QuickAssistLightBulbUpdater.AssistAnnotation;
44
45 /**
46  * Action which gets triggered when selecting (annotations) in the vertical ruler.
47  *
48  * <p>
49  * Was originally called >code>JavaSelectMarkerRulerAction</code>.
50  * </p>
51  */

52 public class JavaSelectAnnotationRulerAction extends SelectMarkerRulerAction {
53
54     private ITextEditor fTextEditor;
55     private Position fPosition;
56     private Annotation fAnnotation;
57     private AnnotationPreferenceLookup fAnnotationPreferenceLookup;
58     private IPreferenceStore fStore;
59     private boolean fHasCorrection;
60     private ResourceBundle JavaDoc fBundle;
61
62     public JavaSelectAnnotationRulerAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, IVerticalRulerInfo ruler) {
63         super(bundle, prefix, editor, ruler);
64         fBundle= bundle;
65         fTextEditor= editor;
66
67         fAnnotationPreferenceLookup= EditorsUI.getAnnotationPreferenceLookup();
68         fStore= JavaPlugin.getDefault().getCombinedPreferenceStore();
69
70         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.JAVA_SELECT_MARKER_RULER_ACTION);
71     }
72
73     public void run() {
74         if (fStore.getBoolean(PreferenceConstants.EDITOR_ANNOTATION_ROLL_OVER))
75             return;
76         
77         runWithEvent(null);
78     }
79     
80     /*
81      * @see org.eclipse.jface.action.IAction#runWithEvent(org.eclipse.swt.widgets.Event)
82      * @since 3.2
83      */

84     public void runWithEvent(Event event) {
85         if (fAnnotation instanceof OverrideIndicatorManager.OverrideIndicator) {
86             ((OverrideIndicatorManager.OverrideIndicator)fAnnotation).open();
87             return;
88         }
89
90         if (fHasCorrection) {
91             ITextOperationTarget operation= (ITextOperationTarget) fTextEditor.getAdapter(ITextOperationTarget.class);
92             final int opCode= ISourceViewer.QUICK_ASSIST;
93             if (operation != null && operation.canDoOperation(opCode)) {
94                 fTextEditor.selectAndReveal(fPosition.getOffset(), fPosition.getLength());
95                 operation.doOperation(opCode);
96             }
97             return;
98         }
99
100         super.run();
101     }
102
103     public void update() {
104         findJavaAnnotation();
105         setEnabled(true); // super.update() might change this later
106

107         if (fAnnotation instanceof OverrideIndicatorManager.OverrideIndicator) {
108             initialize(fBundle, "JavaSelectAnnotationRulerAction.OpenSuperImplementation."); //$NON-NLS-1$
109
return;
110         }
111         if (fHasCorrection) {
112             if (fAnnotation instanceof AssistAnnotation)
113                 initialize(fBundle, "JavaSelectAnnotationRulerAction.QuickAssist."); //$NON-NLS-1$
114
else
115                 initialize(fBundle, "JavaSelectAnnotationRulerAction.QuickFix."); //$NON-NLS-1$
116
return;
117         }
118
119         initialize(fBundle, "JavaSelectAnnotationRulerAction.GotoAnnotation."); //$NON-NLS-1$;
120
super.update();
121     }
122
123     private void findJavaAnnotation() {
124         fPosition= null;
125         fAnnotation= null;
126         fHasCorrection= false;
127
128         AbstractMarkerAnnotationModel model= getAnnotationModel();
129         IAnnotationAccessExtension annotationAccess= getAnnotationAccessExtension();
130
131         IDocument document= getDocument();
132         if (model == null)
133             return ;
134
135         boolean hasAssistLightbulb= fStore.getBoolean(PreferenceConstants.EDITOR_QUICKASSIST_LIGHTBULB);
136
137         Iterator JavaDoc iter= model.getAnnotationIterator();
138         int layer= Integer.MIN_VALUE;
139
140         while (iter.hasNext()) {
141             Annotation annotation= (Annotation) iter.next();
142             if (annotation.isMarkedDeleted())
143                 continue;
144
145             int annotationLayer= layer;
146             if (annotationAccess != null) {
147                 annotationLayer= annotationAccess.getLayer(annotation);
148                 if (annotationLayer < layer)
149                     continue;
150             }
151
152             Position position= model.getPosition(annotation);
153             if (!includesRulerLine(position, document))
154                 continue;
155
156             boolean isReadOnly= fTextEditor instanceof ITextEditorExtension && ((ITextEditorExtension)fTextEditor).isEditorInputReadOnly();
157             if (!isReadOnly
158                     && (
159                         ((hasAssistLightbulb && annotation instanceof AssistAnnotation)
160                         || JavaCorrectionProcessor.hasCorrections(annotation)))) {
161                 fPosition= position;
162                 fAnnotation= annotation;
163                 fHasCorrection= true;
164                 layer= annotationLayer;
165                 continue;
166             } else {
167                 AnnotationPreference preference= fAnnotationPreferenceLookup.getAnnotationPreference(annotation);
168                 if (preference == null)
169                     continue;
170
171                 String JavaDoc key= preference.getVerticalRulerPreferenceKey();
172                 if (key == null)
173                     continue;
174
175                 if (fStore.getBoolean(key)) {
176                     fPosition= position;
177                     fAnnotation= annotation;
178                     fHasCorrection= false;
179                     layer= annotationLayer;
180                 }
181             }
182         }
183     }
184 }
185
186
Popular Tags