remove unnecessary extension.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / HTMLTextPresenter.java
1 package net.sourceforge.phpdt.internal.ui.text;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8 import java.io.IOException;
9 import java.io.Reader;
10 import java.io.StringReader;
11 import java.util.Iterator;
12
13 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
14 import net.sourceforge.phpeclipse.ui.WebUI;
15 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16
17 import org.eclipse.jface.text.DefaultInformationControl;
18 import org.eclipse.jface.text.Region;
19 import org.eclipse.jface.text.TextPresentation;
20 import org.eclipse.swt.custom.StyleRange;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.widgets.Display;
23
24 public class HTMLTextPresenter implements
25                 DefaultInformationControl.IInformationPresenter {
26
27         private static final String LINE_DELIM = System.getProperty(
28                         "line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
29
30         private int fCounter;
31
32         private boolean fEnforceUpperLineLimit;
33
34         public HTMLTextPresenter(boolean enforceUpperLineLimit) {
35                 super();
36                 fEnforceUpperLineLimit = enforceUpperLineLimit;
37         }
38
39         public HTMLTextPresenter() {
40                 this(true);
41         }
42
43         protected Reader createReader(String hoverInfo,
44                         TextPresentation presentation) {
45                 return new HTML2TextReader(new StringReader(hoverInfo), presentation);
46         }
47
48         protected void adaptTextPresentation(TextPresentation presentation,
49                         int offset, int insertLength) {
50
51                 int yoursStart = offset;
52                 int yoursEnd = offset + insertLength - 1;
53                 yoursEnd = Math.max(yoursStart, yoursEnd);
54
55                 Iterator e = presentation.getAllStyleRangeIterator();
56                 while (e.hasNext()) {
57
58                         StyleRange range = (StyleRange) e.next();
59
60                         int myStart = range.start;
61                         int myEnd = range.start + range.length - 1;
62                         myEnd = Math.max(myStart, myEnd);
63
64                         if (myEnd < yoursStart)
65                                 continue;
66
67                         if (myStart < yoursStart)
68                                 range.length += insertLength;
69                         else
70                                 range.start += insertLength;
71                 }
72         }
73
74         private void append(StringBuffer buffer, String string,
75                         TextPresentation presentation) {
76
77                 int length = string.length();
78                 buffer.append(string);
79
80                 if (presentation != null)
81                         adaptTextPresentation(presentation, fCounter, length);
82
83                 fCounter += length;
84         }
85
86         private String getIndent(String line) {
87                 int length = line.length();
88
89                 int i = 0;
90                 while (i < length && Character.isWhitespace(line.charAt(i)))
91                         ++i;
92
93                 return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
94         }
95
96         /*
97          * @see IHoverInformationPresenter#updatePresentation(Display display,
98          *      String, TextPresentation, int, int)
99          */
100         public String updatePresentation(Display display, String hoverInfo,
101                         TextPresentation presentation, int maxWidth, int maxHeight) {
102
103                 if (hoverInfo == null)
104                         return null;
105
106                 GC gc = new GC(display);
107                 try {
108
109                         StringBuffer buffer = new StringBuffer();
110                         int maxNumberOfLines = Math.round(maxHeight
111                                         / gc.getFontMetrics().getHeight());
112
113                         fCounter = 0;
114                         LineBreakingReader reader = new LineBreakingReader(createReader(
115                                         hoverInfo, presentation), gc, maxWidth);
116
117                         boolean lastLineFormatted = false;
118                         String lastLineIndent = null;
119
120                         String line = reader.readLine();
121                         boolean lineFormatted = reader.isFormattedLine();
122                         boolean firstLineProcessed = false;
123
124                         while (line != null) {
125
126                                 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
127                                         break;
128
129                                 if (firstLineProcessed) {
130                                         if (!lastLineFormatted)
131                                                 append(buffer, LINE_DELIM, null);
132                                         else {
133                                                 append(buffer, LINE_DELIM, presentation);
134                                                 if (lastLineIndent != null)
135                                                         append(buffer, lastLineIndent, presentation);
136                                         }
137                                 }
138
139                                 append(buffer, line, null);
140                                 firstLineProcessed = true;
141
142                                 lastLineFormatted = lineFormatted;
143                                 if (!lineFormatted)
144                                         lastLineIndent = null;
145                                 else if (lastLineIndent == null)
146                                         lastLineIndent = getIndent(line);
147
148                                 line = reader.readLine();
149                                 lineFormatted = reader.isFormattedLine();
150
151                                 maxNumberOfLines--;
152                         }
153
154                         if (line != null) {
155                                 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
156                                 append(buffer, PHPUIMessages
157                                                 .getString("HTMLTextPresenter.ellipsis"), presentation); //$NON-NLS-1$
158                         }
159
160                         return trim(buffer, presentation);
161
162                 } catch (IOException e) {
163
164                         WebUI.log(e);
165                         return null;
166
167                 } finally {
168                         gc.dispose();
169                 }
170         }
171
172         private String trim(StringBuffer buffer, TextPresentation presentation) {
173
174                 int length = buffer.length();
175
176                 int end = length - 1;
177                 while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
178                         --end;
179
180                 if (end == -1)
181                         return ""; //$NON-NLS-1$
182
183                 if (end < length - 1)
184                         buffer.delete(end + 1, length);
185                 else
186                         end = length;
187
188                 int start = 0;
189                 while (start < end && Character.isWhitespace(buffer.charAt(start)))
190                         ++start;
191
192                 buffer.delete(0, start);
193                 presentation.setResultWindow(new Region(start, buffer.length()));
194                 return buffer.toString();
195         }
196 }