Package org.eclipse.jface.viewers
Class StyledString
- java.lang.Object
-
- org.eclipse.jface.viewers.StyledString
-
- All Implemented Interfaces:
CharSequence
public class StyledString extends Object implements CharSequence
A mutable string with styled ranges. All ranges mark substrings of the string and do not overlap. Styles are applied using instances ofStyledString.Styler
to compute the result ofgetStyleRanges()
. The styled string can be built in the following two ways:- new strings with stylers can be appended
- stylers can by applied to ranges of the existing string
This class may be instantiated; it is not intended to be subclassed.
- Since:
- 3.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StyledString.Styler
A styler will be asked to apply its styles to one ore more ranges in theStyledString
.
-
Field Summary
Fields Modifier and Type Field Description static StyledString.Styler
COUNTER_STYLER
A built-in styler using theJFacePreferences.COUNTER_COLOR
managed in the JFace color registry (SeeJFaceResources.getColorRegistry()
).static StyledString.Styler
DECORATIONS_STYLER
A built-in styler using theJFacePreferences.DECORATIONS_COLOR
managed in the JFace color registry (SeeJFaceResources.getColorRegistry()
).static StyledString.Styler
QUALIFIER_STYLER
A built-in styler using theJFacePreferences.QUALIFIER_COLOR
managed in the JFace color registry (SeeJFaceResources.getColorRegistry()
).
-
Constructor Summary
Constructors Constructor Description StyledString()
Creates an emptyStyledString
.StyledString(String string)
Creates anStyledString
initialized with a string without a style associated.StyledString(String string, StyledString.Styler styler)
Creates anStyledString
initialized with a string and a style.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StyledString
append(char ch)
Appends the string representation of the given character to theStyledString
.StyledString
append(char[] chars)
Appends the string representation of the given character array to theStyledString
.StyledString
append(char[] chars, StyledString.Styler styler)
Appends the string representation of the given character array with a style to theStyledString
.StyledString
append(char ch, StyledString.Styler styler)
Appends the string representation of the given character with a style to theStyledString
.StyledString
append(String string)
Appends a string to theStyledString
.StyledString
append(String string, StyledString.Styler styler)
Appends a string with a style to theStyledString
.StyledString
append(StyledString string)
Appends a string with styles to theStyledString
.char
charAt(int index)
static StyledString.Styler
createColorRegistryStyler(String foregroundColorName, String backgroundColorName)
Creates a styler that takes the given foreground and background colors from the JFace color registry.String
getString()
Returns the string of thisStyledString
.StyleRange[]
getStyleRanges()
Returns an array ofStyleRange
resulting from applying all associated stylers for this string builder.StyledString
insert(char ch, int offset)
Inserts the character at the given offset.int
length()
Returns the length of the string of thisStyledString
.void
setStyle(int offset, int length, StyledString.Styler styler)
Sets a styler to use for the given source range.CharSequence
subSequence(int start, int end)
String
toString()
Returns the string of thisStyledString
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.CharSequence
chars, codePoints
-
-
-
-
Field Detail
-
QUALIFIER_STYLER
public static final StyledString.Styler QUALIFIER_STYLER
A built-in styler using theJFacePreferences.QUALIFIER_COLOR
managed in the JFace color registry (SeeJFaceResources.getColorRegistry()
).
-
COUNTER_STYLER
public static final StyledString.Styler COUNTER_STYLER
A built-in styler using theJFacePreferences.COUNTER_COLOR
managed in the JFace color registry (SeeJFaceResources.getColorRegistry()
).
-
DECORATIONS_STYLER
public static final StyledString.Styler DECORATIONS_STYLER
A built-in styler using theJFacePreferences.DECORATIONS_COLOR
managed in the JFace color registry (SeeJFaceResources.getColorRegistry()
).
-
-
Constructor Detail
-
StyledString
public StyledString()
Creates an emptyStyledString
.
-
StyledString
public StyledString(String string)
Creates anStyledString
initialized with a string without a style associated.- Parameters:
string
- the string
-
StyledString
public StyledString(String string, StyledString.Styler styler)
Creates anStyledString
initialized with a string and a style.- Parameters:
string
- the stringstyler
- the styler for the string ornull
to not associated a styler.
-
-
Method Detail
-
createColorRegistryStyler
public static StyledString.Styler createColorRegistryStyler(String foregroundColorName, String backgroundColorName)
Creates a styler that takes the given foreground and background colors from the JFace color registry.- Parameters:
foregroundColorName
- the color name for the foreground colorbackgroundColorName
- the color name for the background color- Returns:
- the created style
-
getString
public String getString()
Returns the string of thisStyledString
.- Returns:
- the current string of this
StyledString
.
-
toString
public String toString()
Returns the string of thisStyledString
.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
- Returns:
- the current string of this
StyledString
.
-
length
public int length()
Returns the length of the string of thisStyledString
.- Specified by:
length
in interfaceCharSequence
- Returns:
- the length of the current string
-
subSequence
public CharSequence subSequence(int start, int end)
- Specified by:
subSequence
in interfaceCharSequence
- Since:
- 3.12
-
charAt
public char charAt(int index)
- Specified by:
charAt
in interfaceCharSequence
- Since:
- 3.12
-
append
public StyledString append(String string)
Appends a string to theStyledString
. The appended string will have no associated styler.- Parameters:
string
- the string to append- Returns:
- returns a reference to this object
-
append
public StyledString append(char[] chars)
Appends the string representation of the given character array to theStyledString
. The appended character array will have no associated styler.- Parameters:
chars
- the character array to append- Returns:
- returns a reference to this object
-
append
public StyledString append(char ch)
Appends the string representation of the given character to theStyledString
. The appended character will have no associated styler.- Parameters:
ch
- the character to append- Returns:
- returns a reference to this object
-
append
public StyledString append(StyledString string)
Appends a string with styles to theStyledString
.- Parameters:
string
- the string to append- Returns:
- returns a reference to this object
-
append
public StyledString append(char ch, StyledString.Styler styler)
Appends the string representation of the given character with a style to theStyledString
. The appended character will have the given style associated.- Parameters:
ch
- the character to appendstyler
- the styler to use for styling the character to append ornull
if no styler should be associated with the appended character- Returns:
- returns a reference to this object
-
append
public StyledString append(String string, StyledString.Styler styler)
Appends a string with a style to theStyledString
. The appended string will be styled using the given styler.- Parameters:
string
- the string to appendstyler
- the styler to use for styling the string to append ornull
if no styler should be associated with the appended string.- Returns:
- returns a reference to this object
-
append
public StyledString append(char[] chars, StyledString.Styler styler)
Appends the string representation of the given character array with a style to theStyledString
. The appended character array will be styled using the given styler.- Parameters:
chars
- the character array to appendstyler
- the styler to use for styling the character array to append ornull
if no styler should be associated with the appended character array- Returns:
- returns a reference to this object
-
insert
public StyledString insert(char ch, int offset) throws StringIndexOutOfBoundsException
Inserts the character at the given offset. The inserted character will get the styler that is already at the given offset.- Parameters:
ch
- the character to insertoffset
- the insertion index- Returns:
- returns a reference to this object
- Throws:
StringIndexOutOfBoundsException
- ifoffset
is less than zero, or ifoffset
is greater than the length of this object- Since:
- 3.5
-
setStyle
public void setStyle(int offset, int length, StyledString.Styler styler) throws StringIndexOutOfBoundsException
Sets a styler to use for the given source range. The range must be subrange of actual string of thisStyledString
. Stylers previously set for that range will be overwritten.- Parameters:
offset
- the start offset of the rangelength
- the length of the rangestyler
- the styler to set- Throws:
StringIndexOutOfBoundsException
- ifstart
is less than zero, or if offset plus length is greater than the length of this object.
-
getStyleRanges
public StyleRange[] getStyleRanges()
Returns an array ofStyleRange
resulting from applying all associated stylers for this string builder.- Returns:
- an array of all
StyleRange
resulting from applying the stored stylers to this string.
-
-