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