📄 rtextareaeditorkit.java~1~
字号:
public IncreaseFontSizeAction(String name, Icon icon, String desc,
Integer mnemonic, KeyStroke accelerator) {
super(name, icon, desc, mnemonic, accelerator);
initialize();
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
Font font = textArea.getFont();
float oldSize = font.getSize2D();
float newSize = oldSize + increaseAmount;
if (newSize<=MAXIMUM_SIZE) {
// Grow by increaseAmount.
font = font.deriveFont(newSize);
textArea.setFont(font);
}
else if (oldSize<MAXIMUM_SIZE) {
// Can't grow by full increaseAmount, but can grow a
// little bit.
font = font.deriveFont(MAXIMUM_SIZE);
textArea.setFont(font);
}
else {
// Our font size must be at or bigger than MAXIMUM_SIZE.
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
}
textArea.requestFocusInWindow();
}
public final String getMacroID() {
return rtaIncreaseFontSizeAction;
}
protected void initialize() {
increaseAmount = 1.0f;
}
}
/*****************************************************************************/
/**
* Action for when the user presses the Enter key.
*/
public static class InsertBreakAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = -5025766917286929583L;
public InsertBreakAction() {
super(DefaultEditorKit.insertBreakAction);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
textArea.replaceSelection("\n");
}
public final String getMacroID() {
return DefaultEditorKit.insertBreakAction;
}
}
/*****************************************************************************/
/**
* Action taken when content is to be inserted.
*/
public static class InsertContentAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = -3797537655209982246L;
public InsertContentAction() {
super(DefaultEditorKit.insertContentAction, null, null, null,
null);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
String content = e.getActionCommand();
if (content != null)
textArea.replaceSelection(content);
else
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
}
public final String getMacroID() {
return DefaultEditorKit.insertContentAction;
}
}
/*****************************************************************************/
/**
* Places a tab character into the document. If there is a selection, it
* is removed before the tab is added.
*/
public static class InsertTabAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = -3409076237783259680L;
public InsertTabAction() {
super(insertTabAction);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
textArea.replaceSelection("\t");
}
public final String getMacroID() {
return DefaultEditorKit.insertTabAction;
}
}
/*****************************************************************************/
/**
* Action to invert the selection's case.
*/
public static class InvertSelectionCaseAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = 8208747415774653068L;
public InvertSelectionCaseAction() {
super(rtaInvertSelectionCaseAction);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
String selection = textArea.getSelectedText();
if (selection!=null) {
StringBuffer buffer = new StringBuffer(selection);
int length = buffer.length();
for (int i=0; i<length; i++) {
char c = buffer.charAt(i);
if (Character.isUpperCase(c))
buffer.setCharAt(i, Character.toLowerCase(c));
else if (Character.isLowerCase(c))
buffer.setCharAt(i, Character.toUpperCase(c));
}
textArea.replaceSelection(buffer.toString());
}
textArea.requestFocusInWindow();
}
public final String getMacroID() {
return getName();
}
}
/*****************************************************************************/
/**
* Action to join the current line and the following line.
*/
public static class JoinLinesAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = -4166366494194652388L;
public JoinLinesAction() {
super(rtaJoinLinesAction);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
try {
Caret c = textArea.getCaret();
int caretPos = c.getDot();
Document doc = textArea.getDocument();
Element map = doc.getDefaultRootElement();
int lineCount = map.getElementCount();
int line = map.getElementIndex(caretPos);
if (line==lineCount-1) {
UIManager.getLookAndFeel().
provideErrorFeedback(textArea);
return;
}
Element lineElem = map.getElement(line);
caretPos = lineElem.getEndOffset() - 1;
c.setDot(caretPos); // Gets rid of any selection.
doc.remove(caretPos, 1); // Should be '\n'.
} catch (BadLocationException ble) {
/* Shouldn't ever happen. */
ble.printStackTrace();
}
textArea.requestFocusInWindow();
}
public final String getMacroID() {
return getName();
}
}
/*****************************************************************************/
/**
* Action to make the selection lower-case.
*/
public static class LowerSelectionCaseAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = 1588380231609053820L;
public LowerSelectionCaseAction() {
super(rtaLowerSelectionCaseAction);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
String selection = textArea.getSelectedText();
if (selection!=null)
textArea.replaceSelection(selection.toLowerCase());
textArea.requestFocusInWindow();
}
public final String getMacroID() {
return getName();
}
}
/*****************************************************************************/
/**
* Action to move the selection and/or caret. Constructor indicates
* direction to use.
*/
public static class NextVisualPositionAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = 8304908230171251231L;
private boolean select;
private int direction;
public NextVisualPositionAction(String nm, boolean select, int direction) {
super(nm);
this.select = select;
this.direction = direction;
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
Caret caret = textArea.getCaret();
int dot = caret.getDot();
/*
* Move to the beginning/end of selection on a "non-shifted"
* left- or right-keypress. We shouldn't have to worry about
* navigation filters as, if one is being used, it let us get
* to that position before.
*/
if (!select) {
switch (direction) {
case SwingConstants.EAST:
int mark = caret.getMark();
if (dot!=mark) {
caret.setDot(Math.max(dot, mark));
return;
}
break;
case SwingConstants.WEST:
mark = caret.getMark();
if (dot!=mark) {
caret.setDot(Math.min(dot, mark));
return;
}
break;
default:
}
}
Position.Bias[] bias = new Position.Bias[1];
Point magicPosition = caret.getMagicCaretPosition();
try {
if(magicPosition == null &&
(direction == SwingConstants.NORTH ||
direction == SwingConstants.SOUTH)) {
Rectangle r = textArea.modelToView(dot);
magicPosition = new Point(r.x, r.y);
}
NavigationFilter filter = textArea.getNavigationFilter();
if (filter != null) {
dot = filter.getNextVisualPositionFrom(textArea, dot,
Position.Bias.Forward, direction, bias);
}
else {
dot = textArea.getUI().getNextVisualPositionFrom(
textArea, dot,
Position.Bias.Forward, direction, bias);
}
if (select)
caret.moveDot(dot);
else
caret.setDot(dot);
if(magicPosition != null &&
(direction == SwingConstants.NORTH ||
direction == SwingConstants.SOUTH)) {
caret.setMagicCaretPosition(magicPosition);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
}
public final String getMacroID() {
return getName();
}
}
/*****************************************************************************/
/**
* Positions the caret at the next word.
*/
public static class NextWordAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = 8518249080050357245L;
private boolean select;
public NextWordAction(String name, boolean select) {
super(name);
this.select = select;
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
int offs = textArea.getCaretPosition();
int oldOffs = offs;
// FIXME: Replace Utilities call with custom version to
// cut down on all of the modelToViews, as each call causes
// a getTokenList => expensive!
Element curPara =
Utilities.getParagraphElement(textArea, offs);
try {
offs = Utilities.getNextWord(textArea, offs);
if(offs >= curPara.getEndOffset() &&
oldOffs != curPara.getEndOffset() - 1) {
// we should first move to the end of current
// paragraph (bug #4278839)
offs = curPara.getEndOffset() - 1;
}
} catch (BadLocationException bl) {
int end = textArea.getDocument().getLength();
if (offs != end) {
if(oldOffs != curPara.getEndOffset() - 1)
offs = curPara.getEndOffset() - 1;
else
offs = end;
}
}
if (select)
textArea.moveCaretPosition(offs);
else
textArea.setCaretPosition(offs);
}
public final String getMacroID() {
return getName();
}
}
/*****************************************************************************/
/**
* Pages one view to the left or right.
*/
static class PageAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = -4848291526908497860L;
private boolean select;
private boolean left;
public PageAction(String name, boolean left, boolean select) {
super(name);
this.select = select;
this.left = left;
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
int selectedIndex;
Rectangle visible = new Rectangle();
textArea.computeVisibleRect(visible);
if (left)
visible.x = Math.max(0, visible.x - visible.width);
else
visible.x += visible.width;
selectedIndex = textArea.getCaretPosition();
if(selectedIndex != -1) {
if (left) {
selectedIndex = textArea.viewToModel(
new Point(visible.x, visible.y));
}
else {
selectedIndex = textArea.viewToModel(
new Point(visible.x + visible.width - 1,
visible.y + visible.height - 1));
}
Document doc = textArea.getDocument();
if ((selectedIndex != 0) &&
(selectedIndex > (doc.getLength()-1))) {
selectedIndex = doc.getLength()-1;
}
else if(selectedIndex < 0) {
selectedIndex = 0;
}
if (select)
textArea.moveCaretPosition(selectedIndex);
else
textArea.setCaretPosition(selectedIndex);
}
}
public final String getMacroID() {
return getName();
}
}
/*****************************************************************************/
/**
* Action for pasting text.
*/
public static class PasteAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = 1224348409192617519L;
public PasteAction() {
super(DefaultEditorKit.pasteAction);
}
public PasteAction(String name, Icon icon, String desc,
Integer mnemonic, KeyStroke accelerator) {
super(name, icon, desc, mnemonic, accelerator);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
textArea.paste();
textArea.requestFocusInWindow();
}
public final String getMacroID() {
return DefaultEditorKit.pasteAction;
}
}
/*****************************************************************************/
/**
* "Plays back" the last macro recorded.
*/
public static class PlaybackLastMacroAction extends RecordableTextAction {
/**
*
*/
private static final long serialVersionUID = -2722916895527181020L;
public PlaybackLastMacroAction() {
super(rtaPlaybackLastMacroAction);
}
public PlaybackLastMacroAction(String name, Icon icon,
String desc, Integer mnemonic, KeyStroke accelerator) {
super(name, icon, desc, mnemonic, accelerator);
}
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
textArea.playbackLastMacro();
}
public boolean isRecordable() {
return false; // Don't record macro playbacks.
}
public final String getMacroID() {
return rtaPlaybackLastMacroAction;
}
}
/*****************************************************************************/
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -