2303 lines · 80.4 KB · Java
RawDownload
/* * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */package javax.swing.text; import java.io.*;import java.awt.*;import java.awt.event.ActionEvent;import java.text.*;import javax.swing.Action;import javax.swing.KeyStroke;import javax.swing.SwingConstants;import javax.swing.UIManager; /** * This is the set of things needed by a text component * to be a reasonably functioning editor for some <em>type</em> * 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. * <p> * <dl> * <dt><b><font size=+1>Newlines</font></b> * <dd> * There are two properties which deal with newlines.  The * system property, <code>line.separator</code>, is defined to be * platform-dependent, either "\n", "\r", or "\r\n".  There is also * a property defined in <code>DefaultEditorKit</code>, called * <a href=#EndOfLineStringProperty><code>EndOfLineStringProperty</code></a>, * 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, <code>EndOfLineStringProperty</code> * is set appropriately, and when the document is written back out, the * <code>EndOfLineStringProperty</code> 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 <code>EndOfLineStringProperty</code> has not been defined, * it will use the System property when writing out the * document. * <p>Note that <code>EndOfLineStringProperty</code> is set * on the <code>Document</code> using the <code>get/putProperty</code> * methods.  Subclasses may override this behavior. * * </dl> * * @author  Timothy Prinzing */public class DefaultEditorKit extends EditorKit {     /**     * default constructor for DefaultEditorKit     */    public DefaultEditorKit() {    }     /**     * Gets the MIME type of the data that this     * kit represents support for.  The default     * is <code>text/plain</code>.     *     * @return the type     */    public String getContentType() {        return "text/plain";    }     /**     * 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     */    public ViewFactory getViewFactory() {        return null;    }     /**     * 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     */    public Action[] getActions() {        return defaultActions;    }     /**     * Fetches a caret that can navigate through views     * produced by the associated ViewFactory.     *     * @return the caret     */    public Caret createCaret() {        return null;    }     /**     * Creates an uninitialized text storage model (PlainDocument)     * that is appropriate for this type of editor.     *     * @return the model     */    public Document createDefaultDocument() {        return new PlainDocument();    }     /**     * 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.     */    public void read(InputStream in, Document doc, int pos)        throws IOException, BadLocationException {         read(new InputStreamReader(in), doc, pos);    }     /**     * 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.     */    public void write(OutputStream out, Document doc, int pos, int len)        throws IOException, BadLocationException {        OutputStreamWriter osw = new OutputStreamWriter(out);         write(osw, doc, pos, len);        osw.flush();    }     /**     * 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     */    MutableAttributeSet getInputAttributes() {        return null;    }     /**     * 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.     */    public void read(Reader in, Document doc, int pos)        throws IOException, BadLocationException {         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");            }        }    }     /**     * 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.     */    public void write(Writer out, Document doc, int pos, int len)        throws IOException, BadLocationException {         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();    }      /**     * 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 EndOfLineStringProperty = "__EndOfLine__";     // --- names of well-known actions ---------------------------     /**     * 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.     * @see #getActions     */    public static final String insertContentAction = "insert-content";     /**     * 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.     * @see #getActions     */    public static final String insertBreakAction = "insert-break";     /**     * 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.     * @see #getActions     */    public static final String insertTabAction = "insert-tab";     /**     * Name of the action to delete the character of content that     * precedes the current caret position.     * @see #getActions     */    public static final String deletePrevCharAction = "delete-previous";     /**     * Name of the action to delete the character of content that     * follows the current caret position.     * @see #getActions     */    public static final String deleteNextCharAction = "delete-next";     /**     * Name of the action to delete the word that     * follows the beginning of the selection.     * @see #getActions     * @see JTextComponent#getSelectionStart     * @since 1.6     */    public static final String deleteNextWordAction = "delete-next-word";     /**     * Name of the action to delete the word that     * precedes the beginning of the selection.     * @see #getActions     * @see JTextComponent#getSelectionStart     * @since 1.6     */    public static final String deletePrevWordAction = "delete-previous-word";     /**     * Name of the action to set the editor into read-only     * mode.     * @see #getActions     */    public static final String readOnlyAction = "set-read-only";     /**     * Name of the action to set the editor into writeable     * mode.     * @see #getActions     */    public static final String writableAction = "set-writable";     /**     * Name of the action to cut the selected region     * and place the contents into the system clipboard.     * @see JTextComponent#cut     * @see #getActions     */    public static final String cutAction = "cut-to-clipboard";     /**     * Name of the action to copy the selected region     * and place the contents into the system clipboard.     * @see JTextComponent#copy     * @see #getActions     */    public static final String copyAction = "copy-to-clipboard";     /**     * Name of the action to paste the contents of the     * system clipboard into the selected region, or before the     * caret if nothing is selected.     * @see JTextComponent#paste     * @see #getActions     */    public static final String pasteAction = "paste-from-clipboard";     /**     * Name of the action to create a beep.     * @see #getActions     */    public static final String beepAction = "beep";     /**     * Name of the action to page up vertically.     * @see #getActions     */    public static final String pageUpAction = "page-up";     /**     * Name of the action to page down vertically.     * @see #getActions     */    public static final String pageDownAction = "page-down";     /**     * Name of the action to page up vertically, and move the     * selection.     * @see #getActions     */    /*public*/ static final String selectionPageUpAction = "selection-page-up";     /**     * Name of the action to page down vertically, and move the     * selection.     * @see #getActions

Lines 1–500 of 2303.