KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > derived > HTMLTextPresenter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor.derived;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16 import java.io.StringReader JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.eclipse.ant.internal.ui.AntUIPlugin;
20 import org.eclipse.jface.text.DefaultInformationControl;
21 import org.eclipse.jface.text.Region;
22 import org.eclipse.jface.text.TextPresentation;
23 import org.eclipse.swt.custom.StyleRange;
24 import org.eclipse.swt.graphics.Drawable;
25 import org.eclipse.swt.graphics.GC;
26 import org.eclipse.swt.widgets.Display;
27
28 /**
29  * Derived from org.eclipse.jdt.internal.ui.text.HTMLTextPresenter
30  */

31 public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension {
32     
33     private static final String JavaDoc LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
34

35     private int fCounter;
36     private boolean fEnforceUpperLineLimit;
37     
38     public HTMLTextPresenter(boolean enforceUpperLineLimit) {
39         super();
40         fEnforceUpperLineLimit= enforceUpperLineLimit;
41     }
42     
43     public HTMLTextPresenter() {
44         this(true);
45     }
46     
47     protected Reader JavaDoc createReader(String JavaDoc hoverInfo, TextPresentation presentation) {
48         return new HTML2TextReader(new StringReader JavaDoc(hoverInfo), presentation);
49     }
50     
51     protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) {
52                 
53         int yoursStart= offset;
54         int yoursEnd= offset + insertLength -1;
55         yoursEnd= Math.max(yoursStart, yoursEnd);
56         
57         Iterator JavaDoc e= presentation.getAllStyleRangeIterator();
58         while (e.hasNext()) {
59             
60             StyleRange range= (StyleRange) e.next();
61         
62             int myStart= range.start;
63             int myEnd= range.start + range.length -1;
64             myEnd= Math.max(myStart, myEnd);
65             
66             if (myEnd < yoursStart)
67                 continue;
68             
69             if (myStart < yoursStart)
70                 range.length += insertLength;
71             else
72                 range.start += insertLength;
73         }
74     }
75     
76     private void append(StringBuffer JavaDoc buffer, String JavaDoc string, TextPresentation presentation) {
77         
78         int length= string.length();
79         buffer.append(string);
80         
81         if (presentation != null)
82             adaptTextPresentation(presentation, fCounter, length);
83             
84         fCounter += length;
85     }
86     
87     private String JavaDoc getIndent(String JavaDoc line) {
88         int length= line.length();
89         
90         int i= 0;
91         while (i < length && Character.isWhitespace(line.charAt(i))) ++i;
92         
93         return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
94
}
95     
96     /*
97      * @see IHoverInformationPresenter#updatePresentation(Display display, String, TextPresentation, int, int)
98      */

99     public String JavaDoc updatePresentation(Display display, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
100         return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight);
101     }
102     
103     /*
104      * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int)
105      * @since 3.2
106      */

107     public String JavaDoc updatePresentation(Drawable drawable, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
108         
109         if (hoverInfo == null)
110             return null;
111             
112         GC gc= new GC(drawable);
113         try {
114             
115             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
116             int maxNumberOfLines= Math.round(maxHeight / gc.getFontMetrics().getHeight());
117             
118             fCounter= 0;
119             LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth);
120             
121             boolean lastLineFormatted= false;
122             String JavaDoc lastLineIndent= null;
123             
124             String JavaDoc line=reader.readLine();
125             boolean lineFormatted= reader.isFormattedLine();
126             boolean firstLineProcessed= false;
127             
128             while (line != null) {
129                 
130                 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
131                     break;
132                 
133                 if (firstLineProcessed) {
134                     if (!lastLineFormatted)
135                         append(buffer, LINE_DELIM, null);
136                     else {
137                         append(buffer, LINE_DELIM, presentation);
138                         if (lastLineIndent != null)
139                             append(buffer, lastLineIndent, presentation);
140                     }
141                 }
142                 
143                 append(buffer, line, null);
144                 firstLineProcessed= true;
145                 
146                 lastLineFormatted= lineFormatted;
147                 if (!lineFormatted)
148                     lastLineIndent= null;
149                 else if (lastLineIndent == null)
150                     lastLineIndent= getIndent(line);
151                     
152                 line= reader.readLine();
153                 lineFormatted= reader.isFormattedLine();
154                 
155                 maxNumberOfLines--;
156             }
157             
158             if (line != null) {
159                 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
160                 append(buffer, "...", presentation); //$NON-NLS-1$
161
}
162             
163             return trim(buffer, presentation);
164             
165         } catch (IOException JavaDoc e) {
166             AntUIPlugin.log(e);
167             return null;
168         } finally {
169             gc.dispose();
170         }
171     }
172     
173     private String JavaDoc trim(StringBuffer JavaDoc buffer, TextPresentation presentation) {
174         
175         int length= buffer.length();
176                 
177         int end= length -1;
178         while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
179             -- end;
180         
181         if (end == -1)
182             return ""; //$NON-NLS-1$
183

184         if (end < length -1)
185             buffer.delete(end + 1, length);
186         else
187             end= length;
188             
189         int start= 0;
190         while (start < end && Character.isWhitespace(buffer.charAt(start)))
191             ++ start;
192             
193         buffer.delete(0, start);
194         presentation.setResultWindow(new Region(start, buffer.length()));
195         return buffer.toString();
196     }
197 }
198
199
Popular Tags