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.ui.WebUI;
 
  24 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  26 import org.eclipse.jface.text.Document;
 
  27 import org.eclipse.jface.text.IDocument;
 
  30  * Provides source as hover info for Java elements.
 
  32 public class JavaSourceHover extends AbstractJavaEditorTextHover {
 
  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;
 
  42          * @see JavaElementHover
 
  44         protected String getHoverInfo(IJavaElement[] result) {
 
  45                 int nResults = result.length;
 
  46                 StringBuffer buffer = new StringBuffer();
 
  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);
 
  60                         IJavaElement curr = result[0];
 
  61                         if (curr instanceof IMember && curr instanceof ISourceReference) {
 
  62                                 HTMLPrinter.addSmallHeader(buffer,
 
  63                                                 getInfoText(((IMember) curr)));
 
  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
 
  76                 if (buffer.length() > 0) {
 
  77                         HTMLPrinter.insertPageProlog(buffer, 0);
 
  78                         HTMLPrinter.addPageEpilog(buffer);
 
  79                         return buffer.toString();
 
  85         private String getInfoText(IMember member) {
 
  86                 return JavaElementLabels.getElementLabel(member, LABEL_FLAGS);
 
  89         private String removeLeadingComments(String source) {
 
  90                 PHPCodeReader reader = new PHPCodeReader();
 
  91                 IDocument document = new Document(source);
 
  94                         reader.configureForwardReader(document, 0, document.getLength(),
 
  96                         int c = reader.read();
 
  97                         while (c != -1 && (c == '\r' || c == '\n')) {
 
 100                         i = reader.getOffset();
 
 102                 } catch (IOException ex) {
 
 108                         } catch (IOException ex) {
 
 115                 return source.substring(i);