📄 jedittextarea.java
字号:
{ select(start,end,true); } /** * @deprecated Instead, call either <code>addToSelection()</code>, * or <code>setSelection()</code> with a new Selection instance. */ public void select(int start, int end, boolean doElectricScroll) { selectNone(); int newStart, newEnd; if(start < end) { newStart = start; newEnd = end; } else { newStart = end; newEnd = start; } setSelection(new Selection.Range(newStart,newEnd)); moveCaretPosition(end,doElectricScroll); } /** * @deprecated Instead, check if the appropriate Selection * is an instance of the Selection.Rect class. */ public boolean isSelectionRectangular() { Selection s = getSelectionAtOffset(caret); if(s == null) return false; else return (s instanceof Selection.Rect); } // OLD SELECTION API ENDS HERE /** * Sets the caret position and deactivates the selection. * @param caret The caret position */ public void setCaretPosition(int newCaret) { invalidateSelectedLines(); selection.removeAllElements(); moveCaretPosition(newCaret,true); } /** * Sets the caret position and deactivates the selection. * @param caret The caret position * @param doElectricScroll Do electric scrolling? */ public void setCaretPosition(int newCaret, boolean doElectricScroll) { invalidateSelectedLines(); selection.removeAllElements(); moveCaretPosition(newCaret,doElectricScroll); } /** * Sets the caret position without deactivating the selection. * @param caret The caret position */ public void moveCaretPosition(int newCaret) { moveCaretPosition(newCaret,true); } /** * Sets the caret position without deactivating the selection. * @param caret The caret position * @param doElectricScroll Do electric scrolling? */ public void moveCaretPosition(int newCaret, boolean doElectricScroll) { if(newCaret < 0 || newCaret > buffer.getLength()) { throw new IllegalArgumentException("caret out of bounds: " + newCaret); } // When the user is typing, etc, we don't want the caret // to blink blink = true; caretTimer.restart(); if(caret == newCaret) { // so that C+y <marker>, for example, will return // to the saved location even if the caret was // never moved but the user scrolled instead scrollToCaret(doElectricScroll); return; } int newCaretLine = getLineOfOffset(newCaret); magicCaret = offsetToX(newCaretLine,newCaret - getLineStartOffset(newCaretLine)); // call invalidateLine() twice, as opposed to calling // invalidateLineRange(), because invalidateLineRange() // doesn't handle start > end invalidateLine(caretLine); invalidateLine(newCaretLine); buffer.addUndoableEdit(new CaretUndo(caret)); caret = newCaret; caretLine = newCaretLine; if(focusedComponent == this) scrollToCaret(doElectricScroll); updateBracketHighlight(); fireCaretEvent(); } /** * Returns the caret position. */ public int getCaretPosition() { return caret; } /** * Returns the line number containing the caret. */ public int getCaretLine() { return caretLine; } /** * Returns the number of selections. This is primarily for use by the * the status bar. * @since jEdit 3.2pre2 */ public int getSelectionCount() { return selection.size(); } /** * Returns the current selection. * @since jEdit 3.2pre1 */ public Selection[] getSelection() { Selection[] sel = new Selection[selection.size()]; selection.copyInto(sel); return sel; } /** * Deselects everything. */ public void selectNone() { setSelection((Selection)null); } /** * Sets the selection. * @param selection The new selection * since jEdit 3.2pre1 */ public void setSelection(Selection[] selection) { // invalidate the old selection invalidateSelectedLines(); this.selection.removeAllElements(); if(selection != null) { for(int i = 0; i < selection.length; i++) _addToSelection(selection[i]); } fireCaretEvent(); } /** * Sets the selection. * @param selection The new selection * since jEdit 3.2pre1 */ public void setSelection(Selection selection) { invalidateSelectedLines(); this.selection.removeAllElements(); if(selection != null) _addToSelection(selection); fireCaretEvent(); } /** * Adds to the selection. * @param selection The new selection * since jEdit 3.2pre1 */ public void addToSelection(Selection[] selection) { if(selection != null) { for(int i = 0; i < selection.length; i++) _addToSelection(selection[i]); } fireCaretEvent(); } /** * Adds to the selection. * @param selection The new selection * since jEdit 3.2pre1 */ public void addToSelection(Selection selection) { _addToSelection(selection); fireCaretEvent(); } /** * Returns the selection containing the specific offset, or null * if there is no selection at that offset. * @param offset The offset * @since jEdit 3.2pre1 */ public Selection getSelectionAtOffset(int offset) { if(selection != null) { for(int i = 0; i < selection.size(); i++) { Selection s = (Selection)selection.elementAt(i); if(offset >= s.start && offset <= s.end) return s; } } return null; } /** * Deactivates the specified selection. * @param s The selection * @since jEdit 3.2pre1 */ public void removeFromSelection(Selection sel) { selection.removeElement(sel); invalidateLineRange(sel.startLine,sel.endLine); fireCaretEvent(); } /** * Deactivates the selection at the specified offset. If there is * no selection at that offset, does nothing. * @param offset The offset * @since jEdit 3.2pre1 */ public void removeFromSelection(int offset) { Selection sel = getSelectionAtOffset(offset); if(sel == null) return; selection.removeElement(sel); invalidateLineRange(sel.startLine,sel.endLine); fireCaretEvent(); } /** * Resizes the selection at the specified offset, or creates a new * one if there is no selection at the specified offset. This is a * utility method that is mainly useful in the mouse event handler * because it handles the case of end being before offset gracefully * (unlike the rest of the selection API). * @param offset The offset * @param end The new selection end * @param rect Make the selection rectangular? * @since jEdit 3.2pre1 */ public void resizeSelection(int offset, int end, boolean rect) { Selection s = getSelectionAtOffset(offset); if(s != null) { invalidateLineRange(s.startLine,s.endLine); selection.removeElement(s); } if(end < offset) { int tmp = offset; offset = end; end = tmp; } Selection newSel; if(rect) newSel = new Selection.Rect(offset,end); else newSel = new Selection.Range(offset,end); _addToSelection(newSel); fireCaretEvent(); } /** * Extends the selection at the specified offset, or creates a new * one if there is no selection at the specified offset. This is * different from resizing in that the new chunk is added to the * selection in question, instead of replacing it. * @param offset The offset * @param end The new selection end * @param rect Make the selection rectangular? * @since jEdit 3.2pre1 */ public void extendSelection(int offset, int end) { Selection s = getSelectionAtOffset(offset); if(s != null) { invalidateLineRange(s.startLine,s.endLine); selection.removeElement(s); if(offset == s.start) { offset = end; end = s.end; } else if(offset == s.end) { offset = s.start; } } if(end < offset) { int tmp = end; end = offset; offset = tmp; } _addToSelection(new Selection.Range(offset,end)); fireCaretEvent(); } /** * Returns the text in the specified selection. * @param s The selection * @since jEdit 3.2pre1 */ public String getSelectedText(Selection s) { StringBuffer buf = new StringBuffer(); getSelectedText(s,buf); return buf.toString(); } /** * Returns the text in all active selections. * @param separator The string to insert between each text chunk * (for example, a newline) * @since jEdit 3.2pre1 */ public String getSelectedText(String separator) { if(selection.size() == 0) return null; StringBuffer buf = new StringBuffer(); for(int i = 0; i < selection.size(); i++) { if(i != 0) buf.append(separator); getSelectedText((Selection)selection.elementAt(i),buf); } return buf.toString(); } /** * Returns the text in all active selections, with a newline * between each text chunk. */ public String getSelectedText() { return getSelectedText("\n"); } /** * Replaces the selection with the specified text. * @param s The selection * @param selectedText The new text * @since jEdit 3.2pre1 */ public void setSelectedText(Selection s, String selectedText) { if(!isEditable()) { throw new InternalError("Text component" + " read only"); } try { buffer.beginCompoundEdit(); if(s instanceof Selection.Rect) { Element map = buffer.getDefaultRootElement(); int start = s.start - map.getElement(s.startLine) .getStartOffset(); int end = s.end - map.getElement(s.endLine) .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 = s.startLine; i <= s.endLine; i++) { Element lineElement = map.getElement(i); int lineStart = lineElement.getStartOffset(); int lineEnd = lineElement.getEndOffset() - 1; int rectStart = Math.min(lineEnd,lineStart + start); buffer.remove(rectStart,Math.min(lineEnd - rectStart, end - start)); if(selectedText == null) continue; currNewline = selectedText.indexOf('\n',lastNewline); if(currNewline == -1) currNewline = selectedText.length(); buffer.insertString(rectStart,selectedText .substring(lastNewline,currNewline),null); lastNewline = Math.min(selectedText.length(), currNewline + 1); } if(selectedText != null && currNewline != selectedText.length()) { int offset = map.getElement(s.endLine) .getEndOffset() - 1; buffer.insertString(offset,"\n",null); buffer.insertString(offset + 1,selectedText .substring(currNewline + 1),null); } } else { buffer.remove(s.start,s.end - s.start); if(selectedText != null && selectedText.length() != 0) { buffer.insertString(s.start, selectedText,null); } } } catch(BadLocationException bl) { Log.log(Log.ERROR,this,bl); } // No matter what happends... stops us from leaving buffer // in a bad state finally { buffer.endCompoundEdit(); } // no no no!!!! //selectNone(); } /** * Replaces the selection at the caret with the specified text. * If there is no selection at the caret, the text is inserted at * the caret position. */ public void setSelectedText(String selectedText) { if(!isEditable()) { throw new InternalError("Text component" + " read only"); } Selection[] selection = getSelection(); if(selection.length == 0) { // for compatibility with older jEdit versions try { buffer.insertString(caret,selectedText,null); } catch(BadLocationException bl) { Log.log(Log.ERROR,this,bl); } } else { try { buffer.beginCompoundEdit(); for(int i = 0; i < selection.length; i++) { setSelectedText(selection[i],selectedText); } } finally { buffer.endCompoundEdit(); } } selectNone(); } /** * Returns an array of all line numbers that contain a selection. * This array will also include the line number containing the * caret, for convinience. * @since jEdit 3.2pre1 */ public int[] getSelectedLines() { Integer line; // this algorithm sucks Hashtable hash = new Hashtable(); for(int i = 0; i < selection.size(); i++) { Selection s = (Selection)selection.elementAt(i); for(int j = s.startLine; j <= s.endLine; j++) { line = new Integer(j); hash.put(line,line); } } line = new Integer(caretLine); hash.put(line,line); int[] returnValue = new int[hash.size()]; int i = 0; Enumeration keys = hash.keys(); while(keys.hasMoreElements()) { line = (Integer)keys.nextElement(); returnValue[i++] = line.intValue(); } quicksort(returnValue,0,returnValue.length - 1); return returnValue; } /** * Returns true if this text area is editable, false otherwise. */ public final boolean isEditable() { return buffer.isEditable(); } /** * 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -