|
46 | 46 | import javax.swing.JLabel;
|
47 | 47 | import javax.swing.JPanel;
|
48 | 48 | import javax.swing.SwingConstants;
|
| 49 | +import javax.swing.text.BadLocationException; |
| 50 | +import javax.swing.text.Document; |
| 51 | +import javax.swing.text.Segment; |
| 52 | +import javax.swing.text.html.HTMLDocument; |
| 53 | +import javax.swing.text.html.HTMLEditorKit; |
| 54 | +import javax.swing.text.html.StyleSheet; |
49 | 55 | import org.openide.util.NbBundle;
|
50 | 56 | import org.openide.util.RequestProcessor;
|
51 | 57 |
|
@@ -130,7 +136,7 @@ private void initComponents() {
|
130 | 136 | }
|
131 | 137 |
|
132 | 138 | private static String htmlize(String value) {
|
133 |
| - return value.replace("&", "&").replace("<", "<"); // NOI18N |
| 139 | + return value.replace("&", "&").replace("<", "<").replace("\t", " "); // NOI18N |
134 | 140 | }
|
135 | 141 |
|
136 | 142 | private static String transform(String value) {
|
@@ -164,6 +170,7 @@ public void run() {
|
164 | 170 | }
|
165 | 171 | try {
|
166 | 172 | HTMLTextArea area = new HTMLTextArea();
|
| 173 | + area.setEditorKit(new CustomHtmlEditorKit()); |
167 | 174 | area.setForeground(new Color(0xcc, 0x33, 0));
|
168 | 175 | area.setText("<pre>" + transform(htmlize(new String(data, "UTF-8"))) + "</pre>"); // NOI18N
|
169 | 176 | area.setCaretPosition(0);
|
@@ -192,5 +199,59 @@ public void run() {
|
192 | 199 | }
|
193 | 200 |
|
194 | 201 | }
|
| 202 | + |
| 203 | + private static class CustomHtmlEditorKit extends HTMLEditorKit { |
| 204 | + |
| 205 | + @Override |
| 206 | + public Document createDefaultDocument() { |
| 207 | + StyleSheet styles = getStyleSheet(); |
| 208 | + StyleSheet ss = new StyleSheet(); |
| 209 | + |
| 210 | + ss.addStyleSheet(styles); |
| 211 | + |
| 212 | + HTMLDocument doc = new CustomHTMLDocument(ss); |
| 213 | + doc.setParser(getParser()); |
| 214 | + doc.setAsynchronousLoadPriority(4); |
| 215 | + doc.setTokenThreshold(100); |
| 216 | + return doc; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + private static class CustomHTMLDocument extends HTMLDocument { |
| 221 | + private static final int CACHE_BOUNDARY = 1000; |
| 222 | + private char[] segArray; |
| 223 | + private int segOffset; |
| 224 | + private int segCount; |
| 225 | + private boolean segPartialReturn; |
| 226 | + private int lastOffset; |
| 227 | + private int lastLength; |
| 228 | + |
| 229 | + private CustomHTMLDocument(StyleSheet ss) { |
| 230 | + super(ss); |
| 231 | + lastOffset = -1; |
| 232 | + lastLength = -1; |
| 233 | + putProperty("multiByte", Boolean.TRUE); // NOI18N |
| 234 | + } |
| 235 | + |
| 236 | + @Override |
| 237 | + public void getText(int offset, int length, Segment txt) throws BadLocationException { |
| 238 | + if (lastOffset == offset && lastLength == length) { |
| 239 | + txt.array = segArray; |
| 240 | + txt.offset = segOffset; |
| 241 | + txt.count = segCount; |
| 242 | + txt.setPartialReturn(segPartialReturn); |
| 243 | + return; |
| 244 | + } |
| 245 | + super.getText(offset, length, txt); |
| 246 | + if (length > CACHE_BOUNDARY || lastLength <= CACHE_BOUNDARY) { |
| 247 | + segArray = txt.array; |
| 248 | + segOffset = txt.offset; |
| 249 | + segCount = txt.count; |
| 250 | + segPartialReturn = txt.isPartialReturn(); |
| 251 | + lastOffset = offset; |
| 252 | + lastLength = length; |
| 253 | + } |
| 254 | + } |
| 255 | + } |
195 | 256 |
|
196 | 257 | }
|
0 commit comments