1 package net.sourceforge.phpeclipse.phpeditor;
 
   3 /**********************************************************************
 
   4  Copyright (c) 2000, 2002 IBM Corp. and others.
 
   5  All rights reserved. This program and the accompanying materials
 
   6  are made available under the terms of the Common Public License v1.0
 
   7  which accompanies this distribution, and is available at
 
   8  http://www.eclipse.org/legal/cpl-v10.html
 
  11  IBM Corporation - Initial implementation
 
  13  **********************************************************************/
 
  15 import java.util.ArrayList;
 
  16 import java.util.Iterator;
 
  17 import java.util.List;
 
  19 import org.eclipse.core.resources.IMarker;
 
  20 import org.eclipse.jface.text.BadLocationException;
 
  21 import org.eclipse.jface.text.IDocument;
 
  22 import org.eclipse.jface.text.Position;
 
  23 import org.eclipse.jface.text.source.IAnnotationHover;
 
  24 import org.eclipse.jface.text.source.IAnnotationModel;
 
  25 import org.eclipse.jface.text.source.ISourceViewer;
 
  26 import org.eclipse.ui.texteditor.MarkerAnnotation;
 
  29  * The PHPAnnotationHover provides the hover support for PHP editors.
 
  32 public class PHPAnnotationHover implements IAnnotationHover {
 
  35          * (non-Javadoc) Method declared on IAnnotationHover
 
  37         // public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
 
  38         // IDocument document= sourceViewer.getDocument();
 
  41         // IRegion info= document.getLineInformation(lineNumber);
 
  42         // return document.get(info.getOffset(), info.getLength());
 
  43         // } catch (BadLocationException x) {
 
  49         static final int MAX_INFO_LENGTH = 80;
 
  52          * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer,
 
  56         public String getHoverInfo(ISourceViewer viewer, int line) {
 
  58                 List markers = getMarkersForLine(viewer, line);
 
  59                 if (markers != null) {
 
  61                         for (int i = 0; i < markers.size(); i++) {
 
  62                                 IMarker marker = (IMarker) markers.get(i);
 
  63                                 String message = marker.getAttribute(IMarker.MESSAGE,
 
  65                                 if (message != null && message.trim().length() > 0) {
 
  67                                         if (message.length() > MAX_INFO_LENGTH) {
 
  68                                                 message = splitMessage(message);
 
  72                                         if (i != markers.size() - 1) {
 
  81         private String splitMessage(String message) {
 
  84                 if (message.length() <= MAX_INFO_LENGTH) {
 
  88                 String tmpStr = new String(message);
 
  90                 while (tmpStr.length() > MAX_INFO_LENGTH) {
 
  92                         int spacepos = tmpStr.indexOf(" ", MAX_INFO_LENGTH);
 
  95                                 result += tmpStr.substring(0, spacepos) + "\n";
 
  96                                 tmpStr = tmpStr.substring(spacepos);
 
  98                                 result += tmpStr.substring(0, MAX_INFO_LENGTH) + "\n";
 
  99                                 tmpStr = tmpStr.substring(MAX_INFO_LENGTH);
 
 110          * Returns all markers which includes the ruler's line of activity.
 
 112         protected List getMarkersForLine(ISourceViewer aViewer, int aLine) {
 
 113                 List markers = new ArrayList();
 
 114                 IAnnotationModel model = aViewer.getAnnotationModel();
 
 116                         Iterator e = model.getAnnotationIterator();
 
 117                         while (e.hasNext()) {
 
 119                                 if (o instanceof MarkerAnnotation) {
 
 120                                         MarkerAnnotation a = (MarkerAnnotation) o;
 
 121                                         if (compareRulerLine(model.getPosition(a), aViewer
 
 122                                                         .getDocument(), aLine) != 0) {
 
 123                                                 markers.add(a.getMarker());
 
 132          * Returns one marker which includes the ruler's line of activity.
 
 134         protected IMarker getMarkerForLine(ISourceViewer aViewer, int aLine) {
 
 135                 IMarker marker = null;
 
 136                 IAnnotationModel model = aViewer.getAnnotationModel();
 
 138                         Iterator e = model.getAnnotationIterator();
 
 139                         while (e.hasNext()) {
 
 141                                 if (o instanceof MarkerAnnotation) {
 
 142                                         MarkerAnnotation a = (MarkerAnnotation) o;
 
 143                                         if (compareRulerLine(model.getPosition(a), aViewer
 
 144                                                         .getDocument(), aLine) != 0) {
 
 145                                                 marker = a.getMarker();
 
 154          * Returns distance of given line to specified position (1 = same line, 2 =
 
 155          * included in given position, 0 = not related).
 
 157         protected int compareRulerLine(Position aPosition, IDocument aDocument,
 
 160                 if (aPosition.getOffset() > -1 && aPosition.getLength() > -1) {
 
 162                                 int markerLine = aDocument.getLineOfOffset(aPosition
 
 164                                 if (aLine == markerLine) {
 
 166                                 } else if (markerLine <= aLine
 
 167                                                 && aLine <= aDocument.getLineOfOffset(aPosition
 
 169                                                                 + aPosition.getLength())) {
 
 172                         } catch (BadLocationException e) {