📄 jedittextarea.java
字号:
protected void updateBracketHighlight(int newCaretPosition) { if(newCaretPosition == 0) { bracketPosition = bracketLine = -1; return; } try { int offset = TextUtilities.findMatchingBracket( document,newCaretPosition - 1); if(offset != -1) { bracketLine = getLineOfOffset(offset); bracketPosition = offset - getLineStartOffset(bracketLine); return; } } catch(BadLocationException bl) { bl.printStackTrace(); } bracketLine = bracketPosition = -1; } protected void documentChanged(DocumentEvent evt) { DocumentEvent.ElementChange ch = evt.getChange( document.getDefaultRootElement()); int count; if(ch == null) count = 0; else count = ch.getChildrenAdded().length - ch.getChildrenRemoved().length; int line = getLineOfOffset(evt.getOffset()); if(count == 0) { painter.invalidateLine(line); } // do magic stuff else if(line < firstLine) { setFirstLine(firstLine + count); } // end of magic stuff else { painter.invalidateLineRange(line,firstLine + visibleLines); updateScrollBars(); } } class ScrollLayout implements LayoutManager { public void addLayoutComponent(String name, Component comp) { if(name.equals(CENTER)) center = comp; else if(name.equals(RIGHT)) right = comp; else if(name.equals(BOTTOM)) bottom = comp; else if(name.equals(LEFT_OF_SCROLLBAR)) leftOfScrollBar.addElement(comp); } public void removeLayoutComponent(Component comp) { if(center == comp) center = null; if(right == comp) right = null; if(bottom == comp) bottom = null; else leftOfScrollBar.removeElement(comp); } public Dimension preferredLayoutSize(Container parent) { Dimension dim = new Dimension(); Insets insets = getInsets(); dim.width = insets.left + insets.right; dim.height = insets.top + insets.bottom; Dimension centerPref = center.getPreferredSize(); dim.width += centerPref.width; dim.height += centerPref.height; Dimension rightPref = right.getPreferredSize(); dim.width += rightPref.width; Dimension bottomPref = bottom.getPreferredSize(); dim.height += bottomPref.height; return dim; } public Dimension minimumLayoutSize(Container parent) { Dimension dim = new Dimension(); Insets insets = getInsets(); dim.width = insets.left + insets.right; dim.height = insets.top + insets.bottom; Dimension centerPref = center.getMinimumSize(); dim.width += centerPref.width; dim.height += centerPref.height; Dimension rightPref = right.getMinimumSize(); dim.width += rightPref.width; Dimension bottomPref = bottom.getMinimumSize(); dim.height += bottomPref.height; return dim; } public void layoutContainer(Container parent) { Dimension size = parent.getSize(); Insets insets = parent.getInsets(); int itop = insets.top; int ileft = insets.left; int ibottom = insets.bottom; int iright = insets.right; int rightWidth = right.getPreferredSize().width; int bottomHeight = bottom.getPreferredSize().height; int centerWidth = size.width - rightWidth - ileft - iright; int centerHeight = size.height - bottomHeight - itop - ibottom; center.setBounds( ileft, itop, centerWidth, centerHeight); right.setBounds( ileft + centerWidth, itop, rightWidth, centerHeight); // Lay out all status components, in order Enumeration status = leftOfScrollBar.elements(); while(status.hasMoreElements()) { Component comp = (Component)status.nextElement(); Dimension dim = comp.getPreferredSize(); comp.setBounds(ileft, itop + centerHeight, dim.width, bottomHeight); ileft += dim.width; } bottom.setBounds( ileft, itop + centerHeight, size.width - rightWidth - ileft - iright, bottomHeight); } // private members private Component center; private Component right; private Component bottom; private Vector leftOfScrollBar = new Vector(); } static class CaretBlinker implements ActionListener { public void actionPerformed(ActionEvent evt) { if(focusedComponent != null && focusedComponent.hasFocus()) focusedComponent.blinkCaret(); } } class MutableCaretEvent extends CaretEvent { MutableCaretEvent() { super(JEditTextArea.this); } public int getDot() { return getCaretPosition(); } public int getMark() { return getMarkPosition(); } } class AdjustHandler implements AdjustmentListener { public void adjustmentValueChanged(final AdjustmentEvent evt) { if(!scrollBarsInitialized) return; // If this is not done, mousePressed events accumilate // and the result is that scrolling doesn't stop after // the mouse is released SwingUtilities.invokeLater(new Runnable() { public void run() { if(evt.getAdjustable() == vertical) setFirstLine(vertical.getValue()); else setHorizontalOffset(-horizontal.getValue()); } }); } } class ComponentHandler extends ComponentAdapter { public void componentResized(ComponentEvent evt) { recalculateVisibleLines(); scrollBarsInitialized = true; } } class DocumentHandler implements DocumentListener { public void insertUpdate(DocumentEvent evt) { documentChanged(evt); int offset = evt.getOffset(); int length = evt.getLength(); int newStart; int newEnd; if(selectionStart > offset || (selectionStart == selectionEnd && selectionStart == offset)) newStart = selectionStart + length; else newStart = selectionStart; if(selectionEnd >= offset) newEnd = selectionEnd + length; else newEnd = selectionEnd; select(newStart,newEnd); } public void removeUpdate(DocumentEvent evt) { documentChanged(evt); int offset = evt.getOffset(); int length = evt.getLength(); int newStart; int newEnd; if(selectionStart > offset) { if(selectionStart > offset + length) newStart = selectionStart - length; else newStart = offset; } else newStart = selectionStart; if(selectionEnd > offset) { if(selectionEnd > offset + length) newEnd = selectionEnd - length; else newEnd = offset; } else newEnd = selectionEnd; select(newStart,newEnd); } public void changedUpdate(DocumentEvent evt) { } } class DragHandler implements MouseMotionListener { public void mouseDragged(MouseEvent evt) { if(popup != null && popup.isVisible()) return; setSelectionRectangular((evt.getModifiers() & InputEvent.CTRL_MASK) != 0); select(getMarkPosition(),xyToOffset(evt.getX(),evt.getY())); } public void mouseMoved(MouseEvent evt) {} } class FocusHandler implements FocusListener { public void focusGained(FocusEvent evt) { setCaretVisible(true); focusedComponent = JEditTextArea.this; } public void focusLost(FocusEvent evt) { setCaretVisible(false); focusedComponent = null; } } class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent evt) { requestFocus(); // Focus events not fired sometimes? setCaretVisible(true); focusedComponent = JEditTextArea.this; if((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0 && popup != null) { popup.show(painter,evt.getX(),evt.getY()); return; } int line = yToLine(evt.getY()); int offset = xToOffset(line,evt.getX()); int dot = getLineStartOffset(line) + offset; switch(evt.getClickCount()) { case 1: doSingleClick(evt,line,offset,dot); break; case 2: // It uses the bracket matching stuff, so // it can throw a BLE try { doDoubleClick(evt,line,offset,dot); } catch(BadLocationException bl) { bl.printStackTrace(); } break; case 3: doTripleClick(evt,line,offset,dot); break; } } private void doSingleClick(MouseEvent evt, int line, int offset, int dot) { if((evt.getModifiers() & InputEvent.SHIFT_MASK) != 0) { rectSelect = (evt.getModifiers() & InputEvent.CTRL_MASK) != 0; select(getMarkPosition(),dot); } else setCaretPosition(dot); } private void doDoubleClick(MouseEvent evt, int line, int offset, int dot) throws BadLocationException { // Ignore empty lines if(getLineLength(line) == 0) return; try { int bracket = TextUtilities.findMatchingBracket( document,Math.max(0,dot - 1)); if(bracket != -1) { int mark = getMarkPosition(); // Hack if(bracket > mark) { bracket++; mark--; } select(mark,bracket); return; } } catch(BadLocationException bl) { bl.printStackTrace(); } // Ok, it's not a bracket... select the word String lineText = getLineText(line); char ch = lineText.charAt(Math.max(0,offset - 1)); String noWordSep = (String)document.getProperty("noWordSep"); if(noWordSep == null) noWordSep = ""; // If the user clicked on a non-letter char, // we select the surrounding non-letters boolean selectNoLetter = (!Character .isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1); int wordStart = 0; for(int i = offset - 1; i >= 0; i--) { ch = lineText.charAt(i); if(selectNoLetter ^ (!Character .isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1)) { wordStart = i + 1; break; } } int wordEnd = lineText.length(); for(int i = offset; i < lineText.length(); i++) { ch = lineText.charAt(i); if(selectNoLetter ^ (!Character .isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1)) { wordEnd = i; break; } } int lineStart = getLineStartOffset(line); select(lineStart + wordStart,lineStart + wordEnd); /* String lineText = getLineText(line); String noWordSep = (String)document.getProperty("noWordSep"); int wordStart = TextUtilities.findWordStart(lineText,offset,noWordSep); int wordEnd = TextUtilities.findWordEnd(lineText,offset,noWordSep); int lineStart = getLineStartOffset(line); select(lineStart + wordStart,lineStart + wordEnd); */ } private void doTripleClick(MouseEvent evt, int line, int offset, int dot) { select(getLineStartOffset(line),getLineEndOffset(line)-1); } } class CaretUndo extends AbstractUndoableEdit { private int start; private int end; CaretUndo(int start, int end) { this.start = start; this.end = end; } public boolean isSignificant() { return false; } public String getPresentationName() { return "caret move"; } public void undo() throws CannotUndoException { super.undo(); select(start,end); } public void redo() throws CannotRedoException { super.redo(); select(start,end); } public boolean addEdit(UndoableEdit edit) { if(edit instanceof CaretUndo) { CaretUndo cedit = (CaretUndo)edit; start = cedit.start; end = cedit.end; cedit.die(); return true; } else return false; } } static { caretTimer = new Timer(500,new CaretBlinker()); caretTimer.setInitialDelay(500); caretTimer.start(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -