defaulteditorkit.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,557 行 · 第 1/3 页
JAVA
1,557 行
/* DefaultEditorKit.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.Point;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Reader;import java.io.Writer;import javax.swing.Action;/** * The default implementation of {@link EditorKit}. This <code>EditorKit</code> * a plain text <code>Document</code> and several commands that together * make up a basic editor, like cut / copy + paste. * * @author original author unknown * @author Roman Kennke (roman@kennke.org) */public class DefaultEditorKit extends EditorKit{ static class SelectionPreviousWordAction extends TextAction { SelectionPreviousWordAction() { super(selectionPreviousWordAction); } public void actionPerformed(ActionEvent event) { try { JTextComponent t = getTextComponent(event); if (t != null) { int offs = Utilities.getPreviousWord(t, t.getCaretPosition()); Caret c = t.getCaret(); c.moveDot(offs); c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } } catch(BadLocationException ble) { // Can't happen. } } } static class SelectionNextWordAction extends TextAction { SelectionNextWordAction() { super(selectionNextWordAction); } public void actionPerformed(ActionEvent event) { try { JTextComponent t = getTextComponent(event); if (t != null) { int offs = Utilities.getNextWord(t, t.getCaretPosition()); Caret c = t.getCaret(); c.moveDot(offs); c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } } catch(BadLocationException ble) { // Can't happen. } } } static class PreviousWordAction extends TextAction { PreviousWordAction() { super(previousWordAction); } public void actionPerformed(ActionEvent event) { try { JTextComponent t = getTextComponent(event); if (t != null) { int offs = Utilities.getPreviousWord(t, t.getCaretPosition()); Caret c = t.getCaret(); c.setDot(offs); c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } } catch(BadLocationException ble) { // Can't happen. } } } static class NextWordAction extends TextAction { NextWordAction() { super(nextWordAction); } public void actionPerformed(ActionEvent event) { try { JTextComponent t = getTextComponent(event); if (t != null) { int offs = Utilities.getNextWord(t, t.getCaretPosition()); Caret c = t.getCaret(); c.setDot(offs); c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } } catch(BadLocationException ble) { // Can't happen. } } } static class SelectAllAction extends TextAction { SelectAllAction() { super(selectAllAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); int offs = t.getDocument().getLength(); Caret c = t.getCaret(); c.setDot(0); c.moveDot(offs); try { c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } catch(BadLocationException ble) { // Can't happen. } } } static class SelectionBeginAction extends TextAction { SelectionBeginAction() { super(selectionBeginAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); Caret c = t.getCaret(); c.moveDot(0); try { c.setMagicCaretPosition(t.modelToView(0).getLocation()); } catch(BadLocationException ble) { // Can't happen. } } } static class SelectionEndAction extends TextAction { SelectionEndAction() { super(selectionEndAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); int offs = t.getDocument().getLength(); Caret c = t.getCaret(); c.moveDot(offs); try { c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } catch(BadLocationException ble) { // Can't happen. } } } static class SelectionEndLineAction extends TextAction { SelectionEndLineAction() { super(selectionEndLineAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); try { Point p = t.modelToView(t.getCaret().getDot()).getLocation(); int cur = t.getCaretPosition(); int y = p.y; int length = t.getDocument().getLength(); while (y == p.y && cur < length) y = t.modelToView(++cur).getLocation().y; if (cur != length) cur--; Caret c = t.getCaret(); c.moveDot(cur); c.setMagicCaretPosition(t.modelToView(cur).getLocation()); } catch (BadLocationException ble) { // Nothing to do here } } } static class SelectionBeginLineAction extends TextAction { SelectionBeginLineAction() { super(selectionBeginLineAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); try { // TODO: There is a more efficent solution, but // viewToModel doesn't work properly. Point p = t.modelToView(t.getCaret().getDot()).getLocation(); int cur = t.getCaretPosition(); int y = p.y; while (y == p.y && cur > 0) y = t.modelToView(--cur).getLocation().y; if (cur != 0) cur++; Caret c = t.getCaret(); c.moveDot(cur); c.setMagicCaretPosition(t.modelToView(cur).getLocation()); } catch (BadLocationException ble) { // Do nothing here. } } } static class SelectionDownAction extends TextAction { SelectionDownAction() { super(selectionDownAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); try { if (t != null) { Caret c = t.getCaret(); // The magic caret position may be null when the caret // has not moved yet. Point mcp = c.getMagicCaretPosition(); int x = (mcp != null) ? mcp.x : 0; int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x); if (pos > -1) t.moveCaretPosition(pos); } } catch(BadLocationException ble) { // FIXME: Swallowing allowed? } } } static class SelectionUpAction extends TextAction { SelectionUpAction() { super(selectionUpAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); try { if (t != null) { Caret c = t.getCaret(); // The magic caret position may be null when the caret // has not moved yet. Point mcp = c.getMagicCaretPosition(); int x = (mcp != null) ? mcp.x : 0; int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x); if (pos > -1) t.moveCaretPosition(pos); } } catch(BadLocationException ble) { // FIXME: Swallowing allowed? } } } static class SelectionForwardAction extends TextAction { SelectionForwardAction() { super(selectionForwardAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); if (t != null) { int offs = t.getCaretPosition() + 1; if(offs <= t.getDocument().getLength()) { Caret c = t.getCaret(); c.moveDot(offs); try { c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } catch(BadLocationException ble) { // Can't happen. } } } } } static class SelectionBackwardAction extends TextAction { SelectionBackwardAction() { super(selectionBackwardAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); if (t != null) { int offs = t.getCaretPosition() - 1; if(offs >= 0) { Caret c = t.getCaret(); c.moveDot(offs); try { c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } catch(BadLocationException ble) { // Can't happen. } } } } } static class DownAction extends TextAction { DownAction() { super(downAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); try { if (t != null) { Caret c = t.getCaret(); // The magic caret position may be null when the caret // has not moved yet. Point mcp = c.getMagicCaretPosition(); int x = (mcp != null) ? mcp.x : 0; int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x); if (pos > -1) t.setCaretPosition(pos); } } catch(BadLocationException ble) { // FIXME: Swallowing allowed? } } } static class UpAction extends TextAction { UpAction() { super(upAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); try { if (t != null) { Caret c = t.getCaret(); // The magic caret position may be null when the caret // has not moved yet. Point mcp = c.getMagicCaretPosition(); int x = (mcp != null) ? mcp.x : 0; int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x); if (pos > -1) t.setCaretPosition(pos); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?