📄 jedittextarea.java
字号:
{
bracketLine = getLineOfOffset(offset);
bracketPosition = offset - getLineStartOffset(bracketLine);
return;
}
}
catch (BadLocationException bl)
{
bl.printStackTrace();
}
bracketLine = bracketPosition = -1;
}
private void documentChanged (final DocumentEvent evt)
{
final DocumentEvent.ElementChange ch = evt.getChange(document.getDefaultRootElement());
final int count;
if (ch == null)
{
count = 0;
}
else
{
count = ch.getChildrenAdded().length -
ch.getChildrenRemoved().length;
}
final 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();
}
}
private final class ScrollLayout
implements LayoutManager
{
public final void addLayoutComponent (final String name, final 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 final void removeLayoutComponent (final Component comp)
{
if (center == comp)
{
center = null;
}
if (right == comp)
{
right = null;
}
if (bottom == comp)
{
bottom = null;
}
else
{
leftOfScrollBar.removeElement(comp);
}
}
public final Dimension preferredLayoutSize (final Container parent)
{
final Dimension dim = new Dimension();
final Insets insets = getInsets();
dim.width = insets.left + insets.right;
dim.height = insets.top + insets.bottom;
final Dimension centerPref = center.getPreferredSize();
dim.width += centerPref.width;
dim.height += centerPref.height;
final Dimension rightPref = right.getPreferredSize();
dim.width += rightPref.width;
final Dimension bottomPref = bottom.getPreferredSize();
dim.height += bottomPref.height;
return dim;
}
public final Dimension minimumLayoutSize (final Container parent)
{
final Dimension dim = new Dimension();
final Insets insets = getInsets();
dim.width = insets.left + insets.right;
dim.height = insets.top + insets.bottom;
final Dimension centerPref = center.getMinimumSize();
dim.width += centerPref.width;
dim.height += centerPref.height;
final Dimension rightPref = right.getMinimumSize();
dim.width += rightPref.width;
final Dimension bottomPref = bottom.getMinimumSize();
dim.height += bottomPref.height;
return dim;
}
public final void layoutContainer (final Container parent)
{
final Dimension size = parent.getSize();
final Insets insets = parent.getInsets();
final int itop = insets.top;
int ileft = insets.left;
final int ibottom = insets.bottom;
final int iright = insets.right;
final int rightWidth = right.getPreferredSize().width;
final int bottomHeight = bottom.getPreferredSize().height;
final int centerWidth = size.width - rightWidth - ileft - iright;
final 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
final Enumeration status = leftOfScrollBar.elements();
while (status.hasMoreElements())
{
final Component comp = (Component) status.nextElement();
final 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 final Vector leftOfScrollBar = new Vector();
}
private static final class CaretBlinker
implements ActionListener
{
public final void actionPerformed (final ActionEvent evt)
{
if (focusedComponent != null
&& focusedComponent.hasFocus())
{
focusedComponent.blinkCaret();
}
}
}
private final class MutableCaretEvent
extends CaretEvent
{
private MutableCaretEvent ()
{
super(JEditTextArea.this);
}
public final int getDot ()
{
return getCaretPosition();
}
public final int getMark ()
{
return getMarkPosition();
}
}
private final class AdjustHandler
implements AdjustmentListener
{
public final 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());
}
}
});
}
}
private final class ComponentHandler
extends ComponentAdapter
{
public final void componentResized (final ComponentEvent evt)
{
recalculateVisibleLines();
scrollBarsInitialized = true;
}
}
private final class DocumentHandler
implements DocumentListener
{
public final void insertUpdate (final DocumentEvent evt)
{
documentChanged(evt);
final int offset = evt.getOffset();
final int length = evt.getLength();
final int newStart;
final 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 final void removeUpdate (final DocumentEvent evt)
{
documentChanged(evt);
final int offset = evt.getOffset();
final int length = evt.getLength();
final int newStart;
final 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 final void changedUpdate (final DocumentEvent evt)
{
}
}
private final class DragHandler
implements MouseMotionListener
{
public final void mouseDragged (final MouseEvent evt)
{
if (popup != null && popup.isVisible())
{
return;
}
setSelectionRectangular((evt.getModifiers()
& InputEvent.CTRL_MASK) != 0);
select(getMarkPosition(), xyToOffset(evt.getX(), evt.getY()));
}
public final void mouseMoved (final MouseEvent evt)
{
}
}
private final class FocusHandler
implements FocusListener
{
public final void focusGained (final FocusEvent evt)
{
setCaretVisible(true);
focusedComponent = JEditTextArea.this;
}
public final void focusLost (final FocusEvent evt)
{
setCaretVisible(false);
focusedComponent = null;
}
}
private final class MouseHandler
extends MouseAdapter
{
public final void mousePressed (final 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;
}
final int line = yToLine(evt.getY());
final int offset = xToOffset(line, evt.getX());
final 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
doDoubleClick(evt, line, offset, dot);
break;
case 3:
doTripleClick(evt, line, offset, dot);
break;
}
}
private void doSingleClick (final MouseEvent evt, final int line,
final int offset, final 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 (final MouseEvent evt, final int line,
final int offset, final int dot)
{
// 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
final 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
final 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;
}
}
final 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 (final MouseEvent evt, final int line,
final int offset, final int dot)
{
select(getLineStartOffset(line), getLineEndOffset(line) - 1);
}
}
private final class CaretUndo
extends AbstractUndoableEdit
{
private int start;
private int end;
private CaretUndo (final int start, final int end)
{
this.start = start;
this.end = end;
}
public final boolean isSignificant ()
{
return false;
}
public final String getPresentationName ()
{
return "caret move";
}
public final void undo ()
throws CannotUndoException
{
super.undo();
select(start, end);
}
public final void redo ()
throws CannotRedoException
{
super.redo();
select(start, end);
}
public final boolean addEdit (final UndoableEdit edit)
{
if (edit instanceof CaretUndo)
{
final 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 + -