public class XMLEditorKit extends DefaultEditorKit implements XMLStyleConstants
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();
...
DefaultEditorKit.BeepAction, DefaultEditorKit.CopyAction, DefaultEditorKit.CutAction, DefaultEditorKit.DefaultKeyTypedAction, DefaultEditorKit.InsertBreakAction, DefaultEditorKit.InsertContentAction, DefaultEditorKit.InsertTabAction, DefaultEditorKit.PasteActionbackwardAction, 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, writableActionATTRIBUTE_NAME, ATTRIBUTE_PREFIX, ATTRIBUTE_VALUE, CDATA, COMMENT, DECLARATION, ELEMENT_NAME, ELEMENT_PREFIX, ELEMENT_VALUE, ENTITY, NAMESPACE_NAME, NAMESPACE_PREFIX, NAMESPACE_VALUE, SPECIAL, STRING| Constructor and Description |
|---|
XMLEditorKit(boolean lineWrapping,
XmlTextPane xmlTextPane)
Constructs the view factory and the Context.
|
| Modifier and Type | Method and Description |
|---|---|
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 |
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 |
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.
|
createCaret, getActions, write, writepublic XMLEditorKit(boolean lineWrapping,
XmlTextPane xmlTextPane)
lineWrapping - enables line wrapping feature if true.public void install(JEditorPane c)
public boolean isLineWrapping()
public void setLineWrappingEnabled(boolean enabled)
enabled - true when line-wrapping enabled.public boolean isWrapStyleWord()
public void setWrapStyleWord(boolean enabled)
enabled - true when word-wrapping style enabled.public String getContentType()
text/xml.getContentType in class DefaultEditorKitpublic final ViewFactory getViewFactory()
getViewFactory in class DefaultEditorKitpublic void setStyle(String name, Color foreground, int fontStyle)
name - the style nameforeground - the foreground colorfontStyle - the font style Plain, Italic or Boldpublic Document createDefaultDocument()
createDefaultDocument in class DefaultEditorKitDefaultEditorKit.createDefaultDocument()public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException
read in class DefaultEditorKitIOExceptionBadLocationExceptionDefaultEditorKit.read( java.io.Reader, javax.swing.text.Document, int)public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException
read in class DefaultEditorKitIOExceptionBadLocationExceptionDefaultEditorKit.read( java.io.InputStream, javax.swing.text.Document, int)Copyright © 2022 WSO2. All rights reserved.