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 * 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, int)
55 public String getHoverInfo(ISourceViewer viewer, int line) {
57 List markers = getMarkersForLine(viewer, line);
58 if (markers != null) {
60 for (int i = 0; i < markers.size(); i++) {
61 IMarker marker = (IMarker) markers.get(i);
63 marker.getAttribute(IMarker.MESSAGE, (String) null);
64 if (message != null && message.trim().length() > 0) {
66 if (message.length() > MAX_INFO_LENGTH) {
67 message = splitMessage(message);
71 if(i != markers.size() - 1) {
80 private String splitMessage(String message) {
83 if(message.length() <= MAX_INFO_LENGTH) {
87 String tmpStr = new String(message);
89 while(tmpStr.length() > MAX_INFO_LENGTH) {
91 int spacepos = tmpStr.indexOf(" ", MAX_INFO_LENGTH);
94 result += tmpStr.substring(0, spacepos) + "\n";
95 tmpStr = tmpStr.substring(spacepos);
98 result += tmpStr.substring(0, MAX_INFO_LENGTH) + "\n";
99 tmpStr = tmpStr.substring(MAX_INFO_LENGTH);
112 * Returns all markers which includes the ruler's line of activity.
114 protected List getMarkersForLine(ISourceViewer aViewer, int aLine) {
115 List markers = new ArrayList();
116 IAnnotationModel model = aViewer.getAnnotationModel();
118 Iterator e = model.getAnnotationIterator();
119 while (e.hasNext()) {
121 if (o instanceof MarkerAnnotation) {
122 MarkerAnnotation a = (MarkerAnnotation) o;
123 if (compareRulerLine(model.getPosition(a),
124 aViewer.getDocument(),
127 markers.add(a.getMarker());
136 * Returns one marker which includes the ruler's line of activity.
138 protected IMarker getMarkerForLine(ISourceViewer aViewer, int aLine) {
139 IMarker marker = null;
140 IAnnotationModel model = aViewer.getAnnotationModel();
142 Iterator e = model.getAnnotationIterator();
143 while (e.hasNext()) {
145 if (o instanceof MarkerAnnotation) {
146 MarkerAnnotation a = (MarkerAnnotation) o;
147 if (compareRulerLine(model.getPosition(a),
148 aViewer.getDocument(),
151 marker = a.getMarker();
160 * Returns distance of given line to specified position (1 = same line,
161 * 2 = included in given position, 0 = not related).
163 protected int compareRulerLine(
168 if (aPosition.getOffset() > -1 && aPosition.getLength() > -1) {
171 aDocument.getLineOfOffset(aPosition.getOffset());
172 if (aLine == markerLine) {
177 <= aDocument.getLineOfOffset(
178 aPosition.getOffset()
179 + aPosition.getLength())) {
182 } catch (BadLocationException e) {