e265b5469f299c6e01c1add47bb96164fd28c66d
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
12
13 import java.util.Iterator;
14 import java.util.ResourceBundle;
15
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.JavaCore;
19 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.ITextOperationTarget;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.source.Annotation;
26 import org.eclipse.jface.text.source.IVerticalRulerInfo;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IFileEditorInput;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
31 import org.eclipse.ui.texteditor.ITextEditor;
32 import org.eclipse.ui.texteditor.ITextEditorExtension;
33 import org.eclipse.ui.texteditor.SelectMarkerRulerAction;
34
35 /**
36  * A special select marker ruler action which activates quick fix if clicked on
37  * a quick fixable problem.
38  */
39 public class JavaSelectMarkerRulerAction extends SelectMarkerRulerAction {
40
41         private ITextEditor fTextEditor;
42
43         private Position fPosition;
44
45         public JavaSelectMarkerRulerAction(ResourceBundle bundle, String prefix,
46                         ITextEditor editor, IVerticalRulerInfo ruler) {
47                 super(bundle, prefix, editor, ruler);
48                 fTextEditor = editor;
49                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
50                                 IJavaHelpContextIds.JAVA_SELECT_MARKER_RULER_ACTION);
51         }
52
53         public void run() {
54                 // if
55                 // (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_ANNOTATION_ROLL_OVER))
56                 // return;
57
58                 if (fPosition != null) {
59                         ITextOperationTarget operation = (ITextOperationTarget) fTextEditor
60                                         .getAdapter(ITextOperationTarget.class);
61                         // final int opCode= PHPUnitEditor.CORRECTIONASSIST_PROPOSALS;
62                         // if (operation != null && operation.canDoOperation(opCode)) {
63                         // fTextEditor.selectAndReveal(fPosition.getOffset(),
64                         // fPosition.getLength());
65                         // operation.doOperation(opCode);
66                         // return;
67                         // }
68                         return;
69                 }
70                 super.run();
71         }
72
73         public void update() {
74                 // Begin Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20114
75                 if (!(fTextEditor instanceof ITextEditorExtension)
76                                 || ((ITextEditorExtension) fTextEditor).isEditorInputReadOnly()) {
77                         fPosition = null;
78                         super.update();
79                         return;
80                 }
81                 // End Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20114
82                 fPosition = getJavaAnnotationPosition();
83                 if (fPosition != null)
84                         setEnabled(true);
85                 else
86                         super.update();
87         }
88
89         private Position getJavaAnnotationPosition() {
90                 AbstractMarkerAnnotationModel model = getAnnotationModel();
91                 IDocument document = getDocument();
92                 if (model == null)
93                         return null;
94                 ICompilationUnit cu = getCompilationUnit();
95                 if (cu == null) {
96                         return null;
97                 }
98
99                 // boolean hasAssistLightbulb=
100                 // PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.APPEARANCE_QUICKASSIST_LIGHTBULB);
101                 Annotation assistAnnotation = null;
102
103                 Iterator iter = model.getAnnotationIterator();
104                 while (iter.hasNext()) {
105                         Annotation annotation = (Annotation) iter.next();
106                         if (annotation instanceof IJavaAnnotation) {
107                                 IJavaAnnotation javaAnnotation = (IJavaAnnotation) annotation;
108                                 if (!javaAnnotation.isMarkedDeleted()) {
109                                         Position position = model.getPosition(annotation);
110                                         // if (includesRulerLine(position, document) &&
111                                         // JavaCorrectionProcessor.hasCorrections(javaAnnotation))
112                                         // return position;
113                                 }
114                         }
115                         // else if (hasAssistLightbulb && annotation instanceof
116                         // AssistAnnotation) {
117                         // // there is only one AssistAnnotation at a time
118                         // assistAnnotation= annotation;
119                         // }
120                 }
121                 if (assistAnnotation != null) {
122                         Position position = model.getPosition(assistAnnotation);
123                         // no need to check 'JavaCorrectionProcessor.hasAssists': annotation
124                         // only created when
125                         // there are assists
126                         if (includesRulerLine(position, document))
127                                 return position;
128                 }
129                 return null;
130         }
131
132         private ICompilationUnit getCompilationUnit() {
133                 IEditorInput input = fTextEditor.getEditorInput();
134                 if (input instanceof IFileEditorInput) {
135                         IFile file = ((IFileEditorInput) input).getFile();
136                         IJavaElement element = JavaCore.create(file);
137                         if (element instanceof ICompilationUnit)
138                                 return (ICompilationUnit) element;
139                 }
140                 return null;
141         }
142 }