APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.text.html

public Class HTMLEditorKit

extends StyledEditorKit
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Known Direct Subclasses
javax.swing.plaf.basic.BasicHTML.BasicEditorKit
Annotations
@SuppressWarnings:serial
Imports
java.awt.Color, .Component, .Container, .Cursor, .Graphics, .Insets, .Point, .Rectangle, .Shape, java.awt.event.ActionEvent, .ComponentEvent, .ComponentListener, .MouseAdapter, .MouseEvent, .MouseListener, .MouseMotionListener, java.io.BufferedReader, .IOException, .InputStream, .InputStreamReader, .Reader, .Serializable, .StringReader, .Writer, java.lang.ref.Reference, .WeakReference, java.net.MalformedURLException, .URL, java.util.Enumeration, javax.accessibility.Accessible, .AccessibleAction, .AccessibleContext, javax.swing.Action, .JEditorPane, .JViewport, .SizeRequirements, .SwingUtilities, javax.swing.event.CaretEvent, .CaretListener, .HyperlinkEvent, javax.swing.plaf.TextUI, javax.swing.text.AbstractDocument, .AttributeSet, .BadLocationException, .BoxView, .ComponentView, .DefaultHighlighter, .Document, .EditorKit, .Element, .ElementIterator, .Highlighter, .IconView, .JTextComponent, .LabelView, .MutableAttributeSet, .Position, .StyleConstants, .StyledDocument, .StyledEditorKit, .TextAction, .View, .ViewFactory, javax.swing.text.html.parser.ParserDelegator, sun.swing.SwingAccessor

The Swing JEditorPane text component supports different kinds of content via a plug-in mechanism called an EditorKit. Because HTML is a very popular format of content, some support is provided by default. The default support is provided by this class, which supports HTML version 3.2 (with some extensions), and is migrating toward version 4.0. The <applet> tag is not supported, but some support is provided for the <object> tag.

There are several goals of the HTML EditorKit provided, that have an effect upon the way that HTML is modeled. These have influenced its design in a substantial way.

Support editing
It might seem fairly obvious that a plug-in for JEditorPane should provide editing support, but that fact has several design considerations. There are a substantial number of HTML documents that don't properly conform to an HTML specification. These must be normalized somewhat into a correct form if one is to edit them. Additionally, users don't like to be presented with an excessive amount of structure editing, so using traditional text editing gestures is preferred over using the HTML structure exactly as defined in the HTML document.

The modeling of HTML is provided by the class HTMLDocument. Its documentation describes the details of how the HTML is modeled. The editing support leverages heavily off of the text package.

Extendable/Scalable
To maximize the usefulness of this kit, a great deal of effort has gone into making it extendable. These are some of the features.
  1. The parser is replaceable. The default parser is the Hot Java parser which is DTD based. A different DTD can be used, or an entirely different parser can be used. To change the parser, reimplement the getParser method. The default parser is dynamically loaded when first asked for, so the class files will never be loaded if an alternative parser is used. The default parser is in a separate package called parser below this package.
  2. The parser drives the ParserCallback, which is provided by HTMLDocument. To change the callback, subclass HTMLDocument and reimplement the createDefaultDocument method to return document that produces a different reader. The reader controls how the document is structured. Although the Document provides HTML support by default, there is nothing preventing support of non-HTML tags that result in alternative element structures.
  3. The default view of the models are provided as a hierarchy of View implementations, so one can easily customize how a particular element is displayed or add capabilities for new kinds of elements by providing new View implementations. The default set of views are provided by the HTMLFactory class. This can be easily changed by subclassing or replacing the HTMLFactory and reimplementing the getViewFactory method to return the alternative factory.
  4. The View implementations work primarily off of CSS attributes, which are kept in the views. This makes it possible to have multiple views mapped over the same model that appear substantially different. This can be especially useful for printing. For most HTML attributes, the HTML attributes are converted to CSS attributes for display. This helps make the View implementations more general purpose
Asynchronous Loading
Larger documents involve a lot of parsing and take some time to load. By default, this kit produces documents that will be loaded asynchronously if loaded using JEditorPane.setPage. This is controlled by a property on the document. The method createDefaultDocument can be overridden to change this. The batching of work is done by the HTMLDocument.HTMLReader class. The actual work is done by the DefaultStyledDocument and AbstractDocument classes in the text package.
Customization from current LAF
HTML provides a well known set of features without exactly specifying the display characteristics. Swing has a theme mechanism for its look-and-feel implementations. It is desirable for the look-and-feel to feed display characteristics into the HTML views. An user with poor vision for example would want high contrast and larger than typical fonts.

The support for this is provided by the StyleSheet class. The presentation of the HTML can be heavily influenced by the setting of the StyleSheet property on the EditorKit.

Not lossy
An EditorKit has the ability to be read and save documents. It is generally the most pleasing to users if there is no loss of data between the two operation. The policy of the HTMLEditorKit will be to store things not recognized or not necessarily visible so they can be subsequently written out. The model of the HTML document should therefore contain all information discovered while reading the document. This is constrained in some ways by the need to support editing (i.e. incorrect documents sometimes must be normalized). The guiding principle is that information shouldn't be lost, but some might be synthesized to produce a more correct model or it might be rearranged.
Author
Timothy Prinzing

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class
public static class
HTMLEditorKit.HTMLFactory

A factory to build views for HTML.

public abstract static class
HTMLEditorKit.HTMLTextAction

An abstract Action providing some convenience methods that may be useful in inserting HTML into an existing document.

pack-priv static class
HTMLEditorKit.InsertHRAction

InsertHRAction is special, at actionPerformed time it will determine the parent HTML.Tag based on the paragraph element at the selection start.

public static class
HTMLEditorKit.InsertHTMLTextAction

InsertHTMLTextAction can be used to insert an arbitrary string of HTML into an existing HTML document.

public static class
HTMLEditorKit.LinkController

Class to watch the associated component and fire hyperlink events on it when appropriate.

public abstract static class
HTMLEditorKit.Parser

Interface to be supported by the parser.

public static class
HTMLEditorKit.ParserCallback

The result of parsing drives these callback methods.

Field Summary

Modifier and TypeField and Description
private AccessibleContext
private static final HTMLEditorKit.ActivateLinkAction
public static final String
BOLD_ACTION

The bold action identifier

public static final String
COLOR_ACTION

The Color choice action identifier The color is passed as an argument

public static final String
DEFAULT_CSS

Default Cascading Style Sheet file that sets up the tag views.

private static final Object
private static final Action[]
private static final Cursor
private Cursor
private static final ViewFactory
defaultFactory

Hides javax.swing.text.StyledEditorKit.defaultFactory.

Shared factory for creating HTML Views.
private static HTMLEditorKit.Parser
private static StyleSheet
public static final String
FONT_CHANGE_BIGGER

The font size increase to next value action identifier

public static final String
FONT_CHANGE_SMALLER

The font size decrease to next value action identifier

private boolean
public static final String
IMG_ALIGN_BOTTOM

Align images at the bottom.

public static final String
IMG_ALIGN_MIDDLE

Align images in the middle.

public static final String
IMG_ALIGN_TOP

Align images at the top.

public static final String
IMG_BORDER

Align images at the border.

pack-priv MutableAttributeSet
private static final String
INSERT_HR_HTML

HTML used when inserting hr.

private static final String
INSERT_OL_HTML

HTML used when inserting ordered lists.

private static final String
INSERT_PRE_HTML

HTML used when inserting pre.

private static final String
INSERT_TABLE_HTML

HTML used when inserting tables.

private static final String
INSERT_UL_HTML

HTML used when inserting unordered lists.

private boolean
public static final String
ITALIC_ACTION

The italic action identifier

private Cursor
private HTMLEditorKit.LinkController
private Object
public static final String
LOGICAL_STYLE_ACTION

The logical style choice action identifier The logical style is passed in as an argument

private static final Cursor
private static final HTMLEditorKit.NavigateLinkAction
public static final String
PARA_INDENT_LEFT

The paragraph left indent action identifier

public static final String
PARA_INDENT_RIGHT

The paragraph right indent action identifier

private int
private static final HTMLEditorKit.NavigateLinkAction
private JEditorPane

Constructor Summary

AccessConstructor and Description
public
HTMLEditorKit()

Constructs an HTMLEditorKit, creates a StyleContext, and loads the style sheet.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the copy
clone
()

Overrides javax.swing.text.StyledEditorKit.clone.

Creates a copy of the editor kit.

public Document

Returns:

the model
createDefaultDocument
()

Overrides javax.swing.text.StyledEditorKit.createDefaultDocument.

Create an uninitialized text storage model that is appropriate for this type of editor.

protected void
createInputAttributes(Element element, MutableAttributeSet set)

Overrides javax.swing.text.StyledEditorKit.createInputAttributes.

Copies the key/values in elements AttributeSet into set.

public void
deinstall(JEditorPane
the JEditorPane
c
)

Overrides javax.swing.text.StyledEditorKit.deinstall.

Called when the kit is being removed from the JEditorPane.

private HTMLEditorKit.Parser
ensureParser(HTMLDocument doc)

Try to get an HTML parser from the document.

public AccessibleContext

Returns:

the AccessibleContext associated with this editor kit
getAccessibleContext
()

Implements javax.accessibility.Accessible.getAccessibleContext.

returns the AccessibleContext associated with this editor kit

public Action[]

Returns:

the command list
getActions
()

Overrides javax.swing.text.StyledEditorKit.getActions.

Fetches the command list for the editor.

private static Object
private static int
public String

Returns:

the type
getContentType
()

Overrides javax.swing.text.DefaultEditorKit.getContentType.

Get the MIME type of the data that this kit represents support for.

public Cursor

Returns:

the cursor
getDefaultCursor
()

Returns the default cursor.

public MutableAttributeSet

Returns:

the attribute set
getInputAttributes
()

Overrides javax.swing.text.StyledEditorKit.getInputAttributes.

Gets the input attributes used for the styled editing actions.

public Cursor

Returns:

the cursor
getLinkCursor
()

Returns the cursor to use over hyper links.

protected HTMLEditorKit.Parser

Returns:

the parser
getParser
()

Fetch the parser to use for reading HTML streams.

pack-priv static InputStream

Returns:

a stream representing the resource
getResourceAsStream
(final String
the name of the resource, relative to the HTMLEditorKit class
name
)

Fetch a resource relative to the HTMLEditorKit classfile.

public StyleSheet

Returns:

the StyleSheet
getStyleSheet
()

Get the set of styles currently being used to render the HTML elements.

public ViewFactory

Returns:

the factory
getViewFactory
()

Overrides javax.swing.text.StyledEditorKit.getViewFactory.

Fetch a factory that is suitable for producing views of any models that are produced by this kit.

public void
insertHTML(HTMLDocument
the document to insert into
doc
,
int
the offset to insert HTML at
offset
,
String
the HTML string
html
,
int
the number of ElementSpec.EndTagTypes to generate before inserting
popDepth
,
int
the number of ElementSpec.StartTagTypes with a direction of ElementSpec.JoinNextDirection that should be generated before inserting, but after the end tags have been generated
pushDepth
,
HTML.Tag
the first tag to start inserting into document
insertTag
)

Inserts HTML into an existing document.

public void
install(JEditorPane
the JEditorPane
c
)

Overrides javax.swing.text.StyledEditorKit.install.

Called when the kit is being installed into the a JEditorPane.

public boolean

Returns:

true if html form submission is processed automatically, false otherwise.
isAutoFormSubmission
()

Indicates whether an html form submission is processed automatically or only FormSubmitEvent is fired.

public void
read(Reader
the stream to read from
in
,
Document
the destination for the insertion
doc
,
int
the location in the document to place the content
pos
)

Overrides javax.swing.text.DefaultEditorKit.read.

Inserts content from the given stream.

public void
setAutoFormSubmission(boolean
if true, html form submission is processed automatically.
isAuto
)

Specifies if an html form submission is processed automatically or only FormSubmitEvent is fired.

public void
setDefaultCursor(Cursor
a cursor
cursor
)

Sets the default cursor.

public void
setLinkCursor(Cursor
a cursor
cursor
)

Sets the cursor to use over links.

public void
setStyleSheet(StyleSheet
a StyleSheet
s
)

Set the set of styles to be used to render the various HTML elements.

public void
write(Writer
the stream to write to
out
,
Document
the source for the write
doc
,
int
the location in the document to fetch the content
pos
,
int
the amount to write out
len
)

Overrides javax.swing.text.DefaultEditorKit.write.

Write content from a document to the given stream in a format appropriate for this kind of content handler.

Inherited from javax.swing.text.StyledEditorKit:
getCharacterAttributeRun