]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.ResourceBundle; | |
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 | ||
30 | import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel; | |
31 | import org.eclipse.ui.texteditor.AnnotationPreference; | |
32 | import org.eclipse.ui.texteditor.AnnotationPreferenceLookup; | |
33 | import org.eclipse.ui.texteditor.ITextEditor; | |
34 | import org.eclipse.ui.texteditor.ITextEditorExtension; | |
35 | import org.eclipse.ui.texteditor.SelectMarkerRulerAction; | |
36 | ||
37 | import org.eclipse.ui.editors.text.EditorsUI; | |
38 | ||
39 | import org.eclipse.jdt.ui.PreferenceConstants; | |
40 | ||
41 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
42 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
43 | import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor; | |
44 | import org.eclipse.jdt.internal.ui.text.correction.QuickAssistLightBulbUpdater.AssistAnnotation; | |
45 | ||
46 | /** | |
47 | * Action which gets triggered when selecting (annotations) in the vertical ruler. | |
48 | * | |
49 | * <p> | |
50 | * Was originally called >code>JavaSelectMarkerRulerAction</code>. | |
51 | * </p> | |
52 | */ | |
53 | public class JavaSelectAnnotationRulerAction extends SelectMarkerRulerAction { | |
54 | ||
55 | private ITextEditor fTextEditor; | |
56 | private Position fPosition; | |
57 | private Annotation fAnnotation; | |
58 | private AnnotationPreferenceLookup fAnnotationPreferenceLookup; | |
59 | private IPreferenceStore fStore; | |
60 | private boolean fHasCorrection; | |
61 | private ResourceBundle fBundle; | |
62 | ||
63 | public JavaSelectAnnotationRulerAction(ResourceBundle bundle, String prefix, ITextEditor editor, IVerticalRulerInfo ruler) { | |
64 | super(bundle, prefix, editor, ruler); | |
65 | fBundle= bundle; | |
66 | fTextEditor= editor; | |
67 | ||
68 | fAnnotationPreferenceLookup= EditorsUI.getAnnotationPreferenceLookup(); | |
69 | fStore= JavaPlugin.getDefault().getCombinedPreferenceStore(); | |
70 | ||
71 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.JAVA_SELECT_MARKER_RULER_ACTION); | |
72 | } | |
73 | ||
74 | @Override | |
75 | public void run() { | |
76 | if (fStore.getBoolean(PreferenceConstants.EDITOR_ANNOTATION_ROLL_OVER)) | |
77 | return; | |
78 | ||
79 | runWithEvent(null); | |
80 | } | |
81 | ||
82 | /* | |
83 | * @see org.eclipse.jface.action.IAction#runWithEvent(org.eclipse.swt.widgets.Event) | |
84 | * @since 3.2 | |
85 | */ | |
86 | @Override | |
87 | public void runWithEvent(Event event) { | |
88 | if (fAnnotation instanceof OverrideIndicatorManager.OverrideIndicator) { | |
89 | ((OverrideIndicatorManager.OverrideIndicator)fAnnotation).open(); | |
90 | return; | |
91 | } | |
92 | ||
93 | if (fHasCorrection) { | |
94 | ITextOperationTarget operation= (ITextOperationTarget) fTextEditor.getAdapter(ITextOperationTarget.class); | |
95 | final int opCode= ISourceViewer.QUICK_ASSIST; | |
96 | if (operation != null && operation.canDoOperation(opCode)) { | |
97 | fTextEditor.selectAndReveal(fPosition.getOffset(), fPosition.getLength()); | |
98 | operation.doOperation(opCode); | |
99 | } | |
100 | return; | |
101 | } | |
102 | ||
103 | super.run(); | |
104 | } | |
105 | ||
106 | @Override | |
107 | public void update() { | |
108 | findJavaAnnotation(); | |
109 | setEnabled(true); // super.update() might change this later | |
110 | ||
111 | if (fAnnotation instanceof OverrideIndicatorManager.OverrideIndicator) { | |
112 | initialize(fBundle, "JavaSelectAnnotationRulerAction.OpenSuperImplementation."); //$NON-NLS-1$ | |
113 | return; | |
114 | } | |
115 | if (fHasCorrection) { | |
116 | if (fAnnotation instanceof AssistAnnotation) | |
117 | initialize(fBundle, "JavaSelectAnnotationRulerAction.QuickAssist."); //$NON-NLS-1$ | |
118 | else | |
119 | initialize(fBundle, "JavaSelectAnnotationRulerAction.QuickFix."); //$NON-NLS-1$ | |
120 | return; | |
121 | } | |
122 | ||
123 | initialize(fBundle, "JavaSelectAnnotationRulerAction.GotoAnnotation."); //$NON-NLS-1$; | |
124 | super.update(); | |
125 | } | |
126 | ||
127 | private void findJavaAnnotation() { | |
128 | fPosition= null; | |
129 | fAnnotation= null; | |
130 | fHasCorrection= false; | |
131 | ||
132 | AbstractMarkerAnnotationModel model= getAnnotationModel(); | |
133 | IAnnotationAccessExtension annotationAccess= getAnnotationAccessExtension(); | |
134 | ||
135 | IDocument document= getDocument(); | |
136 | if (model == null) | |
137 | return ; | |
138 | ||
139 | boolean hasAssistLightbulb= fStore.getBoolean(PreferenceConstants.EDITOR_QUICKASSIST_LIGHTBULB); | |
140 | ||
141 | Iterator<Annotation> iter= model.getAnnotationIterator(); | |
142 | int layer= Integer.MIN_VALUE; | |
143 | ||
144 | while (iter.hasNext()) { | |
145 | Annotation annotation= iter.next(); | |
146 | if (annotation.isMarkedDeleted()) | |
147 | continue; | |
148 | ||
149 | int annotationLayer= layer; | |
150 | if (annotationAccess != null) { | |
151 | annotationLayer= annotationAccess.getLayer(annotation); | |
152 | if (annotationLayer < layer) | |
153 | continue; | |
154 | } | |
155 | ||
156 | Position position= model.getPosition(annotation); | |
157 | if (!includesRulerLine(position, document)) | |
158 | continue; | |
159 | ||
160 | boolean isReadOnly= fTextEditor instanceof ITextEditorExtension && ((ITextEditorExtension)fTextEditor).isEditorInputReadOnly(); | |
161 | if (!isReadOnly | |
162 | && ( | |
163 | ((hasAssistLightbulb && annotation instanceof AssistAnnotation) | |
164 | || JavaCorrectionProcessor.hasCorrections(annotation)))) { | |
165 | fPosition= position; | |
166 | fAnnotation= annotation; | |
167 | fHasCorrection= true; | |
168 | layer= annotationLayer; | |
169 | continue; | |
170 | } else if (!fHasCorrection) { | |
171 | AnnotationPreference preference= fAnnotationPreferenceLookup.getAnnotationPreference(annotation); | |
172 | if (preference == null) | |
173 | continue; | |
174 | ||
175 | String key= preference.getVerticalRulerPreferenceKey(); | |
176 | if (key == null) | |
177 | continue; | |
178 | ||
179 | if (fStore.getBoolean(key)) { | |
180 | fPosition= position; | |
181 | fAnnotation= annotation; | |
182 | layer= annotationLayer; | |
183 | } | |
184 | } | |
185 | } | |
186 | } | |
187 | } | |
188 |