Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / PHPAnnotationHover.java
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
8
9 Contributors:
10     IBM Corporation - Initial implementation
11     Klaus Hartlage - www.eclipseproject.de
12 **********************************************************************/
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
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;
26
27 /** 
28  * The PHPAnnotationHover provides the hover support for PHP editors.
29  */
30  
31 public class PHPAnnotationHover implements IAnnotationHover {
32
33         /* (non-Javadoc)
34          * Method declared on IAnnotationHover
35          */
36 //      public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
37 //              IDocument document= sourceViewer.getDocument();
38 //
39 //              try {
40 //                      IRegion info= document.getLineInformation(lineNumber);
41 //                      return document.get(info.getOffset(), info.getLength());
42 //              } catch (BadLocationException x) {
43 //              }
44 //
45 //              return null;
46 //      }
47 //      
48   static final int MAX_INFO_LENGTH = 80;
49
50   /**
51    * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int)
52    */
53         
54   public String getHoverInfo(ISourceViewer viewer, int line) {
55     String info = null;
56     List markers = getMarkersForLine(viewer, line);
57     if (markers != null) {
58       info = "";
59       for (int i =  0; i < markers.size(); i++) {
60         IMarker marker = (IMarker) markers.get(i);
61         String message =
62           marker.getAttribute(IMarker.MESSAGE, (String) null);
63         if (message != null && message.trim().length() > 0) {
64                                         
65           if (message.length() > MAX_INFO_LENGTH) {
66             message = splitMessage(message);
67           }
68           info += message;
69                                         
70           if(i != markers.size() - 1) {
71              info += "\n";
72           }
73         }
74       }
75     }
76     return info;
77   }
78
79   private String splitMessage(String message) {
80     String result = "";
81                 
82     if(message.length() <= MAX_INFO_LENGTH) {
83       return message;
84     }
85                 
86     String tmpStr = new String(message);
87                 
88     while(tmpStr.length() > MAX_INFO_LENGTH) {
89                         
90       int spacepos = tmpStr.indexOf(" ", MAX_INFO_LENGTH);
91                         
92       if(spacepos != -1) {
93         result += tmpStr.substring(0, spacepos) + "\n";
94         tmpStr = tmpStr.substring(spacepos);
95       }
96       else {
97         result += tmpStr.substring(0, MAX_INFO_LENGTH) + "\n";
98         tmpStr = tmpStr.substring(MAX_INFO_LENGTH);
99       }
100                         
101                         
102                         
103     }
104                 
105     result += tmpStr;
106                 
107     return result;
108   }
109
110   /**
111    * Returns all markers which includes the ruler's line of activity.
112    */
113   protected List getMarkersForLine(ISourceViewer aViewer, int aLine) {
114     List markers = new ArrayList();
115     IAnnotationModel model = aViewer.getAnnotationModel();
116     if (model != null) {
117       Iterator e = model.getAnnotationIterator();
118       while (e.hasNext()) {
119         Object o = e.next();
120         if (o instanceof MarkerAnnotation) {
121           MarkerAnnotation a = (MarkerAnnotation) o;
122           if (compareRulerLine(model.getPosition(a),
123             aViewer.getDocument(),
124             aLine)
125             != 0) {
126             markers.add(a.getMarker());
127           }
128         }
129       }
130     }
131     return markers;
132   }
133
134   /**
135    * Returns one marker which includes the ruler's line of activity.
136    */
137   protected IMarker getMarkerForLine(ISourceViewer aViewer, int aLine) {
138     IMarker marker = null;
139     IAnnotationModel model = aViewer.getAnnotationModel();
140     if (model != null) {
141       Iterator e = model.getAnnotationIterator();
142       while (e.hasNext()) {
143         Object o = e.next();
144         if (o instanceof MarkerAnnotation) {
145           MarkerAnnotation a = (MarkerAnnotation) o;
146           if (compareRulerLine(model.getPosition(a),
147             aViewer.getDocument(),
148             aLine)
149             != 0) {
150             marker = a.getMarker();
151           }
152         }
153       }
154     }
155     return marker;
156   }
157
158   /**
159    * Returns distance of given line to specified position (1 = same line,
160    * 2 = included in given position, 0 = not related).
161    */
162   protected int compareRulerLine(
163     Position aPosition,
164     IDocument aDocument,
165     int aLine) {
166     int distance = 0;
167     if (aPosition.getOffset() > -1 && aPosition.getLength() > -1) {
168       try {
169         int markerLine =
170           aDocument.getLineOfOffset(aPosition.getOffset());
171         if (aLine == markerLine) {
172           distance = 1;
173         } else if (
174           markerLine <= aLine
175             && aLine
176               <= aDocument.getLineOfOffset(
177                 aPosition.getOffset()
178                   + aPosition.getLength())) {
179           distance = 2;
180         }
181       } catch (BadLocationException e) {
182       }
183     }
184     return distance;
185   }
186 }