Package jsyntaxpane.actions
Class ActionUtils
java.lang.Object
jsyntaxpane.actions.ActionUtils
Various utility methods to work on JEditorPane and its SyntaxDocument
for use by Actions
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Action>
TgetAction
(JTextComponent target, Class<T> aClass) Searches all actions of a JTextComponent for ab action of the given class and returns the first one that matches that class, or null if no Action is foundstatic int
getColumnNumber
(JTextComponent editor, int pos) Gets the column number at given position of editor.static int
getDocumentPosition
(JTextComponent editor, int line, int column) Get the closest position within the document of the component that has given line and column.static Frame
getFrameFor
(Component comp) Returns the Frame that contains this component or null if the component is not within a Window or the containing window is not a framestatic String
Get the indentation of a line of text.static ActionUtils
Get the Singleton instance.static String
getLine
(JTextComponent target) Return the line of text at the TextComponent's current positionstatic String
getLineAt
(JTextComponent target, int pos) Return the line of text at the given position.static int
getLineCount
(JTextComponent pane) static int
getLineNumber
(JTextComponent editor, int pos) Gets the Line Number at the give position of the editor component.static String[]
getSelectedLines
(JTextComponent target) Return the lines that span the selection (split as an array of Strings) if there is no selection then current line is returned.static SyntaxDocument
getSyntaxDocument
(JTextComponent component) A helper function that will return the SyntaxDocument attached to the given text component.static DefaultSyntaxKit
getSyntaxKit
(JTextComponent target) Return the DefaultSyntaxKit of this target, or null if the target does not have a DefaultSyntaxKitstatic String
getTab
(JTextComponent target) Return a string with number of spaces equal to the tab-stop of the TextComponentstatic int
getTabSize
(JTextComponent text) Return the TabStop property for the given text component, or 0 if not usedstatic String
getTokenStringAt
(SyntaxDocument doc, int pos) Returns the the Token at pos as a Stringstatic void
insertIntoCombo
(JComboBox combo, Object item) Insert the given item into the combo box, and set it as first selected item.static void
insertLinesTemplate
(JTextComponent target, String[] templateLines) Expand the string template and replaces the selection with the expansion of the template.static void
insertMagicString
(JTextComponent target, int dot, String toInsert) Insert the given String into the textcomponent.static void
insertMagicString
(JTextComponent target, String result) static void
insertSimpleTemplate
(JTextComponent target, String template) Expand the string template and replaces the selection with the expansion of the template.static boolean
isEmptyOrBlanks
(String string) Checks if the given string is null, empty or contains whitespace onlystatic String
repeatString
(String source, int repeat) Repeat the string source repeat times.static boolean
selectLines
(JTextComponent target) If the selection is multi lined, then the full lines are selected, otherwise, nothing is done.static void
sendKeyPress
(JTextComponent target, int v_key, int modifiers) Create and send a KeyPress KeyEvent to the component givenstatic void
setCaretPosition
(JTextComponent target, int line, int column) Sets the caret position of the given target to the given line and column
-
Field Details
-
PTAGS_PATTERN
The Pattern to use for PTags in insertSimpleTemplate -
TEMPLATE_SELECTION
- See Also:
-
-
Method Details
-
getInstance
Get the Singleton instance. Will be created lazily.- Returns:
-
getIndent
Get the indentation of a line of text. This is the subString from beginning of line to the first non-space char- Parameters:
line
- the line of text- Returns:
- indentation of line.
-
getSelectedLines
Return the lines that span the selection (split as an array of Strings) if there is no selection then current line is returned. Note that the strings returned will not contain the terminating line feeds If the document is empty, then an empty string array is returned. So you can always iterate over the returned array without a null check The text component will then have the full lines set as selection- Parameters:
target
-- Returns:
- String[] of lines spanning selection / or line containing dot
-
getLine
Return the line of text at the TextComponent's current position- Parameters:
target
-- Returns:
-
getLineAt
Return the line of text at the given position. The returned value may be null. It will not contain the trailing new-line character.- Parameters:
target
- the text componentpos
- char position- Returns:
-
getFrameFor
Returns the Frame that contains this component or null if the component is not within a Window or the containing window is not a frame- Parameters:
comp
-- Returns:
-
getTokenStringAt
Returns the the Token at pos as a String- Parameters:
doc
-pos
-- Returns:
-
getSyntaxDocument
A helper function that will return the SyntaxDocument attached to the given text component. Return null if the document is not a SyntaxDocument, or if the text component is null- Parameters:
component
-- Returns:
-
getLineNumber
Gets the Line Number at the give position of the editor component. The first line number is ZERO- Parameters:
editor
-pos
-- Returns:
- line number
- Throws:
BadLocationException
-
getColumnNumber
Gets the column number at given position of editor. The first column is ZERO- Parameters:
editor
-pos
-- Returns:
- the 0 based column number
- Throws:
BadLocationException
-
getDocumentPosition
Get the closest position within the document of the component that has given line and column.- Parameters:
editor
-line
- the first being 1column
- the first being 1- Returns:
- the closest positon for the text component at given line and column
-
getLineCount
-
insertIntoCombo
Insert the given item into the combo box, and set it as first selected item. If the item already exists, it is removed, so there are no duplicates.- Parameters:
combo
-item
- the item to insert. if it's null, then nothing is inserted
-
insertMagicString
-
repeatString
Repeat the string source repeat times. If repeats == 0 then empty String is returned if source is null, then empty string is returned- Parameters:
source
-repeat
-- Returns:
- source String repeated repeat times.
-
isEmptyOrBlanks
Checks if the given string is null, empty or contains whitespace only- Parameters:
string
-- Returns:
- true if string is null, empty or contains whitespace only, false otherwise.
-
getTabSize
Return the TabStop property for the given text component, or 0 if not used- Parameters:
text
-- Returns:
-
insertMagicString
public static void insertMagicString(JTextComponent target, int dot, String toInsert) throws BadLocationException Insert the given String into the textcomponent. If the string contains the | vertical BAr char, then it will not be inserted, and the cursor will be set to its location. If there are TWO vertical bars, then the text between them will be selected If the toInsert String is multiLine, then indentation of all following lines will be the same as the first line. TAB characters will be replaced by the number of spaces in the document's TAB property.- Parameters:
target
-dot
-toInsert
-- Throws:
BadLocationException
-
insertLinesTemplate
Expand the string template and replaces the selection with the expansion of the template. The template String may contain any of the following special tags.#{selection}
replaced with the selection, if any. If there is no selection, then the#{selection}
tag will be removed.#{p:any text}
will be replaced byany text
and then set selection toany text
This method properly handles indentation as follows: The indentation of the whole block will match the indentation of the caret line, or the line with the beginning of the selection, if the selection is in whole line, i.e.e one or more lines of selected text.- Parameters:
target
- JEditorCOmponent to be affectedtemplateLines
- template split as a String array of lines.- See Also:
-
insertSimpleTemplate
Expand the string template and replaces the selection with the expansion of the template. The template String may contain any of the following special tags.#{selection}
replaced with the selection, if any. If there is no selection, then the#{selection}
tag will be removed.#{p:AnyText}
will be replaced byany text
and then set the text selection toAnyText
This methood does NOT perform any indentation and the template should generally span one line only- Parameters:
target
-template
-
-
selectLines
If the selection is multi lined, then the full lines are selected, otherwise, nothing is done.- Parameters:
target
-- Returns:
- true if the selection is multi-line, or a whole line
-
setCaretPosition
Sets the caret position of the given target to the given line and column- Parameters:
target
-line
- the first being 1column
- the first being 1
-
getTab
Return a string with number of spaces equal to the tab-stop of the TextComponent- Parameters:
target
-- Returns:
-
getAction
Searches all actions of a JTextComponent for ab action of the given class and returns the first one that matches that class, or null if no Action is found- Type Parameters:
T
-- Parameters:
target
-aClass
-- Returns:
- Action object of that class or null
-
getSyntaxKit
Return the DefaultSyntaxKit of this target, or null if the target does not have a DefaultSyntaxKit- Parameters:
target
-- Returns:
- kit or null
-
sendKeyPress
Create and send a KeyPress KeyEvent to the component given- Parameters:
target
- Editor to get the actionv_key
- from KeyEvent.V_ constantsmodifiers
- from KeyEvent.*MASK constants
-