jtextcomponent.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,854 行 · 第 1/4 页
JAVA
1,854 行
/* JTextComponent.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text;import java.awt.AWTEvent;import java.awt.Color;import java.awt.Dimension;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.event.ActionEvent;import java.awt.event.InputMethodListener;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.io.IOException;import java.io.Reader;import java.io.Writer;import java.util.Enumeration;import java.util.Hashtable;import javax.accessibility.Accessible;import javax.accessibility.AccessibleAction;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleEditableText;import javax.accessibility.AccessibleRole;import javax.accessibility.AccessibleStateSet;import javax.accessibility.AccessibleText;import javax.swing.Action;import javax.swing.ActionMap;import javax.swing.InputMap;import javax.swing.JComponent;import javax.swing.JViewport;import javax.swing.KeyStroke;import javax.swing.Scrollable;import javax.swing.SwingConstants;import javax.swing.TransferHandler;import javax.swing.UIManager;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.plaf.ActionMapUIResource;import javax.swing.plaf.InputMapUIResource;import javax.swing.plaf.TextUI;public abstract class JTextComponent extends JComponent implements Scrollable, Accessible{ /** * This class implements accessibility support for the JTextComponent class. * It provides an implementation of the Java Accessibility API appropriate * to menu user-interface elements. */ public class AccessibleJTextComponent extends AccessibleJComponent implements AccessibleText, CaretListener, DocumentListener, AccessibleAction, AccessibleEditableText { private static final long serialVersionUID = 7664188944091413696L; /** The caret's offset. */ int dot = 0; /** The current JTextComponent. */ JTextComponent textComp = JTextComponent.this; /** * Constructs an AccessibleJTextComponent. * Adds a listener to track caret change. */ public AccessibleJTextComponent() { super(); textComp.addCaretListener(this); } /** * Returns the zero-based offset of the caret. Note: The character * to the right of the caret will have the same index value as the * offset (the caret is between two characters). * * @return offset of caret */ public int getCaretPosition() { dot = textComp.getCaretPosition(); return dot; } /** * Returns the portion of the text that is selected. * * @return null if no text is selected. */ public String getSelectedText() { return textComp.getSelectedText(); } /** * Returns the start offset within the selected text. If there is no * selection, but there is a caret, the start and end offsets will be * the same. Return 0 if the text is empty, or the caret position if no selection. * * @return index of the start of the text >= 0. */ public int getSelectionStart() { if (getSelectedText() == null || (textComp.getText().equals(""))) return 0; return textComp.getSelectionStart(); } /** * Returns the end offset within the selected text. If there is no * selection, but there is a caret, the start and end offsets will * be the same. Return 0 if the text is empty, or the caret position * if no selection. * * @return index of the end of the text >= 0. */ public int getSelectionEnd() { if (getSelectedText() == null || (textComp.getText().equals(""))) return 0; return textComp.getSelectionEnd(); } /** * Handles caret updates (fire appropriate property change event, which are * AccessibleContext.ACCESSIBLE_CARET_PROPERTY and * AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY). This keeps track of * the dot position internally. When the caret moves, the internal position * is updated after firing the event. * * @param e - caret event */ public void caretUpdate(CaretEvent e) { // TODO: fire appropriate event. dot = e.getDot(); } /** * Returns the accessible state set of this component. * * @return the accessible state set of this component */ public AccessibleStateSet getAccessibleStateSet() { AccessibleStateSet state = super.getAccessibleStateSet(); // TODO: Figure out what state must be added here to the super's state. return state; } /** * Returns the accessible role of this component. * * @return the accessible role of this component * * @see AccessibleRole */ public AccessibleRole getAccessibleRole() { return AccessibleRole.TEXT; } /** * Returns the AccessibleEditableText interface for this text component. * * @return this */ public AccessibleEditableText getAccessibleEditableText() { return this; } /** * Get the AccessibleText associated with this object. In the implementation * of the Java Accessibility API for this class, return this object, * which is responsible for implementing the AccessibleText interface on * behalf of itself. * * @return this * * @see AccessibleText */ public AccessibleText getAccessibleText() { return this; } /** * Insert update. Fire appropriate property change event which * is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY. * * @param e - document event */ public void insertUpdate(DocumentEvent e) { // TODO } /** * Remove update. Fire appropriate property change event which * is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY. * * @param e - document event */ public void removeUpdate(DocumentEvent e) { // TODO } /** * Changed update. Fire appropriate property change event which * is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY. * * @param e - document event */ public void changedUpdate(DocumentEvent e) { // TODO } /** * Given a point in the coordinate system of this object, return the * 0-based index of the character at that point, or -1 if there is none. * * @param p the point to look at * @return the character index, or -1 */ public int getIndexAtPoint(Point p) { return 0; // TODO } /** * Determines the bounding box of the indexed character. Returns an empty * rectangle if the index is out of bounds. The bounds are returned in local coordinates. * If the index is invalid a null rectangle is returned. The screen coordinates returned are * "unscrolled coordinates" if the JTextComponent is contained in a JScrollPane in which * case the resulting rectangle should be composed with the parent coordinates. * Note: the JTextComponent must have a valid size (e.g. have been added to a parent * container whose ancestor container is a valid top-level window) for this method to * be able to return a meaningful (non-null) value. * * @param index the 0-based character index * @return the bounding box, may be empty or null. */ public Rectangle getCharacterBounds(int index) { return null; // TODO } /** * Return the number of characters. * * @return the character count */ public int getCharCount() { return textComp.getText().length(); } /** * Returns the attributes of a character at an index, or null if the index * is out of bounds. * * @param index the 0-based character index * @return the character's attributes */ public AttributeSet getCharacterAttribute(int index) { return null; // TODO } /** * Returns the section of text at the index, or null if the index or part * is invalid. * * @param part {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE} * @param index the 0-based character index * @return the selection of text at that index, or null */ public String getAtIndex(int part, int index) { return null; // TODO } /** * Returns the section of text after the index, or null if the index or part * is invalid. * * @param part {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE} * @param index the 0-based character index * @return the selection of text after that index, or null */ public String getAfterIndex(int part, int index) { return null; // TODO } /** * Returns the section of text before the index, or null if the index or part * is invalid. * * @param part {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE} * @param index the 0-based character index * @return the selection of text before that index, or null */ public String getBeforeIndex(int part, int index) { return null; // TODO } /** * Get the number possible actions for this object, with the zeroth * representing the default action. * * @return the 0-based number of actions */ public int getAccessibleActionCount() { return 0; // TODO } /** * Get a description for the specified action. Returns null if out of * bounds. * * @param i * the action to describe, 0-based * @return description of the action */ public String getAccessibleActionDescription(int i) { // TODO: Not implemented fully return super.getAccessibleDescription(); } /** * Perform the specified action. Does nothing if out of bounds. * * @param i the action to perform, 0-based * @return true if the action was performed */ public boolean doAccessibleAction(int i) { return false; // TODO } /** * Set the text contents to the given string. * * @param s the new text */ public void setTextContents(String s) { // TODO } /** * Inserts the given string at the specified location. * * @param index the index for insertion * @param s the new text */ public void insertTextAtIndex(int index, String s) { replaceText(index, index, s); } /** * Return the text between two points. * * @param start the start position, inclusive * @param end the end position, exclusive */ public String getTextRange(int start, int end) { try { return textComp.getText(start, end - start); } catch (BadLocationException ble) { return ""; } } /** * Delete the text between two points. * * @param start the start position, inclusive * @param end the end position, exclusive */ public void delete(int start, int end) { replaceText(start, end, ""); } /** * Cut the text between two points to the system clipboard. * * @param start the start position, inclusive * @param end the end position, exclusive */ public void cut(int start, int end) { textComp.select(start, end); textComp.cut(); } /** * Paste the text from the system clipboard at the given index. * * @param start the start position */ public void paste(int start) { textComp.setCaretPosition(start); textComp.paste(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?