Bounce

org.bounce.text.xml
Class XMLEditorKit

java.lang.Object
  extended by javax.swing.text.EditorKit
      extended by javax.swing.text.DefaultEditorKit
          extended by org.bounce.text.xml.XMLEditorKit
All Implemented Interfaces:
Serializable, Cloneable, XMLStyleConstants

public class XMLEditorKit
extends DefaultEditorKit
implements XMLStyleConstants

The XML editor kit supports handling of editing XML content. It supports syntax highlighting, line wrapping, automatic indentation and tag completion.

Note: The XML Editor package is based on the JavaEditorKit example as described in the article 'Customizing a Text Editor' by Timothy Prinzing . See: http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/


 JEditorPane editor = new JEditorPane(); 
 
 // Instantiate a XMLEditorKit with wrapping enabled.
 XMLEditorKit kit = new XMLEditorKit( true); 
 
 // Set the wrapping style.
 kit.setWrapStyleWord( true);
 
 editor.setEditorKit( kit); 

 // Set the font style.
 editor.setFont( new Font( "Courier", Font.PLAIN, 12)); 
 
 // Set the tab size
 editor.getDocument().putProperty( PlainDocument.tabSizeAttribute, new Integer(4));
 
 // Enable auto indentation.
 editor.getDocument().putProperty( XMLDocument.AUTO_INDENTATION_ATTRIBUTE, new Boolean( true));
 
 // Enable tag completion.
 editor.getDocument().putProperty( XMLDocument.TAG_COMPLETION_ATTRIBUTE, new Boolean( true));
 
 // Set a style
 kit.setStyle( XMLStyleConstants.ATTRIBUTE_NAME, new Color( 255, 0, 0), Font.BOLD);
 
 // Put the editor in a panel that will force it to resize, when a different view is choosen.
 ScrollableEditorPanel editorPanel = new ScrollableEditorPanel( editor);
 
 JScrollPane scroller = new JScrollPane( editorPanel);
 
 ...
 

To switch between line wrapped and non wrapped views use:


 ...
 
 XMLEditorKit kit = (XMLEditorKit)editor.getEditorKit();
 kit.setLineWrappingEnabled( false);
 
 // Update the UI and create a new view...
 editor.updateUI();
 
 ...
 

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.text.DefaultEditorKit
DefaultEditorKit.BeepAction, DefaultEditorKit.CopyAction, DefaultEditorKit.CutAction, DefaultEditorKit.DefaultKeyTypedAction, DefaultEditorKit.InsertBreakAction, DefaultEditorKit.InsertContentAction, DefaultEditorKit.InsertTabAction, DefaultEditorKit.PasteAction
 
Field Summary
 
Fields inherited from class javax.swing.text.DefaultEditorKit
backwardAction, beepAction, beginAction, beginLineAction, beginParagraphAction, beginWordAction, copyAction, cutAction, defaultKeyTypedAction, deleteNextCharAction, deletePrevCharAction, downAction, endAction, endLineAction, EndOfLineStringProperty, endParagraphAction, endWordAction, forwardAction, insertBreakAction, insertContentAction, insertTabAction, nextWordAction, pageDownAction, pageUpAction, pasteAction, previousWordAction, readOnlyAction, selectAllAction, selectionBackwardAction, selectionBeginAction, selectionBeginLineAction, selectionBeginParagraphAction, selectionBeginWordAction, selectionDownAction, selectionEndAction, selectionEndLineAction, selectionEndParagraphAction, selectionEndWordAction, selectionForwardAction, selectionNextWordAction, selectionPreviousWordAction, selectionUpAction, selectLineAction, selectParagraphAction, selectWordAction, upAction, writableAction
 
Fields inherited from interface org.bounce.text.xml.XMLStyleConstants
ATTRIBUTE_NAME, ATTRIBUTE_PREFIX, ATTRIBUTE_VALUE, COMMENT, DECLARATION, ELEMENT_NAME, ELEMENT_PREFIX, ELEMENT_VALUE, ENTITY, NAMESPACE_NAME, NAMESPACE_PREFIX, NAMESPACE_VALUE, SPECIAL, STRING
 
Constructor Summary
XMLEditorKit()
          Constructs an XMLEditorKit with view factory and Context, but with line wrapping turned off.
XMLEditorKit(boolean lineWrapping)
          Constructs the view factory and the Context.
 
Method Summary
 Document createDefaultDocument()
           
 String getContentType()
          Get the MIME type of the data that this kit represents support for.
 ViewFactory getViewFactory()
          Fetches the XML factory that can produce views for XML Documents.
 void install(JEditorPane c)
          Called when the kit is being installed into the a JEditorPane.
 boolean isFolding()
          Returns true when the folding is enables
 boolean isLineWrapping()
          Returns true when line-wrapping has been turned on.
 boolean isWrapStyleWord()
          Returns true when the wrapping style is 'word wrapping'.
 void read(InputStream in, Document doc, int pos)
           
 void read(Reader in, Document doc, int pos)
           
 void setFolding(boolean folding)
          Enables/disables folding for xml
 void setLineWrappingEnabled(boolean enabled)
          Eanbles/disables the line-wrapping feature.
 void setStyle(String name, Color foreground, int fontStyle)
          Set the style identified by the name.
 void setWrapStyleWord(boolean enabled)
          Enables/disables the word-wrapping style.
 
Methods inherited from class javax.swing.text.DefaultEditorKit
createCaret, getActions, write, write
 
Methods inherited from class javax.swing.text.EditorKit
clone, deinstall
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLEditorKit

public XMLEditorKit()
Constructs an XMLEditorKit with view factory and Context, but with line wrapping turned off.


XMLEditorKit

public XMLEditorKit(boolean lineWrapping)
Constructs the view factory and the Context.

Parameters:
lineWrapping - enables line wrapping feature if true.
Method Detail

install

public void install(JEditorPane c)
Called when the kit is being installed into the a JEditorPane.

Overrides:
install in class EditorKit
Parameters:
c - the JEditorPane

isLineWrapping

public boolean isLineWrapping()
Returns true when line-wrapping has been turned on.

Returns:
state of line-wrapping feature.

setLineWrappingEnabled

public void setLineWrappingEnabled(boolean enabled)
Eanbles/disables the line-wrapping feature.

Parameters:
enabled - true when line-wrapping enabled.

isWrapStyleWord

public boolean isWrapStyleWord()
Returns true when the wrapping style is 'word wrapping'.

Returns:
the style of wrapping.

setWrapStyleWord

public void setWrapStyleWord(boolean enabled)
Enables/disables the word-wrapping style.

Parameters:
enabled - true when word-wrapping style enabled.

isFolding

public boolean isFolding()
Returns true when the folding is enables

Returns:
folding status

setFolding

public void setFolding(boolean folding)
Enables/disables folding for xml

Parameters:
folding - true when folding enabled

getContentType

public String getContentType()
Get the MIME type of the data that this kit represents support for. This kit supports the type text/xml.

Overrides:
getContentType in class DefaultEditorKit
Returns:
the type.

getViewFactory

public final ViewFactory getViewFactory()
Fetches the XML factory that can produce views for XML Documents.

Overrides:
getViewFactory in class DefaultEditorKit
Returns:
the XML factory

setStyle

public void setStyle(String name,
                     Color foreground,
                     int fontStyle)
Set the style identified by the name.

Parameters:
name - the style name
foreground - the foreground color
fontStyle - the font style Plain, Italic or Bold

createDefaultDocument

public Document createDefaultDocument()
Overrides:
createDefaultDocument in class DefaultEditorKit
See Also:
DefaultEditorKit.createDefaultDocument()

read

public void read(Reader in,
                 Document doc,
                 int pos)
          throws IOException,
                 BadLocationException
Overrides:
read in class DefaultEditorKit
Throws:
IOException
BadLocationException
See Also:
DefaultEditorKit.read( java.io.Reader, javax.swing.text.Document, int)

read

public void read(InputStream in,
                 Document doc,
                 int pos)
          throws IOException,
                 BadLocationException
Overrides:
read in class DefaultEditorKit
Throws:
IOException
BadLocationException
See Also:
DefaultEditorKit.read( java.io.InputStream, javax.swing.text.Document, int)

Bounce

Copyright © 2002 - 2007 Edwin Dankert. All Rights Reserved.