APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.plaf.basic

public Class BasicHTML

extends Object
Class Inheritance
Imports
java.io.*, java.awt.*, java.net.URL, javax.accessibility.AccessibleContext, javax.swing.*, javax.swing.text.*, javax.swing.text.html.*, sun.swing.SwingAccessor, .SwingUtilities2

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
BasicHTML.BasicDocument

The subclass of HTMLDocument that is used as the model.

pack-priv static class
BasicHTML.BasicEditorKit

The views produced for the ComponentUI implementations aren't going to be edited and don't need full html support.

pack-priv static class
BasicHTML.BasicHTMLViewFactory

BasicHTMLViewFactory extends HTMLFactory to force images to be loaded synchronously.

pack-priv static class
BasicHTML.Renderer

Root text view that acts as an HTML renderer.

Field Summary

Modifier and TypeField and Description
private static BasicHTML.BasicEditorKit
basicHTMLFactory

The source of the html renderers

private static ViewFactory
basicHTMLViewFactory

Creates the Views that visually represent the model.

public static final String
documentBaseKey

Key stored as a client property to indicate the base that relative references are resolved against.

private static final String
htmlDisable

If this client property of a JComponent is set to Boolean.TRUE the component's 'text' property is never treated as HTML.

public static final String
propertyKey

Key to use for the html renderer when stored as a client property of a JComponent.

private static final String
styleChanges

Overrides to the default stylesheet.

Constructor Summary

AccessConstructor and Description
public
BasicHTML()

Constructs a BasicHTML.

Method Summary

Modifier and TypeMethod and Description
public static View

Returns:

an HTML renderer
createHTMLView
(JComponent
a component
c
,
String
an HTML string
html
)

Create an html renderer for the given component and string of html.

pack-priv static int
getBaseline(JComponent c, int y, int ascent, int w, int h)

Gets the baseline for the specified component.

pack-priv static int
getBaseline(View view, int w, int h)

Gets the baseline for the specified View.

private static int
getBaseline(View view, Shape bounds)

pack-priv static BasicHTML.BasicEditorKit
public static int

Returns:

baseline or a value < 0 indicating there is no reasonable baseline
getHTMLBaseline
(View
the View to get the baseline for
view
,
int
the width to get the baseline for
w
,
int
the height to get the baseline for
h
)

Returns the baseline for the html renderer.

private static boolean
public static boolean

Returns:

true if the given string should trigger the html rendering logic in a non-text component
isHTMLString
(String
a text
s
)

Check the given string to see if it should trigger the html rendering logic in a non-text component that supports html rendering.

public static void
updateRenderer(JComponent
a component
c
,
String
a text
text
)

Stash the HTML render for the given text into the client properties of the given JComponent.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

basicHTMLFactoryback to summary
private static BasicHTML.BasicEditorKit basicHTMLFactory

The source of the html renderers

basicHTMLViewFactoryback to summary
private static ViewFactory basicHTMLViewFactory

Creates the Views that visually represent the model.

documentBaseKeyback to summary
public static final String documentBaseKey

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:

  jComponent.putClientProperty(documentBaseKey,
                               xxx.class.getResource("resources/"));
htmlDisableback to summary
private static final String htmlDisable

If this client property of a JComponent is set to Boolean.TRUE the component's 'text' property is never treated as HTML.

propertyKeyback to summary
public static final String propertyKey

Key to use for the html renderer when stored as a client property of a JComponent.

styleChangesback to summary
private static final String styleChanges

Overrides to the default stylesheet. Should consider just creating a completely fresh stylesheet.

Constructor Detail

BasicHTMLback to summary
public BasicHTML()

Constructs a BasicHTML.

Method Detail

createHTMLViewback to summary
public static View createHTMLView(JComponent c, String html)

Create an html renderer for the given component and string of html.

Parameters
c:JComponent

a component

html:String

an HTML string

Returns:View

an HTML renderer

getBaselineback to summary
pack-priv static int getBaseline(JComponent c, int y, int ascent, int w, int h)

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 y + ascent.

getBaselineback to summary
pack-priv static int getBaseline(View view, int w, int h)

Gets the baseline for the specified View.

getBaselineback to summary
private static int getBaseline(View view, Shape bounds)
getFactoryback to summary
pack-priv static BasicHTML.BasicEditorKit getFactory()
getHTMLBaselineback to summary
public static int getHTMLBaseline(View view, int w, int h)

Returns the baseline for the html renderer.

Parameters
view:View

the View to get the baseline for

w:int

the width to get the baseline for

h:int

the height to get the baseline for

Returns:int

baseline or a value < 0 indicating there is no reasonable baseline

Exceptions
IllegalArgumentException:
if width or height is < 0
Since
1.6
See Also
java.awt.FontMetrics, javax.swing.JComponent#getBaseline(int, int)
hasParagraphback to summary
private static boolean hasParagraph(View view)
isHTMLStringback to summary
public static boolean isHTMLString(String s)

Check the given string to see if it should trigger the html rendering logic in a non-text component that supports html rendering.

Parameters
s:String

a text

Returns:boolean

true if the given string should trigger the html rendering logic in a non-text component

updateRendererback to summary
public static void updateRenderer(JComponent c, String text)

Stash the HTML render for the given text into the client properties of the given JComponent. If the given text is NOT HTML the property will be cleared of any renderer.

This method is useful for ComponentUI implementations that are static (i.e. shared) and get their state entirely from the JComponent.

Parameters
c:JComponent

a component

text:String

a text

javax.swing.plaf.basic back to summary

pack-priv Class BasicHTML.BasicDocument

extends HTMLDocument
Class Inheritance
Annotations
@SuppressWarnings:serial

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.

Field Summary

Inherited from javax.swing.text.html.HTMLDocument:
AdditionalComments

Constructor Summary

AccessConstructor and Description
pack-priv
BasicDocument(StyleSheet s, Font defaultFont, Color foreground)

The host, that is where we are rendering.

Method Summary

Modifier and TypeMethod and Description
private void
setFontAndColor(Font font, Color fg)

Sets the default font and default color.

Inherited from javax.swing.text.html.HTMLDocument:
createcreateBranchElementcreateDefaultRootcreateLeafElementfireChangedUpdatefireUndoableEditUpdategetBasegetElementgetElementgetIteratorgetParsergetPreservesUnknownTagsgetReadergetReadergetStyleSheetgetTokenThresholdinsertinsertAfterEndinsertAfterStartinsertBeforeEndinsertBeforeStartinsertUpdateprocessHTMLFrameHyperlinkEventsetBasesetInnerHTMLsetOuterHTMLsetParagraphAttributessetParsersetPreservesUnknownTagssetTokenThreshold

Constructor Detail

BasicDocumentback to summary
pack-priv BasicDocument(StyleSheet s, Font defaultFont, Color foreground)

The host, that is where we are rendering.

Method Detail

setFontAndColorback to summary
private void setFontAndColor(Font font, Color fg)

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.

javax.swing.plaf.basic back to summary

pack-priv Class BasicHTML.BasicEditorKit

extends HTMLEditorKit
Class Inheritance
Annotations
@SuppressWarnings:serial

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:

Field Summary

Modifier and TypeField and Description
private static StyleSheet
defaultStyles

Hides javax.swing.text.html.HTMLEditorKit.defaultStyles.

Shared base style for all documents created by us use.
Inherited from javax.swing.text.html.HTMLEditorKit:
BOLD_ACTIONCOLOR_ACTIONDEFAULT_CSSFONT_CHANGE_BIGGERFONT_CHANGE_SMALLERIMG_ALIGN_BOTTOMIMG_ALIGN_MIDDLEIMG_ALIGN_TOPIMG_BORDERITALIC_ACTIONLOGICAL_STYLE_ACTIONPARA_INDENT_LEFTPARA_INDENT_RIGHT

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Document
createDefaultDocument(Font defaultFont, Color foreground)

Sets the async policy to flush everything in one chunk, and to not display unknown tags.

public StyleSheet
getStyleSheet()

Overrides javax.swing.text.html.HTMLEditorKit.getStyleSheet.

Overridden to return our own slimmed down style sheet.

public ViewFactory
getViewFactory()

Overrides javax.swing.text.html.HTMLEditorKit.getViewFactory.

Returns the ViewFactory that is used to make sure the Views don't load in the background.

Inherited from javax.swing.text.html.HTMLEditorKit:
clonecreateDefaultDocumentcreateInputAttributesdeinstallgetAccessibleContextgetActionsgetContentTypegetDefaultCursorgetInputAttributesgetLinkCursorgetParserinsertHTMLinstallisAutoFormSubmissionreadsetAutoFormSubmissionsetDefaultCursorsetLinkCursorsetStyleSheetwrite

Field Detail

defaultStylesback to summary
private static StyleSheet defaultStyles

Hides javax.swing.text.html.HTMLEditorKit.defaultStyles.

Shared base style for all documents created by us use.

Constructor Detail

BasicEditorKitback to summary
pack-priv BasicEditorKit()

Method Detail

createDefaultDocumentback to summary
public Document createDefaultDocument(Font defaultFont, Color foreground)

Sets the async policy to flush everything in one chunk, and to not display unknown tags.

getStyleSheetback to summary
public StyleSheet getStyleSheet()

Overrides javax.swing.text.html.HTMLEditorKit.getStyleSheet.

Overridden to return our own slimmed down style sheet.

getViewFactoryback to summary
public ViewFactory getViewFactory()

Overrides javax.swing.text.html.HTMLEditorKit.getViewFactory.

Returns the ViewFactory that is used to make sure the Views don't load in the background.

javax.swing.plaf.basic back to summary

pack-priv Class BasicHTML.BasicHTMLViewFactory

extends HTMLFactory
Class Inheritance

BasicHTMLViewFactory extends HTMLFactory to force images to be loaded synchronously.

Field Summary

Modifier and TypeField and Description
private static Boolean

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private static void
public View
private static void

Field Detail

useOVback to summary
private static Boolean useOV

Constructor Detail

BasicHTMLViewFactoryback to summary
pack-priv BasicHTMLViewFactory()

Method Detail

clearAllowHTMLObjectback to summary
private static void clearAllowHTMLObject()
createback to summary
public View create(Element elem)

Overrides javax.swing.text.html.HTMLEditorKit.HTMLFactory.create.

Implements javax.swing.text.ViewFactory.create.

Doc from javax.swing.text.html.HTMLEditorKit.HTMLFactory.create.

Creates a view from an element.

setAllowHTMLObjectback to summary
private static void setAllowHTMLObject()
javax.swing.plaf.basic back to summary

pack-priv Class BasicHTML.Renderer

extends View
Class Inheritance

Root text view that acts as an HTML renderer.

Field Summary

Modifier and TypeField and Description
private ViewFactory
private JComponent
private View
private int
Inherited from javax.swing.text.View:
BadBreakWeightExcellentBreakWeightForcedBreakWeightGoodBreakWeightX_AXISY_AXIS

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public float

Returns:

the desired alignment, where 0.0 indicates the origin and 1.0 the full span away from the origin
getAlignment
(int
may be either X_AXIS or Y_AXIS
axis
)

Overrides javax.swing.text.View.getAlignment.

Determines the desired alignment for this view along an axis.

public AttributeSet
getAttributes()

Overrides javax.swing.text.View.getAttributes.

Fetches the attributes to use when rendering.

public Container

Returns:

the container
getContainer
()

Overrides javax.swing.text.View.getContainer.

Fetches the container hosting the view.

public Document

Returns:

the model
getDocument
()

Overrides javax.swing.text.View.getDocument.

Returns the document model underlying the view.

public Element

Returns:

the view
getElement
()

Overrides javax.swing.text.View.getElement.

Gets the element that this view is mapped to.

public int

Returns:

the ending offset
getEndOffset
()

Overrides javax.swing.text.View.getEndOffset.

Returns the ending offset into the model for this view.

public float

Returns:

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.
getMaximumSpan
(int
may be either X_AXIS or Y_AXIS
axis
)

Overrides javax.swing.text.View.getMaximumSpan.

Determines the maximum span for this view along an axis.

public float

Returns:

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.
getMinimumSpan
(int
may be either X_AXIS or Y_AXIS
axis
)

Overrides javax.swing.text.View.getMinimumSpan.

Determines the minimum span for this view along an axis.

public float

Returns:

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.
getPreferredSpan
(int
may be either X_AXIS or Y_AXIS
axis
)

Implements abstract javax.swing.text.View.getPreferredSpan.

Determines the preferred span for this view along an axis.

public int

Returns:

the starting offset
getStartOffset
()

Overrides javax.swing.text.View.getStartOffset.

Returns the starting offset into the model for this view.

public View

Returns:

the view
getView
(int
the number of the view to get
n
)

Overrides javax.swing.text.View.getView.

Gets the n-th view in this container.

public int

Returns:

the number of views
getViewCount
()

Overrides javax.swing.text.View.getViewCount.

Returns the number of views in this view.

public ViewFactory

Returns:

the factory
getViewFactory
()

Overrides javax.swing.text.View.getViewFactory.

Fetches the factory to be used for building the various view fragments that make up the view that represents the model.

public Shape

Returns:

the bounding box of the given position
modelToView
(int
the position to convert
pos
,
Shape
the allocated region to render into
a
,
Position.Bias b)

Implements abstract javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

public Shape

Returns:

the bounding box of the given position is returned
modelToView
(int
the position to convert >= 0
p0
,
Position.Bias
the bias toward the previous character or the next character represented by p0, in case the position is a boundary of two views.
b0
,
int
the position to convert >= 0
p1
,
Position.Bias
the bias toward the previous character or the next character represented by p1, in case the position is a boundary of two views.
b1
,
Shape
the allocated region to render into
a
)

Overrides javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

public void
paint(Graphics
the graphics context
g
,
Shape
the region to render into
allocation
)

Implements abstract javax.swing.text.View.paint.

Renders the view.

public void
preferenceChanged(View
the child view
child
,
boolean
true if the width preference has changed
width
,
boolean
true if the height preference has changed
height
)

Overrides javax.swing.text.View.preferenceChanged.

Specifies that a preference has changed.

public void
setParent(View
the parent view
parent
)

Overrides javax.swing.text.View.setParent.

Sets the view parent.

public void
setSize(float
the width
width
,
float
the height
height
)

Overrides javax.swing.text.View.setSize.

Sets the view size.

public int

Returns:

the location within the model that best represents the given point in the view
viewToModel
(float
x coordinate of the view location to convert
x
,
float
y coordinate of the view location to convert
y
,
Shape
the allocated region to render into
a
,
Position.Bias[] bias)

Implements abstract javax.swing.text.View.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Inherited from javax.swing.text.View:
appendbreakViewchangedUpdatecreateFragmentforwardUpdateforwardUpdateToViewgetBreakWeightgetChildAllocationgetGraphicsgetNextVisualPositionFromgetParentgetResizeWeightgetToolTipTextgetViewIndexgetViewIndexinsertinsertUpdateisVisiblemodelToViewremoveremoveAllremoveUpdatereplaceupdateChildrenupdateLayoutviewToModel

Field Detail

factoryback to summary
private ViewFactory factory
hostback to summary
private JComponent host
viewback to summary
private View view
widthback to summary
private int width

Constructor Detail

Rendererback to summary
pack-priv Renderer(JComponent c, ViewFactory f, View v)

Method Detail

getAlignmentback to summary
public float getAlignment(int axis)

Overrides javax.swing.text.View.getAlignment.

Determines the desired alignment for this view along an axis.

Parameters
axis:int

may be either X_AXIS or Y_AXIS

Returns:float

the desired alignment, where 0.0 indicates the origin and 1.0 the full span away from the origin

getAttributesback to summary
public AttributeSet getAttributes()

Overrides javax.swing.text.View.getAttributes.

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.

getContainerback to summary
public Container getContainer()

Overrides javax.swing.text.View.getContainer.

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.

Returns:Container

the container

getDocumentback to summary
public Document getDocument()

Overrides javax.swing.text.View.getDocument.

Returns the document model underlying the view.

Returns:Document

the model

getElementback to summary
public Element getElement()

Overrides javax.swing.text.View.getElement.

Gets the element that this view is mapped to.

Returns:Element

the view

getEndOffsetback to summary
public int getEndOffset()

Overrides javax.swing.text.View.getEndOffset.

Returns the ending offset into the model for this view.

Returns:int

the ending offset

getMaximumSpanback to summary
public float getMaximumSpan(int axis)

Overrides javax.swing.text.View.getMaximumSpan.

Determines the maximum span for this view along an axis.

Parameters
axis:int

may be either X_AXIS or Y_AXIS

Returns:float

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.

getMinimumSpanback to summary
public float getMinimumSpan(int axis)

Overrides javax.swing.text.View.getMinimumSpan.

Determines the minimum span for this view along an axis.

Parameters
axis:int

may be either X_AXIS or Y_AXIS

Returns:float

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.

getPreferredSpanback to summary
public float getPreferredSpan(int axis)

Implements abstract javax.swing.text.View.getPreferredSpan.

Determines the preferred span for this view along an axis.

Parameters
axis:int

may be either X_AXIS or Y_AXIS

Returns:float

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.

getStartOffsetback to summary
public int getStartOffset()

Overrides javax.swing.text.View.getStartOffset.

Returns the starting offset into the model for this view.

Returns:int

the starting offset

getViewback to summary
public View getView(int n)

Overrides javax.swing.text.View.getView.

Gets the n-th view in this container.

Parameters
n:int

the number of the view to get

Returns:View

the view

getViewCountback to summary
public int getViewCount()

Overrides javax.swing.text.View.getViewCount.

Returns the number of views in this view. Since this view simply wraps the root of the view hierarchy it has exactly one child.

Returns:int

the number of views

See Also
getView
getViewFactoryback to summary
public ViewFactory getViewFactory()

Overrides javax.swing.text.View.getViewFactory.

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.

Returns:ViewFactory

the factory

modelToViewback to summary
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException

Implements abstract javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

Parameters
pos:int

the position to convert

a:Shape

the allocated region to render into

Returns:Shape

the bounding box of the given position

Exceptions
BadLocationException:

Doc from javax.swing.text.View.modelToView.

if the specified position does not represent a valid location in the associated document

modelToViewback to summary
public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException

Overrides javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

Parameters
p0:int

the position to convert >= 0

b0:Position.Bias

the bias toward the previous character or the next character represented by p0, in case the position is a boundary of two views.

p1:int

the position to convert >= 0

b1:Position.Bias

the bias toward the previous character or the next character represented by p1, in case the position is a boundary of two views.

a:Shape

the allocated region to render into

Returns:Shape

the bounding box of the given position is returned

Exceptions
BadLocationException:
if the given position does not represent a valid location in the associated document
IllegalArgumentException:
for an invalid bias argument
See Also
View#viewToModel
paintback to summary
public void paint(Graphics g, Shape allocation)

Implements abstract javax.swing.text.View.paint.

Renders the view.

Parameters
g:Graphics

the graphics context

allocation:Shape

the region to render into

preferenceChangedback to summary
public void preferenceChanged(View child, boolean width, boolean height)

Overrides javax.swing.text.View.preferenceChanged.

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.

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.

Parameters
child:View

the child view

width:boolean

true if the width preference has changed

height:boolean

true if the height preference has changed

setParentback to summary
public void setParent(View parent)

Overrides javax.swing.text.View.setParent.

Sets the view parent.

Parameters
parent:View

the parent view

setSizeback to summary
public void setSize(float width, float height)

Overrides javax.swing.text.View.setSize.

Sets the view size.

Parameters
width:float

the width

height:float

the height

viewToModelback to summary
public int viewToModel(float x, float y, Shape a, Position.Bias[] bias)

Implements abstract javax.swing.text.View.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Parameters
x:float

x coordinate of the view location to convert

y:float

y coordinate of the view location to convert

a:Shape

the allocated region to render into

Returns:int

the location within the model that best represents the given point in the view