1 package net.sourceforge.phpdt.internal.ui.text;
2 /**********************************************************************
3 Copyright (c) 2000, 2002 IBM Corp. and others.
4 All rights reserved. This program and the accompanying materials
5 are made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
10 IBM Corporation - Initial implementation
11 Klaus Hartlage - www.eclipseproject.de
12 **********************************************************************/
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.Position;
22 import org.eclipse.jface.text.source.IAnnotationHover;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24 import org.eclipse.jface.text.source.ISourceViewer;
25 import org.eclipse.ui.texteditor.MarkerAnnotation;
28 * The PHPAnnotationHover provides the hover support for PHP editors.
31 public class PHPAnnotationHover implements IAnnotationHover {
34 * Method declared on IAnnotationHover
36 // public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
37 // IDocument document= sourceViewer.getDocument();
40 // IRegion info= document.getLineInformation(lineNumber);
41 // return document.get(info.getOffset(), info.getLength());
42 // } catch (BadLocationException x) {
48 static final int MAX_INFO_LENGTH = 80;
51 * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int)
54 public String getHoverInfo(ISourceViewer viewer, int line) {
56 List markers = getMarkersForLine(viewer, line);
57 if (markers != null) {
59 for (int i = 0; i < markers.size(); i++) {
60 IMarker marker = (IMarker) markers.get(i);
62 marker.getAttribute(IMarker.MESSAGE, (String) null);
63 if (message != null && message.trim().length() > 0) {
65 if (message.length() > MAX_INFO_LENGTH) {
66 message = splitMessage(message);
70 if(i != markers.size() - 1) {
79 private String splitMessage(String message) {
82 if(message.length() <= MAX_INFO_LENGTH) {
86 String tmpStr = new String(message);
88 while(tmpStr.length() > MAX_INFO_LENGTH) {
90 int spacepos = tmpStr.indexOf(" ", MAX_INFO_LENGTH);
93 result += tmpStr.substring(0, spacepos) + "\n";
94 tmpStr = tmpStr.substring(spacepos);
97 result += tmpStr.substring(0, MAX_INFO_LENGTH) + "\n";
98 tmpStr = tmpStr.substring(MAX_INFO_LENGTH);
111 * Returns all markers which includes the ruler's line of activity.
113 protected List getMarkersForLine(ISourceViewer aViewer, int aLine) {
114 List markers = new ArrayList();
115 IAnnotationModel model = aViewer.getAnnotationModel();
117 Iterator e = model.getAnnotationIterator();
118 while (e.hasNext()) {
120 if (o instanceof MarkerAnnotation) {
121 MarkerAnnotation a = (MarkerAnnotation) o;
122 if (compareRulerLine(model.getPosition(a),
123 aViewer.getDocument(),
126 markers.add(a.getMarker());
135 * Returns one marker which includes the ruler's line of activity.
137 protected IMarker getMarkerForLine(ISourceViewer aViewer, int aLine) {
138 IMarker marker = null;
139 IAnnotationModel model = aViewer.getAnnotationModel();
141 Iterator e = model.getAnnotationIterator();
142 while (e.hasNext()) {
144 if (o instanceof MarkerAnnotation) {
145 MarkerAnnotation a = (MarkerAnnotation) o;
146 if (compareRulerLine(model.getPosition(a),
147 aViewer.getDocument(),
150 marker = a.getMarker();
159 * Returns distance of given line to specified position (1 = same line,
160 * 2 = included in given position, 0 = not related).
162 protected int compareRulerLine(
167 if (aPosition.getOffset() > -1 && aPosition.getLength() > -1) {
170 aDocument.getLineOfOffset(aPosition.getOffset());
171 if (aLine == markerLine) {
176 <= aDocument.getLineOfOffset(
177 aPosition.getOffset()
178 + aPosition.getLength())) {
181 } catch (BadLocationException e) {