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