Class ActionUtils

java.lang.Object
jsyntaxpane.actions.ActionUtils

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

    • PTAGS_PATTERN

      public static final Pattern PTAGS_PATTERN
      The Pattern to use for PTags in insertSimpleTemplate
    • TEMPLATE_SELECTION

      public static final String TEMPLATE_SELECTION
      See Also:
  • Method Details

    • getInstance

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

      public static String getIndent(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 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. 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 String getLine(JTextComponent target)
      Return the line of text at the TextComponent's current position
      Parameters:
      target -
      Returns:
    • getLineAt

      public static String getLineAt(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 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 frame
      Parameters:
      comp -
      Returns:
    • getTokenStringAt

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

      public static SyntaxDocument getSyntaxDocument(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(JTextComponent editor, int pos) throws 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:
      BadLocationException
    • getColumnNumber

      public static int getColumnNumber(JTextComponent editor, int pos) throws 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:
      BadLocationException
    • getDocumentPosition

      public 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.
      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(JTextComponent pane)
    • insertIntoCombo

      public static void insertIntoCombo(JComboBox combo, 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(JTextComponent target, String result)
    • repeatString

      public static String repeatString(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(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(JTextComponent text)
      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

      public static void insertLinesTemplate(JTextComponent target, 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.
    • Parameters:
      target - JEditorCOmponent to be affected
      templateLines - template split as a String array of lines.
      See Also:
    • insertSimpleTemplate

      public static void insertSimpleTemplate(JTextComponent target, String template)
      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 by any text and then set the text selection to AnyText This methood does NOT perform any indentation and the template should generally span one line only
    • Parameters:
      target -
      template -
    • selectLines

      public static boolean selectLines(JTextComponent target)
      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

      public static void setCaretPosition(JTextComponent target, int line, int column)
      Sets the caret position of the given target to the given line and column
      Parameters:
      target -
      line - the first being 1
      column - the first being 1
    • getTab

      public static String getTab(JTextComponent target)
      Return a string with number of spaces equal to the tab-stop of the TextComponent
      Parameters:
      target -
      Returns:
    • getAction

      public static <T extends Action> T getAction(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 found
      Type Parameters:
      T -
      Parameters:
      target -
      aClass -
      Returns:
      Action object of that class or null
    • getSyntaxKit

      public static DefaultSyntaxKit getSyntaxKit(JTextComponent target)
      Return the DefaultSyntaxKit of this target, or null if the target does not have a DefaultSyntaxKit
      Parameters:
      target -
      Returns:
      kit or null
    • sendKeyPress

      public static void sendKeyPress(JTextComponent target, int v_key, int modifiers)
      Create and send a KeyPress KeyEvent to the component given
      Parameters:
      target - Editor to get the action
      v_key - from KeyEvent.V_ constants
      modifiers - from KeyEvent.*MASK constants