Package org.fife.ui.rsyntaxtextarea
Class SyntaxScheme
- java.lang.Object
-
- org.fife.ui.rsyntaxtextarea.SyntaxScheme
-
- All Implemented Interfaces:
java.lang.Cloneable
,TokenTypes
public class SyntaxScheme extends java.lang.Object implements java.lang.Cloneable, TokenTypes
The set of colors and styles used by anRSyntaxTextArea
to color tokens.You can use this class to programmatically set the fonts and colors used in an RSyntaxTextArea, but for more powerful, externalized control, consider using
Theme
s instead.- Version:
- 1.0
- See Also:
Theme
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SyntaxScheme.SyntaxSchemeLoader
Loads aSyntaxScheme
from an XML file.
-
Field Summary
Fields Modifier and Type Field Description private Style[]
styles
private static java.lang.String
VERSION
-
Fields inherited from interface org.fife.ui.rsyntaxtextarea.TokenTypes
ANNOTATION, COMMENT_DOCUMENTATION, COMMENT_EOL, COMMENT_KEYWORD, COMMENT_MARKUP, COMMENT_MULTILINE, DATA_TYPE, DEFAULT_NUM_TOKEN_TYPES, ERROR_CHAR, ERROR_IDENTIFIER, ERROR_NUMBER_FORMAT, ERROR_STRING_DOUBLE, FUNCTION, IDENTIFIER, LITERAL_BACKQUOTE, LITERAL_BOOLEAN, LITERAL_CHAR, LITERAL_NUMBER_DECIMAL_INT, LITERAL_NUMBER_FLOAT, LITERAL_NUMBER_HEXADECIMAL, LITERAL_STRING_DOUBLE_QUOTE, MARKUP_CDATA, MARKUP_CDATA_DELIMITER, MARKUP_COMMENT, MARKUP_DTD, MARKUP_ENTITY_REFERENCE, MARKUP_PROCESSING_INSTRUCTION, MARKUP_TAG_ATTRIBUTE, MARKUP_TAG_ATTRIBUTE_VALUE, MARKUP_TAG_DELIMITER, MARKUP_TAG_NAME, NULL, OPERATOR, PREPROCESSOR, REGEX, RESERVED_WORD, RESERVED_WORD_2, SEPARATOR, VARIABLE, WHITESPACE
-
-
Constructor Summary
Constructors Constructor Description SyntaxScheme(boolean useDefaults)
Creates a color scheme that either has all color values set to a default value or set tonull
.SyntaxScheme(java.awt.Font baseFont)
Creates a default color scheme.SyntaxScheme(java.awt.Font baseFont, boolean fontStyles)
Creates a default color scheme.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
changeBaseFont(java.awt.Font old, java.awt.Font font)
Changes the "base font" for this syntax scheme.java.lang.Object
clone()
Returns a deep copy of this color scheme.boolean
equals(java.lang.Object otherScheme)
Tests whether this color scheme is the same as another color scheme.private static java.lang.String
getHexString(java.awt.Color c)
Returns a hex string representing an RGB color, of the form"$rrggbb"
.Style
getStyle(int index)
Returns the specified style.int
getStyleCount()
Returns the number of styles.Style[]
getStyles()
Used by third party implementors e.g.int
hashCode()
This is implemented to be consistent withequals(Object)
.static SyntaxScheme
load(java.awt.Font baseFont, java.io.InputStream in)
Loads a syntax scheme from an input stream.static SyntaxScheme
loadFromString(java.lang.String string)
Loads a syntax highlighting color scheme from a string created fromtoCommaSeparatedString
.static SyntaxScheme
loadFromString(java.lang.String string, int tokenTypeCount)
Loads a syntax highlighting color scheme from a string created fromtoCommaSeparatedString
.(package private) void
refreshFontMetrics(java.awt.Graphics2D g2d)
void
restoreDefaults(java.awt.Font baseFont)
Restores all colors and fonts to their default values.void
restoreDefaults(java.awt.Font baseFont, boolean fontStyles)
Restores all colors and fonts to their default values.void
setStyle(int type, Style style)
Sets a style to use when rendering a token type.void
setStyles(Style[] styles)
Used by third party implementors e.g.private static java.awt.Color
stringToColor(java.lang.String s)
Returns the color represented by a string.java.lang.String
toCommaSeparatedString()
Returns this syntax highlighting scheme as a comma-separated list of values as follows: If a color is non-null, it is added as a 24-bit integer of the form((r<*lt;16) | (g<*lt;8) | (b))
; if it isnull
, it is added as "-,".
-
-
-
Field Detail
-
styles
private Style[] styles
-
VERSION
private static final java.lang.String VERSION
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SyntaxScheme
public SyntaxScheme(boolean useDefaults)
Creates a color scheme that either has all color values set to a default value or set tonull
.- Parameters:
useDefaults
- Iftrue
, all color values will be set to default colors; iffalse
, all colors will be initiallynull
.
-
SyntaxScheme
public SyntaxScheme(java.awt.Font baseFont)
Creates a default color scheme.- Parameters:
baseFont
- The base font to use. Keywords will be a bold version of this font, and comments will be an italicized version of this font.
-
SyntaxScheme
public SyntaxScheme(java.awt.Font baseFont, boolean fontStyles)
Creates a default color scheme.- Parameters:
baseFont
- The base font to use. Keywords will be a bold version of this font, and comments will be an italicized version of this font.fontStyles
- Whether bold and italic should be used in the scheme (vs. all tokens using a plain font).
-
-
Method Detail
-
changeBaseFont
void changeBaseFont(java.awt.Font old, java.awt.Font font)
Changes the "base font" for this syntax scheme. This is called byRSyntaxTextArea
when its font changes viasetFont()
. This looks for tokens that use a derivative of the text area's old font (but bolded and/or italicized) and makes them use the new font with those styles instead. This is desirable because most programmers prefer a single font to be used in their text editor, but might want bold (say for keywords) or italics.- Parameters:
old
- The old font of the text area.font
- The new font of the text area.
-
clone
public java.lang.Object clone()
Returns a deep copy of this color scheme.- Overrides:
clone
in classjava.lang.Object
- Returns:
- The copy.
-
equals
public boolean equals(java.lang.Object otherScheme)
Tests whether this color scheme is the same as another color scheme.- Overrides:
equals
in classjava.lang.Object
- Parameters:
otherScheme
- The color scheme to compare to.- Returns:
true
if this color scheme andotherScheme
are the same scheme;false
otherwise.
-
getHexString
private static java.lang.String getHexString(java.awt.Color c)
Returns a hex string representing an RGB color, of the form"$rrggbb"
.- Parameters:
c
- The color.- Returns:
- The string representation of the color.
-
getStyle
public Style getStyle(int index)
Returns the specified style.- Parameters:
index
- The index of the style.- Returns:
- The style.
- See Also:
setStyle(int, Style)
,getStyleCount()
-
getStyleCount
public int getStyleCount()
Returns the number of styles.- Returns:
- The number of styles.
- See Also:
getStyle(int)
-
getStyles
public Style[] getStyles()
Used by third party implementors e.g. SquirreL SQL. Most applications do not need to call this method.Note that the returned array is not a copy of the style data; editing the array will modify the styles used by any
RSyntaxTextArea
using this scheme.- Returns:
- The style array.
- See Also:
setStyles(Style[])
-
hashCode
public int hashCode()
This is implemented to be consistent withequals(Object)
. This is a requirement to keep FindBugs happy.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- The hash code for this object.
-
load
public static SyntaxScheme load(java.awt.Font baseFont, java.io.InputStream in) throws java.io.IOException
Loads a syntax scheme from an input stream.Consider using the
Theme
class for saving and loading RSTA styles rather than using this API.- Parameters:
baseFont
- The font to use as the "base" for the syntax scheme. If this isnull
, a default monospaced font is used.in
- The stream to load from. It is up to the caller to close this stream when they are done.- Returns:
- The syntax scheme.
- Throws:
java.io.IOException
- If an IO error occurs.
-
loadFromString
public static SyntaxScheme loadFromString(java.lang.String string)
Loads a syntax highlighting color scheme from a string created fromtoCommaSeparatedString
. This method is useful for saving and restoring color schemes.Consider using the
Theme
class for saving and loading RSTA styles rather than using this API.- Parameters:
string
- A string generated fromtoCommaSeparatedString()
.- Returns:
- A color scheme.
- See Also:
toCommaSeparatedString()
-
loadFromString
public static SyntaxScheme loadFromString(java.lang.String string, int tokenTypeCount)
Loads a syntax highlighting color scheme from a string created fromtoCommaSeparatedString
. This method is useful for saving and restoring color schemes.Consider using the
Theme
class for saving and loading RSTA styles rather than using this API.- Parameters:
string
- A string generated fromtoCommaSeparatedString()
.tokenTypeCount
- The number of token types saved in this string. This should be the number of token types saved by your custom SyntaxScheme subclass, orTokenTypes.DEFAULT_NUM_TOKEN_TYPES
if you used the standard implementation (which most people will).- Returns:
- A color scheme.
- See Also:
loadFromString(String)
,toCommaSeparatedString()
-
refreshFontMetrics
void refreshFontMetrics(java.awt.Graphics2D g2d)
-
restoreDefaults
public void restoreDefaults(java.awt.Font baseFont)
Restores all colors and fonts to their default values.- Parameters:
baseFont
- The base font to use when creating this scheme. If this isnull
, then a default monospaced font is used.
-
restoreDefaults
public void restoreDefaults(java.awt.Font baseFont, boolean fontStyles)
Restores all colors and fonts to their default values.- Parameters:
baseFont
- The base font to use when creating this scheme. If this isnull
, then a default monospaced font is used.fontStyles
- Whether bold and italic should be used in the scheme (vs. all tokens using a plain font).
-
setStyle
public void setStyle(int type, Style style)
Sets a style to use when rendering a token type.- Parameters:
type
- The token type.style
- The style for the token type.- See Also:
getStyle(int)
-
setStyles
public void setStyles(Style[] styles)
Used by third party implementors e.g. SquirreL SQL. Most applications do not need to call this method; individual styles can be set viasetStyle(int, Style)
.- Parameters:
styles
- The new array of styles to use. Note that this should have length of at leastTokenTypes.DEFAULT_NUM_TOKEN_TYPES
.- See Also:
setStyle(int, Style)
,getStyles()
-
stringToColor
private static java.awt.Color stringToColor(java.lang.String s)
Returns the color represented by a string. If the first char in the string is '$
', it is assumed to be in hex, otherwise it is assumed to be decimal. So, for example, both of these:"$00ff00" "65280"
will returnnew Color(0, 255, 0)
.- Parameters:
s
- The string to evaluate.- Returns:
- The color.
-
toCommaSeparatedString
public java.lang.String toCommaSeparatedString()
Returns this syntax highlighting scheme as a comma-separated list of values as follows:- If a color is non-null, it is added as a 24-bit integer
of the form
((r<*lt;16) | (g<*lt;8) | (b))
; if it isnull
, it is added as "-,". - The font and style (bold/italic) is added as an integer like so: "family, style, size".
- The entire syntax highlighting scheme is thus one long string of
color schemes of the format "i,[fg],[bg],uline,[style],
where:
i
is the index of the syntax scheme.- fg and bg are the foreground and background
colors for the scheme, and may be null (represented by
-
). uline
is whether the font should be underlined, and is eithert
orf
.style
is thefamily,style,size
triplet described above.
- Returns:
- A string representing the rgb values of the colors.
- See Also:
loadFromString(String)
- If a color is non-null, it is added as a 24-bit integer
of the form
-
-