FileDocCategorySizeDatePackage
StyledEditorKit.javaAPI DocJava SE 5 API29361Fri Aug 26 14:58:16 BST 2005javax.swing.text

StyledEditorKit

public class StyledEditorKit extends DefaultEditorKit
This is the set of things needed by a text component to be a reasonably functioning editor for some type of text document. This implementation provides a default implementation which treats text as styled text and provides a minimal set of actions for editing styled text.
author
Timothy Prinzing
version
1.45 02/04/04

Fields Summary
private static final ViewFactory
defaultFactory
Element
currentRun
Element
currentParagraph
MutableAttributeSet
inputAttributes
This is the set of attributes used to store the input attributes.
private AttributeTracker
inputAttributeUpdater
This listener will be attached to the caret of the text component that the EditorKit gets installed into. This should keep the input attributes updated for use by the styled actions.
private static final Action[]
defaultActions
Constructors Summary
public StyledEditorKit()
Creates a new EditorKit used for styled documents.

        createInputAttributeUpdated();
        createInputAttributes();
    
Methods Summary
public java.lang.Objectclone()
Creates a copy of the editor kit.

return
the copy

	StyledEditorKit o = (StyledEditorKit)super.clone();
        o.currentRun = o.currentParagraph = null;
        o.createInputAttributeUpdated();
        o.createInputAttributes();
	return o;
    
public javax.swing.text.DocumentcreateDefaultDocument()
Creates an uninitialized text storage model that is appropriate for this type of editor.

return
the model

	return new DefaultStyledDocument();
    
private voidcreateInputAttributeUpdated()
Creates a new AttributeTracker.

        inputAttributeUpdater = new AttributeTracker();
    
private voidcreateInputAttributes()
Creates the AttributeSet used for the selection.

        inputAttributes = new SimpleAttributeSet() {
            public AttributeSet getResolveParent() {
                return (currentParagraph != null) ?
                           currentParagraph.getAttributes() : null;
            }

            public Object clone() {
                return new SimpleAttributeSet(this);
            }
        };
    
protected voidcreateInputAttributes(javax.swing.text.Element element, javax.swing.text.MutableAttributeSet set)
Copies the key/values in elements AttributeSet into set. This does not copy component, icon, or element names attributes. Subclasses may wish to refine what is and what isn't copied here. But be sure to first remove all the attributes that are in set.

This is called anytime the caret moves over a different location.

        if (element.getAttributes().getAttributeCount() > 0
            || element.getEndOffset() - element.getStartOffset() > 1
            || element.getEndOffset() < element.getDocument().getLength()) {
	    set.removeAttributes(set);
	    set.addAttributes(element.getAttributes());
	    set.removeAttribute(StyleConstants.ComponentAttribute);
	    set.removeAttribute(StyleConstants.IconAttribute);
	    set.removeAttribute(AbstractDocument.ElementNameAttribute);
	    set.removeAttribute(StyleConstants.ComposedTextAttribute);
	}
    
public voiddeinstall(javax.swing.JEditorPane c)
Called when the kit is being removed from the JEditorPane. This is used to unregister any listeners that were attached.

param
c the JEditorPane

	c.removeCaretListener(inputAttributeUpdater);
	c.removePropertyChangeListener(inputAttributeUpdater);

	// remove references to current document so it can be collected.
	currentRun = null;
	currentParagraph = null;
    
public javax.swing.Action[]getActions()
Fetches the command list for the editor. This is the list of commands supported by the superclass augmented by the collection of commands defined locally for style operations.

return
the command list

	return TextAction.augmentList(super.getActions(), this.defaultActions);
    
public javax.swing.text.ElementgetCharacterAttributeRun()
Fetches the element representing the current run of character attributes for the caret.

return
the element

	return currentRun;
    
public javax.swing.text.MutableAttributeSetgetInputAttributes()
Gets the input attributes for the pane. When the caret moves and there is no selection, the input attributes are automatically mutated to reflect the character attributes of the current caret location. The styled editing actions use the input attributes to carry out their actions.

return
the attribute set

	return inputAttributes;
    
public javax.swing.text.ViewFactorygetViewFactory()
Fetches a factory that is suitable for producing views of any models that are produced by this kit. This is implemented to return View implementations for the following kinds of elements:
  • AbstractDocument.ContentElementName
  • AbstractDocument.ParagraphElementName
  • AbstractDocument.SectionElementName
  • StyleConstants.ComponentElementName
  • StyleConstants.IconElementName

return
the factory

	return defaultFactory;
    
public voidinstall(javax.swing.JEditorPane c)
Called when the kit is being installed into a JEditorPane.

param
c the JEditorPane

	c.addCaretListener(inputAttributeUpdater);
	c.addPropertyChangeListener(inputAttributeUpdater);
	Caret caret = c.getCaret();
	if (caret != null) {
	    inputAttributeUpdater.updateInputAttributes
		                  (caret.getDot(), caret.getMark(), c);
	}