Class JEditTextArea
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- net.sf.gridarta.textedit.textarea.JEditTextArea
-
- All Implemented Interfaces:
java.awt.image.ImageObserver,java.awt.MenuContainer,java.io.Serializable
public class JEditTextArea extends javax.swing.JComponentjEdit's text area component. It is more suited for editing program source code than JEditorPane, because it drops the unnecessary features (images, variable-width lines, and so on) and adds a whole bunch of useful goodies such as:- More flexible key binding scheme
- Supports macro recorders
- Rectangular selection
- Bracket highlighting
- Syntax highlighting
- Command repetition
- Block caret can be enabled
To use it in your app, treat it like any other component, for example:
JEditTextArea ta = new JEditTextArea(); ta.setTokenMarker(new JavaTokenMarker()); ta.setText("public class Test {\n" + " public static void main(String[] args) {\n" + " System.err.println(\"Hello World\");\n" + " }\n" + "}");- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
-
Constructor Summary
Constructors Constructor Description JEditTextArea(@NotNull TextAreaDefaults defaults, @NotNull SyntaxDocument document, boolean paintInvalid)Creates a new JEditTextArea with the specified settings.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcut()Deletes the selected text from the text area and places it into the clipboard.intgetCaretLine()Returns the caret line.intgetCaretPosition()Returns the caret position.@NotNull javax.swing.text.DocumentgetDocument()Returns the document this text area is editing.intgetDocumentLength()Returns the length of the document.intgetElectricScroll()Returns the number of lines from the top and button of the text area that are always visible.intgetFirstLine()Returns the line displayed at the text area's origin.@NotNull InputHandlergetInputHandler()Returns the input handler.intgetLineCount()Returns the number of lines in the document.intgetLineEndOffset(int line)Returns the end offset of the specified line.intgetLineStartOffset(int line)Returns the start offset of the specified line.@NotNull java.lang.CharSequencegetLineText(int lineIndex)Returns the text on the specified line.intgetMagicCaretPosition()Returns the `magic' caret position.intgetMarkPosition()Returns the mark position.@Nullable java.lang.StringgetSelectedText()Returns the selected text, or null if no selection is active.intgetSelectionEnd()Returns the selection end offset.intgetSelectionStart()Returns the selection start offset.@NotNull java.lang.StringgetText()Returns the entire text of this text area.@Nullable java.lang.StringgetText(int start, int len)Returns the specified substring of the document.voidgetText(int start, int len, @NotNull javax.swing.text.Segment segment)Copies the specified substring of the document into a segment.intgetVisibleLines()Returns the number of lines visible in this text area.booleanisEditable()Returns true if this text area is editable, false otherwise.booleanisModified()Return whether the text content has been modified from the "unmodified" state.booleanisOverwriteEnabled()Returns whether overwrite mode is active.booleanisSelectionRectangular()Returns true if the selection is rectangular, false otherwise.intlineToY(int line)Converts a line index to a y co-ordinate.intoffsetToX(int line, int offset)Converts an offset in a line into an x co-ordinate.intoffsetToX2(int line, int offset)Converts an offset in a line into an x co-ordinate.voidoverwriteSetSelectedText(@NotNull java.lang.String str)Similar tosetSelectedText(), but overstrikes the appropriate number of characters if overwrite mode is enabled.voidpaste()Inserts the clipboard contents into the text.voidprocessKeyEvent(@NotNull java.awt.event.KeyEvent e)Forwards key events directly to the input handler.voidremoveNotify()Called by the AWT when this component is removed from it's parent.voidresetModified()Reset the "modified" state.voidselect(int start, int end)Selects from the start offset to the end offset.voidselectAll()Selects all text in the document.voidsetCaretPosition(int caret)Sets the caret position.voidsetEditingFocus()Sets the focus to this TextArea, so this component is instantly registered for key press events.voidsetFirstLine(int firstLine)Sets the line displayed at the text area's origin without updating the scroll bars.voidsetFont(@NotNull java.awt.Font font)Set the TextArea fontvoidsetMagicCaretPosition(int magicCaret)Sets the `magic' caret position.voidsetOverwriteEnabled(boolean overwrite)Sets whether overwrite mode is active.voidsetSelectedText(@NotNull java.lang.String selectedText)Replaces the selection with the specified text.voidsetSelectionRectangular(boolean rectangleSelect)Sets if the selection should be rectangular.voidsetText(@NotNull java.lang.String text)Sets the entire text of this text area.voidupdateScrollBars()Updates the state of the scroll bars.intxToOffset(int line, int x)Converts a x co-ordinate to an offset within a line.-
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Constructor Detail
-
JEditTextArea
public JEditTextArea(@NotNull @NotNull TextAreaDefaults defaults, @NotNull @NotNull SyntaxDocument document, boolean paintInvalid)Creates a new JEditTextArea with the specified settings.- Parameters:
defaults- the default settingspaintInvalid- whether invalid lines should be painted as red tildes
-
-
Method Detail
-
setFont
public void setFont(@NotNull @NotNull java.awt.Font font)Set the TextArea font- Overrides:
setFontin classjavax.swing.JComponent- Parameters:
font- font
-
getInputHandler
@NotNull public @NotNull InputHandler getInputHandler()
Returns the input handler.
-
getElectricScroll
public int getElectricScroll()
Returns the number of lines from the top and button of the text area that are always visible.
-
updateScrollBars
public void updateScrollBars()
Updates the state of the scroll bars. This should be called if the number of lines in the document changes, or when the size of the text are changes.
-
getFirstLine
public int getFirstLine()
Returns the line displayed at the text area's origin.
-
setFirstLine
public void setFirstLine(int firstLine)
Sets the line displayed at the text area's origin without updating the scroll bars.
-
getVisibleLines
public int getVisibleLines()
Returns the number of lines visible in this text area.
-
setEditingFocus
public void setEditingFocus()
Sets the focus to this TextArea, so this component is instantly registered for key press events. The graphics context must be fully initialized before calling this method.
-
lineToY
public int lineToY(int line)
Converts a line index to a y co-ordinate.- Parameters:
line- the line
-
offsetToX
public int offsetToX(int line, int offset)Converts an offset in a line into an x co-ordinate. This is a slow version that can be used any time.- Parameters:
line- the lineoffset- the offset, from the start of the line
-
offsetToX2
public int offsetToX2(int line, int offset)Converts an offset in a line into an x co-ordinate. This is a fast version that should only be used if no changes were made to the text since the last repaint.- Parameters:
line- the lineoffset- the offset, from the start of the line
-
xToOffset
public int xToOffset(int line, int x)Converts a x co-ordinate to an offset within a line.- Parameters:
line- the linex- the x co-ordinate
-
getDocument
@NotNull public @NotNull javax.swing.text.Document getDocument()
Returns the document this text area is editing.
-
getDocumentLength
public int getDocumentLength()
Returns the length of the document. Equivalent to callinggetDocument().getLength().
-
getLineCount
public int getLineCount()
Returns the number of lines in the document.
-
getLineStartOffset
public int getLineStartOffset(int line)
Returns the start offset of the specified line.- Parameters:
line- the line- Returns:
- the start offset of the specified line, or -1 if the line is invalid
-
getLineEndOffset
public int getLineEndOffset(int line)
Returns the end offset of the specified line.- Parameters:
line- the line- Returns:
- the end offset of the specified line, or -1 if the line is invalid
-
getText
@NotNull public @NotNull java.lang.String getText()
Returns the entire text of this text area.
-
setText
public void setText(@NotNull @NotNull java.lang.String text)Sets the entire text of this text area.
-
getText
@Nullable public @Nullable java.lang.String getText(int start, int len)Returns the specified substring of the document.- Parameters:
start- the start offsetlen- the length of the substring- Returns:
- the substring, or null if the offsets are invalid
-
getText
public void getText(int start, int len, @NotNull @NotNull javax.swing.text.Segment segment)Copies the specified substring of the document into a segment. If the offsets are invalid, the segment will contain a null string.- Parameters:
start- the start offsetlen- the length of the substringsegment- the segment
-
getLineText
@NotNull public @NotNull java.lang.CharSequence getLineText(int lineIndex)
Returns the text on the specified line.- Parameters:
lineIndex- the line- Returns:
- the text, or null if the line is invalid
-
getSelectionStart
public int getSelectionStart()
Returns the selection start offset.
-
getSelectionEnd
public int getSelectionEnd()
Returns the selection end offset.
-
getCaretPosition
public int getCaretPosition()
Returns the caret position. This will either be the selection start or the selection end, depending on which direction the selection was made in.
-
getCaretLine
public int getCaretLine()
Returns the caret line.
-
getMarkPosition
public int getMarkPosition()
Returns the mark position. This will be the opposite selection bound to the caret position.- See Also:
getCaretPosition()
-
setCaretPosition
public void setCaretPosition(int caret)
Sets the caret position. The new selection will consist of the caret position only (hence no text will be selected).- Parameters:
caret- the caret position- See Also:
select(int, int)
-
selectAll
public void selectAll()
Selects all text in the document.
-
select
public void select(int start, int end)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.- Parameters:
start- the start offsetend- the end offset
-
getSelectedText
@Nullable public @Nullable java.lang.String getSelectedText()
Returns the selected text, or null if no selection is active.
-
setSelectedText
public void setSelectedText(@NotNull @NotNull java.lang.String selectedText)Replaces the selection with the specified text.- Parameters:
selectedText- the replacement text for the selection
-
isEditable
public boolean isEditable()
Returns true if this text area is editable, false otherwise.
-
getMagicCaretPosition
public int getMagicCaretPosition()
Returns the `magic' caret position. This can be used to preserve the column position when moving up and down lines.
-
setMagicCaretPosition
public void setMagicCaretPosition(int magicCaret)
Sets the `magic' caret position. This can be used to preserve the column position when moving up and down lines.- Parameters:
magicCaret- the magic caret position
-
overwriteSetSelectedText
public void overwriteSetSelectedText(@NotNull @NotNull java.lang.String str)Similar tosetSelectedText(), but overstrikes the appropriate number of characters if overwrite mode is enabled.- Parameters:
str- the string- See Also:
setSelectedText(String),isOverwriteEnabled()
-
isOverwriteEnabled
public boolean isOverwriteEnabled()
Returns whether overwrite mode is active.- Returns:
- whether overwrite mode is active
-
setOverwriteEnabled
public void setOverwriteEnabled(boolean overwrite)
Sets whether overwrite mode is active.- Parameters:
overwrite- whether overwrite mode is active
-
isSelectionRectangular
public boolean isSelectionRectangular()
Returns true if the selection is rectangular, false otherwise.
-
setSelectionRectangular
public void setSelectionRectangular(boolean rectangleSelect)
Sets if the selection should be rectangular.- Parameters:
rectangleSelect- true if the selection should be rectangular, false otherwise
-
cut
public void cut()
Deletes the selected text from the text area and places it into the clipboard.
-
paste
public void paste()
Inserts the clipboard contents into the text.
-
removeNotify
public void removeNotify()
Called by the AWT when this component is removed from it's parent. This stops clears the currently focused component.- Overrides:
removeNotifyin classjavax.swing.JComponent
-
processKeyEvent
public void processKeyEvent(@NotNull @NotNull java.awt.event.KeyEvent e)Forwards key events directly to the input handler. This is slightly faster than using a KeyListener because some Swing overhead is avoided.- Overrides:
processKeyEventin classjavax.swing.JComponent
-
isModified
public boolean isModified()
Return whether the text content has been modified from the "unmodified" state.- Returns:
trueif the text content has been modified, orfalseif it is unmodified
-
resetModified
public void resetModified()
Reset the "modified" state.
-
-