Eclipse 3M7
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / JavaAnnotationHover.java
index c57a86d..bdecc4a 100644 (file)
@@ -16,9 +16,11 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import net.sourceforge.phpdt.internal.corext.Assert;
 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
-import net.sourceforge.phpeclipse.phpeditor.IJavaAnnotation;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
+import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.Position;
@@ -26,6 +28,8 @@ import org.eclipse.jface.text.source.Annotation;
 import org.eclipse.jface.text.source.IAnnotationHover;
 import org.eclipse.jface.text.source.IAnnotationModel;
 import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.ui.editors.text.EditorsUI;
+import org.eclipse.ui.texteditor.AnnotationPreference;
 
 // TODO: delete this class ? we use PHPAnnotationHover instead !
 /**
@@ -33,6 +37,21 @@ import org.eclipse.jface.text.source.ISourceViewer;
  * their messages.
  */
 public class JavaAnnotationHover implements IAnnotationHover {
+       private static class JavaAnnotationHoverType {
+       }
+       
+       public static final JavaAnnotationHoverType OVERVIEW_RULER_HOVER= new JavaAnnotationHoverType();
+       public static final JavaAnnotationHoverType TEXT_RULER_HOVER= new JavaAnnotationHoverType();
+       public static final JavaAnnotationHoverType VERTICAL_RULER_HOVER= new JavaAnnotationHoverType();
+       
+       private IPreferenceStore fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
+       
+       private JavaAnnotationHoverType fType;
+       
+       public JavaAnnotationHover(JavaAnnotationHoverType type) {
+               Assert.isTrue(OVERVIEW_RULER_HOVER.equals(type) || TEXT_RULER_HOVER.equals(type) || VERTICAL_RULER_HOVER.equals(type));
+               fType= type;
+       }
        
        /**
         * Returns the distance to the ruler line. 
@@ -60,7 +79,6 @@ public class JavaAnnotationHover implements IAnnotationHover {
        protected List select(List exactMatch, List including) {
                return exactMatch;
        }
-       
        /**
         * Returns one marker which includes the ruler's line of activity.
         */
@@ -78,31 +96,97 @@ public class JavaAnnotationHover implements IAnnotationHover {
                Iterator e= model.getAnnotationIterator();
                HashMap messagesAtPosition= new HashMap();
                while (e.hasNext()) {
-                       Object o= e.next();
-                       if (o instanceof IJavaAnnotation) {
-                               IJavaAnnotation a= (IJavaAnnotation)o;
-                               if (!a.hasOverlay()) {
-                                       Position position= model.getPosition((Annotation)a);
-                                       if (position == null)
-                                               continue;
+                       Annotation annotation= (Annotation) e.next();
 
-                                       if (isDuplicateJavaAnnotation(messagesAtPosition, position, a.getMessage()))
+                       if (annotation.getText() == null)
+                               continue;
+                       
+                       Position position= model.getPosition(annotation);
+                       if (position == null)
+                               continue;
+                       
+                       AnnotationPreference preference= getAnnotationPreference(annotation);
+                       if (preference == null)
+                               continue;
+                       
+                       if (OVERVIEW_RULER_HOVER.equals(fType)) {                               
+                               String key= preference.getOverviewRulerPreferenceKey();
+                               if (key == null || !fStore.getBoolean(key))
+                                       continue;
+                       } else if (TEXT_RULER_HOVER.equals(fType)) {
+                               String key= preference.getTextPreferenceKey();
+                               if (key != null) {
+                                       if (!fStore.getBoolean(key))
+                                               continue;
+                               } else {
+                                       key= preference.getHighlightPreferenceKey();
+                                       if (key == null || !fStore.getBoolean(key))
                                                continue;
-       
-                                       switch (compareRulerLine(position, document, line)) {
-                                               case 1:
-                                                       exact.add(a);
-                                                       break;
-                                               case 2:
-                                                       including.add(a);
-                                                       break;
-                                       }
                                }
+                       } else if (VERTICAL_RULER_HOVER.equals(fType)) {
+                               String key= preference.getVerticalRulerPreferenceKey();
+                               // backward compatibility
+                               if (key != null && !fStore.getBoolean(key))
+                                       continue;
+                       }
+                       
+                       if (isDuplicateJavaAnnotation(messagesAtPosition, position, annotation.getText()))
+                               continue;
+                       
+                       switch (compareRulerLine(position, document, line)) {
+                       case 1:
+                               exact.add(annotation);
+                               break;
+                       case 2:
+                               including.add(annotation);
+                               break;
                        }
                }
                
                return select(exact, including);
        }
+//     /**
+//      * Returns one marker which includes the ruler's line of activity.
+//      */
+//     protected List getJavaAnnotationsForLine(ISourceViewer viewer, int line) {
+//             
+//             IDocument document= viewer.getDocument();
+//             IAnnotationModel model= viewer.getAnnotationModel();
+//             
+//             if (model == null)
+//                     return null;
+//                     
+//             List exact= new ArrayList();
+//             List including= new ArrayList();
+//             
+//             Iterator e= model.getAnnotationIterator();
+//             HashMap messagesAtPosition= new HashMap();
+//             while (e.hasNext()) {
+//                     Object o= e.next();
+//                     if (o instanceof IJavaAnnotation) {
+//                             IJavaAnnotation a= (IJavaAnnotation)o;
+//                             if (!a.hasOverlay()) {
+//                                     Position position= model.getPosition((Annotation)a);
+//                                     if (position == null)
+//                                             continue;
+//
+//                                     if (isDuplicateJavaAnnotation(messagesAtPosition, position, a.getMessage()))
+//                                             continue;
+//     
+//                                     switch (compareRulerLine(position, document, line)) {
+//                                             case 1:
+//                                                     exact.add(a);
+//                                                     break;
+//                                             case 2:
+//                                                     including.add(a);
+//                                                     break;
+//                                     }
+//                             }
+//                     }
+//             }
+//             
+//             return select(exact, including);
+//     }
 
        private boolean isDuplicateJavaAnnotation(Map messagesAtPosition, Position position, String message) {
                if (messagesAtPosition.containsKey(position)) {
@@ -126,7 +210,7 @@ public class JavaAnnotationHover implements IAnnotationHover {
                        messagesAtPosition.put(position, message);
                return false;
        }
-               
+       
        /*
         * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
         */
@@ -137,8 +221,8 @@ public class JavaAnnotationHover implements IAnnotationHover {
                        if (javaAnnotations.size() == 1) {
                                
                                // optimization
-                               IJavaAnnotation javaAnnotation= (IJavaAnnotation) javaAnnotations.get(0);
-                               String message= javaAnnotation.getMessage();
+                               Annotation annotation= (Annotation) javaAnnotations.get(0);
+                               String message= annotation.getText();
                                if (message != null && message.trim().length() > 0)
                                        return formatSingleMessage(message);
                                        
@@ -148,8 +232,8 @@ public class JavaAnnotationHover implements IAnnotationHover {
                                
                                Iterator e= javaAnnotations.iterator();
                                while (e.hasNext()) {
-                                       IJavaAnnotation javaAnnotation= (IJavaAnnotation) e.next();
-                                       String message= javaAnnotation.getMessage();
+                                       Annotation annotation= (Annotation) e.next();
+                                       String message= annotation.getText();
                                        if (message != null && message.trim().length() > 0)
                                                messages.add(message.trim());
                                }
@@ -164,6 +248,43 @@ public class JavaAnnotationHover implements IAnnotationHover {
                
                return null;
        }
+       /*
+        * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
+        */
+//     public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
+//             List javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber);
+//             if (javaAnnotations != null) {
+//                     
+//                     if (javaAnnotations.size() == 1) {
+//                             
+//                             // optimization
+//                             IJavaAnnotation javaAnnotation= (IJavaAnnotation) javaAnnotations.get(0);
+//                             String message= javaAnnotation.getMessage();
+//                             if (message != null && message.trim().length() > 0)
+//                                     return formatSingleMessage(message);
+//                                     
+//                     } else {
+//                                     
+//                             List messages= new ArrayList();
+//                             
+//                             Iterator e= javaAnnotations.iterator();
+//                             while (e.hasNext()) {
+//                                     IJavaAnnotation javaAnnotation= (IJavaAnnotation) e.next();
+//                                     String message= javaAnnotation.getMessage();
+//                                     if (message != null && message.trim().length() > 0)
+//                                             messages.add(message.trim());
+//                             }
+//                             
+//                             if (messages.size() == 1)
+//                                     return formatSingleMessage((String) messages.get(0));
+//                                     
+//                             if (messages.size() > 1)
+//                                     return formatMultipleMessages(messages);
+//                     }
+//             }
+//             
+//             return null;
+//     }
        
        /*
         * Formats a message as HTML text.
@@ -193,4 +314,14 @@ public class JavaAnnotationHover implements IAnnotationHover {
                HTMLPrinter.addPageEpilog(buffer);
                return buffer.toString();
        }
+       
+       /**
+        * Returns the annotation preference for the given annotation.
+        * 
+        * @param annotation the annotation
+        * @return the annotation preference or <code>null</code> if none
+        */     
+       private AnnotationPreference getAnnotationPreference(Annotation annotation) {
+               return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
+       }
 }