Constructor: |
AttributedString(AttributedCharacterIterator[] iterators) {
if (iterators == null) {
throw new NullPointerException("Iterators must not be null");
}
if (iterators.length == 0) {
text = "";
}
else {
// Build the String contents
StringBuffer buffer = new StringBuffer();
for (int counter = 0; counter < iterators.length; counter++) {
appendContents(buffer, iterators[counter]);
}
text = buffer.toString();
if (text.length() > 0) {
// Determine the runs, creating a new run when the attributes
// differ.
int offset = 0;
Map last = null;
for (int counter = 0; counter < iterators.length; counter++) {
AttributedCharacterIterator iterator = iterators[counter];
int start = iterator.getBeginIndex();
int end = iterator.getEndIndex();
int index = start;
while (index < end) {
iterator.setIndex(index);
Map attrs = iterator.getAttributes();
if (mapsDiffer(last, attrs)) {
setAttributes(attrs, index - start + offset);
}
last = attrs;
index = iterator.getRunLimit();
}
offset += (end - start);
}
}
}
}
Constructs an AttributedString instance with the given
AttributedCharacterIterators. Parameters:
iterators - AttributedCharacterIterators to construct
AttributedString from.
Throws:
NullPointerException - if iterators is null
|
public AttributedString(String text) {
if (text == null) {
throw new NullPointerException();
}
this.text = text;
}
Constructs an AttributedString instance with the given text. Parameters:
text - The text for this attributed string.
Throws:
NullPointerException - if text is null.
- exception:
NullPointerException - if text is null.
|
public AttributedString(AttributedCharacterIterator text) {
// If performance is critical, this constructor should be
// implemented here rather than invoking the constructor for a
// subrange. We can avoid some range checking in the loops.
this(text, text.getBeginIndex(), text.getEndIndex(), null);
}
Constructs an AttributedString instance with the given attributed
text represented by AttributedCharacterIterator. Parameters:
text - The text for this attributed string.
Throws:
NullPointerException - if text is null.
- exception:
NullPointerException - if text is null.
|
public AttributedString(String text,
Map<Attribute, ?> attributes) {
if (text == null || attributes == null) {
throw new NullPointerException();
}
this.text = text;
if (text.length() == 0) {
if (attributes.isEmpty())
return;
throw new IllegalArgumentException("Can't add attribute to 0-length text");
}
int attributeCount = attributes.size();
if (attributeCount > 0) {
createRunAttributeDataVectors();
Vector newRunAttributes = new Vector(attributeCount);
Vector newRunAttributeValues = new Vector(attributeCount);
runAttributes[0] = newRunAttributes;
runAttributeValues[0] = newRunAttributeValues;
Iterator iterator = attributes.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
newRunAttributes.addElement(entry.getKey());
newRunAttributeValues.addElement(entry.getValue());
}
}
}
Constructs an AttributedString instance with the given text and attributes. Parameters:
text - The text for this attributed string.
attributes - The attributes that apply to the entire string.
Throws:
NullPointerException - if text or
attributes is null.
IllegalArgumentException - if the text has length 0
and the attributes parameter is not an empty Map (attributes
cannot be applied to a 0-length range).
- exception:
NullPointerException - if text or
attributes is null.
- exception:
IllegalArgumentException - if the text has length 0
and the attributes parameter is not an empty Map (attributes
cannot be applied to a 0-length range).
|
public AttributedString(AttributedCharacterIterator text,
int beginIndex,
int endIndex) {
this(text, beginIndex, endIndex, null);
}
Constructs an AttributedString instance with the subrange of
the given attributed text represented by
AttributedCharacterIterator. If the given range produces an
empty text, all attributes will be discarded. Note that any
attributes wrapped by an Annotation object are discarded for a
subrange of the original attribute range. Parameters:
text - The text for this attributed string.
beginIndex - Index of the first character of the range.
endIndex - Index of the character following the last character
of the range.
Throws:
NullPointerException - if text is null.
IllegalArgumentException - if the subrange given by
beginIndex and endIndex is out of the text range.
Also see:
- java.text.Annotation
- exception:
NullPointerException - if text is null.
- exception:
IllegalArgumentException - if the subrange given by
beginIndex and endIndex is out of the text range.
|
public AttributedString(AttributedCharacterIterator text,
int beginIndex,
int endIndex,
Attribute[] attributes) {
if (text == null) {
throw new NullPointerException();
}
// Validate the given subrange
int textBeginIndex = text.getBeginIndex();
int textEndIndex = text.getEndIndex();
if (beginIndex < textBeginIndex || endIndex > textEndIndex || beginIndex > endIndex)
throw new IllegalArgumentException("Invalid substring range");
// Copy the given string
StringBuffer textBuffer = new StringBuffer();
text.setIndex(beginIndex);
for (char c = text.current(); text.getIndex() < endIndex; c = text.next())
textBuffer.append(c);
this.text = textBuffer.toString();
if (beginIndex == endIndex)
return;
// Select attribute keys to be taken care of
HashSet keys = new HashSet();
if (attributes == null) {
keys.addAll(text.getAllAttributeKeys());
} else {
for (int i = 0; i < attributes.length; i++)
keys.add(attributes[i]);
keys.retainAll(text.getAllAttributeKeys());
}
if (keys.isEmpty())
return;
// Get and set attribute runs for each attribute name. Need to
// scan from the top of the text so that we can discard any
// Annotation that is no longer applied to a subset text segment.
Iterator itr = keys.iterator();
while (itr.hasNext()) {
Attribute attributeKey = (Attribute)itr.next();
text.setIndex(textBeginIndex);
while (text.getIndex() < endIndex) {
int start = text.getRunStart(attributeKey);
int limit = text.getRunLimit(attributeKey);
Object value = text.getAttribute(attributeKey);
if (value != null) {
if (value instanceof Annotation) {
if (start >= beginIndex && limit < = endIndex) {
addAttribute(attributeKey, value, start - beginIndex, limit - beginIndex);
} else {
if (limit > endIndex)
break;
}
} else {
// if the run is beyond the given (subset) range, we
// don't need to process further.
if (start >= endIndex)
break;
if (limit > beginIndex) {
// attribute is applied to any subrange
if (start < beginIndex)
start = beginIndex;
if (limit > endIndex)
limit = endIndex;
if (start != limit) {
addAttribute(attributeKey, value, start - beginIndex, limit - beginIndex);
}
}
}
}
text.setIndex(limit);
}
}
}
Constructs an AttributedString instance with the subrange of
the given attributed text represented by
AttributedCharacterIterator. Only attributes that match the
given attributes will be incorporated into the instance. If the
given range produces an empty text, all attributes will be
discarded. Note that any attributes wrapped by an Annotation
object are discarded for a subrange of the original attribute
range. Parameters:
text - The text for this attributed string.
beginIndex - Index of the first character of the range.
endIndex - Index of the character following the last character
of the range.
attributes - Specifies attributes to be extracted
from the text. If null is specified, all available attributes will
be used.
Throws:
NullPointerException - if text is null.
IllegalArgumentException - if the subrange given by
beginIndex and endIndex is out of the text range.
Also see:
- java.text.Annotation
- exception:
NullPointerException - if text is null.
- exception:
IllegalArgumentException - if the subrange given by
beginIndex and endIndex is out of the text range.
|
Method from java.text.AttributedString Detail: |
public void addAttribute(Attribute attribute,
Object value) {
if (attribute == null) {
throw new NullPointerException();
}
int len = length();
if (len == 0) {
throw new IllegalArgumentException("Can't add attribute to 0-length text");
}
addAttributeImpl(attribute, value, 0, len);
}
Adds an attribute to the entire string. |
public void addAttribute(Attribute attribute,
Object value,
int beginIndex,
int endIndex) {
if (attribute == null) {
throw new NullPointerException();
}
if (beginIndex < 0 || endIndex > length() || beginIndex >= endIndex) {
throw new IllegalArgumentException("Invalid substring range");
}
addAttributeImpl(attribute, value, beginIndex, endIndex);
}
Adds an attribute to a subrange of the string. |
public void addAttributes(Map<Attribute, ?> attributes,
int beginIndex,
int endIndex) {
if (attributes == null) {
throw new NullPointerException();
}
if (beginIndex < 0 || endIndex > length() || beginIndex > endIndex) {
throw new IllegalArgumentException("Invalid substring range");
}
if (beginIndex == endIndex) {
if (attributes.isEmpty())
return;
throw new IllegalArgumentException("Can't add attribute to 0-length text");
}
// make sure we have run attribute data vectors
if (runCount == 0) {
createRunAttributeDataVectors();
}
// break up runs if necessary
int beginRunIndex = ensureRunBreak(beginIndex);
int endRunIndex = ensureRunBreak(endIndex);
Iterator iterator = attributes.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
addAttributeRunData((Attribute) entry.getKey(), entry.getValue(), beginRunIndex, endRunIndex);
}
}
Adds a set of attributes to a subrange of the string. |
public AttributedCharacterIterator getIterator() {
return getIterator(null, 0, length());
}
Creates an AttributedCharacterIterator instance that provides access to the entire contents of
this string. |
public AttributedCharacterIterator getIterator(Attribute[] attributes) {
return getIterator(attributes, 0, length());
}
Creates an AttributedCharacterIterator instance that provides access to
selected contents of this string.
Information about attributes not listed in attributes that the
implementor may have need not be made accessible through the iterator.
If the list is null, all available attribute information should be made
accessible. |
public AttributedCharacterIterator getIterator(Attribute[] attributes,
int beginIndex,
int endIndex) {
return new AttributedStringIterator(attributes, beginIndex, endIndex);
}
Creates an AttributedCharacterIterator instance that provides access to
selected contents of this string.
Information about attributes not listed in attributes that the
implementor may have need not be made accessible through the iterator.
If the list is null, all available attribute information should be made
accessible. |
int length() {
return text.length();
}
|