1 7 package javax.swing.plaf.basic; 8 9 import java.io.*; 10 import java.awt.*; 11 import java.net.URL ; 12 13 import javax.swing.*; 14 import javax.swing.text.*; 15 import javax.swing.text.html.*; 16 17 import com.sun.java.swing.SwingUtilities2; 18 19 28 public class BasicHTML { 29 30 34 public static View createHTMLView(JComponent c, String html) { 35 BasicEditorKit kit = getFactory(); 36 Document doc = kit.createDefaultDocument(c.getFont(), 37 c.getForeground()); 38 Object base = c.getClientProperty(documentBaseKey); 39 if (base instanceof URL ) { 40 ((HTMLDocument)doc).setBase((URL )base); 41 } 42 Reader r = new StringReader(html); 43 try { 44 kit.read(r, doc, 0); 45 } catch (Throwable e) { 46 } 47 ViewFactory f = kit.getViewFactory(); 48 View hview = f.create(doc.getDefaultRootElement()); 49 View v = new Renderer(c, f, hview); 50 return v; 51 } 52 53 58 public static boolean isHTMLString(String s) { 59 if (s != null) { 60 if ((s.length() >= 6) && (s.charAt(0) == '<') && (s.charAt(5) == '>')) { 61 String tag = s.substring(1,5); 62 return tag.equalsIgnoreCase(propertyKey); 63 } 64 } 65 return false; 66 } 67 68 78 public static void updateRenderer(JComponent c, String text) { 79 View value = null; 80 View oldValue = (View)c.getClientProperty(BasicHTML.propertyKey); 81 Boolean htmlDisabled = (Boolean ) c.getClientProperty(htmlDisable); 82 if (htmlDisabled != Boolean.TRUE && BasicHTML.isHTMLString(text)) { 83 value = BasicHTML.createHTMLView(c, text); 84 } 85 if (value != oldValue && oldValue != null) { 86 for (int i = 0; i < oldValue.getViewCount(); i++) { 87 oldValue.getView(i).setParent(null); 88 } 89 } 90 c.putClientProperty(BasicHTML.propertyKey, value); 91 } 92 93 97 private static final String htmlDisable = "html.disable"; 98 99 103 public static final String propertyKey = "html"; 104 105 115 public static final String documentBaseKey = "html.base"; 116 117 static BasicEditorKit getFactory() { 118 if (basicHTMLFactory == null) { 119 basicHTMLViewFactory = new BasicHTMLViewFactory(); 120 basicHTMLFactory = new BasicEditorKit(); 121 } 122 return basicHTMLFactory; 123 } 124 125 128 private static BasicEditorKit basicHTMLFactory; 129 130 133 private static ViewFactory basicHTMLViewFactory; 134 135 139 private static final String styleChanges = 140 "p { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }" + 141 "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }"; 142 143 155 static class BasicEditorKit extends HTMLEditorKit { 156 157 private static StyleSheet defaultStyles; 158 159 162 public StyleSheet getStyleSheet() { 163 if (defaultStyles == null) { 164 defaultStyles = new StyleSheet(); 165 StringReader r = new StringReader(styleChanges); 166 try { 167 defaultStyles.loadRules(r, null); 168 } catch (Throwable e) { 169 } 172 r.close(); 173 defaultStyles.addStyleSheet(super.getStyleSheet()); 174 } 175 return defaultStyles; 176 } 177 178 182 public Document createDefaultDocument(Font defaultFont, 183 Color foreground) { 184 StyleSheet styles = getStyleSheet(); 185 StyleSheet ss = new StyleSheet(); 186 ss.addStyleSheet(styles); 187 BasicDocument doc = new BasicDocument(ss, defaultFont, foreground); 188 doc.setAsynchronousLoadPriority(Integer.MAX_VALUE); 189 doc.setPreservesUnknownTags(false); 190 return doc; 191 } 192 193 197 public ViewFactory getViewFactory() { 198 return basicHTMLViewFactory; 199 } 200 } 201 202 203 207 static class BasicHTMLViewFactory extends HTMLEditorKit.HTMLFactory { 208 public View create(Element elem) { 209 View view = super.create(elem); 210 211 if (view instanceof ImageView) { 212 ((ImageView)view).setLoadsSynchronously(true); 213 } 214 return view; 215 } 216 } 217 218 219 224 static class BasicDocument extends HTMLDocument { 225 226 228 BasicDocument(StyleSheet s, Font defaultFont, Color foreground) { 229 super(s); 230 setPreservesUnknownTags(false); 231 setFontAndColor(defaultFont, foreground); 232 } 233 234 240 private void setFontAndColor(Font font, Color fg) { 241 getStyleSheet().addRule(com.sun.java.swing.SwingUtilities2. 242 displayPropertiesToCSS(font,fg)); 243 } 244 } 245 246 247 250 static class Renderer extends View { 251 252 Renderer(JComponent c, ViewFactory f, View v) { 253 super(null); 254 host = c; 255 factory = f; 256 view = v; 257 view.setParent(this); 258 setSize(view.getPreferredSpan(X_AXIS), view.getPreferredSpan(Y_AXIS)); 260 } 261 262 267 public AttributeSet getAttributes() { 268 return null; 269 } 270 271 280 public float getPreferredSpan(int axis) { 281 if (axis == X_AXIS) { 282 return width; 284 } 285 return view.getPreferredSpan(axis); 286 } 287 288 297 public float getMinimumSpan(int axis) { 298 return view.getMinimumSpan(axis); 299 } 300 301 310 public float getMaximumSpan(int axis) { 311 return Integer.MAX_VALUE; 312 } 313 314 332 public void preferenceChanged(View child, boolean width, boolean height) { 333 host.revalidate(); 334 host.repaint(); 335 } 336 337 344 public float getAlignment(int axis) { 345 return view.getAlignment(axis); 346 } 347 348 354 public void paint(Graphics g, Shape allocation) { 355 Rectangle alloc = allocation.getBounds(); 356 view.setSize(alloc.width, alloc.height); 357 view.paint(g, allocation); 358 } 359 360 365 public void setParent(View parent) { 366 throw new Error ("Can't set parent on root view"); 367 } 368 369 377 public int getViewCount() { 378 return 1; 379 } 380 381 387 public View getView(int n) { 388 return view; 389 } 390 391 399 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 400 return view.modelToView(pos, a, b); 401 } 402 403 422 public Shape modelToView(int p0, Position.Bias b0, int p1, 423 Position.Bias b1, Shape a) throws BadLocationException { 424 return view.modelToView(p0, b0, p1, b1, a); 425 } 426 427 437 public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) { 438 return view.viewToModel(x, y, a, bias); 439 } 440 441 446 public Document getDocument() { 447 return view.getDocument(); 448 } 449 450 455 public int getStartOffset() { 456 return view.getStartOffset(); 457 } 458 459 464 public int getEndOffset() { 465 return view.getEndOffset(); 466 } 467 468 473 public Element getElement() { 474 return view.getElement(); 475 } 476 477 483 public void setSize(float width, float height) { 484 this.width = (int) width; 485 view.setSize(width, height); 486 } 487 488 496 public Container getContainer() { 497 return host; 498 } 499 500 510 public ViewFactory getViewFactory() { 511 return factory; 512 } 513 514 private int width; 515 private View view; 516 private ViewFactory factory; 517 private JComponent host; 518 519 } 520 } 521 | Popular Tags |