jedittextarea.java
来自「java写的多功能文件编辑器」· Java 代码 · 共 2,487 行 · 第 1/5 页
JAVA
2,487 行
/** * Sets the caret position. The new selection will consist of the * caret position only (hence no text will be selected) * @param caret The caret position * @see #select(int,int) */ public final void setCaretPosition(int caret) { select(caret, caret); } /** * Selects all text in the document. */ public final void selectAll() { select(0, getDocumentLength()); } /** * Moves the mark to the caret position. */ public final void selectNone() { select(getCaretPosition(), getCaretPosition()); } /** * Selects from the start offset to the end offset. This is the * general selection method used by all other selecting methods. * The caret position will be start if start < end, and end * if end > start. * @param start The start offset * @param end The end offset */ public void select(int start, int end) { int newStart, newEnd; boolean newBias; if (start <= end) { newStart = start; newEnd = end; newBias = false; } else { newStart = end; newEnd = start; newBias = true; } if (newStart < 0 || newEnd > getDocumentLength()) { throw new IllegalArgumentException("Bounds out of" + " range: " + newStart + "," + newEnd); } // If the new position is the same as the old, we don't // do all this crap, however we still do the stuff at // the end (clearing magic position, scrolling) if (newStart != selectionStart || newEnd != selectionEnd || newBias != biasLeft) { updateBracketHighlight(end); int newStartLine = getLineOfOffset(newStart); int newEndLine = getLineOfOffset(newEnd); painter.invalidateLineRange(selectionStartLine, selectionEndLine); painter.invalidateLineRange(newStartLine, newEndLine); document.addUndoableEdit(new CaretUndo(selectionStart, selectionEnd, newStart, newEnd)); selectionStart = newStart; selectionEnd = newEnd; selectionStartLine = newStartLine; selectionEndLine = newEndLine; biasLeft = newBias; gutter.repaint(); fireCaretEvent(); } // When the user is typing, etc, we don't want the caret // to blink blink = true; caretTimer.restart(); // Disable rectangle select if selection start = selection end if (selectionStart == selectionEnd) rectSelect = false; // Clear the `magic' caret position used by up/down magicCaret = -1; scrollToCaret(); } /** * Returns the selected text, or null if no selection is active. */ public final String getSelectedText() { if (selectionStart == selectionEnd) return null; if (rectSelect) { // Return each row of the selection on a new line Element map = document.getDefaultRootElement(); int start = selectionStart - map.getElement(selectionStartLine).getStartOffset(); int end = selectionEnd - map.getElement(selectionEndLine).getStartOffset(); // Certain rectangles satisfy this condition... if (end < start) { int tmp = end; end = start; start = tmp; } StringBuffer buf = new StringBuffer(); Segment seg = new Segment(); for (int i = selectionStartLine; i <= selectionEndLine; i++) { Element lineElement = map.getElement(i); int lineStart = lineElement.getStartOffset(); int lineEnd = lineElement.getEndOffset() - 1; int lineLen = lineEnd - lineStart; lineStart = Math.min(lineStart + start, lineEnd); lineLen = Math.min(end - start, lineEnd - lineStart); getText(lineStart, lineLen, seg); buf.append(seg.array, seg.offset, seg.count); if (i != selectionEndLine) buf.append('\n'); } return buf.toString(); } else { return getText(selectionStart, selectionEnd - selectionStart); } } /** * Replaces the selection with the specified text. * @param selectedText The replacement text for the selection */ public void setSelectedText(String selectedText) { if (!editable) { throw new InternalError("Text component" + " read only"); } document.beginCompoundEdit(); try { if (rectSelect) { Element map = document.getDefaultRootElement(); int start = selectionStart - map.getElement(selectionStartLine).getStartOffset(); int end = selectionEnd - map.getElement(selectionEndLine).getStartOffset(); // Certain rectangles satisfy this condition... if (end < start) { int tmp = end; end = start; start = tmp; } int lastNewline = 0; int currNewline = 0; for (int i = selectionStartLine; i <= selectionEndLine; i++) { Element lineElement = map.getElement(i); int lineStart = lineElement.getStartOffset(); int lineEnd = lineElement.getEndOffset() - 1; int rectStart = Math.min(lineEnd, lineStart + start); document.remove(rectStart, Math.min(lineEnd - rectStart, end - start)); if (selectedText == null) continue; currNewline = selectedText.indexOf('\n',lastNewline); if (currNewline == -1) currNewline = selectedText.length(); document.insertString(rectStart, selectedText .substring(lastNewline, currNewline), null); lastNewline = Math.min(selectedText.length(), currNewline + 1); } if (selectedText != null && currNewline != selectedText.length()) { int offset = map.getElement(selectionEndLine).getEndOffset() - 1; document.insertString(offset, "\n",null); document.insertString(offset + 1, selectedText .substring(currNewline + 1), null); } } else { document.remove(selectionStart, selectionEnd - selectionStart); if (selectedText != null) { document.insertString(selectionStart, selectedText, null); } } } catch (BadLocationException bl) { bl.printStackTrace(); throw new InternalError("Cannot replace" + " selection"); } finally { document.endCompoundEdit(); } setCaretPosition(selectionEnd); } /** * Returns true if this text area is editable, false otherwise. */ public final boolean isEditable() { return editable; } /** * Sets if this component is editable. * @param editable True if this text area should be editable, * false otherwise */ public final void setEditable(boolean editable) { this.editable = editable; } /** * Returns the right click popup menu. */ public final JPopupMenu getRightClickPopup() { return popup; } /** * Sets the right click popup menu. * @param popup The popup */ public final void setRightClickPopup(JPopupMenu popup) { this.popup = popup; } /** * Returns the `magic' caret position. This can be used to preserve * the column position when moving up and down lines. */ public final int getMagicCaretPosition() { return magicCaret; } /** * Sets the `magic' caret position. This can be used to preserve * the column position when moving up and down lines. * @param magicCaret The magic caret position */ public final void setMagicCaretPosition(int magicCaret) { this.magicCaret = magicCaret; } /** * Similar to <code>setSelectedText()</code>, but overstrikes the * appropriate number of characters if overwrite mode is enabled. * @param str The string * @see #setSelectedText(String) * @see #isOverwriteEnabled() */ public void overwriteSetSelectedText(String str) { // Don't overstrike if there is a selection if (!overwrite || selectionStart != selectionEnd) { setSelectedText(str); return; } // Don't overstrike if we're on the end of // the line int caret = getCaretPosition(); int caretLineEnd = getLineEndOffset(getCaretLine()); if (caretLineEnd - caret <= str.length()) { setSelectedText(str); return; } document.beginCompoundEdit(); try { document.remove(caret, str.length()); document.insertString(caret, str, null); } catch (BadLocationException bl) { bl.printStackTrace(); } finally { document.endCompoundEdit(); } } /** * Returns true if overwrite mode is enabled, false otherwise. */ public final boolean isOverwriteEnabled() { return overwrite; } /** * Sets if overwrite mode should be enabled. * @param overwrite True if overwrite mode should be enabled, * false otherwise. */ public final void setOverwriteEnabled(boolean overwrite) { this.overwrite = overwrite; painter.invalidateSelectedLines(); } /** * Returns true if the selection is rectangular, false otherwise. */ public final boolean isSelectionRectangular() { return rectSelect; } /** * Sets if the selection should be rectangular. * @param overwrite True if the selection should be rectangular, * false otherwise. */ public final void setSelectionRectangular(boolean rectSelect) { this.rectSelect = rectSelect; painter.invalidateSelectedLines(); } /** * Returns the position of the highlighted bracket (the bracket * matching the one before the caret) */ public final int getBracketPosition() { return bracketPosition; } /** * Returns the line of the highlighted bracket (the bracket * matching the one before the caret) */ public final int getBracketLine() { return bracketLine; } /** * Adds a caret change listener to this text area. * @param listener The listener */ public final void addCaretListener(CaretListener listener) { listenerList.add(CaretListener.class, listener); } /** * Removes a caret change listener from this text area. * @param listener The listener */ public final void removeCaretListener(CaretListener listener) { listenerList.remove(CaretListener.class, listener); } public void appendCut() { if (editable) { appendCopy(); if (selectionStart == selectionEnd) { int line = getCaretLine(); int start = getLineStartOffset(line); int end = getLineEndOffset(line); if (end == document.getLength() + 1) end--; try { document.remove(start, end - start); } catch (BadLocationException ble) { } } else setSelectedText(""); } } /** * Deletes the selected text from the text area and places it * into the clipboard. */ public void cut() { if (editable) { copy(); if (selectionStart == selectionEnd) { int line = getCaretLine(); int start = getLineStartOffset(line); int end = getLineEndOffset(line); if (end == document.getLength() + 1) end--; try { document.remove(start, end - start); } catch (BadLocationException ble) { } } else setSelectedText(""); } } public void appendCopy() { String selection; if (selectionStart == selectionEnd) { int line = getCaretLine(); int start = getLineStartOffset(line); int end = getLineEndOffset(line); selection = getText(start, end - start); setSelectionStart(start); setSelectionEnd(start); } else selection = getSelectedText(); Clipboard clipboard = getToolkit().getSystemClipboard(); try { String clipped = ((String) clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor)).replace('\r','\n'); clipboard.setContents(new StringSelection(clipped + selection), null); } catch (Exception e) { clipboard.setContents(new StringSelection(selection), null); } } /** * Places the selected text into the clipboard. */ public void copy() { String selection; if (selectionStart == selectionEnd) { int line = getCaretLine(); int start = getLineStartOffset(line); int end = getLineEndOffset(line); selection = getText(start, end - start); setSelectionStart(start); setSelectionEnd(start); } else selection = getSelectedText(); Clipboard clipboard = getToolkit().getSystemClipboard(); clipboard.setContents(new StringSelection(selection), null); } /** * Inserts the clipboard contents into the text. */ public void paste() { if (editable) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?