📄 ekit.java
字号:
package util2;/* * @(#)DefaultEditorKit.java 1.50 00/02/02 * * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */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.text.*;import javax.swing.*;import javax.swing.event.*;/** * 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. * * @author Timothy Prinzing * @version 1.50 02/02/00 */public class Ekit extends EditorKit { /** * default constructor for DefaultEditorKit */ public Ekit() { } /** * 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 myActions; } /** * 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(); } /** * 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); // 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", null); 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), null); 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", null); 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), null); pos += (nch - last - 1); } } else { doc.insertString(pos, new String(buff, last, nch - last), null); pos += (nch - last); } } } if (lastWasCR) { doc.insertString(pos, "\n", null); 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; } // 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 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -