📄 rtextarea.java
字号:
* tabs.
* @param regex Whether <code>toMark</code> is a Java regular expression.
* @return The number of matches marked.
* @see #clearMarkAllHighlights
* @see #getMarkAllHighlightColor
* @see #setMarkAllHighlightColor
*/
public int markAll(String toMark, boolean matchCase, boolean wholeWord,
boolean regex) {
Highlighter h = getHighlighter();
int numMarked = 0;
if (toMark != null && !toMark.equals(markedWord) && h != null) {
if (markAllHighlights != null) {
clearMarkAllHighlights();
}
else {
markAllHighlights = new ArrayList(10);
}
int caretPos = getCaretPosition();
markedWord = toMark;
setCaretPosition(0);
boolean found = find(toMark, true, matchCase, wholeWord, regex);
while (found) {
int start = getSelectionStart();
int end = getSelectionEnd();
try {
markAllHighlights.add(h.addHighlight(start, end,
markAllHighlightPainter));
}
catch (BadLocationException ble) {
ble.printStackTrace();
}
numMarked++;
found = find(toMark, true, matchCase, wholeWord, regex);
}
setCaretPosition(caretPos);
repaint();
}
return numMarked;
}
/*****************************************************************************/
/**
* "Plays back" the last recorded macro in this text area.
*/
public synchronized void playbackLastMacro() {
if (currentMacro != null) {
Action[] actions = getActions();
int numActions = actions.length;
List macroRecords = currentMacro.getMacroRecords();
int num = macroRecords.size();
if (num > 0) {
undoManager.beginInternalAtomicEdit();
for (int i = 0; i < num; i++) {
MacroRecord record = (MacroRecord) macroRecords.get(i);
for (int j = 0; j < numActions; j++) {
if ( (actions[j] instanceof RecordableTextAction) &&
record.id.equals(
( (RecordableTextAction) actions[j]).getMacroID())) {
actions[j].actionPerformed(
new ActionEvent(this,
ActionEvent.ACTION_PERFORMED,
record.actionCommand));
break;
}
}
}
undoManager.endInternalAtomicEdit();
}
}
}
/*****************************************************************************/
/**
* Method called when it's time to print this badboy (the oldschool,
* AWT way).
*
* @param graphics The context into which the page is drawn.
* @param pageFormat The size and orientation of the page being drawn.
* @param pageIndex The zero based index of the page to be drawn.
*/
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
//return RPrintUtilities.printDocumentMonospaced(graphics, this.getDocument(), 10, pageIndex, pageFormat, this.getTabSize());
//return RPrintUtilities.printDocumentMonospacedWordWrap(graphics, this.getDocument(), 10, pageIndex, pageFormat, this.getTabSize());
return RPrintUtilities.printDocumentWordWrap(graphics, this, this.getFont(),
pageIndex, pageFormat,
this.getTabSize());
}
/*****************************************************************************/
/**
* We override this method because the super version gives us an entirely
* new <code>Document</code>, thus requiring us to re-attach our Undo
* manager. With this version we just replace the text.
*/
public void read(Reader in, Object desc) throws IOException {
RTextAreaEditorKit kit = (RTextAreaEditorKit) getUI().
getEditorKit(this);
setText(null);
Document doc = getDocument();
if (desc != null) {
doc.putProperty(Document.StreamDescriptionProperty, desc);
}
try {
// NOTE: Resets the "line terminator" property of the document.
kit.read(in, doc, 0);
}
catch (BadLocationException e) {
throw new IOException(e.getMessage());
}
}
/*****************************************************************************/
// Attempt to redo an "action" from the RTextEditorPane.
public void redoLastAction() {
// NOTE: The try/catch block shouldn't be necessary...
try {
if (undoManager.canRedo()) {
undoManager.redo();
}
}
catch (CannotRedoException f) {
f.printStackTrace();
}
}
/*****************************************************************************/
/**
* Replaces text from the indicated start to end position with the
* new text specified. Does nothing if the model is null. Simply
* does a delete if the new string is null or empty.
* <p>
* This method is thread safe, although most Swing methods
* are not.<p>
* This method is overridden so that our Undo manager remembers it as a
* single operation (it has trouble with this, especially for
* <code>RSyntaxTextArea</code> and the "auto-indent" feature).
*
* @param str the text to use as the replacement
* @param start the start position >= 0
* @param end the end position >= start
* @exception IllegalArgumentException if part of the range is an
* invalid position in the model
* @see #insert
* @see #replaceRange
*/
public void replaceRange(String str, int start, int end) {
if (end < start) {
throw new IllegalArgumentException("end before start");
}
Document doc = getDocument();
if (doc != null) {
try {
// Without this, in some cases we'll have to do two undos
// for one logical operation (for example, try editing a
// Java source file in an RSyntaxTextArea, and moving a line
// with text already on it down via Enter. Without this
// line, doing a single "undo" moves all later text up,
// but the first line moved down isn't there! Doing a
// second undo puts it back.
undoManager.beginInternalAtomicEdit();
( (RTextAreaDocument) doc).replace(start, end - start,
str, null);
}
catch (BadLocationException e) {
throw new IllegalArgumentException(e.getMessage());
}
finally {
undoManager.endInternalAtomicEdit();
}
}
}
/*****************************************************************************/
/**
* This method overrides <code>JTextComponent</code>'s
* <code>replaceSelection</code>, so that if <code>textMode</code> is
* <code>OVERWRITE_MODE</code>, it actually overwrites.
*
* @param text The content to replace the selection with.
*/
public void replaceSelection(String text) {
// It's legal for null to be used here...
if (text == null) {
super.replaceSelection(text);
return;
}
int caretPosition = getCaretPosition();
int textLength = text.length();
if (getTabsEmulated() && text.indexOf('\t') > -1) {
text = replaceTabsWithSpaces(text);
// If the user wants to overwrite text...
}
if (textMode == OVERWRITE_MODE) {
try {
int currentLine = getLineOfOffset(caretPosition);
int lastLine = getLineCount() - 1;
// If the user hit Enter, just go to the next line.
if (text.equals("\n")) {
if (currentLine == lastLine) {
setCaretPosition(getLineEndOffset(currentLine));
super.replaceSelection(text); // Okay because undoManager will still work, as it's definitely not a replace.
}
else {
super.setCaretPosition(getLineStartOffset(currentLine + 1));
}
return;
} // End of if (text.equals("\n")).
// If we're not at the end of a line, select the characters that will be
// overwritten (otherwise JTextArea will simply insert in front of them).
int currentLineEnd = getLineEndOffset(currentLine);
if (caretPosition == getCaret().getMark() &&
caretPosition != currentLineEnd) { //!getText(caretPosition,1).equals("\n")) {
if (currentLine == lastLine) {
caretPosition = Math.min(caretPosition + textLength, currentLineEnd);
}
else {
caretPosition = Math.min(caretPosition + textLength,
currentLineEnd - 1);
}
moveCaretPosition(caretPosition);
}
}
catch (BadLocationException ble) {
/* This should never happen. */
UIManager.getLookAndFeel().provideErrorFeedback(RTextArea.this);
ble.printStackTrace();
}
} // End of if (textMode==OVERWRITE_MODE).
// Now, actually do the inserting/replacing. Our undoManager will
// take care of remembering the remove/insert as atomic if we are in
// overwrite mode.
handleReplaceSelection(text);
}
/*****************************************************************************/
/**
* Replaces all instances of the tab character in <code>text</code> with
* the number of spaces equivalent to a tab in this text area.
*
* @param text The <code>java.lang.String</code> in which to replace tabs
* with spaces.
* @return A <code>java.lang.String</code> just like <code>text</code>, but
* with spaces instead of tabs.
*/
protected final String replaceTabsWithSpaces(final String text) {
String tabText = "";
int tabSize = getTabSize();
for (int i = 0; i < tabSize; i++) {
tabText += " ";
}
return text.replaceAll("\t", tabText);
}
/*****************************************************************************/
/**
* Sets the properties of one of the actions this text area owns.
*
* @param action The action to modify; for example,
* <code>CUT_ACTION</code>.
* @param name The new name for the action.
* @param mnemonic The new mnemonic for the action.
* @param accelerator The new accelerator key for the action.
*/
public static void setActionProperties(int action, String name,
char mnemonic, KeyStroke accelerator) {
setActionProperties(action, name, new Integer(mnemonic), accelerator);
}
/*****************************************************************************/
/**
* Sets the properties of one of the actions this text area owns.
*
* @param action The action to modify; for example,
* <code>CUT_ACTION</code>.
* @param name The new name for the action.
* @param mnemonic The new mnemonic for the action.
* @param accelerator The new accelerator key for the action.
*/
public static void setActionProperties(int action, String name,
Integer mnemonic,
KeyStroke accelerator) {
Action tempAction = null;
switch (action) {
case CUT_ACTION:
tempAction = cutAction;
break;
case COPY_ACTION:
tempAction = copyAction;
break;
case PASTE_ACTION:
tempAction = pasteAction;
break;
case DELETE_ACTION:
tempAction = deleteAction;
break;
case SELECT_ALL_ACTION:
tempAction = selectAllAction;
break;
case UNDO_ACTION:
case REDO_ACTION:
default:
return;
}
tempAction.putValue(Action.NAME, name);
tempAction.putValue(Action.SHORT_DESCRIPTION, name);
tempAction.putValue(Action.ACCELERATOR_KEY, accelerator);
tempAction.putValue(Action.MNEMONIC_KEY, mnemonic);
}
/*****************************************************************************/
/**
* This method is overridden to make sure that instances of
* <code>RTextArea</code> only use <code>ConfigurableCaret</code>s.
* To set the style of caret (vertical line, block, etc.) used for
* insert or overwrite mode, use <code>setCaretStyle</code>.
*
* @param caret The caret to use. If this is not an instance of
* <code>ConfigurableCaret</code>, an exception is thrown.
* @throws IllegalArgumentException If the specified caret is not an
* <code>ConfigurableCaret</code>.
* @see #setCaretStyle
*/
public void setCaret(Caret caret) {
if (! (caret instanceof ConfigurableCaret)) {
throw new IllegalArgumentException(
"RTextArea needs ConfigurableCaret");
}
super.setCaret(caret);
}
/*****************************************************************************/
/**
* Sets the style of caret used when in insert or overwrite mode.
*
* @param mode Either <code>INSERT_MODE</code> or
* <code>OVERWRITE_MODE</code>.
* @param style The style for the caret (such as
* <code>ConfigurableCaret.VERTICAL_LINE_STYLE</code>).
* @see org.fife.ui.rtextarea.ConfigurableCaret
*/
public void setCaretStyle(int mode, int style) {
style = (style >= ConfigurableCaret.MIN_STYLE &&
style <= ConfigurableCaret.MAX_STYLE ?
style :
ConfigurableCaret.VERTICAL_LINE_STYLE);
carets[mode] = style;
if (mode == getTextMode()) {
// Will repaint the caret if necessary.
( (ConfigurableCaret) getCaret()).setStyle(style);
}
}
/*****************************************************************************/
/**
* Sets the document used by this text area.
*
* @param document The new document to use.
* @throws IllegalArgumentException If the document is not an instance of
* <code>RTextAreaDocument</code>.
*/
public void setDocument(Document document) {
if (! (document instanceof RTextAreaDocument)) {
throw new IllegalArgumentException("RTextArea requires " +
"instances of RTextAreaDocument for its document!");
}
super.setDocument(document);
}
/*****************************************************************************/
/**
* Sets the path in which to find images to associate with the editor's
* actions. The path MUST contain the following images (with the
* appropriate extension as defined by the icon group):<br>
* <ul>
* <li>cut</li>
* <li>copy</li>
* <li>paste</li>
* <li>delete</li>
* <li>undo</li>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -