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