0N/A/*
2362N/A * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.plaf.basic;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.awt.*;
0N/Aimport java.net.URL;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.text.*;
0N/Aimport javax.swing.text.html.*;
0N/A
0N/Aimport sun.swing.SwingUtilities2;
0N/A
0N/A/**
0N/A * Support for providing html views for the swing components.
0N/A * This translates a simple html string to a javax.swing.text.View
0N/A * implementation that can render the html and provide the necessary
0N/A * layout semantics.
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @since 1.3
0N/A */
0N/Apublic class BasicHTML {
0N/A
0N/A /**
0N/A * Create an html renderer for the given component and
0N/A * string of html.
0N/A */
0N/A public static View createHTMLView(JComponent c, String html) {
0N/A BasicEditorKit kit = getFactory();
0N/A Document doc = kit.createDefaultDocument(c.getFont(),
0N/A c.getForeground());
0N/A Object base = c.getClientProperty(documentBaseKey);
0N/A if (base instanceof URL) {
0N/A ((HTMLDocument)doc).setBase((URL)base);
0N/A }
0N/A Reader r = new StringReader(html);
0N/A try {
0N/A kit.read(r, doc, 0);
0N/A } catch (Throwable e) {
0N/A }
0N/A ViewFactory f = kit.getViewFactory();
0N/A View hview = f.create(doc.getDefaultRootElement());
0N/A View v = new Renderer(c, f, hview);
0N/A return v;
0N/A }
0N/A
0N/A /**
0N/A * Returns the baseline for the html renderer.
0N/A *
0N/A * @param view the View to get the baseline for
0N/A * @param w the width to get the baseline for
0N/A * @param h the height to get the baseline for
0N/A * @throws IllegalArgumentException if width or height is < 0
0N/A * @return baseline or a value < 0 indicating there is no reasonable
0N/A * baseline
0N/A * @see java.awt.FontMetrics
0N/A * @see javax.swing.JComponent#getBaseline(int,int)
0N/A * @since 1.6
0N/A */
0N/A public static int getHTMLBaseline(View view, int w, int h) {
0N/A if (w < 0 || h < 0) {
0N/A throw new IllegalArgumentException(
0N/A "Width and height must be >= 0");
0N/A }
0N/A if (view instanceof Renderer) {
0N/A return getBaseline(view.getView(0), w, h);
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Gets the baseline for the specified component. This digs out
0N/A * the View client property, and if non-null the baseline is calculated
0N/A * from it. Otherwise the baseline is the value <code>y + ascent</code>.
0N/A */
0N/A static int getBaseline(JComponent c, int y, int ascent,
0N/A int w, int h) {
0N/A View view = (View)c.getClientProperty(BasicHTML.propertyKey);
0N/A if (view != null) {
0N/A int baseline = getHTMLBaseline(view, w, h);
0N/A if (baseline < 0) {
0N/A return baseline;
0N/A }
0N/A return y + baseline;
0N/A }
0N/A return y + ascent;
0N/A }
0N/A
0N/A /**
0N/A * Gets the baseline for the specified View.
0N/A */
0N/A static int getBaseline(View view, int w, int h) {
0N/A if (hasParagraph(view)) {
0N/A view.setSize(w, h);
0N/A return getBaseline(view, new Rectangle(0, 0, w, h));
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A private static int getBaseline(View view, Shape bounds) {
0N/A if (view.getViewCount() == 0) {
0N/A return -1;
0N/A }
0N/A AttributeSet attributes = view.getElement().getAttributes();
0N/A Object name = null;
0N/A if (attributes != null) {
0N/A name = attributes.getAttribute(StyleConstants.NameAttribute);
0N/A }
0N/A int index = 0;
0N/A if (name == HTML.Tag.HTML && view.getViewCount() > 1) {
0N/A // For html on widgets the header is not visible, skip it.
0N/A index++;
0N/A }
0N/A bounds = view.getChildAllocation(index, bounds);
0N/A if (bounds == null) {
0N/A return -1;
0N/A }
0N/A View child = view.getView(index);
0N/A if (view instanceof javax.swing.text.ParagraphView) {
0N/A Rectangle rect;
0N/A if (bounds instanceof Rectangle) {
0N/A rect = (Rectangle)bounds;
0N/A }
0N/A else {
0N/A rect = bounds.getBounds();
0N/A }
0N/A return rect.y + (int)(rect.height *
0N/A child.getAlignment(View.Y_AXIS));
0N/A }
0N/A return getBaseline(child, bounds);
0N/A }
0N/A
0N/A private static boolean hasParagraph(View view) {
0N/A if (view instanceof javax.swing.text.ParagraphView) {
0N/A return true;
0N/A }
0N/A if (view.getViewCount() == 0) {
0N/A return false;
0N/A }
0N/A AttributeSet attributes = view.getElement().getAttributes();
0N/A Object name = null;
0N/A if (attributes != null) {
0N/A name = attributes.getAttribute(StyleConstants.NameAttribute);
0N/A }
0N/A int index = 0;
0N/A if (name == HTML.Tag.HTML && view.getViewCount() > 1) {
0N/A // For html on widgets the header is not visible, skip it.
0N/A index = 1;
0N/A }
0N/A return hasParagraph(view.getView(index));
0N/A }
0N/A
0N/A /**
0N/A * Check the given string to see if it should trigger the
0N/A * html rendering logic in a non-text component that supports
0N/A * html rendering.
0N/A */
0N/A public static boolean isHTMLString(String s) {
0N/A if (s != null) {
0N/A if ((s.length() >= 6) && (s.charAt(0) == '<') && (s.charAt(5) == '>')) {
0N/A String tag = s.substring(1,5);
0N/A return tag.equalsIgnoreCase(propertyKey);
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Stash the HTML render for the given text into the client
0N/A * properties of the given JComponent. If the given text is
0N/A * <em>NOT HTML</em> the property will be cleared of any
0N/A * renderer.
0N/A * <p>
0N/A * This method is useful for ComponentUI implementations
0N/A * that are static (i.e. shared) and get their state
0N/A * entirely from the JComponent.
0N/A */
0N/A public static void updateRenderer(JComponent c, String text) {
0N/A View value = null;
0N/A View oldValue = (View)c.getClientProperty(BasicHTML.propertyKey);
0N/A Boolean htmlDisabled = (Boolean) c.getClientProperty(htmlDisable);
0N/A if (htmlDisabled != Boolean.TRUE && BasicHTML.isHTMLString(text)) {
0N/A value = BasicHTML.createHTMLView(c, text);
0N/A }
0N/A if (value != oldValue && oldValue != null) {
0N/A for (int i = 0; i < oldValue.getViewCount(); i++) {
0N/A oldValue.getView(i).setParent(null);
0N/A }
0N/A }
0N/A c.putClientProperty(BasicHTML.propertyKey, value);
0N/A }
0N/A
0N/A /**
0N/A * If this client property of a JComponent is set to Boolean.TRUE
0N/A * the component's 'text' property is never treated as HTML.
0N/A */
0N/A private static final String htmlDisable = "html.disable";
0N/A
0N/A /**
0N/A * Key to use for the html renderer when stored as a
0N/A * client property of a JComponent.
0N/A */
0N/A public static final String propertyKey = "html";
0N/A
0N/A /**
0N/A * Key stored as a client property to indicate the base that relative
0N/A * references are resolved against. For example, lets say you keep
0N/A * your images in the directory resources relative to the code path,
0N/A * you would use the following the set the base:
0N/A * <pre>
0N/A * jComponent.putClientProperty(documentBaseKey,
0N/A * xxx.class.getResource("resources/"));
0N/A * </pre>
0N/A */
0N/A public static final String documentBaseKey = "html.base";
0N/A
0N/A static BasicEditorKit getFactory() {
0N/A if (basicHTMLFactory == null) {
0N/A basicHTMLViewFactory = new BasicHTMLViewFactory();
0N/A basicHTMLFactory = new BasicEditorKit();
0N/A }
0N/A return basicHTMLFactory;
0N/A }
0N/A
0N/A /**
0N/A * The source of the html renderers
0N/A */
0N/A private static BasicEditorKit basicHTMLFactory;
0N/A
0N/A /**
0N/A * Creates the Views that visually represent the model.
0N/A */
0N/A private static ViewFactory basicHTMLViewFactory;
0N/A
0N/A /**
0N/A * Overrides to the default stylesheet. Should consider
0N/A * just creating a completely fresh stylesheet.
0N/A */
0N/A private static final String styleChanges =
0N/A "p { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }" +
0N/A "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }";
0N/A
0N/A /**
0N/A * The views produced for the ComponentUI implementations aren't
0N/A * going to be edited and don't need full html support. This kit
0N/A * alters the HTMLEditorKit to try and trim things down a bit.
0N/A * It does the following:
0N/A * <ul>
0N/A * <li>It doesn't produce Views for things like comments,
0N/A * head, title, unknown tags, etc.
0N/A * <li>It installs a different set of css settings from the default
0N/A * provided by HTMLEditorKit.
0N/A * </ul>
0N/A */
0N/A static class BasicEditorKit extends HTMLEditorKit {
0N/A /** Shared base style for all documents created by us use. */
0N/A private static StyleSheet defaultStyles;
0N/A
0N/A /**
0N/A * Overriden to return our own slimmed down style sheet.
0N/A */
0N/A public StyleSheet getStyleSheet() {
0N/A if (defaultStyles == null) {
0N/A defaultStyles = new StyleSheet();
0N/A StringReader r = new StringReader(styleChanges);
0N/A try {
0N/A defaultStyles.loadRules(r, null);
0N/A } catch (Throwable e) {
0N/A // don't want to die in static initialization...
0N/A // just display things wrong.
0N/A }
0N/A r.close();
0N/A defaultStyles.addStyleSheet(super.getStyleSheet());
0N/A }
0N/A return defaultStyles;
0N/A }
0N/A
0N/A /**
0N/A * Sets the async policy to flush everything in one chunk, and
0N/A * to not display unknown tags.
0N/A */
0N/A public Document createDefaultDocument(Font defaultFont,
0N/A Color foreground) {
0N/A StyleSheet styles = getStyleSheet();
0N/A StyleSheet ss = new StyleSheet();
0N/A ss.addStyleSheet(styles);
0N/A BasicDocument doc = new BasicDocument(ss, defaultFont, foreground);
0N/A doc.setAsynchronousLoadPriority(Integer.MAX_VALUE);
0N/A doc.setPreservesUnknownTags(false);
0N/A return doc;
0N/A }
0N/A
0N/A /**
0N/A * Returns the ViewFactory that is used to make sure the Views don't
0N/A * load in the background.
0N/A */
0N/A public ViewFactory getViewFactory() {
0N/A return basicHTMLViewFactory;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * BasicHTMLViewFactory extends HTMLFactory to force images to be loaded
0N/A * synchronously.
0N/A */
0N/A static class BasicHTMLViewFactory extends HTMLEditorKit.HTMLFactory {
0N/A public View create(Element elem) {
0N/A View view = super.create(elem);
0N/A
0N/A if (view instanceof ImageView) {
0N/A ((ImageView)view).setLoadsSynchronously(true);
0N/A }
0N/A return view;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * The subclass of HTMLDocument that is used as the model. getForeground
0N/A * is overridden to return the foreground property from the Component this
0N/A * was created for.
0N/A */
0N/A static class BasicDocument extends HTMLDocument {
0N/A /** The host, that is where we are rendering. */
0N/A // private JComponent host;
0N/A
0N/A BasicDocument(StyleSheet s, Font defaultFont, Color foreground) {
0N/A super(s);
0N/A setPreservesUnknownTags(false);
0N/A setFontAndColor(defaultFont, foreground);
0N/A }
0N/A
0N/A /**
0N/A * Sets the default font and default color. These are set by
0N/A * adding a rule for the body that specifies the font and color.
0N/A * This allows the html to override these should it wish to have
0N/A * a custom font or color.
0N/A */
0N/A private void setFontAndColor(Font font, Color fg) {
0N/A getStyleSheet().addRule(sun.swing.SwingUtilities2.
0N/A displayPropertiesToCSS(font,fg));
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Root text view that acts as an HTML renderer.
0N/A */
0N/A static class Renderer extends View {
0N/A
0N/A Renderer(JComponent c, ViewFactory f, View v) {
0N/A super(null);
0N/A host = c;
0N/A factory = f;
0N/A view = v;
0N/A view.setParent(this);
0N/A // initially layout to the preferred size
0N/A setSize(view.getPreferredSpan(X_AXIS), view.getPreferredSpan(Y_AXIS));
0N/A }
0N/A
0N/A /**
0N/A * Fetches the attributes to use when rendering. At the root
0N/A * level there are no attributes. If an attribute is resolved
0N/A * up the view hierarchy this is the end of the line.
0N/A */
0N/A public AttributeSet getAttributes() {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Determines the preferred span for this view along an axis.
0N/A *
0N/A * @param axis may be either X_AXIS or Y_AXIS
0N/A * @return the span the view would like to be rendered into.
0N/A * Typically the view is told to render into the span
0N/A * that is returned, although there is no guarantee.
0N/A * The parent may choose to resize or break the view.
0N/A */
0N/A public float getPreferredSpan(int axis) {
0N/A if (axis == X_AXIS) {
0N/A // width currently laid out to
0N/A return width;
0N/A }
0N/A return view.getPreferredSpan(axis);
0N/A }
0N/A
0N/A /**
0N/A * Determines the minimum span for this view along an axis.
0N/A *
0N/A * @param axis may be either X_AXIS or Y_AXIS
0N/A * @return the span the view would like to be rendered into.
0N/A * Typically the view is told to render into the span
0N/A * that is returned, although there is no guarantee.
0N/A * The parent may choose to resize or break the view.
0N/A */
0N/A public float getMinimumSpan(int axis) {
0N/A return view.getMinimumSpan(axis);
0N/A }
0N/A
0N/A /**
0N/A * Determines the maximum span for this view along an axis.
0N/A *
0N/A * @param axis may be either X_AXIS or Y_AXIS
0N/A * @return the span the view would like to be rendered into.
0N/A * Typically the view is told to render into the span
0N/A * that is returned, although there is no guarantee.
0N/A * The parent may choose to resize or break the view.
0N/A */
0N/A public float getMaximumSpan(int axis) {
0N/A return Integer.MAX_VALUE;
0N/A }
0N/A
0N/A /**
0N/A * Specifies that a preference has changed.
0N/A * Child views can call this on the parent to indicate that
0N/A * the preference has changed. The root view routes this to
0N/A * invalidate on the hosting component.
0N/A * <p>
0N/A * This can be called on a different thread from the
0N/A * event dispatching thread and is basically unsafe to
0N/A * propagate into the component. To make this safe,
0N/A * the operation is transferred over to the event dispatching
0N/A * thread for completion. It is a design goal that all view
0N/A * methods be safe to call without concern for concurrency,
0N/A * and this behavior helps make that true.
0N/A *
0N/A * @param child the child view
0N/A * @param width true if the width preference has changed
0N/A * @param height true if the height preference has changed
0N/A */
0N/A public void preferenceChanged(View child, boolean width, boolean height) {
0N/A host.revalidate();
0N/A host.repaint();
0N/A }
0N/A
0N/A /**
0N/A * Determines the desired alignment for this view along an axis.
0N/A *
0N/A * @param axis may be either X_AXIS or Y_AXIS
0N/A * @return the desired alignment, where 0.0 indicates the origin
0N/A * and 1.0 the full span away from the origin
0N/A */
0N/A public float getAlignment(int axis) {
0N/A return view.getAlignment(axis);
0N/A }
0N/A
0N/A /**
0N/A * Renders the view.
0N/A *
0N/A * @param g the graphics context
0N/A * @param allocation the region to render into
0N/A */
0N/A public void paint(Graphics g, Shape allocation) {
0N/A Rectangle alloc = allocation.getBounds();
0N/A view.setSize(alloc.width, alloc.height);
0N/A view.paint(g, allocation);
0N/A }
0N/A
0N/A /**
0N/A * Sets the view parent.
0N/A *
0N/A * @param parent the parent view
0N/A */
0N/A public void setParent(View parent) {
0N/A throw new Error("Can't set parent on root view");
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of views in this view. Since
0N/A * this view simply wraps the root of the view hierarchy
0N/A * it has exactly one child.
0N/A *
0N/A * @return the number of views
0N/A * @see #getView
0N/A */
0N/A public int getViewCount() {
0N/A return 1;
0N/A }
0N/A
0N/A /**
0N/A * Gets the n-th view in this container.
0N/A *
0N/A * @param n the number of the view to get
0N/A * @return the view
0N/A */
0N/A public View getView(int n) {
0N/A return view;
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the document model coordinate space
0N/A * to the coordinate space of the view mapped to it.
0N/A *
0N/A * @param pos the position to convert
0N/A * @param a the allocated region to render into
0N/A * @return the bounding box of the given position
0N/A */
0N/A public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
0N/A return view.modelToView(pos, a, b);
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the document model coordinate space
0N/A * to the coordinate space of the view mapped to it.
0N/A *
0N/A * @param p0 the position to convert >= 0
0N/A * @param b0 the bias toward the previous character or the
0N/A * next character represented by p0, in case the
0N/A * position is a boundary of two views.
0N/A * @param p1 the position to convert >= 0
0N/A * @param b1 the bias toward the previous character or the
0N/A * next character represented by p1, in case the
0N/A * position is a boundary of two views.
0N/A * @param a the allocated region to render into
0N/A * @return the bounding box of the given position is returned
0N/A * @exception BadLocationException if the given position does
0N/A * not represent a valid location in the associated document
0N/A * @exception IllegalArgumentException for an invalid bias argument
0N/A * @see View#viewToModel
0N/A */
0N/A public Shape modelToView(int p0, Position.Bias b0, int p1,
0N/A Position.Bias b1, Shape a) throws BadLocationException {
0N/A return view.modelToView(p0, b0, p1, b1, a);
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the view coordinate space to the logical
0N/A * coordinate space of the model.
0N/A *
0N/A * @param x x coordinate of the view location to convert
0N/A * @param y y coordinate of the view location to convert
0N/A * @param a the allocated region to render into
0N/A * @return the location within the model that best represents the
0N/A * given point in the view
0N/A */
0N/A public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
0N/A return view.viewToModel(x, y, a, bias);
0N/A }
0N/A
0N/A /**
0N/A * Returns the document model underlying the view.
0N/A *
0N/A * @return the model
0N/A */
0N/A public Document getDocument() {
0N/A return view.getDocument();
0N/A }
0N/A
0N/A /**
0N/A * Returns the starting offset into the model for this view.
0N/A *
0N/A * @return the starting offset
0N/A */
0N/A public int getStartOffset() {
0N/A return view.getStartOffset();
0N/A }
0N/A
0N/A /**
0N/A * Returns the ending offset into the model for this view.
0N/A *
0N/A * @return the ending offset
0N/A */
0N/A public int getEndOffset() {
0N/A return view.getEndOffset();
0N/A }
0N/A
0N/A /**
0N/A * Gets the element that this view is mapped to.
0N/A *
0N/A * @return the view
0N/A */
0N/A public Element getElement() {
0N/A return view.getElement();
0N/A }
0N/A
0N/A /**
0N/A * Sets the view size.
0N/A *
0N/A * @param width the width
0N/A * @param height the height
0N/A */
0N/A public void setSize(float width, float height) {
0N/A this.width = (int) width;
0N/A view.setSize(width, height);
0N/A }
0N/A
0N/A /**
0N/A * Fetches the container hosting the view. This is useful for
0N/A * things like scheduling a repaint, finding out the host
0N/A * components font, etc. The default implementation
0N/A * of this is to forward the query to the parent view.
0N/A *
0N/A * @return the container
0N/A */
0N/A public Container getContainer() {
0N/A return host;
0N/A }
0N/A
0N/A /**
0N/A * Fetches the factory to be used for building the
0N/A * various view fragments that make up the view that
0N/A * represents the model. This is what determines
0N/A * how the model will be represented. This is implemented
0N/A * to fetch the factory provided by the associated
0N/A * EditorKit.
0N/A *
0N/A * @return the factory
0N/A */
0N/A public ViewFactory getViewFactory() {
0N/A return factory;
0N/A }
0N/A
0N/A private int width;
0N/A private View view;
0N/A private ViewFactory factory;
0N/A private JComponent host;
0N/A
0N/A }
0N/A}