6e01d7bb6ec5c146742a7f26fc431ae2aa0f17c0
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / JavaSelectMarkerRulerAction.java
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 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.ITextOperationTarget;
26 import org.eclipse.jface.text.Position;
27 import org.eclipse.jface.text.source.Annotation;
28 import org.eclipse.jface.text.source.IVerticalRulerInfo;
29 import org.eclipse.ui.IEditorInput;
30 import org.eclipse.ui.IFileEditorInput;
31 import org.eclipse.ui.help.WorkbenchHelp;
32 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
33 import org.eclipse.ui.texteditor.ITextEditor;
34 import org.eclipse.ui.texteditor.ITextEditorExtension;
35 import org.eclipse.ui.texteditor.SelectMarkerRulerAction;
36
37
38 /**
39  * A special select marker ruler action which activates quick fix if clicked on a quick fixable problem.
40  */
41 public class JavaSelectMarkerRulerAction extends SelectMarkerRulerAction {
42
43         private ITextEditor fTextEditor;
44         private Position fPosition;
45
46         public JavaSelectMarkerRulerAction(ResourceBundle bundle, String prefix, ITextEditor editor, IVerticalRulerInfo ruler) {
47                 super(bundle, prefix, editor, ruler);
48                 fTextEditor= editor;
49                 WorkbenchHelp.setHelp(this, IJavaHelpContextIds.JAVA_SELECT_MARKER_RULER_ACTION);
50         }
51         
52         public void run() {
53 //              if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_ANNOTATION_ROLL_OVER))
54 //                      return;
55
56                 if (fPosition != null) {
57                         ITextOperationTarget operation= (ITextOperationTarget) fTextEditor.getAdapter(ITextOperationTarget.class);
58 //                      final int opCode= PHPUnitEditor.CORRECTIONASSIST_PROPOSALS;
59 //                      if (operation != null && operation.canDoOperation(opCode)) {
60 //                              fTextEditor.selectAndReveal(fPosition.getOffset(), fPosition.getLength());
61 //                              operation.doOperation(opCode);
62 //                              return;
63 //                      }
64                         return;
65                 }
66                 super.run();
67         }
68         
69         public void update() {
70                 // Begin Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20114
71                 if (!(fTextEditor instanceof ITextEditorExtension) || ((ITextEditorExtension) fTextEditor).isEditorInputReadOnly()) {
72                         fPosition= null;
73                         super.update();
74                         return;
75                 }
76                 // End Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20114
77                 fPosition= getJavaAnnotationPosition();
78                 if (fPosition != null)
79                         setEnabled(true);
80                 else
81                         super.update();
82         }
83         
84         private Position getJavaAnnotationPosition() {
85                 AbstractMarkerAnnotationModel model= getAnnotationModel();
86                 IDocument document= getDocument();
87                 if (model == null)
88                         return null;
89                 ICompilationUnit cu= getCompilationUnit();
90                 if (cu == null) {
91                         return null;
92                 }
93                 
94 //              boolean hasAssistLightbulb= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.APPEARANCE_QUICKASSIST_LIGHTBULB);
95                 Annotation assistAnnotation= null;
96                         
97                 Iterator iter= model.getAnnotationIterator();
98                 while (iter.hasNext()) {
99                         Annotation annotation= (Annotation) iter.next();
100                         if (annotation instanceof IJavaAnnotation) {
101                                 IJavaAnnotation javaAnnotation= (IJavaAnnotation)annotation;
102                                 if (!javaAnnotation.isMarkedDeleted()) {
103                                         Position position= model.getPosition(annotation);
104 //                                      if (includesRulerLine(position, document) && JavaCorrectionProcessor.hasCorrections(javaAnnotation))
105 //                                              return position;
106                                 }
107                         } 
108 //                      else if (hasAssistLightbulb && annotation instanceof AssistAnnotation) {
109 //                              // there is only one AssistAnnotation at a time
110 //                              assistAnnotation= annotation; 
111 //                      }
112                 }
113                 if (assistAnnotation != null) {
114                         Position position= model.getPosition(assistAnnotation);
115                         // no need to check 'JavaCorrectionProcessor.hasAssists': annotation only created when
116                         // there are assists
117                         if (includesRulerLine(position, document))
118                                 return position;
119                 }
120                 return null;
121         }
122         
123         private ICompilationUnit getCompilationUnit() {
124                 IEditorInput input= fTextEditor.getEditorInput();
125                 if (input instanceof IFileEditorInput) {
126                         IFile file= ((IFileEditorInput) input).getFile();
127                         IJavaElement element= JavaCore.create(file);
128                         if (element instanceof ICompilationUnit)
129                                 return (ICompilationUnit) element;
130                 }
131                 return null;
132         }
133 }
134