Class SyntaxDocument

All Implemented Interfaces:
Serializable, Document, StyledDocument

public class SyntaxDocument extends DefaultStyledDocument
Highlights syntax in a DefaultStyledDocument. Allows any number of keywords to be formatted in any number of user-defined styles.
Author:
camickr (primary author; java sun forums user), David Underhill, FracPete (fracpete at waikato dot ac dot nz) - use of a properties file to setup syntax highlighting instead of hard-coded
See Also:
  • Field Details

    • MAX_TABS

      public static final int MAX_TABS
      the maximum number of tabs.
      See Also:
    • DEFAULT_FONT_FAMILY

      public static final String DEFAULT_FONT_FAMILY
      the font family.
      See Also:
    • DEFAULT_FONT_SIZE

      public static final int DEFAULT_FONT_SIZE
      the font size.
      See Also:
    • DEFAULT_NORMAL

      public static final SimpleAttributeSet DEFAULT_NORMAL
      the attribute set for normal code.
    • DEFAULT_COMMENT

      public static final SimpleAttributeSet DEFAULT_COMMENT
      the attribute set for comments.
    • DEFAULT_STRING

      public static final SimpleAttributeSet DEFAULT_STRING
      the attribute set for strings.
    • DEFAULT_KEYWORD

      public static final SimpleAttributeSet DEFAULT_KEYWORD
      the attribute set for keywords.
  • Constructor Details

    • SyntaxDocument

      public SyntaxDocument(Properties props)
      Initializes the document.
      Parameters:
      props - the properties to obtain the setup from
  • Method Details

    • setAttributeFont

      public void setAttributeFont(SyntaxDocument.ATTR_TYPE attr, int style)
      Sets the font of the specified attribute.
      Parameters:
      attr - the attribute to apply this font to (normal, comment, string)
      style - font style (Font.BOLD, Font.ITALIC, Font.PLAIN)
    • setAttributeFont

      public static void setAttributeFont(MutableAttributeSet attr, Font f)
      Sets the font of the specified attribute.
      Parameters:
      attr - attribute to apply this font to
      f - the font to use
    • setAttributeColor

      public void setAttributeColor(SyntaxDocument.ATTR_TYPE attr, Color c)
      Sets the foreground (font) color of the specified attribute.
      Parameters:
      attr - the attribute to apply this font to (normal, comment, string)
      c - the color to use
    • setAttributeColor

      public static void setAttributeColor(MutableAttributeSet attr, Color c)
      Sets the foreground (font) color of the specified attribute.
      Parameters:
      attr - attribute to apply this color to
      c - the color to use
    • addKeywords

      public void addKeywords(String[] keywords, MutableAttributeSet attr)
      Associates the keywords with a particular formatting style.
      Parameters:
      keywords - the tokens or words to format
      attr - how to format the keywords
    • addKeyword

      public void addKeyword(String keyword, MutableAttributeSet attr)
      Associates a keyword with a particular formatting style.
      Parameters:
      keyword - the token or word to format
      attr - how to format keyword
    • getKeywordFormatting

      public MutableAttributeSet getKeywordFormatting(String keyword)
      Gets the formatting for a keyword.
      Parameters:
      keyword - the token or word to stop formatting
      Returns:
      how keyword is formatted, or null if no formatting is applied to it
    • removeKeyword

      public void removeKeyword(String keyword)
      Removes an association between a keyword with a particular formatting style.
      Parameters:
      keyword - the token or word to stop formatting
    • setTabs

      public void setTabs(int charactersPerTab)
      sets the number of characters per tab.
      Parameters:
      charactersPerTab - the characters per tab
    • insertString

      public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
      Override to apply syntax highlighting after the document has been updated.
      Specified by:
      insertString in interface Document
      Overrides:
      insertString in class AbstractDocument
      Parameters:
      offset - the offset
      str - the string to insert
      a - the attribute set, can be null
      Throws:
      BadLocationException - if offset is invalid
    • remove

      public void remove(int offset, int length) throws BadLocationException
      Applies syntax highlighting after the document has been updated.
      Specified by:
      remove in interface Document
      Overrides:
      remove in class AbstractDocument
      Parameters:
      offset - the offset of the deletion
      length - the length of the deletion
      Throws:
      BadLocationException - if offsets are invalid
    • processChangedLines

      public void processChangedLines(int offset, int length) throws BadLocationException
      Determine how many lines have been changed, then apply highlighting to each line.
      Parameters:
      offset - the offset of the changed lines
      length - the length of the change
      Throws:
      BadLocationException - if offset is invalid
    • isDelimiter

      public boolean isDelimiter(String character)
      Checks whether the character is a delimiter.
      Parameters:
      character - the character to check
      Returns:
      true if a delimiter
    • isQuoteDelimiter

      public boolean isQuoteDelimiter(String character)
      Checks whether the character is quote delimiter.
      Parameters:
      character - the character to check
      Returns:
      true if a quote delimiter
    • escapeQuote

      public String escapeQuote(String quoteDelimiter)
      Escapes the quote delimiter.
      Parameters:
      quoteDelimiter - the string to escape
      Returns:
      the escaped string
    • getFontSize

      public int getFontSize()
      gets the current font size.
      Returns:
      the font size
    • setFontSize

      public void setFontSize(int fontSize)
      sets the current font size (affects all built-in styles).
      Parameters:
      fontSize - the size
    • getFontName

      public String getFontName()
      gets the current font family.
      Returns:
      the font name
    • setFontName

      public void setFontName(String fontName)
      sets the current font family (affects all built-in styles).
      Parameters:
      fontName - the font name
    • setIndentationSize

      public void setIndentationSize(int value)
      Sets the number of blanks to use for indentation.
      Parameters:
      value - the number of blanks
    • getIndentationSize

      public int getIndentationSize()
      Returns the number of blanks used for indentation.
      Returns:
      the number of blanks
    • setDelimiters

      public void setDelimiters(String value)
      Sets the delimiter characters to use.
      Parameters:
      value - the characters
    • getDelimiters

      public String getDelimiters()
      Returns the delimiter characters to use.
      Returns:
      the characters
    • setQuoteDelimiters

      public void setQuoteDelimiters(String value)
      Sets the quote delimiter characters to use.
      Parameters:
      value - the characters
    • getQuoteDelimiters

      public String getQuoteDelimiters()
      Returns the quote delimiter characters to use.
      Returns:
      the characters
    • setQuoteEscape

      public void setQuoteEscape(String value)
      Sets the character to use for escaping a quote character.
      Parameters:
      value - the character
    • getQuoteEscape

      public String getQuoteEscape()
      Returns the character for escaping a quote delimiter.
      Returns:
      the character
    • setSingleLineCommentStart

      public void setSingleLineCommentStart(String value)
      Sets the string that is the start of a single-line comment.
      Parameters:
      value - the string
    • getSingleLineCommentStart

      public String getSingleLineCommentStart()
      Retrusn the single line comment start string.
      Returns:
      the start string
    • setMultiLineCommentStart

      public void setMultiLineCommentStart(String value)
      Sets the string that is the start of a multi-line comment.
      Parameters:
      value - the string
    • getMultiLineCommentStart

      public String getMultiLineCommentStart()
      Returns the string that is the start of a multi-line comment.
      Returns:
      the string
    • setMultiLineCommentEnd

      public void setMultiLineCommentEnd(String value)
      Sets the string that is the end of a multi-line comment.
      Parameters:
      value - the string
    • getMultiLineCommentEnd

      public String getMultiLineCommentEnd()
      Returns the end of a multi-line comment.
      Returns:
      the end string
    • setBlockStart

      public void setBlockStart(String value)
      Sets the string that is the start of a block.
      Parameters:
      value - the string
    • getBlockStart

      public String getBlockStart()
      Returns the start of a block.
      Returns:
      the end string
    • setBlockEnd

      public void setBlockEnd(String value)
      Sets the string that is the end of a block.
      Parameters:
      value - the string
    • getBlockEnd

      public String getBlockEnd()
      Returns the end of a block.
      Returns:
      the end string
    • setAddMatchingEndBlocks

      public void setAddMatchingEndBlocks(boolean value)
      Sets whether matching block ends are inserted or not.
      Parameters:
      value - if true then matching block ends are inserted
    • getAddMatchingEndBlocks

      public boolean getAddMatchingEndBlocks()
      Returns whether matching block ends are inserted or not.
      Returns:
      true if matching block ends are inserted
    • setUseBlanks

      public void setUseBlanks(boolean value)
      Sets whether to use blanks instead of tabs.
      Parameters:
      value - if true then blanks are used instead of tabs
    • getUseBlanks

      public boolean getUseBlanks()
      Returns whether blanks are used instead of tabs.
      Returns:
      true if blanks are used instead of tabs
    • setBackgroundColor

      public void setBackgroundColor(Color value)
      Sets the background color.
      Parameters:
      value - the background color
    • getBackgroundColor

      public Color getBackgroundColor()
      Returns the background color.
      Returns:
      the background color
    • setMultiLineComment

      public void setMultiLineComment(boolean value)
      Sets whether to enable multi-line comments.
      Parameters:
      value - if true then multi-line comments are enabled
    • getMultiLineComment

      public boolean getMultiLineComment()
      Returns whether multi-line comments are enabled.
      Returns:
      true if multi-line comments are enabled
    • setCaseSensitive

      public void setCaseSensitive(boolean value)
      Sets whether the keywords are case-sensitive or not.
      Parameters:
      value - if true then keywords are treated case-sensitive
    • getCaseSensitive

      public boolean getCaseSensitive()
      Returns whether blanks are used instead of tabs.
      Returns:
      true if keywords are case-sensitive