FileDocCategorySizeDatePackage
DefaultEditorKit.javaAPI DocJava SE 5 API74024Fri Aug 26 14:58:14 BST 2005javax.swing.text

DefaultEditorKit

public class DefaultEditorKit extends EditorKit
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 plain text and provides a minimal set of actions for a simple editor.

Newlines
There are two properties which deal with newlines. The system property, line.separator, is defined to be platform-dependent, either "\n", "\r", or "\r\n". There is also a property defined in DefaultEditorKit, called EndOfLineStringProperty, which is defined automatically when a document is loaded, to be the first occurrence of any of the newline characters. When a document is loaded, EndOfLineStringProperty is set appropriately, and when the document is written back out, the EndOfLineStringProperty is used. But while the document is in memory, the "\n" character is used to define a newline, regardless of how the newline is defined when the document is on disk. Therefore, for searching purposes, "\n" should always be used. When a new document is created, and the EndOfLineStringProperty has not been defined, it will use the System property when writing out the document.

Note that EndOfLineStringProperty is set on the Document using the get/setProperty methods. Subclasses may override this behavior.

author
Timothy Prinzing
version
1.67 12/19/03

Fields Summary
public static final String
EndOfLineStringProperty
When reading a document if a CRLF is encountered a property with this name is added and the value will be "\r\n".
public static final String
insertContentAction
Name of the action to place content into the associated document. If there is a selection, it is removed before the new content is added.
public static final String
insertBreakAction
Name of the action to place a line/paragraph break into the document. If there is a selection, it is removed before the break is added.
public static final String
insertTabAction
Name of the action to place a tab character into the document. If there is a selection, it is removed before the tab is added.
public static final String
deletePrevCharAction
Name of the action to delete the character of content that precedes the current caret position.
public static final String
deleteNextCharAction
Name of the action to delete the character of content that follows the current caret position.
public static final String
readOnlyAction
Name of the action to set the editor into read-only mode.
public static final String
writableAction
Name of the action to set the editor into writeable mode.
public static final String
cutAction
Name of the action to cut the selected region and place the contents into the system clipboard.
public static final String
copyAction
Name of the action to copy the selected region and place the contents into the system clipboard.
public static final String
pasteAction
Name of the action to paste the contents of the system clipboard into the selected region, or before the caret if nothing is selected.
public static final String
beepAction
Name of the action to create a beep.
public static final String
pageUpAction
Name of the action to page up vertically.
public static final String
pageDownAction
Name of the action to page down vertically.
static final String
selectionPageUpAction
Name of the action to page up vertically, and move the selection.
static final String
selectionPageDownAction
Name of the action to page down vertically, and move the selection.
static final String
selectionPageLeftAction
Name of the action to page left horizontally, and move the selection.
static final String
selectionPageRightAction
Name of the action to page right horizontally, and move the selection.
public static final String
forwardAction
Name of the Action for moving the caret logically forward one position.
public static final String
backwardAction
Name of the Action for moving the caret logically backward one position.
public static final String
selectionForwardAction
Name of the Action for extending the selection by moving the caret logically forward one position.
public static final String
selectionBackwardAction
Name of the Action for extending the selection by moving the caret logically backward one position.
public static final String
upAction
Name of the Action for moving the caret logically upward one position.
public static final String
downAction
Name of the Action for moving the caret logically downward one position.
public static final String
selectionUpAction
Name of the Action for moving the caret logically upward one position, extending the selection.
public static final String
selectionDownAction
Name of the Action for moving the caret logically downward one position, extending the selection.
public static final String
beginWordAction
Name of the Action for moving the caret to the beginning of a word.
public static final String
endWordAction
Name of the Action for moving the caret to the end of a word.
public static final String
selectionBeginWordAction
Name of the Action for moving the caret to the beginning of a word, extending the selection.
public static final String
selectionEndWordAction
Name of the Action for moving the caret to the end of a word, extending the selection.
public static final String
previousWordAction
Name of the Action for moving the caret to the beginning of the previous word.
public static final String
nextWordAction
Name of the Action for moving the caret to the beginning of the next word.
public static final String
selectionPreviousWordAction
Name of the Action for moving the selection to the beginning of the previous word, extending the selection.
public static final String
selectionNextWordAction
Name of the Action for moving the selection to the beginning of the next word, extending the selection.
public static final String
beginLineAction
Name of the Action for moving the caret to the beginning of a line.
public static final String
endLineAction
Name of the Action for moving the caret to the end of a line.
public static final String
selectionBeginLineAction
Name of the Action for moving the caret to the beginning of a line, extending the selection.
public static final String
selectionEndLineAction
Name of the Action for moving the caret to the end of a line, extending the selection.
public static final String
beginParagraphAction
Name of the Action for moving the caret to the beginning of a paragraph.
public static final String
endParagraphAction
Name of the Action for moving the caret to the end of a paragraph.
public static final String
selectionBeginParagraphAction
Name of the Action for moving the caret to the beginning of a paragraph, extending the selection.
public static final String
selectionEndParagraphAction
Name of the Action for moving the caret to the end of a paragraph, extending the selection.
public static final String
beginAction
Name of the Action for moving the caret to the beginning of the document.
public static final String
endAction
Name of the Action for moving the caret to the end of the document.
public static final String
selectionBeginAction
Name of the Action for moving the caret to the beginning of the document.
public static final String
selectionEndAction
Name of the Action for moving the caret to the end of the document.
public static final String
selectWordAction
Name of the Action for selecting a word around the caret.
public static final String
selectLineAction
Name of the Action for selecting a line around the caret.
public static final String
selectParagraphAction
Name of the Action for selecting a paragraph around the caret.
public static final String
selectAllAction
Name of the Action for selecting the entire document
static final String
unselectAction
Name of the Action for removing selection
static final String
toggleComponentOrientationAction
Name of the Action for toggling the component's orientation.
public static final String
defaultKeyTypedAction
Name of the action that is executed by default if a key typed event is received and there is no keymap entry.
private static final Action[]
defaultActions
Constructors Summary
public DefaultEditorKit()
default constructor for DefaultEditorKit

    
Methods Summary
public javax.swing.text.CaretcreateCaret()
Fetches a caret that can navigate through views produced by the associated ViewFactory.

return
the caret

        return null;
    
public javax.swing.text.DocumentcreateDefaultDocument()
Creates an uninitialized text storage model (PlainDocument) that is appropriate for this type of editor.

return
the model

        return new PlainDocument();
    
public javax.swing.Action[]getActions()
Fetches the set of commands that can be used on a text component that is using a model and view produced by this kit.

return
the command list

        return defaultActions;
    
public java.lang.StringgetContentType()
Gets the MIME type of the data that this kit represents support for. The default is text/plain.

return
the type

        return "text/plain";
    
javax.swing.text.MutableAttributeSetgetInputAttributes()
Gets the input attributes for the pane. This method exists for the benefit of StyledEditorKit so that the read method will pick up the correct attributes to apply to inserted text. This class's implementation simply returns null.

return
null

        return null;
    
public javax.swing.text.ViewFactorygetViewFactory()
Fetches a factory that is suitable for producing views of any models that are produced by this kit. The default is to have the UI produce the factory, so this method has no implementation.

return
the view factory

        return null;
    
public voidread(java.io.Reader in, javax.swing.text.Document doc, int pos)
Inserts content from the given stream, which will be treated as plain text.

param
in The stream to read from
param
doc The destination for the insertion.
param
pos The location in the document to place the content >= 0.
exception
IOException on any I/O error
exception
BadLocationException if pos represents an invalid location within the document.


        char[] buff = new char[4096];
        int nch;
	boolean lastWasCR = false;
	boolean isCRLF = false;
	boolean isCR = false;
	int last;
	boolean wasEmpty = (doc.getLength() == 0);
        AttributeSet attr = getInputAttributes();

	// Read in a block at a time, mapping \r\n to \n, as well as single
        // \r's to \n's. If a \r\n is encountered, \r\n will be set as the
        // newline string for the document, if \r is encountered it will
        // be set as the newline character, otherwise the newline property
        // for the document will be removed.
        while ((nch = in.read(buff, 0, buff.length)) != -1) {
	    last = 0;
	    for(int counter = 0; counter < nch; counter++) {
		switch(buff[counter]) {
		case '\r":
		    if (lastWasCR) {
			isCR = true;
			if (counter == 0) {
                            doc.insertString(pos, "\n", attr);
			    pos++;
			}
			else {
			    buff[counter - 1] = '\n";
			}
		    }
		    else {
			lastWasCR = true;
		    }
		    break;
		case '\n":
		    if (lastWasCR) {
			if (counter > (last + 1)) {
			    doc.insertString(pos, new String(buff, last,
                                            counter - last - 1), attr);
			    pos += (counter - last - 1);
			}
			// else nothing to do, can skip \r, next write will
			// write \n
			lastWasCR = false;
			last = counter;
			isCRLF = true;
		    }
		    break;
		default:
		    if (lastWasCR) {
			isCR = true;
			if (counter == 0) {
                            doc.insertString(pos, "\n", attr);
			    pos++;
			}
			else {
			    buff[counter - 1] = '\n";
			}
			lastWasCR = false;
		    }
		    break;
		}
	    }
	    if (last < nch) {
		if(lastWasCR) {
		    if (last < (nch - 1)) {
			doc.insertString(pos, new String(buff, last,
                                         nch - last - 1), attr);
			pos += (nch - last - 1);
		    }
		}
		else {
		    doc.insertString(pos, new String(buff, last,
                                     nch - last), attr);
		    pos += (nch - last);
		}
	    }
        }
	if (lastWasCR) {
            doc.insertString(pos, "\n", attr);
	    isCR = true;
	}
	if (wasEmpty) {
	    if (isCRLF) {
		doc.putProperty(EndOfLineStringProperty, "\r\n");
	    }
	    else if (isCR) {
		doc.putProperty(EndOfLineStringProperty, "\r");
	    }
	    else {
		doc.putProperty(EndOfLineStringProperty, "\n");
	    }
	}
    
public voidread(java.io.InputStream in, javax.swing.text.Document doc, int pos)
Inserts content from the given stream which is expected to be in a format appropriate for this kind of content handler.

param
in The stream to read from
param
doc The destination for the insertion.
param
pos The location in the document to place the content >= 0.
exception
IOException on any I/O error
exception
BadLocationException if pos represents an invalid location within the document.


        read(new InputStreamReader(in), doc, pos);
    
public voidwrite(java.io.Writer out, javax.swing.text.Document doc, int pos, int len)
Writes content from a document to the given stream as plain text.

param
out The stream to write to
param
doc The source for the write.
param
pos The location in the document to fetch the content from >= 0.
param
len The amount to write out >= 0.
exception
IOException on any I/O error
exception
BadLocationException if pos is not within 0 and the length of the document.


	if ((pos < 0) || ((pos + len) > doc.getLength())) {
	    throw new BadLocationException("DefaultEditorKit.write", pos);
	}
        Segment data = new Segment();
        int nleft = len;
        int offs = pos;
	Object endOfLineProperty = doc.getProperty(EndOfLineStringProperty);
	if (endOfLineProperty == null) {
	    try {
		endOfLineProperty = System.getProperty("line.separator");
	    } catch (SecurityException se) { }
	}
	String endOfLine;
	if (endOfLineProperty instanceof String) {
	    endOfLine = (String)endOfLineProperty;
	}
	else {
	    endOfLine = null;
	}
	if (endOfLineProperty != null && !endOfLine.equals("\n")) {
	    // There is an end of line string that isn't \n, have to iterate
	    // through and find all \n's and translate to end of line string.
	    while (nleft > 0) {
		int n = Math.min(nleft, 4096);
		doc.getText(offs, n, data);
		int last = data.offset;
		char[] array = data.array;
		int maxCounter = last + data.count;
		for (int counter = last; counter < maxCounter; counter++) {
		    if (array[counter] == '\n") {
			if (counter > last) {
			    out.write(array, last, counter - last);
			}
			out.write(endOfLine);
			last = counter + 1;
		    }
		}
		if (maxCounter > last) {
		    out.write(array, last, maxCounter - last);
		}
		offs += n;
		nleft -= n;
	    }
	}
	else {
	    // Just write out text, will already have \n, no mapping to
	    // do.
	    while (nleft > 0) {
		int n = Math.min(nleft, 4096);
		doc.getText(offs, n, data);
		out.write(data.array, data.offset, data.count);
		offs += n;
		nleft -= n;
	    }
	}
	out.flush();
    
public voidwrite(java.io.OutputStream out, javax.swing.text.Document doc, int pos, int len)
Writes content from a document to the given stream in a format appropriate for this kind of content handler.

param
out The stream to write to
param
doc The source for the write.
param
pos The location in the document to fetch the content >= 0.
param
len The amount to write out >= 0.
exception
IOException on any I/O error
exception
BadLocationException if pos represents an invalid location within the document.

	OutputStreamWriter osw = new OutputStreamWriter(out);

        write(osw, doc, pos, len);
	osw.flush();