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