AttributedStringpublic class AttributedString extends Object Holds a string with attributes describing the characters of
this string. |
Fields Summary |
---|
String | text | Map | attributeMap |
Constructors Summary |
---|
public AttributedString(AttributedCharacterIterator iterator)Constructs an {@code AttributedString} from an {@code
AttributedCharacterIterator}, which represents attributed text.
if (iterator.getBeginIndex() > iterator.getEndIndex()) {
// text.0A=Invalid substring range
throw new IllegalArgumentException(Messages.getString("text.0A")); //$NON-NLS-1$
}
StringBuffer buffer = new StringBuffer();
for (int i = iterator.getBeginIndex(); i < iterator.getEndIndex(); i++) {
buffer.append(iterator.current());
iterator.next();
}
text = buffer.toString();
Set<AttributedCharacterIterator.Attribute> attributes = iterator
.getAllAttributeKeys();
if (attributes == null) {
return;
}
attributeMap = new HashMap<Attribute, List<Range>>(
(attributes.size() * 4 / 3) + 1);
Iterator<Attribute> it = attributes.iterator();
while (it.hasNext()) {
AttributedCharacterIterator.Attribute attribute = it.next();
iterator.setIndex(0);
while (iterator.current() != CharacterIterator.DONE) {
int start = iterator.getRunStart(attribute);
int limit = iterator.getRunLimit(attribute);
Object value = iterator.getAttribute(attribute);
if (value != null) {
addAttribute(attribute, value, start, limit);
}
iterator.setIndex(limit);
}
}
| private AttributedString(AttributedCharacterIterator iterator, int start, int end, Set attributes)
if (start < iterator.getBeginIndex() || end > iterator.getEndIndex()
|| start > end) {
throw new IllegalArgumentException();
}
if (attributes == null) {
return;
}
StringBuffer buffer = new StringBuffer();
iterator.setIndex(start);
while (iterator.getIndex() < end) {
buffer.append(iterator.current());
iterator.next();
}
text = buffer.toString();
attributeMap = new HashMap<Attribute, List<Range>>(
(attributes.size() * 4 / 3) + 1);
Iterator<Attribute> it = attributes.iterator();
while (it.hasNext()) {
AttributedCharacterIterator.Attribute attribute = it.next();
iterator.setIndex(start);
while (iterator.getIndex() < end) {
Object value = iterator.getAttribute(attribute);
int runStart = iterator.getRunStart(attribute);
int limit = iterator.getRunLimit(attribute);
if ((value instanceof Annotation && runStart >= start && limit <= end)
|| (value != null && !(value instanceof Annotation))) {
addAttribute(attribute, value, (runStart < start ? start
: runStart)
- start, (limit > end ? end : limit) - start);
}
iterator.setIndex(limit);
}
}
| public AttributedString(AttributedCharacterIterator iterator, int start, int end)Constructs an {@code AttributedString} from a range of the text contained
in the specified {@code AttributedCharacterIterator}, starting at {@code
start} and ending at {@code end}. All attributes will be copied to this
attributed string.
this(iterator, start, end, iterator.getAllAttributeKeys());
| public AttributedString(AttributedCharacterIterator iterator, int start, int end, java.text.AttributedCharacterIterator.Attribute[] attributes)Constructs an {@code AttributedString} from a range of the text contained
in the specified {@code AttributedCharacterIterator}, starting at {@code
start}, ending at {@code end} and it will copy the attributes defined in
the specified set. If the set is {@code null} then all attributes are
copied.
// BEGIN android-removed
// this(iterator, start, end, new HashSet<Attribute>(Arrays
// .asList(attributes)));
// END android-removed
// BEGIN android-added
this(iterator, start, end, (attributes == null
? new HashSet<Attribute>()
: new HashSet<Attribute>(Arrays.asList(attributes))));
// END android-added
| public AttributedString(String value)Creates an {@code AttributedString} from the given text.
if (value == null) {
throw new NullPointerException();
}
text = value;
attributeMap = new HashMap<Attribute, List<Range>>(11);
| public AttributedString(String value, Map attributes)Creates an {@code AttributedString} from the given text and the
attributes. The whole text has the given attributes applied.
if (value == null) {
throw new NullPointerException();
}
if (value.length() == 0 && !attributes.isEmpty()) {
// text.0B=Cannot add attributes to empty string
throw new IllegalArgumentException(Messages.getString("text.0B")); //$NON-NLS-1$
}
text = value;
attributeMap = new HashMap<Attribute, List<Range>>(
(attributes.size() * 4 / 3) + 1);
Iterator<?> it = attributes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
ArrayList<Range> ranges = new ArrayList<Range>(1);
ranges.add(new Range(0, text.length(), entry.getValue()));
attributeMap.put((AttributedCharacterIterator.Attribute) entry
.getKey(), ranges);
}
|
Methods Summary |
---|
public void | addAttribute(java.text.AttributedCharacterIterator.Attribute attribute, java.lang.Object value)Applies a given attribute to this string.
if (null == attribute) {
throw new NullPointerException();
}
if (text.length() == 0) {
throw new IllegalArgumentException();
}
List<Range> ranges = attributeMap.get(attribute);
if (ranges == null) {
ranges = new ArrayList<Range>(1);
attributeMap.put(attribute, ranges);
} else {
ranges.clear();
}
ranges.add(new Range(0, text.length(), value));
| public void | addAttribute(java.text.AttributedCharacterIterator.Attribute attribute, java.lang.Object value, int start, int end)Applies a given attribute to the given range of this string.
if (null == attribute) {
throw new NullPointerException();
}
if (start < 0 || end > text.length() || start >= end) {
throw new IllegalArgumentException();
}
if (value == null) {
return;
}
List<Range> ranges = attributeMap.get(attribute);
if (ranges == null) {
ranges = new ArrayList<Range>(1);
ranges.add(new Range(start, end, value));
attributeMap.put(attribute, ranges);
return;
}
ListIterator<Range> it = ranges.listIterator();
// BEGIN android-changed
// copied from a newer version of harmony
// value can't be null
while (it.hasNext()) {
Range range = it.next();
if (end <= range.start) {
it.previous();
break;
} else if (start < range.end
|| (start == range.end && value.equals(range.value))) {
Range r1 = null, r3;
it.remove();
r1 = new Range(range.start, start, range.value);
r3 = new Range(end, range.end, range.value);
while (end > range.end && it.hasNext()) {
range = it.next();
if (end <= range.end) {
if (end > range.start
|| (end == range.start && value.equals(range.value))) {
it.remove();
r3 = new Range(end, range.end, range.value);
break;
}
} else {
it.remove();
}
}
if (value.equals(r1.value)) {
if (value.equals(r3.value)) {
it.add(new Range(r1.start < start ? r1.start : start,
r3.end > end ? r3.end : end, r1.value));
} else {
it.add(new Range(r1.start < start ? r1.start : start,
end, r1.value));
if (r3.start < r3.end) {
it.add(r3);
}
}
} else {
if (value.equals(r3.value)) {
if (r1.start < r1.end) {
it.add(r1);
}
it.add(new Range(start, r3.end > end ? r3.end : end,
r3.value));
} else {
if (r1.start < r1.end) {
it.add(r1);
}
it.add(new Range(start, end, value));
if (r3.start < r3.end) {
it.add(r3);
}
}
}
return;
}
}
// END android-changed
it.add(new Range(start, end, value));
| public void | addAttributes(java.util.Map attributes, int start, int end)Applies a given set of attributes to the given range of the string.
Iterator<?> it = attributes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
addAttribute(
(AttributedCharacterIterator.Attribute) entry.getKey(),
entry.getValue(), start, end);
}
| public java.text.AttributedCharacterIterator | getIterator()Returns an {@code AttributedCharacterIterator} that gives access to the
complete content of this attributed string.
return new AttributedIterator(this);
| public java.text.AttributedCharacterIterator | getIterator(java.text.AttributedCharacterIterator.Attribute[] attributes)Returns an {@code AttributedCharacterIterator} that gives access to the
complete content of this attributed string. Only attributes contained in
{@code attributes} are available from this iterator if they are defined
for this text.
return new AttributedIterator(this, attributes, 0, text.length());
| public java.text.AttributedCharacterIterator | getIterator(java.text.AttributedCharacterIterator.Attribute[] attributes, int start, int end)Returns an {@code AttributedCharacterIterator} that gives access to the
contents of this attributed string starting at index {@code start} up to
index {@code end}. Only attributes contained in {@code attributes} are
available from this iterator if they are defined for this text.
return new AttributedIterator(this, attributes, start, end);
|
|