129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.html.HTMLTextPresenter;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.io.IOException;
|
|
18 import java.io.Reader;
|
|
19 import java.io.StringReader;
|
|
20 import java.util.Iterator;
|
|
21
|
|
22 import dwt.custom.StyleRange;
|
|
23 import dwt.graphics.Drawable;
|
|
24 import dwt.graphics.GC;
|
|
25 import dwt.widgets.Display;
|
|
26 import dwtx.jface.internal.text.link.contentassist.LineBreakingReader;
|
|
27 import dwtx.jface.text.DefaultInformationControl;
|
|
28 import dwtx.jface.text.Region;
|
|
29 import dwtx.jface.text.TextPresentation;
|
|
30
|
|
31
|
|
32 /**
|
|
33 * <p>
|
|
34 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p>
|
|
35 */
|
|
36 public class HTMLTextPresenter : DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension {
|
|
37
|
|
38 private static final String LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
39
|
|
40 private int fCounter;
|
|
41 private bool fEnforceUpperLineLimit;
|
|
42
|
|
43 public HTMLTextPresenter(bool enforceUpperLineLimit) {
|
|
44 super();
|
|
45 fEnforceUpperLineLimit= enforceUpperLineLimit;
|
|
46 }
|
|
47
|
|
48 public HTMLTextPresenter() {
|
|
49 this(true);
|
|
50 }
|
|
51
|
|
52 protected Reader createReader(String hoverInfo, TextPresentation presentation) {
|
|
53 return new HTML2TextReader(new StringReader(hoverInfo), presentation);
|
|
54 }
|
|
55
|
|
56 protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) {
|
|
57
|
|
58 int yoursStart= offset;
|
|
59 int yoursEnd= offset + insertLength -1;
|
|
60 yoursEnd= Math.max(yoursStart, yoursEnd);
|
|
61
|
|
62 Iterator e= presentation.getAllStyleRangeIterator();
|
|
63 while (e.hasNext()) {
|
|
64
|
|
65 StyleRange range= (StyleRange) e.next();
|
|
66
|
|
67 int myStart= range.start;
|
|
68 int myEnd= range.start + range.length -1;
|
|
69 myEnd= Math.max(myStart, myEnd);
|
|
70
|
|
71 if (myEnd < yoursStart)
|
|
72 continue;
|
|
73
|
|
74 if (myStart < yoursStart)
|
|
75 range.length += insertLength;
|
|
76 else
|
|
77 range.start += insertLength;
|
|
78 }
|
|
79 }
|
|
80
|
|
81 private void append(StringBuffer buffer, String string, TextPresentation presentation) {
|
|
82
|
|
83 int length= string.length();
|
|
84 buffer.append(string);
|
|
85
|
|
86 if (presentation !is null)
|
|
87 adaptTextPresentation(presentation, fCounter, length);
|
|
88
|
|
89 fCounter += length;
|
|
90 }
|
|
91
|
|
92 private String getIndent(String line) {
|
|
93 int length= line.length();
|
|
94
|
|
95 int i= 0;
|
|
96 while (i < length && Character.isWhitespace(line.charAt(i))) ++i;
|
|
97
|
|
98 return (i is length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
|
|
99 }
|
|
100
|
|
101 /**
|
|
102 * {@inheritDoc}
|
|
103 *
|
|
104 * @see dwtx.jface.text.DefaultInformationControl.IInformationPresenter#updatePresentation(dwt.widgets.Display, java.lang.String, dwtx.jface.text.TextPresentation, int, int)
|
|
105 * @deprecated
|
|
106 */
|
|
107 public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
|
|
108 return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight);
|
|
109 }
|
|
110
|
|
111 /*
|
|
112 * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int)
|
|
113 * @since 3.2
|
|
114 */
|
|
115 public String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
|
|
116
|
|
117 if (hoverInfo is null)
|
|
118 return null;
|
|
119
|
|
120 GC gc= new GC(drawable);
|
|
121 try {
|
|
122
|
|
123 StringBuffer buffer= new StringBuffer();
|
|
124 int maxNumberOfLines= Math.round(maxHeight / gc.getFontMetrics().getHeight());
|
|
125
|
|
126 fCounter= 0;
|
|
127 LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth);
|
|
128
|
|
129 bool lastLineFormatted= false;
|
|
130 String lastLineIndent= null;
|
|
131
|
|
132 String line=reader.readLine();
|
|
133 bool lineFormatted= reader.isFormattedLine();
|
|
134 bool firstLineProcessed= false;
|
|
135
|
|
136 while (line !is null) {
|
|
137
|
|
138 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
|
|
139 break;
|
|
140
|
|
141 if (firstLineProcessed) {
|
|
142 if (!lastLineFormatted)
|
|
143 append(buffer, LINE_DELIM, null);
|
|
144 else {
|
|
145 append(buffer, LINE_DELIM, presentation);
|
|
146 if (lastLineIndent !is null)
|
|
147 append(buffer, lastLineIndent, presentation);
|
|
148 }
|
|
149 }
|
|
150
|
|
151 append(buffer, line, null);
|
|
152 firstLineProcessed= true;
|
|
153
|
|
154 lastLineFormatted= lineFormatted;
|
|
155 if (!lineFormatted)
|
|
156 lastLineIndent= null;
|
|
157 else if (lastLineIndent is null)
|
|
158 lastLineIndent= getIndent(line);
|
|
159
|
|
160 line= reader.readLine();
|
|
161 lineFormatted= reader.isFormattedLine();
|
|
162
|
|
163 maxNumberOfLines--;
|
|
164 }
|
|
165
|
|
166 if (line !is null) {
|
|
167 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
|
|
168 append(buffer, HTMLMessages.getString("HTMLTextPresenter.ellipse"), presentation); //$NON-NLS-1$
|
|
169 }
|
|
170
|
|
171 return trim(buffer, presentation);
|
|
172
|
|
173 } catch (IOException e) {
|
|
174
|
|
175 // ignore TODO do something else?
|
|
176 return null;
|
|
177
|
|
178 } finally {
|
|
179 gc.dispose();
|
|
180 }
|
|
181 }
|
|
182
|
|
183 private String trim(StringBuffer buffer, TextPresentation presentation) {
|
|
184
|
|
185 int length= buffer.length();
|
|
186
|
|
187 int end= length -1;
|
|
188 while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
|
|
189 -- end;
|
|
190
|
|
191 if (end is -1)
|
|
192 return ""; //$NON-NLS-1$
|
|
193
|
|
194 if (end < length -1)
|
|
195 buffer.delete(end + 1, length);
|
|
196 else
|
|
197 end= length;
|
|
198
|
|
199 int start= 0;
|
|
200 while (start < end && Character.isWhitespace(buffer.charAt(start)))
|
|
201 ++ start;
|
|
202
|
|
203 buffer.delete(0, start);
|
|
204 presentation.setResultWindow(new Region(start, buffer.length()));
|
|
205 return buffer.toString();
|
|
206 }
|
|
207 }
|
|
208
|