 |
Gridarta Editor
|
Go to the documentation of this file.
11 package net.sf.gridarta.textedit.textarea;
13 import java.awt.AWTEvent;
14 import java.awt.Adjustable;
16 import java.awt.datatransfer.Clipboard;
17 import java.awt.datatransfer.DataFlavor;
18 import java.awt.datatransfer.StringSelection;
19 import java.awt.datatransfer.UnsupportedFlavorException;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.awt.event.AdjustmentEvent;
23 import java.awt.event.AdjustmentListener;
24 import java.awt.event.ComponentAdapter;
25 import java.awt.event.ComponentEvent;
26 import java.awt.event.FocusEvent;
27 import java.awt.event.FocusListener;
28 import java.awt.event.InputEvent;
29 import java.awt.event.KeyEvent;
30 import java.awt.event.MouseAdapter;
31 import java.awt.event.MouseEvent;
32 import java.awt.event.MouseMotionListener;
33 import java.awt.event.MouseWheelEvent;
34 import java.io.IOException;
35 import java.lang.reflect.Field;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.util.HashSet;
40 import javax.swing.JComponent;
41 import javax.swing.JPopupMenu;
42 import javax.swing.JScrollBar;
43 import javax.swing.KeyStroke;
44 import javax.swing.SwingUtilities;
45 import javax.swing.Timer;
46 import javax.swing.event.DocumentEvent;
47 import javax.swing.event.DocumentEvent.ElementChange;
48 import javax.swing.event.DocumentListener;
49 import javax.swing.text.BadLocationException;
50 import javax.swing.text.Document;
51 import javax.swing.text.Segment;
52 import javax.swing.undo.AbstractUndoableEdit;
53 import javax.swing.undo.CannotRedoException;
54 import javax.swing.undo.CannotUndoException;
55 import javax.swing.undo.UndoableEdit;
56 import org.apache.log4j.Category;
57 import org.apache.log4j.Logger;
58 import org.jetbrains.annotations.NotNull;
59 import org.jetbrains.annotations.Nullable;
130 private final JScrollBar
vertical =
new JScrollBar(Adjustable.VERTICAL);
133 private final JScrollBar
horizontal =
new JScrollBar(Adjustable.HORIZONTAL);
163 enableEvents(AWTEvent.KEY_EVENT_MASK);
183 painter.addMouseListener(mouseHandler);
184 painter.addMouseWheelListener(mouseHandler);
195 popup = defaults.getPopup();
232 final Set<KeyStroke> forwardTraversalKeys =
new HashSet<>();
233 forwardTraversalKeys.add(KeyStroke.getKeyStroke(
"control TAB"));
236 final Field field = Class.forName(
"java.awt.KeyboardFocusManager").getField(
"FORWARD_TRAVERSAL_KEYS");
237 final Integer value = field.getInt(field);
239 for (
final Method method : getClass().getMethods()) {
241 if (method.getName().equalsIgnoreCase(
"setFocusTraversalKeys")) {
242 method.invoke(
this, value, forwardTraversalKeys);
246 }
catch (
final ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchFieldException ignored) {
256 public void setFont(@NotNull
final Font font) {
301 if (visibleLines != 0) {
305 vertical.setBlockIncrement(visibleLines);
308 final int width =
painter.getWidth();
333 if (firstLine !=
vertical.getValue()) {
356 if (horizontalOffset !=
horizontal.getValue()) {
368 private void setOrigin(
final int firstLine,
final int horizontalOffset) {
369 boolean changed =
false;
408 }
catch (
final NullPointerException e) {
409 LOG.error(
"Null Pointer Exception in JEditTextArea.setEditingFocus()");
419 private void scrollTo(
final int line,
final int offset) {
425 if (visibleLines == 0) {
433 if (line < newFirstLine + electricScroll) {
434 newFirstLine = Math.max(0, line - electricScroll);
435 }
else if (line + electricScroll >= newFirstLine + visibleLines) {
436 newFirstLine = (line - visibleLines) + electricScroll + 1;
440 if (newFirstLine < 0) {
448 int newHorizontalOffset = horizontalOffset;
450 newHorizontalOffset = Math.min(0, horizontalOffset - x + width + 5);
451 }
else if (x + width >=
painter.getWidth()) {
452 newHorizontalOffset = horizontalOffset + (
painter.getWidth() - x) - width - 5;
455 setOrigin(newFirstLine, newHorizontalOffset);
472 public int offsetToX(
final int line,
final int offset) {
550 public void setText(@NotNull
final String text) {
561 public String
getText(
final int start,
final int len) {
572 public void getText(
final int start,
final int len, @NotNull
final Segment segment) {
649 public void select(
final int start,
final int end) {
652 final boolean newBias;
664 throw new IllegalArgumentException(
"Bounds out of range: " + newStart +
", " + newEnd);
676 if (oldBracketLine != -1) {
681 if (newBracketLine != -1) {
719 throw new InternalError(
"Text component read only");
769 if (caretLineEnd -
caret <= str.length()) {
779 }
catch (
final BadLocationException bl) {
780 bl.printStackTrace();
836 final Clipboard clipboard = getToolkit().getSystemClipboard();
841 final StringBuilder buf =
new StringBuilder();
842 for (
int i = 0; i < repeatCount; i++) {
846 clipboard.setContents(
new StringSelection(buf.toString()),
null);
855 final Clipboard clipboard = getToolkit().getSystemClipboard();
859 final String
selection = ((String) clipboard.getContents(
this).getTransferData(DataFlavor.stringFlavor)).replace(
'\r',
'\n');
862 final StringBuilder buf =
new StringBuilder();
863 for (
int i = 0; i < repeatCount; i++) {
867 }
catch (
final IOException | UnsupportedFlavorException e) {
869 LOG.error(
"Clipboard does not contain a string");
880 super.removeNotify();
893 case KeyEvent.KEY_TYPED:
897 case KeyEvent.KEY_PRESSED:
901 case KeyEvent.KEY_RELEASED:
906 super.processKeyEvent(e);
910 if (newCaretPosition == 0) {
923 }
catch (
final BadLocationException bl) {
924 bl.printStackTrace();
931 final ElementChange ch = evt.getChange(
document.getDefaultRootElement());
932 final int count = ch ==
null ? 0 : ch.getChildrenAdded().length - ch.getChildrenRemoved().length;
938 if (line < firstLine) {
988 SwingUtilities.invokeLater(() -> {
989 if (e.getAdjustable() ==
vertical) {
990 setFirstLine(vertical.getValue());
992 setHorizontalOffset(-horizontal.getValue());
1015 final int offset = e.getOffset();
1016 final int length = e.getLength();
1021 if (selectionStart > offset || (selectionStart == selectionEnd && selectionStart == offset)) {
1022 newStart = selectionStart + length;
1024 newStart = selectionStart;
1028 if (selectionEnd >= offset) {
1029 newEnd = selectionEnd + length;
1031 newEnd = selectionEnd;
1034 select(newStart, newEnd);
1041 final int offset = e.getOffset();
1042 final int length = e.getLength();
1047 if (selectionStart > offset) {
1048 if (selectionStart > offset + length) {
1049 newStart = selectionStart - length;
1054 newStart = selectionStart;
1058 if (selectionEnd > offset) {
1059 if (selectionEnd > offset + length) {
1060 newEnd = selectionEnd - length;
1065 newEnd = selectionEnd;
1068 select(newStart, newEnd);
1124 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0 &&
popup !=
null) {
1130 final int offset =
xToOffset(line, e.getX());
1133 switch (e.getClickCount()) {
1149 if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
1150 diff = e.getUnitsToScroll() *
vertical.getUnitIncrement(1);
1152 diff = e.getWheelRotation() *
vertical.getBlockIncrement();
1158 if ((evt.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
1174 if (bracket != -1) {
1177 if (bracket > mark) {
1184 }
catch (
final BadLocationException bl) {
1185 bl.printStackTrace();
1190 char ch = lineText.charAt(Math.max(0, offset - 1));
1192 String noWordSep = (String)
document.getProperty(
"noWordSep");
1193 if (noWordSep ==
null) {
1199 final boolean selectNoLetter = !Character.isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1;
1203 for (
int i = offset - 1; i >= 0; i--) {
1204 ch = lineText.charAt(i);
1205 if (selectNoLetter ^ (!Character.isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1)) {
1211 int wordEnd = lineText.length();
1212 for (
int i = offset; i < lineText.length(); i++) {
1213 ch = lineText.charAt(i);
1214 if (selectNoLetter ^ (!Character.isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1)) {
1221 select(lineStart + wordStart, lineStart + wordEnd);
1264 return "caret move";
1268 public void undo() throws CannotUndoException {
1275 public void redo() throws CannotRedoException {
1282 public boolean addEdit(@NotNull
final UndoableEdit anEdit) {
boolean setHorizontalOffset(final int horizontalOffset)
Encapsulates default settings for a text area.
JEditTextArea(@NotNull final TextAreaDefaults defaults, @NotNull final SyntaxDocument document, final boolean paintInvalid)
Creates a new JEditTextArea with the specified settings.
Document getDocument()
Returns the document this text area is editing.
void insertUpdate(@NotNull final DocumentEvent e)
jEdit's text area component.
int getSelectionStart()
Returns the selection start offset.
void cut()
Deletes the selected text from the text area and places it into the clipboard.
final SyntaxDocument document
int getSelectionEnd()
Returns the selection end offset.
int getLineEndOffset(final int line)
Returns the end offset of the specified line.
void paste()
Inserts the clipboard contents into the text.
int getElectricScroll()
Returns the number of lines from the top and button of the text area that are always visible.
int getSelectionEndLine()
Returns the selection end line.
void setHorizontalOffset(final int horizontalOffset)
Sets the horizontal offset of drawn lines.
void setEditingFocus()
Sets the focus to this TextArea, so this component is instantly registered for key press events.
int getVisibleLines()
Returns the number of lines visible in this text area.
static int findMatchingBracket(final Document doc, final int offset)
Returns the offset of the bracket matching the one at the specified offset of the document,...
void invalidateLineRange(final int firstLine, final int lastLine)
Marks a range of lines as needing a repaint.
int getCaretLine()
Returns the caret line.
int getSelectionStart()
Returns the selection start offset.
boolean isSelectionRectangular()
Returns true if the selection is rectangular, false otherwise.
boolean isOverwriteEnabled()
Returns whether overwrite mode is active.
int getFirstLine()
Returns the line displayed at the text area's origin.
static final long serialVersionUID
Serial Version UID.
void mousePressed(@NotNull final MouseEvent e)
int getHorizontalOffset()
Returns the horizontal offset of drawn lines.
void clear()
Clears the highlighted bracket.
void mouseWheelMoved(@NotNull final MouseWheelEvent e)
void setFont(@NotNull final Font font)
Set the TextArea font.
int getMarkPosition()
Returns the mark position.
int xToOffset(final int line, final int x)
Converts a x co-ordinate to an offset within a line.
void removeNotify()
Called by the AWT when this component is removed from it's parent.
void focusLost(@NotNull final FocusEvent e)
void invalidateSelectedLines()
Repaints the lines containing the selection.
boolean isModified()
Return whether the text content has been modified from the "unmodified" state.
void copy()
Places the selected text into the clipboard.
CharSequence getLineText(final int lineIndex)
Returns the text on the specified line.
String getText()
Returns the entire text of this text area.
Class with several utility functions used by the text area component.
String getSelectedText()
Returns the selected text, or null if no selection is active.
An input handler converts the user's key strokes into concrete actions.
int offsetToX2(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
void setFirstLine(final int firstLine)
Sets the line displayed at the text area's origin without updating the scroll bars.
SyntaxDocument getDocument()
Returns the document this text area is editing.
FontMetrics getFontMetrics()
Returns the font metrics used by this component.
void resetModified()
Reset the "modified" state.
void recalculateVisibleLines()
Recalculates the number of visible lines.
void setOverwriteEnabled(final boolean overwrite)
Sets whether overwrite mode is active.
int getSelectionEnd()
Returns the selection end offset.
boolean setFirstLine(final int firstLine)
void setMagicCaretPosition(final int magicCaret)
Sets the ‘magic’ caret position.
boolean scrollBarsInitialized
final JScrollBar vertical
A document implementation that can be tokenized by the syntax highlighting system.
boolean addEdit(@NotNull final UndoableEdit anEdit)
The text area repaint manager.
static final long serialVersionUID
Serial Version UID.
void processKeyEvent(@NotNull final KeyEvent e)
Forwards key events directly to the input handler.
int offsetToX(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
InputHandler getInputHandler()
Returns the input handler.
String getText(final int start, final int len)
Returns the specified substring of the document.
String getSelectedText()
Returns the selected text, or null if no selection is active.
void doTripleClick(final int line)
int getLineCount()
Returns the number of lines in the document.
int getLineStartOffset(final int line)
Returns the start offset of the specified line.
void setCaretVisible(final boolean caretVisible)
Sets if the caret should be visible.
boolean blinkCaret()
Blinks the caret.
int getDocumentLength()
Returns the length of the document.
int xToOffset(final int line, final int x)
Converts a x co-ordinate to an offset within a line.
int getCaretPosition()
Returns the caret position.
static void beginCompoundEdit()
Starts a compound edit that can be undone in one operation.
void setOrigin(final int firstLine, final int horizontalOffset)
A fast way of changing both the first line and horizontal offset.
final TextAreaCaret caret
int getLineEndOffset(final int line)
Returns the end offset of the specified line.
void setSelectedText(@NotNull final String selectedText)
Replaces the selection with the specified text.
void setMagicCaret(final int magicCaret)
final InputHandler inputHandler
void doDoubleClick(final int line, final int offset, final int dot)
void removeUpdate(@NotNull final DocumentEvent e)
void changedUpdate(@NotNull final DocumentEvent e)
String getPresentationName()
void doSingleClick(@NotNull final InputEvent evt, final int dot)
void setOverwrite(final boolean overwrite)
Sets whether overwrite mode is active.
void setSelection(final int newStart, final int newEnd, final int newStartLine, final int newEndLine, final boolean newBias)
int offsetToX2(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
static JEditTextArea focusedComponent
void setSelectionRectangular(final boolean rectangleSelect)
Sets if the selection should be rectangular.
int getLineOfOffset(final int offset)
Returns the line containing the specified offset.
void setSelectionRectangular(final boolean rectangleSelect)
Sets if the selection should be rectangular.
void updateBracketHighlight(final int newCaretPosition)
void freeTabKeyFromFocusTraversal()
In JDKs above 1.4, the tab key is used for focus traversal.
void setCaretVisible(final boolean caretVisible)
Sets if the caret should be visible.
void setCaretPosition(final int caret)
Sets the caret position.
void scrollToCaret()
Ensures that the caret is visible by scrolling the text area if necessary.
int lineToY(final int line)
Converts a line index to a y co-ordinate.
static void endCompoundEdit()
Ends a compound edit that can be undone in one operation.
final TextAreaConfig config
The TextAreaConfig for this instance.
void selectAll()
Selects all text in the document.
boolean isSelectionRectangular()
Returns true if the selection is rectangular, false otherwise.
static final Category LOG
The Logger for printing log messages.
int getFirstLine()
Returns the line displayed at the text area's origin.
static final String BOTTOM
int getDocumentLength()
Returns the length of the document.
boolean isOverwrite()
Returns whether overwrite mode is active.
CaretUndo(final int start, final int end)
int yToLine(final int y)
Converts a y co-ordinate to a line index.
void setText(@NotNull final String text)
Sets the entire text of this text area.
void updateScrollBars()
Updates the state of the scroll bars.
void overwriteSetSelectedText(@NotNull final String str)
Similar to.
boolean isEditable()
Returns true if this text area is editable, false otherwise.
final TextAreaSelection selection
int lineToY(final int line)
Converts a line index to a y co-ordinate.
int getLineCount()
Returns the number of lines in the document.
final TextAreaBrackets brackets
void scrollTo(final int line, final int offset)
Ensures that the specified line and offset is visible by scrolling the text area if necessary.
Maintains information about the highlighted pairs of brackets.
boolean isBracketHighlightEnabled()
Returns whether bracket highlighting is enabled.
void documentChanged(@NotNull final DocumentEvent evt)
void set(final int bracketPosition, final int bracketLine)
Sets the highlighted bracket.
void disableSelectionIfEmpty()
String getText(final int start, final int len)
Returns the specified substring of the document.
@noinspection RefusedBequest
int getRepeatCount()
Returns the number of times the next action will be repeated.
int getLineStartOffset(final int line)
Returns the start offset of the specified line.
final void setFont(@NotNull final Font font)
int getMarkPosition()
Returns the mark position.
void getText(final int start, final int len, @NotNull final Segment segment)
Copies the specified substring of the document into a segment.
void focusGained(@NotNull final FocusEvent e)
void componentResized(@NotNull final ComponentEvent e)
int getMagicCaretPosition()
Returns the ‘magic’ caret position.
void blinkCaret()
Blinks the caret.
void adjustmentValueChanged(@NotNull final AdjustmentEvent e)
static final String CENTER
static final String RIGHT
static final Timer CARET_TIMER
void mouseMoved(@NotNull final MouseEvent e)
int offsetToX(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
Miscellaneous configuration settings for JEditTextArea.
int getLineLength(final int line)
Returns the length of the specified line.
void invalidateLine(final int line)
Marks a line as needing a repaint.
void mouseDragged(@NotNull final MouseEvent e)
String unmodifiedText
The text contents in the last "unmodified" state.
void select(final int start, final int end)
Selects from the start offset to the end offset.
final TextAreaPainter painter
void setSelectedText(@NotNull final String selectedText)
Replaces the selection with the specified text.
static void addUndoableEdit(@NotNull final UndoableEdit edit)
Adds an undoable edit to this document's undo list.
int getSelectionStartLine()
Returns the selection start line.
void setText(@NotNull final String text)
Sets the entire text of this text area.
int getCaretPosition()
Returns the caret position.
int xyToOffset(final int x, final int y)
Converts a point to an offset, from the start of the text.
int getCaretLine()
Returns the caret line.
final JScrollBar horizontal
CharSequence getLineText(final int lineIndex)
Returns the text on the specified line.
void actionPerformed(@NotNull final ActionEvent e)
int getBracketLine()
Returns the line of the highlighted bracket (the bracket matching the one before the caret).
void setBlink(final boolean blink)
int getDefaultCharWidth()
This works only for fonts with fixed line height.