1ca1d0b52c33d3123186df5c91a8b0f4e4f32778
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / hover / JavaProblemHover.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
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
13
14 import java.util.Iterator;
15
16 import net.sourceforge.phpdt.ui.PreferenceConstants;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.phpeditor.IJavaAnnotation;
19 import net.sourceforge.phpeclipse.phpeditor.JavaAnnotationIterator;
20 import net.sourceforge.phpeclipse.phpeditor.PHPUnitEditor;
21
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.source.Annotation;
26 import org.eclipse.jface.text.source.IAnnotationModel;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.externaltools.internal.ant.editor.derived.HTMLPrinter;
29 import org.eclipse.ui.texteditor.IDocumentProvider;
30
31
32 public class JavaProblemHover extends AbstractJavaEditorTextHover {
33
34         /*
35          * Formats a message as HTML text.
36          */
37         private String formatMessage(String message) {
38                 StringBuffer buffer= new StringBuffer();
39                 HTMLPrinter.addPageProlog(buffer);
40                 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
41                 HTMLPrinter.addPageEpilog(buffer);
42                 return buffer.toString();
43         }
44         
45         /*
46          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
47          */
48         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
49                 
50                 if (getEditor() == null)
51                         return null;
52                         
53                 IDocumentProvider provider= PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider();
54                 IAnnotationModel model= provider.getAnnotationModel(getEditor().getEditorInput());
55                 
56                 if (model != null) {
57                         Iterator e= new JavaAnnotationIterator(model, true);
58                         while (e.hasNext()) {
59                                 Annotation a= (Annotation) e.next();
60                                 Position p= model.getPosition(a);
61                                 if (p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
62                                         String msg= ((IJavaAnnotation) a).getMessage();
63                                         if (msg != null && msg.trim().length() > 0)
64                                                 return formatMessage(msg);
65                                 }
66                         }
67                 }
68                 
69                 return null;
70         }
71         
72         /*
73          * @see IJavaEditorTextHover#setEditor(IEditorPart)
74          */
75         public void setEditor(IEditorPart editor) {
76                 if (editor instanceof PHPUnitEditor)
77                         super.setEditor(editor);
78                 else
79                         super.setEditor(null);
80         }
81
82         static boolean isJavaProblemHover(String id) {
83                 return PreferenceConstants.ID_PROBLEM_HOVER.equals(id);
84         }
85 }