786076fca4a577c665c400f5cce283faa06e8c46
[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
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
13
14 import java.io.IOException;
15
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IMember;
18 import net.sourceforge.phpdt.core.ISourceReference;
19 import net.sourceforge.phpdt.core.JavaModelException;
20 import net.sourceforge.phpdt.internal.ui.text.PHPCodeReader;
21 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23
24 import org.eclipse.jface.text.Document;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.ui.externaltools.internal.ant.editor.derived.HTMLPrinter;
27
28 /**
29  * Provides source as hover info for Java elements.
30  */
31 public class JavaSourceHover extends AbstractJavaEditorTextHover {
32
33         private final int LABEL_FLAGS=  JavaElementLabels.ALL_FULLY_QUALIFIED
34                 | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS 
35                 | JavaElementLabels.F_PRE_TYPE_SIGNATURE;
36
37
38         /*
39          * @see JavaElementHover
40          */
41         protected String getHoverInfo(IJavaElement[] result) {
42                 int nResults= result.length;
43                 StringBuffer buffer= new StringBuffer();
44                 
45                 if (nResults > 1) {
46                         
47                         for (int i= 0; i < result.length; i++) {
48                                 HTMLPrinter.startBulletList(buffer);
49                                 IJavaElement curr= result[i];
50                                 if (curr instanceof IMember)
51                                         HTMLPrinter.addBullet(buffer, getInfoText((IMember) curr));
52                                 HTMLPrinter.endBulletList(buffer);
53                         }
54                         
55                 } else {
56                         
57                         IJavaElement curr= result[0];
58                         if (curr instanceof IMember && curr instanceof ISourceReference) {
59                                 HTMLPrinter.addSmallHeader(buffer, getInfoText(((IMember)curr)));
60                                 try {
61                                         String source= ((ISourceReference)curr).getSource();
62                                         source= removeLeadingComments(source);
63                                         HTMLPrinter.addParagraph(buffer, "<pre>"); //$NON-NLS-1$
64                                         HTMLPrinter.addParagraph(buffer, source);
65                                         HTMLPrinter.addParagraph(buffer, "</pre>"); //$NON-NLS-1$
66                                 } catch (JavaModelException ex) {
67                                         // only write small header
68                                 }
69                         }
70                 }
71                 
72                 if (buffer.length() > 0) {
73                         HTMLPrinter.insertPageProlog(buffer, 0);
74                         HTMLPrinter.addPageEpilog(buffer);
75                         return buffer.toString();
76                 }
77                 
78                 return null;
79         }
80
81         private String getInfoText(IMember member) {
82                 return JavaElementLabels.getElementLabel(member, LABEL_FLAGS);
83         }
84
85         private String removeLeadingComments(String source) {
86                 PHPCodeReader reader= new PHPCodeReader();
87                 IDocument document= new Document(source);
88                 int i;
89                 try {
90                         reader.configureForwardReader(document, 0, document.getLength(), true, false);
91                         int c= reader.read();
92                         while (c != -1 && (c == '\r' || c == '\n')) {
93                                 c= reader.read();
94                         }
95                         i= reader.getOffset();
96                         reader.close();
97                 } catch (IOException ex) {
98                         i= 0;
99                 } finally {
100                         try {
101                                 if (reader != null)
102                                         reader.close();
103                         } catch (IOException ex) {
104                                 PHPeclipsePlugin.log(ex);
105                         }
106                 }
107
108                 if (i < 0)
109                         return source;
110                 return source.substring(i);
111         }
112 }