KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ResourceBundle JavaDoc;
14
15 import org.eclipse.jface.action.IAction;
16
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.IAnnotationModel;
21 import org.eclipse.jface.text.source.ISourceViewer;
22 import org.eclipse.jface.text.source.VerticalRulerEvent;
23
24 import org.eclipse.ui.ISelectionListener;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.texteditor.ITextEditor;
27 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
28 import org.eclipse.ui.texteditor.IUpdate;
29 import org.eclipse.ui.texteditor.SelectAnnotationRulerAction;
30
31 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
32 import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
33 import org.eclipse.jdt.internal.ui.text.correction.QuickAssistLightBulbUpdater.AssistAnnotation;
34 import org.eclipse.jdt.internal.ui.text.java.hover.JavaExpandHover;
35
36 /**
37  * A special select marker ruler action which activates quick fix if clicked on a quick fixable problem.
38  */

39 public class JavaSelectMarkerRulerAction2 extends SelectAnnotationRulerAction {
40
41     public JavaSelectMarkerRulerAction2(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
42         super(bundle, prefix, editor);
43         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.JAVA_SELECT_MARKER_RULER_ACTION);
44     }
45
46     /*
47      * @see org.eclipse.ui.texteditor.IVerticalRulerListener#annotationDefaultSelected(org.eclipse.ui.texteditor.VerticalRulerEvent)
48      */

49     public void annotationDefaultSelected(VerticalRulerEvent event) {
50         Annotation annotation= event.getSelectedAnnotation();
51         IAnnotationModel model= getAnnotationModel();
52
53         if (isOverrideIndicator(annotation)) {
54             ((OverrideIndicatorManager.OverrideIndicator)annotation).open();
55             return;
56         }
57
58         if (isBreakpoint(annotation))
59             triggerAction(ITextEditorActionConstants.RULER_DOUBLE_CLICK);
60
61         Position position= model.getPosition(annotation);
62         if (position == null)
63             return;
64
65         if (isQuickFixTarget(annotation)) {
66             ITextOperationTarget operation= (ITextOperationTarget) getTextEditor().getAdapter(ITextOperationTarget.class);
67             final int opCode= ISourceViewer.QUICK_ASSIST;
68             if (operation != null && operation.canDoOperation(opCode)) {
69                 getTextEditor().selectAndReveal(position.getOffset(), position.getLength());
70                 operation.doOperation(opCode);
71                 return;
72             }
73         }
74
75         // default:
76
super.annotationDefaultSelected(event);
77     }
78
79     /**
80      * Tells whether the given annotation is an override annotation.
81      *
82      * @param annotation the annotation
83      * @return <code>true</code> iff the annotation is an override annotation
84      */

85     private boolean isOverrideIndicator(Annotation annotation) {
86         return annotation instanceof OverrideIndicatorManager.OverrideIndicator;
87     }
88
89     /**
90      * Checks whether the given annotation is a breakpoint annotation.
91      *
92      * @param annotation
93      * @return <code>true</code> if the annotation is a breakpoint annotation
94      */

95     private boolean isBreakpoint(Annotation annotation) {
96         return annotation.getType().equals("org.eclipse.debug.core.breakpoint") || annotation.getType().equals(JavaExpandHover.NO_BREAKPOINT_ANNOTATION); //$NON-NLS-1$
97
}
98
99     private boolean isQuickFixTarget(Annotation a) {
100         return JavaCorrectionProcessor.hasCorrections(a) || a instanceof AssistAnnotation;
101     }
102
103     private void triggerAction(String JavaDoc actionID) {
104         IAction action= getTextEditor().getAction(actionID);
105         if (action != null) {
106             if (action instanceof IUpdate)
107                 ((IUpdate) action).update();
108             // hack to propagate line change
109
if (action instanceof ISelectionListener) {
110                 ((ISelectionListener)action).selectionChanged(null, null);
111             }
112             if (action.isEnabled())
113                 action.run();
114         }
115     }
116
117 }
118
119
Popular Tags