BasicHTML.java revision 2362
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 * 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 * 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 * 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. 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 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 * @author Timothy Prinzing 0N/A * Create an html renderer for the given component and 0N/A * Returns the baseline for the html renderer. 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 * @see java.awt.FontMetrics 0N/A * @see javax.swing.JComponent#getBaseline(int,int) 0N/A if (w <
0 || h <
0) {
0N/A "Width and height must be >= 0");
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 * Gets the baseline for the specified View. 0N/A // For html on widgets the header is not visible, skip it. 0N/A // For html on widgets the header is not visible, skip it. 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 * 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 * 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 * 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 * Key to use for the html renderer when stored as a 0N/A * client property of a JComponent. 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 * jComponent.putClientProperty(documentBaseKey, 0N/A * xxx.class.getResource("resources/")); 0N/A * The source of the html renderers 0N/A * Creates the Views that visually represent the model. 0N/A * Overrides to the default stylesheet. Should consider 0N/A * just creating a completely fresh stylesheet. 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 * 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 * <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 /** Shared base style for all documents created by us use. */ 0N/A * Overriden to return our own slimmed down style sheet. 0N/A // don't want to die in static initialization... 0N/A // just display things wrong. 0N/A * Sets the async policy to flush everything in one chunk, and 0N/A * to not display unknown tags. 0N/A * Returns the ViewFactory that is used to make sure the Views don't 0N/A * load in the background. 0N/A * BasicHTMLViewFactory extends HTMLFactory to force images to be loaded 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 /** The host, that is where we are rendering. */ 0N/A // private JComponent host; 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 * Root text view that acts as an HTML renderer. 0N/A // initially layout to the preferred size 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 * Determines the preferred span for this view along an axis. 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 // width currently laid out to 0N/A * Determines the minimum span for this view along an axis. 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 * Determines the maximum span for this view along an axis. 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 * 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 * 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 * @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 * Determines the desired alignment for this view along an axis. 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 * @param g the graphics context 0N/A * @param allocation the region to render into 0N/A * Sets the view parent. 0N/A * @param parent the parent view 0N/A throw new Error(
"Can't set parent on root view");
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 * @return the number of views 0N/A * Gets the n-th view in this container. 0N/A * @param n the number of the view to get 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 * @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 * Provides a mapping from the document model coordinate space 0N/A * to the coordinate space of the view mapped to it. 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 * Provides a mapping from the view coordinate space to the logical 0N/A * coordinate space of the model. 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 * Returns the document model underlying the view. 0N/A * Returns the starting offset into the model for this view. 0N/A * @return the starting offset 0N/A * Returns the ending offset into the model for this view. 0N/A * @return the ending offset 0N/A * Gets the element that this view is mapped to. 0N/A * Sets the view size. 0N/A * @param width the width 0N/A * @param height the height 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 * @return the container 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 * @return the factory