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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
14 import java.io.IOException;
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.PHPeclipsePlugin;
25 import org.eclipse.jface.text.Document;
26 import org.eclipse.jface.text.IDocument;
29 * Provides source as hover info for Java elements.
31 public class JavaSourceHover extends AbstractJavaEditorTextHover {
33 private final int LABEL_FLAGS = JavaElementLabels.ALL_FULLY_QUALIFIED
34 | JavaElementLabels.M_PRE_RETURNTYPE
35 | JavaElementLabels.M_PARAMETER_TYPES
36 | JavaElementLabels.M_PARAMETER_NAMES
37 | JavaElementLabels.M_EXCEPTIONS
38 | JavaElementLabels.F_PRE_TYPE_SIGNATURE;
41 * @see JavaElementHover
43 protected String getHoverInfo(IJavaElement[] result) {
44 int nResults = result.length;
45 StringBuffer buffer = new StringBuffer();
49 for (int i = 0; i < result.length; i++) {
50 HTMLPrinter.startBulletList(buffer);
51 IJavaElement curr = result[i];
52 if (curr instanceof IMember)
53 HTMLPrinter.addBullet(buffer, getInfoText((IMember) curr));
54 HTMLPrinter.endBulletList(buffer);
59 IJavaElement curr = result[0];
60 if (curr instanceof IMember && curr instanceof ISourceReference) {
61 HTMLPrinter.addSmallHeader(buffer,
62 getInfoText(((IMember) curr)));
64 String source = ((ISourceReference) curr).getSource();
65 source = removeLeadingComments(source);
66 HTMLPrinter.addParagraph(buffer, "<pre>"); //$NON-NLS-1$
67 HTMLPrinter.addParagraph(buffer, source);
68 HTMLPrinter.addParagraph(buffer, "</pre>"); //$NON-NLS-1$
69 } catch (JavaModelException ex) {
70 // only write small header
75 if (buffer.length() > 0) {
76 HTMLPrinter.insertPageProlog(buffer, 0);
77 HTMLPrinter.addPageEpilog(buffer);
78 return buffer.toString();
84 private String getInfoText(IMember member) {
85 return JavaElementLabels.getElementLabel(member, LABEL_FLAGS);
88 private String removeLeadingComments(String source) {
89 PHPCodeReader reader = new PHPCodeReader();
90 IDocument document = new Document(source);
93 reader.configureForwardReader(document, 0, document.getLength(),
95 int c = reader.read();
96 while (c != -1 && (c == '\r' || c == '\n')) {
99 i = reader.getOffset();
101 } catch (IOException ex) {
107 } catch (IOException ex) {
108 PHPeclipsePlugin.log(ex);
114 return source.substring(i);