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