Package weka.gui.scripting
Class SyntaxDocument
- java.lang.Object
-
- javax.swing.text.AbstractDocument
-
- javax.swing.text.DefaultStyledDocument
-
- weka.gui.scripting.SyntaxDocument
-
- All Implemented Interfaces:
java.io.Serializable,javax.swing.text.Document,javax.swing.text.StyledDocument
public class SyntaxDocument extends javax.swing.text.DefaultStyledDocumentHighlights 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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSyntaxDocument.ATTR_TYPEThe attribute type.-
Nested classes/interfaces inherited from class javax.swing.text.DefaultStyledDocument
javax.swing.text.DefaultStyledDocument.AttributeUndoableEdit, javax.swing.text.DefaultStyledDocument.ElementBuffer, javax.swing.text.DefaultStyledDocument.ElementSpec
-
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
javax.swing.text.AbstractDocument.AbstractElement, javax.swing.text.AbstractDocument.AttributeContext, javax.swing.text.AbstractDocument.BranchElement, javax.swing.text.AbstractDocument.Content, javax.swing.text.AbstractDocument.DefaultDocumentEvent, javax.swing.text.AbstractDocument.ElementEdit, javax.swing.text.AbstractDocument.LeafElement
-
-
Field Summary
Fields Modifier and Type Field Description static javax.swing.text.SimpleAttributeSetDEFAULT_COMMENTthe attribute set for comments.static java.lang.StringDEFAULT_FONT_FAMILYthe font family.static intDEFAULT_FONT_SIZEthe font size.static javax.swing.text.SimpleAttributeSetDEFAULT_KEYWORDthe attribute set for keywords.static javax.swing.text.SimpleAttributeSetDEFAULT_NORMALthe attribute set for normal code.static javax.swing.text.SimpleAttributeSetDEFAULT_STRINGthe attribute set for strings.static intMAX_TABSthe maximum number of tabs.
-
Constructor Summary
Constructors Constructor Description SyntaxDocument(java.util.Properties props)Initializes the document.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddKeyword(java.lang.String keyword, javax.swing.text.MutableAttributeSet attr)Associates a keyword with a particular formatting style.voidaddKeywords(java.lang.String[] keywords, javax.swing.text.MutableAttributeSet attr)Associates the keywords with a particular formatting style.java.lang.StringescapeQuote(java.lang.String quoteDelimiter)Escapes the quote delimiter.booleangetAddMatchingEndBlocks()Returns whether matching block ends are inserted or not.java.awt.ColorgetBackgroundColor()Returns the background color.java.lang.StringgetBlockEnd()Returns the end of a block.java.lang.StringgetBlockStart()Returns the start of a block.booleangetCaseSensitive()Returns whether blanks are used instead of tabs.java.lang.StringgetDelimiters()Returns the delimiter characters to use.java.lang.StringgetFontName()gets the current font family.intgetFontSize()gets the current font size.intgetIndentationSize()Returns the number of blanks used for indentation.javax.swing.text.MutableAttributeSetgetKeywordFormatting(java.lang.String keyword)Gets the formatting for a keyword.booleangetMultiLineComment()Returns whether multi-line comments are enabled.java.lang.StringgetMultiLineCommentEnd()Returns the end of a multi-line comment.java.lang.StringgetMultiLineCommentStart()Returns the string that is the start of a multi-line comment.java.lang.StringgetQuoteDelimiters()Returns the quote delimiter characters to use.java.lang.StringgetQuoteEscape()Returns the character for escaping a quote delimiter.java.lang.StringgetSingleLineCommentStart()Retrusn the single line comment start string.booleangetUseBlanks()Returns whether blanks are used instead of tabs.voidinsertString(int offset, java.lang.String str, javax.swing.text.AttributeSet a)Override to apply syntax highlighting after the document has been updated.booleanisDelimiter(java.lang.String character)Checks whether the character is a delimiter.booleanisQuoteDelimiter(java.lang.String character)Checks whether the character is quote delimiter.voidprocessChangedLines(int offset, int length)Determine how many lines have been changed, then apply highlighting to each line.voidremove(int offset, int length)Applies syntax highlighting after the document has been updated.voidremoveKeyword(java.lang.String keyword)Removes an association between a keyword with a particular formatting style.voidsetAddMatchingEndBlocks(boolean value)Sets whether matching block ends are inserted or not.static voidsetAttributeColor(javax.swing.text.MutableAttributeSet attr, java.awt.Color c)Sets the foreground (font) color of the specified attribute.voidsetAttributeColor(SyntaxDocument.ATTR_TYPE attr, java.awt.Color c)Sets the foreground (font) color of the specified attribute.static voidsetAttributeFont(javax.swing.text.MutableAttributeSet attr, java.awt.Font f)Sets the font of the specified attribute.voidsetAttributeFont(SyntaxDocument.ATTR_TYPE attr, int style)Sets the font of the specified attribute.voidsetBackgroundColor(java.awt.Color value)Sets the background color.voidsetBlockEnd(java.lang.String value)Sets the string that is the end of a block.voidsetBlockStart(java.lang.String value)Sets the string that is the start of a block.voidsetCaseSensitive(boolean value)Sets whether the keywords are case-sensitive or not.voidsetDelimiters(java.lang.String value)Sets the delimiter characters to use.voidsetFontName(java.lang.String fontName)sets the current font family (affects all built-in styles).voidsetFontSize(int fontSize)sets the current font size (affects all built-in styles).voidsetIndentationSize(int value)Sets the number of blanks to use for indentation.voidsetMultiLineComment(boolean value)Sets whether to enable multi-line comments.voidsetMultiLineCommentEnd(java.lang.String value)Sets the string that is the end of a multi-line comment.voidsetMultiLineCommentStart(java.lang.String value)Sets the string that is the start of a multi-line comment.voidsetQuoteDelimiters(java.lang.String value)Sets the quote delimiter characters to use.voidsetQuoteEscape(java.lang.String value)Sets the character to use for escaping a quote character.voidsetSingleLineCommentStart(java.lang.String value)Sets the string that is the start of a single-line comment.voidsetTabs(int charactersPerTab)sets the number of characters per tab.voidsetUseBlanks(boolean value)Sets whether to use blanks instead of tabs.-
Methods inherited from class javax.swing.text.DefaultStyledDocument
addDocumentListener, addStyle, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, removeDocumentListener, removeElement, removeStyle, setCharacterAttributes, setLogicalStyle, setParagraphAttributes
-
Methods inherited from class javax.swing.text.AbstractDocument
addUndoableEditListener, createPosition, dump, getAsynchronousLoadPriority, getBidiRootElement, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, putProperty, readLock, readUnlock, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties
-
-
-
-
Field Detail
-
MAX_TABS
public static final int MAX_TABS
the maximum number of tabs.- See Also:
- Constant Field Values
-
DEFAULT_FONT_FAMILY
public static final java.lang.String DEFAULT_FONT_FAMILY
the font family.- See Also:
- Constant Field Values
-
DEFAULT_FONT_SIZE
public static final int DEFAULT_FONT_SIZE
the font size.- See Also:
- Constant Field Values
-
DEFAULT_NORMAL
public static final javax.swing.text.SimpleAttributeSet DEFAULT_NORMAL
the attribute set for normal code.
-
DEFAULT_COMMENT
public static final javax.swing.text.SimpleAttributeSet DEFAULT_COMMENT
the attribute set for comments.
-
DEFAULT_STRING
public static final javax.swing.text.SimpleAttributeSet DEFAULT_STRING
the attribute set for strings.
-
DEFAULT_KEYWORD
public static final javax.swing.text.SimpleAttributeSet DEFAULT_KEYWORD
the attribute set for keywords.
-
-
Method Detail
-
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(javax.swing.text.MutableAttributeSet attr, java.awt.Font f)Sets the font of the specified attribute.- Parameters:
attr- attribute to apply this font tof- the font to use
-
setAttributeColor
public void setAttributeColor(SyntaxDocument.ATTR_TYPE attr, java.awt.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(javax.swing.text.MutableAttributeSet attr, java.awt.Color c)Sets the foreground (font) color of the specified attribute.- Parameters:
attr- attribute to apply this color toc- the color to use
-
addKeywords
public void addKeywords(java.lang.String[] keywords, javax.swing.text.MutableAttributeSet attr)Associates the keywords with a particular formatting style.- Parameters:
keywords- the tokens or words to formatattr- how to format the keywords
-
addKeyword
public void addKeyword(java.lang.String keyword, javax.swing.text.MutableAttributeSet attr)Associates a keyword with a particular formatting style.- Parameters:
keyword- the token or word to formatattr- how to format keyword
-
getKeywordFormatting
public javax.swing.text.MutableAttributeSet getKeywordFormatting(java.lang.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(java.lang.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, java.lang.String str, javax.swing.text.AttributeSet a) throws javax.swing.text.BadLocationExceptionOverride to apply syntax highlighting after the document has been updated.- Specified by:
insertStringin interfacejavax.swing.text.Document- Overrides:
insertStringin classjavax.swing.text.AbstractDocument- Parameters:
offset- the offsetstr- the string to inserta- the attribute set, can be null- Throws:
javax.swing.text.BadLocationException- if offset is invalid
-
remove
public void remove(int offset, int length) throws javax.swing.text.BadLocationExceptionApplies syntax highlighting after the document has been updated.- Specified by:
removein interfacejavax.swing.text.Document- Overrides:
removein classjavax.swing.text.AbstractDocument- Parameters:
offset- the offset of the deletionlength- the length of the deletion- Throws:
javax.swing.text.BadLocationException- if offsets are invalid
-
processChangedLines
public void processChangedLines(int offset, int length) throws javax.swing.text.BadLocationExceptionDetermine how many lines have been changed, then apply highlighting to each line.- Parameters:
offset- the offset of the changed lineslength- the length of the change- Throws:
javax.swing.text.BadLocationException- if offset is invalid
-
isDelimiter
public boolean isDelimiter(java.lang.String character)
Checks whether the character is a delimiter.- Parameters:
character- the character to check- Returns:
- true if a delimiter
-
isQuoteDelimiter
public boolean isQuoteDelimiter(java.lang.String character)
Checks whether the character is quote delimiter.- Parameters:
character- the character to check- Returns:
- true if a quote delimiter
-
escapeQuote
public java.lang.String escapeQuote(java.lang.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 java.lang.String getFontName()
gets the current font family.- Returns:
- the font name
-
setFontName
public void setFontName(java.lang.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(java.lang.String value)
Sets the delimiter characters to use.- Parameters:
value- the characters
-
getDelimiters
public java.lang.String getDelimiters()
Returns the delimiter characters to use.- Returns:
- the characters
-
setQuoteDelimiters
public void setQuoteDelimiters(java.lang.String value)
Sets the quote delimiter characters to use.- Parameters:
value- the characters
-
getQuoteDelimiters
public java.lang.String getQuoteDelimiters()
Returns the quote delimiter characters to use.- Returns:
- the characters
-
setQuoteEscape
public void setQuoteEscape(java.lang.String value)
Sets the character to use for escaping a quote character.- Parameters:
value- the character
-
getQuoteEscape
public java.lang.String getQuoteEscape()
Returns the character for escaping a quote delimiter.- Returns:
- the character
-
setSingleLineCommentStart
public void setSingleLineCommentStart(java.lang.String value)
Sets the string that is the start of a single-line comment.- Parameters:
value- the string
-
getSingleLineCommentStart
public java.lang.String getSingleLineCommentStart()
Retrusn the single line comment start string.- Returns:
- the start string
-
setMultiLineCommentStart
public void setMultiLineCommentStart(java.lang.String value)
Sets the string that is the start of a multi-line comment.- Parameters:
value- the string
-
getMultiLineCommentStart
public java.lang.String getMultiLineCommentStart()
Returns the string that is the start of a multi-line comment.- Returns:
- the string
-
setMultiLineCommentEnd
public void setMultiLineCommentEnd(java.lang.String value)
Sets the string that is the end of a multi-line comment.- Parameters:
value- the string
-
getMultiLineCommentEnd
public java.lang.String getMultiLineCommentEnd()
Returns the end of a multi-line comment.- Returns:
- the end string
-
setBlockStart
public void setBlockStart(java.lang.String value)
Sets the string that is the start of a block.- Parameters:
value- the string
-
getBlockStart
public java.lang.String getBlockStart()
Returns the start of a block.- Returns:
- the end string
-
setBlockEnd
public void setBlockEnd(java.lang.String value)
Sets the string that is the end of a block.- Parameters:
value- the string
-
getBlockEnd
public java.lang.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(java.awt.Color value)
Sets the background color.- Parameters:
value- the background color
-
getBackgroundColor
public java.awt.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
-
-