1. /*
  2. * @(#)BasicHTML.java 1.17 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.net.URL;
  11. import javax.swing.*;
  12. import javax.swing.text.*;
  13. import javax.swing.text.html.*;
  14. /**
  15. * Support for providing html views for the swing components.
  16. * This translates a simple html string to a javax.swing.text.View
  17. * implementation that can render the html and provide the necessary
  18. * layout semantics.
  19. *
  20. * @author Timothy Prinzing
  21. * @version 1.17 01/23/03
  22. */
  23. public class BasicHTML {
  24. /**
  25. * Create an html renderer for the given component and
  26. * string of html.
  27. */
  28. public static View createHTMLView(JComponent c, String html) {
  29. BasicEditorKit kit = getFactory();
  30. Document doc = kit.createDefaultDocument(c.getFont(),
  31. c.getForeground());
  32. Object base = c.getClientProperty(documentBaseKey);
  33. if (base instanceof URL) {
  34. ((HTMLDocument)doc).setBase((URL)base);
  35. }
  36. Reader r = new StringReader(html);
  37. try {
  38. kit.read(r, doc, 0);
  39. } catch (Throwable e) {
  40. }
  41. ViewFactory f = kit.getViewFactory();
  42. View hview = f.create(doc.getDefaultRootElement());
  43. View v = new Renderer(c, f, hview);
  44. return v;
  45. }
  46. /**
  47. * Check the given string to see if it should trigger the
  48. * html rendering logic in a non-text component that supports
  49. * html rendering.
  50. */
  51. public static boolean isHTMLString(String s) {
  52. if (s != null) {
  53. if ((s.length() >= 6) && (s.charAt(0) == '<') && (s.charAt(5) == '>')) {
  54. String tag = s.substring(1,5);
  55. return tag.equalsIgnoreCase(propertyKey);
  56. }
  57. }
  58. return false;
  59. }
  60. /**
  61. * Stash the HTML render for the given text into the client
  62. * properties of the given JComponent. If the given text is
  63. * <em>NOT HTML</em> the property will be cleared of any
  64. * renderer.
  65. * <p>
  66. * This method is useful for ComponentUI implementations
  67. * that are static (i.e. shared) and get their state
  68. * entirely from the JComponent.
  69. */
  70. public static void updateRenderer(JComponent c, String text) {
  71. View value = null;
  72. String key = null;
  73. if (BasicHTML.isHTMLString(text)) {
  74. value = BasicHTML.createHTMLView(c, text);
  75. }
  76. c.putClientProperty(BasicHTML.propertyKey, value);
  77. }
  78. /**
  79. * Key to use for the html renderer when stored as a
  80. * client property of a JComponent.
  81. */
  82. public static final String propertyKey = "html";
  83. /**
  84. * Key stored as a client property to indicate the base that relative
  85. * references are resolved against. For example, lets say you keep
  86. * your images in the directory resources relative to the code path,
  87. * you would use the following the set the base:
  88. * <pre>
  89. * jComponent.putClientProperty(documentBaseKey,
  90. * xxx.class.getResource("resources/"));
  91. * </pre>
  92. */
  93. public static final String documentBaseKey = "html.base";
  94. static BasicEditorKit getFactory() {
  95. if (basicHTMLFactory == null) {
  96. basicHTMLViewFactory = new BasicHTMLViewFactory();
  97. basicHTMLFactory = new BasicEditorKit();
  98. }
  99. return basicHTMLFactory;
  100. }
  101. /**
  102. * The source of the html renderers
  103. */
  104. private static BasicEditorKit basicHTMLFactory;
  105. /**
  106. * Creates the Views that visually represent the model.
  107. */
  108. private static ViewFactory basicHTMLViewFactory;
  109. /**
  110. * Overrides to the default stylesheet. Should consider
  111. * just creating a completely fresh stylesheet.
  112. */
  113. private static final String styleChanges =
  114. "p { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }" +
  115. "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }";
  116. /**
  117. * The views produced for the ComponentUI implementations aren't
  118. * going to be edited and don't need full html support. This kit
  119. * alters the HTMLEditorKit to try and trim things down a bit.
  120. * It does the following:
  121. * <ul>
  122. * <li>It doesn't produce Views for things like comments,
  123. * head, title, unknown tags, etc.
  124. * <li>It installs a different set of css settings from the default
  125. * provided by HTMLEditorKit.
  126. * </ul>
  127. */
  128. static class BasicEditorKit extends HTMLEditorKit {
  129. /** Shared base style for all documents created by us use. */
  130. private static StyleSheet defaultStyles;
  131. /**
  132. * Overriden to return our own slimmed down style sheet.
  133. */
  134. public StyleSheet getStyleSheet() {
  135. if (defaultStyles == null) {
  136. defaultStyles = new StyleSheet();
  137. StringReader r = new StringReader(styleChanges);
  138. try {
  139. defaultStyles.loadRules(r, null);
  140. } catch (Throwable e) {
  141. // don't want to die in static initialization...
  142. // just display things wrong.
  143. }
  144. r.close();
  145. defaultStyles.addStyleSheet(super.getStyleSheet());
  146. }
  147. return defaultStyles;
  148. }
  149. /**
  150. * Sets the async policy to flush everything in one chunk, and
  151. * to not display unknown tags.
  152. */
  153. public Document createDefaultDocument(Font defaultFont,
  154. Color foreground) {
  155. StyleSheet styles = getStyleSheet();
  156. StyleSheet ss = new StyleSheet();
  157. ss.addStyleSheet(styles);
  158. BasicDocument doc = new BasicDocument(ss, defaultFont, foreground);
  159. doc.setAsynchronousLoadPriority(Integer.MAX_VALUE);
  160. doc.setPreservesUnknownTags(false);
  161. return doc;
  162. }
  163. /**
  164. * Returns the ViewFactory that is used to make sure the Views don't
  165. * load in the background.
  166. */
  167. public ViewFactory getViewFactory() {
  168. return basicHTMLViewFactory;
  169. }
  170. }
  171. /**
  172. * BasicHTMLViewFactory extends HTMLFactory to force images to be loaded
  173. * synchronously.
  174. */
  175. static class BasicHTMLViewFactory extends HTMLEditorKit.HTMLFactory {
  176. public View create(Element elem) {
  177. View view = super.create(elem);
  178. if (view instanceof ImageView) {
  179. ((ImageView)view).setLoadsSynchronously(true);
  180. }
  181. return view;
  182. }
  183. }
  184. /**
  185. * The subclass of HTMLDocument that is used as the model. getForeground
  186. * is overridden to return the foreground property from the Component this
  187. * was created for.
  188. */
  189. static class BasicDocument extends HTMLDocument {
  190. /** The host, that is where we are rendering. */
  191. // private JComponent host;
  192. BasicDocument(StyleSheet s, Font defaultFont, Color foreground) {
  193. super(s);
  194. setPreservesUnknownTags(false);
  195. setFontAndColor(defaultFont, foreground);
  196. }
  197. /**
  198. * Sets the default font and default color. These are set by
  199. * adding a rule for the body that specifies the font and color.
  200. * This allows the html to override these should it wish to have
  201. * a custom font or color.
  202. */
  203. private void setFontAndColor(Font font, Color fg) {
  204. StringBuffer rule = null;
  205. if (font != null) {
  206. rule = new StringBuffer("body { font-family: ");
  207. rule.append(font.getFamily());
  208. rule.append(";");
  209. rule.append(" font-size: ");
  210. rule.append(font.getSize());
  211. rule.append("pt");
  212. if (font.isBold()) {
  213. rule.append("; font-weight: 700");
  214. }
  215. if (font.isItalic()) {
  216. rule.append("; font-style: italic");
  217. }
  218. }
  219. if (fg != null) {
  220. if (rule == null) {
  221. rule = new StringBuffer("body { color: #");
  222. }
  223. else {
  224. rule.append("; color: #");
  225. }
  226. if (fg.getRed() < 16) {
  227. rule.append('0');
  228. }
  229. rule.append(Integer.toHexString(fg.getRed()));
  230. if (fg.getGreen() < 16) {
  231. rule.append('0');
  232. }
  233. rule.append(Integer.toHexString(fg.getGreen()));
  234. if (fg.getBlue() < 16) {
  235. rule.append('0');
  236. }
  237. rule.append(Integer.toHexString(fg.getBlue()));
  238. }
  239. if (rule != null) {
  240. rule.append(" }");
  241. try {
  242. getStyleSheet().addRule(rule.toString());
  243. } catch (RuntimeException re) {}
  244. }
  245. }
  246. }
  247. /**
  248. * Root text view that acts as an HTML renderer.
  249. */
  250. static class Renderer extends View {
  251. Renderer(JComponent c, ViewFactory f, View v) {
  252. super(null);
  253. host = c;
  254. factory = f;
  255. view = v;
  256. view.setParent(this);
  257. // initially layout to the preferred size
  258. setSize(view.getPreferredSpan(X_AXIS), view.getPreferredSpan(Y_AXIS));
  259. }
  260. /**
  261. * Fetches the attributes to use when rendering. At the root
  262. * level there are no attributes. If an attribute is resolved
  263. * up the view hierarchy this is the end of the line.
  264. */
  265. public AttributeSet getAttributes() {
  266. return null;
  267. }
  268. /**
  269. * Determines the preferred span for this view along an axis.
  270. *
  271. * @param axis may be either X_AXIS or Y_AXIS
  272. * @return the span the view would like to be rendered into.
  273. * Typically the view is told to render into the span
  274. * that is returned, although there is no guarantee.
  275. * The parent may choose to resize or break the view.
  276. */
  277. public float getPreferredSpan(int axis) {
  278. if (axis == X_AXIS) {
  279. // width currently laid out to
  280. return width;
  281. }
  282. return view.getPreferredSpan(axis);
  283. }
  284. /**
  285. * Determines the minimum span for this view along an axis.
  286. *
  287. * @param axis may be either X_AXIS or Y_AXIS
  288. * @return the span the view would like to be rendered into.
  289. * Typically the view is told to render into the span
  290. * that is returned, although there is no guarantee.
  291. * The parent may choose to resize or break the view.
  292. */
  293. public float getMinimumSpan(int axis) {
  294. return view.getMinimumSpan(axis);
  295. }
  296. /**
  297. * Determines the maximum span for this view along an axis.
  298. *
  299. * @param axis may be either X_AXIS or Y_AXIS
  300. * @return the span the view would like to be rendered into.
  301. * Typically the view is told to render into the span
  302. * that is returned, although there is no guarantee.
  303. * The parent may choose to resize or break the view.
  304. */
  305. public float getMaximumSpan(int axis) {
  306. return Integer.MAX_VALUE;
  307. }
  308. /**
  309. * Specifies that a preference has changed.
  310. * Child views can call this on the parent to indicate that
  311. * the preference has changed. The root view routes this to
  312. * invalidate on the hosting component.
  313. * <p>
  314. * This can be called on a different thread from the
  315. * event dispatching thread and is basically unsafe to
  316. * propagate into the component. To make this safe,
  317. * the operation is transferred over to the event dispatching
  318. * thread for completion. It is a design goal that all view
  319. * methods be safe to call without concern for concurrency,
  320. * and this behavior helps make that true.
  321. *
  322. * @param child the child view
  323. * @param width true if the width preference has changed
  324. * @param height true if the height preference has changed
  325. */
  326. public void preferenceChanged(View child, boolean width, boolean height) {
  327. host.revalidate();
  328. host.repaint();
  329. }
  330. /**
  331. * Determines the desired alignment for this view along an axis.
  332. *
  333. * @param axis may be either X_AXIS or Y_AXIS
  334. * @return the desired alignment, where 0.0 indicates the origin
  335. * and 1.0 the full span away from the origin
  336. */
  337. public float getAlignment(int axis) {
  338. return view.getAlignment(axis);
  339. }
  340. /**
  341. * Renders the view.
  342. *
  343. * @param g the graphics context
  344. * @param allocation the region to render into
  345. */
  346. public void paint(Graphics g, Shape allocation) {
  347. Rectangle alloc = allocation.getBounds();
  348. view.setSize(alloc.width, alloc.height);
  349. view.paint(g, allocation);
  350. }
  351. /**
  352. * Sets the view parent.
  353. *
  354. * @param parent the parent view
  355. */
  356. public void setParent(View parent) {
  357. throw new Error("Can't set parent on root view");
  358. }
  359. /**
  360. * Returns the number of views in this view. Since
  361. * this view simply wraps the root of the view hierarchy
  362. * it has exactly one child.
  363. *
  364. * @return the number of views
  365. * @see #getView
  366. */
  367. public int getViewCount() {
  368. return 1;
  369. }
  370. /**
  371. * Gets the n-th view in this container.
  372. *
  373. * @param n the number of the view to get
  374. * @return the view
  375. */
  376. public View getView(int n) {
  377. return view;
  378. }
  379. /**
  380. * Provides a mapping from the document model coordinate space
  381. * to the coordinate space of the view mapped to it.
  382. *
  383. * @param pos the position to convert
  384. * @param a the allocated region to render into
  385. * @return the bounding box of the given position
  386. */
  387. public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
  388. return view.modelToView(pos, a, b);
  389. }
  390. /**
  391. * Provides a mapping from the document model coordinate space
  392. * to the coordinate space of the view mapped to it.
  393. *
  394. * @param p0 the position to convert >= 0
  395. * @param b0 the bias toward the previous character or the
  396. * next character represented by p0, in case the
  397. * position is a boundary of two views.
  398. * @param p1 the position to convert >= 0
  399. * @param b1 the bias toward the previous character or the
  400. * next character represented by p1, in case the
  401. * position is a boundary of two views.
  402. * @param a the allocated region to render into
  403. * @return the bounding box of the given position is returned
  404. * @exception BadLocationException if the given position does
  405. * not represent a valid location in the associated document
  406. * @exception IllegalArgumentException for an invalid bias argument
  407. * @see View#viewToModel
  408. */
  409. public Shape modelToView(int p0, Position.Bias b0, int p1,
  410. Position.Bias b1, Shape a) throws BadLocationException {
  411. return view.modelToView(p0, b0, p1, b1, a);
  412. }
  413. /**
  414. * Provides a mapping from the view coordinate space to the logical
  415. * coordinate space of the model.
  416. *
  417. * @param x x coordinate of the view location to convert
  418. * @param y y coordinate of the view location to convert
  419. * @param a the allocated region to render into
  420. * @return the location within the model that best represents the
  421. * given point in the view
  422. */
  423. public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
  424. return view.viewToModel(x, y, a, bias);
  425. }
  426. /**
  427. * Returns the document model underlying the view.
  428. *
  429. * @return the model
  430. */
  431. public Document getDocument() {
  432. return view.getDocument();
  433. }
  434. /**
  435. * Returns the starting offset into the model for this view.
  436. *
  437. * @return the starting offset
  438. */
  439. public int getStartOffset() {
  440. return view.getStartOffset();
  441. }
  442. /**
  443. * Returns the ending offset into the model for this view.
  444. *
  445. * @return the ending offset
  446. */
  447. public int getEndOffset() {
  448. return view.getEndOffset();
  449. }
  450. /**
  451. * Gets the element that this view is mapped to.
  452. *
  453. * @return the view
  454. */
  455. public Element getElement() {
  456. return view.getElement();
  457. }
  458. /**
  459. * Sets the view size.
  460. *
  461. * @param width the width
  462. * @param height the height
  463. */
  464. public void setSize(float width, float height) {
  465. this.width = (int) width;
  466. view.setSize(width, height);
  467. }
  468. /**
  469. * Fetches the container hosting the view. This is useful for
  470. * things like scheduling a repaint, finding out the host
  471. * components font, etc. The default implementation
  472. * of this is to forward the query to the parent view.
  473. *
  474. * @return the container
  475. */
  476. public Container getContainer() {
  477. return host;
  478. }
  479. /**
  480. * Fetches the factory to be used for building the
  481. * various view fragments that make up the view that
  482. * represents the model. This is what determines
  483. * how the model will be represented. This is implemented
  484. * to fetch the factory provided by the associated
  485. * EditorKit.
  486. *
  487. * @return the factory
  488. */
  489. public ViewFactory getViewFactory() {
  490. return factory;
  491. }
  492. private int width;
  493. private View view;
  494. private ViewFactory factory;
  495. private JComponent host;
  496. }
  497. }