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