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.PHPCodeReader;
21 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementLabels;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
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 | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS
35 | JavaElementLabels.F_PRE_TYPE_SIGNATURE;
39 * @see JavaElementHover
41 protected String getHoverInfo(IJavaElement[] result) {
42 int nResults= result.length;
43 StringBuffer buffer= new StringBuffer();
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);
57 IJavaElement curr= result[0];
58 if (curr instanceof IMember && curr instanceof ISourceReference) {
59 HTMLPrinter.addSmallHeader(buffer, getInfoText(((IMember)curr)));
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
72 if (buffer.length() > 0) {
73 HTMLPrinter.insertPageProlog(buffer, 0);
74 HTMLPrinter.addPageEpilog(buffer);
75 return buffer.toString();
81 private String getInfoText(IMember member) {
82 return JavaElementLabels.getElementLabel(member, LABEL_FLAGS);
85 private String removeLeadingComments(String source) {
86 PHPCodeReader reader= new PHPCodeReader();
87 IDocument document= new Document(source);
90 reader.configureForwardReader(document, 0, document.getLength(), true, false);
92 while (c != -1 && (c == '\r' || c == '\n')) {
95 i= reader.getOffset();
97 } catch (IOException ex) {
103 } catch (IOException ex) {
104 PHPeclipsePlugin.log(ex);
110 return source.substring(i);