Class ActionUtils


  • public class ActionUtils
    extends java.lang.Object
    Various utility methods to work on JEditorPane and its SyntaxDocument for use by Actions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.regex.Pattern PTAGS_PATTERN
      The Pattern to use for PTags in insertSimpleTemplate
      static java.lang.String TEMPLATE_SELECTION  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends javax.swing.Action>
      T
      getAction​(javax.swing.text.JTextComponent target, java.lang.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 found
      static int getColumnNumber​(javax.swing.text.JTextComponent editor, int pos)
      Gets the column number at given position of editor.
      static int getDocumentPosition​(javax.swing.text.JTextComponent editor, int line, int column)
      Get the closest position within the document of the component that has given line and column.
      static java.awt.Frame getFrameFor​(java.awt.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 frame
      static java.lang.String getIndent​(java.lang.String line)
      Get the indentation of a line of text.
      static ActionUtils getInstance()
      Get the Singleton instance.
      static java.lang.String getLine​(javax.swing.text.JTextComponent target)
      Return the line of text at the TextComponent's current position
      static java.lang.String getLineAt​(javax.swing.text.JTextComponent target, int pos)
      Return the line of text at the given position.
      static int getLineCount​(javax.swing.text.JTextComponent pane)  
      static int getLineNumber​(javax.swing.text.JTextComponent editor, int pos)
      Gets the Line Number at the give position of the editor component.
      static java.lang.String[] getSelectedLines​(javax.swing.text.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​(javax.swing.text.JTextComponent component)
      A helper function that will return the SyntaxDocument attached to the given text component.
      static DefaultSyntaxKit getSyntaxKit​(javax.swing.text.JTextComponent target)
      Return the DefaultSyntaxKit of this target, or null if the target does not have a DefaultSyntaxKit
      static java.lang.String getTab​(javax.swing.text.JTextComponent target)
      Return a string with number of spaces equal to the tab-stop of the TextComponent
      static int getTabSize​(javax.swing.text.JTextComponent text)
      Return the TabStop property for the given text component, or 0 if not used
      static java.lang.String getTokenStringAt​(SyntaxDocument doc, int pos)
      Returns the the Token at pos as a String
      static void insertIntoCombo​(javax.swing.JComboBox combo, java.lang.Object item)
      Insert the given item into the combo box, and set it as first selected item.
      static void insertLinesTemplate​(javax.swing.text.JTextComponent target, java.lang.String[] templateLines)
      Expand the string template and replaces the selection with the expansion of the template.
      static void insertMagicString​(javax.swing.text.JTextComponent target, int dot, java.lang.String toInsert)
      Insert the given String into the textcomponent.
      static void insertMagicString​(javax.swing.text.JTextComponent target, java.lang.String result)  
      static void insertSimpleTemplate​(javax.swing.text.JTextComponent target, java.lang.String template)
      Expand the string template and replaces the selection with the expansion of the template.
      static boolean isEmptyOrBlanks​(java.lang.String string)
      Checks if the given string is null, empty or contains whitespace only
      static java.lang.String repeatString​(java.lang.String source, int repeat)
      Repeat the string source repeat times.
      static boolean selectLines​(javax.swing.text.JTextComponent target)
      If the selection is multi lined, then the full lines are selected, otherwise, nothing is done.
      static void sendKeyPress​(javax.swing.text.JTextComponent target, int v_key, int modifiers)
      Create and send a KeyPress KeyEvent to the component given
      static void setCaretPosition​(javax.swing.text.JTextComponent target, int line, int column)
      Sets the caret position of the given target to the given line and column
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PTAGS_PATTERN

        public static final java.util.regex.Pattern PTAGS_PATTERN
        The Pattern to use for PTags in insertSimpleTemplate
      • TEMPLATE_SELECTION

        public static final java.lang.String TEMPLATE_SELECTION
        See Also:
        Constant Field Values
    • Method Detail

      • getInstance

        public static ActionUtils getInstance()
        Get the Singleton instance. Will be created lazily.
        Returns:
      • getIndent

        public static java.lang.String getIndent​(java.lang.String line)
        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

        public static java.lang.String[] getSelectedLines​(javax.swing.text.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. 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

        public static java.lang.String getLine​(javax.swing.text.JTextComponent target)
        Return the line of text at the TextComponent's current position
        Parameters:
        target -
        Returns:
      • getLineAt

        public static java.lang.String getLineAt​(javax.swing.text.JTextComponent target,
                                                 int pos)
        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 component
        pos - char position
        Returns:
      • getFrameFor

        public static java.awt.Frame getFrameFor​(java.awt.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 frame
        Parameters:
        comp -
        Returns:
      • getTokenStringAt

        public static java.lang.String getTokenStringAt​(SyntaxDocument doc,
                                                        int pos)
        Returns the the Token at pos as a String
        Parameters:
        doc -
        pos -
        Returns:
      • getSyntaxDocument

        public static SyntaxDocument getSyntaxDocument​(javax.swing.text.JTextComponent component)
        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

        public static int getLineNumber​(javax.swing.text.JTextComponent editor,
                                        int pos)
                                 throws javax.swing.text.BadLocationException
        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:
        javax.swing.text.BadLocationException
      • getColumnNumber

        public static int getColumnNumber​(javax.swing.text.JTextComponent editor,
                                          int pos)
                                   throws javax.swing.text.BadLocationException
        Gets the column number at given position of editor. The first column is ZERO
        Parameters:
        editor -
        pos -
        Returns:
        the 0 based column number
        Throws:
        javax.swing.text.BadLocationException
      • getDocumentPosition

        public static int getDocumentPosition​(javax.swing.text.JTextComponent editor,
                                              int line,
                                              int column)
        Get the closest position within the document of the component that has given line and column.
        Parameters:
        editor -
        line - the first being 1
        column - the first being 1
        Returns:
        the closest positon for the text component at given line and column
      • getLineCount

        public static int getLineCount​(javax.swing.text.JTextComponent pane)
      • insertIntoCombo

        public static void insertIntoCombo​(javax.swing.JComboBox combo,
                                           java.lang.Object item)
        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

        public static void insertMagicString​(javax.swing.text.JTextComponent target,
                                             java.lang.String result)
      • repeatString

        public static java.lang.String repeatString​(java.lang.String source,
                                                    int repeat)
        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

        public static boolean isEmptyOrBlanks​(java.lang.String string)
        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

        public static int getTabSize​(javax.swing.text.JTextComponent text)
        Return the TabStop property for the given text component, or 0 if not used
        Parameters:
        text -
        Returns:
      • insertMagicString

        public static void insertMagicString​(javax.swing.text.JTextComponent target,
                                             int dot,
                                             java.lang.String toInsert)
                                      throws javax.swing.text.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:
        javax.swing.text.BadLocationException
      • insertLinesTemplate

        public static void insertLinesTemplate​(javax.swing.text.JTextComponent target,
                                               java.lang.String[] templateLines)
        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 by any text and then set selection to any 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. {@see selectLines()}
Parameters:
target - JEditorCOmponent to be affected
templateLines - template split as a String array of lines.
See Also:
insertLinesTemplate(javax.swing.text.JTextComponent,java.lang.String[])
Parameters:
target -
template -