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:
KeyListener, Serializable, Cloneable, EventListener, XMLStyleConstants

public class XMLEditorKit
extends DefaultEditorKit
implements XMLStyleConstants, KeyListener

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);
 kit.setAutoIndentation(true);
 kit.setTagCompletion(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));
 
 // 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();
 
 ...
 

Version:
$Revision: 1.5 $, $Date: 2008/01/28 21:02:14 $
Author:
Edwin Dankert
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
static String ERROR_HIGHLIGHTING_ATTRIBUTE
           
 
Fields inherited from class javax.swing.text.DefaultEditorKit
backwardAction, beepAction, beginAction, beginLineAction, beginParagraphAction, beginWordAction, copyAction, cutAction, defaultKeyTypedAction, deleteNextCharAction, deleteNextWordAction, deletePrevCharAction, deletePrevWordAction, 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, CDATA, COMMENT, DECLARATION, ELEMENT_NAME, ELEMENT_PREFIX, ELEMENT_VALUE, ENTITY, ENTITY_REFERENCE, NAMESPACE_NAME, NAMESPACE_PREFIX, NAMESPACE_VALUE, SPECIAL, STRING, WHITESPACE
 
Constructor Summary
XMLEditorKit()
          Constructs an XMLEditorKit with view factory and Context, but with line wrapping turned off.
 
Method Summary
 void deinstall(JEditorPane editor)
           
 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 editor)
           
 void keyPressed(KeyEvent event)
           
 void keyReleased(KeyEvent keyevent)
           
 void keyTyped(KeyEvent keyevent)
           
 void setAutoIndentation(boolean enabled)
           
 void setStyle(String token, Color foreground, int fontStyle)
          Set the style identified by the name.
 void setTagCompletion(boolean enabled)
           
 
Methods inherited from class javax.swing.text.DefaultEditorKit
createCaret, createDefaultDocument, getActions, read, read, write, write
 
Methods inherited from class javax.swing.text.EditorKit
clone
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ERROR_HIGHLIGHTING_ATTRIBUTE

public static final String ERROR_HIGHLIGHTING_ATTRIBUTE
See Also:
Constant Field Values
Constructor Detail

XMLEditorKit

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

Method Detail

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 ViewFactory getViewFactory()
Fetches the XML factory that can produce views for XML Documents.

Overrides:
getViewFactory in class DefaultEditorKit
Returns:
the XML factory

setTagCompletion

public final void setTagCompletion(boolean enabled)
Parameters:
enabled - true enables the tag completion

setAutoIndentation

public final void setAutoIndentation(boolean enabled)
Parameters:
enabled - true enables the auto indentation

setStyle

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

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

install

public void install(JEditorPane editor)
Overrides:
install in class EditorKit

deinstall

public void deinstall(JEditorPane editor)
Overrides:
deinstall in class EditorKit

keyPressed

public void keyPressed(KeyEvent event)
Specified by:
keyPressed in interface KeyListener

keyReleased

public void keyReleased(KeyEvent keyevent)
Specified by:
keyReleased in interface KeyListener

keyTyped

public void keyTyped(KeyEvent keyevent)
Specified by:
keyTyped in interface KeyListener


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