68 * @param iterators AttributedCharacterIterators to construct
69 * AttributedString from.
70 * @throws NullPointerException if iterators is null
71 */
72 AttributedString(AttributedCharacterIterator[] iterators) {
73 if (iterators == null) {
74 throw new NullPointerException("Iterators must not be null");
75 }
76 if (iterators.length == 0) {
77 text = "";
78 }
79 else {
80 // Build the String contents
81 StringBuffer buffer = new StringBuffer();
82 for (int counter = 0; counter < iterators.length; counter++) {
83 appendContents(buffer, iterators[counter]);
84 }
85
86 text = buffer.toString();
87
88 if (text.length() > 0) {
89 // Determine the runs, creating a new run when the attributes
90 // differ.
91 int offset = 0;
92 Map<Attribute,Object> last = null;
93
94 for (int counter = 0; counter < iterators.length; counter++) {
95 AttributedCharacterIterator iterator = iterators[counter];
96 int start = iterator.getBeginIndex();
97 int end = iterator.getEndIndex();
98 int index = start;
99
100 while (index < end) {
101 iterator.setIndex(index);
102
103 Map<Attribute,Object> attrs = iterator.getAttributes();
104
105 if (mapsDiffer(last, attrs)) {
106 setAttributes(attrs, index - start + offset);
107 }
108 last = attrs;
127 }
128
129 /**
130 * Constructs an AttributedString instance with the given text and attributes.
131 * @param text The text for this attributed string.
132 * @param attributes The attributes that apply to the entire string.
133 * @exception NullPointerException if <code>text</code> or
134 * <code>attributes</code> is null.
135 * @exception IllegalArgumentException if the text has length 0
136 * and the attributes parameter is not an empty Map (attributes
137 * cannot be applied to a 0-length range).
138 */
139 public AttributedString(String text,
140 Map<? extends Attribute, ?> attributes)
141 {
142 if (text == null || attributes == null) {
143 throw new NullPointerException();
144 }
145 this.text = text;
146
147 if (text.length() == 0) {
148 if (attributes.isEmpty())
149 return;
150 throw new IllegalArgumentException("Can't add attribute to 0-length text");
151 }
152
153 int attributeCount = attributes.size();
154 if (attributeCount > 0) {
155 createRunAttributeDataVectors();
156 Vector<Attribute> newRunAttributes = new Vector<>(attributeCount);
157 Vector<Object> newRunAttributeValues = new Vector<>(attributeCount);
158 runAttributes[0] = newRunAttributes;
159 runAttributeValues[0] = newRunAttributeValues;
160
161 Iterator<? extends Map.Entry<? extends Attribute, ?>> iterator = attributes.entrySet().iterator();
162 while (iterator.hasNext()) {
163 Map.Entry<? extends Attribute, ?> entry = iterator.next();
164 newRunAttributes.addElement(entry.getKey());
165 newRunAttributeValues.addElement(entry.getValue());
166 }
167 }
|
68 * @param iterators AttributedCharacterIterators to construct
69 * AttributedString from.
70 * @throws NullPointerException if iterators is null
71 */
72 AttributedString(AttributedCharacterIterator[] iterators) {
73 if (iterators == null) {
74 throw new NullPointerException("Iterators must not be null");
75 }
76 if (iterators.length == 0) {
77 text = "";
78 }
79 else {
80 // Build the String contents
81 StringBuffer buffer = new StringBuffer();
82 for (int counter = 0; counter < iterators.length; counter++) {
83 appendContents(buffer, iterators[counter]);
84 }
85
86 text = buffer.toString();
87
88 if (!text.isEmpty()) {
89 // Determine the runs, creating a new run when the attributes
90 // differ.
91 int offset = 0;
92 Map<Attribute,Object> last = null;
93
94 for (int counter = 0; counter < iterators.length; counter++) {
95 AttributedCharacterIterator iterator = iterators[counter];
96 int start = iterator.getBeginIndex();
97 int end = iterator.getEndIndex();
98 int index = start;
99
100 while (index < end) {
101 iterator.setIndex(index);
102
103 Map<Attribute,Object> attrs = iterator.getAttributes();
104
105 if (mapsDiffer(last, attrs)) {
106 setAttributes(attrs, index - start + offset);
107 }
108 last = attrs;
127 }
128
129 /**
130 * Constructs an AttributedString instance with the given text and attributes.
131 * @param text The text for this attributed string.
132 * @param attributes The attributes that apply to the entire string.
133 * @exception NullPointerException if <code>text</code> or
134 * <code>attributes</code> is null.
135 * @exception IllegalArgumentException if the text has length 0
136 * and the attributes parameter is not an empty Map (attributes
137 * cannot be applied to a 0-length range).
138 */
139 public AttributedString(String text,
140 Map<? extends Attribute, ?> attributes)
141 {
142 if (text == null || attributes == null) {
143 throw new NullPointerException();
144 }
145 this.text = text;
146
147 if (text.isEmpty()) {
148 if (attributes.isEmpty())
149 return;
150 throw new IllegalArgumentException("Can't add attribute to 0-length text");
151 }
152
153 int attributeCount = attributes.size();
154 if (attributeCount > 0) {
155 createRunAttributeDataVectors();
156 Vector<Attribute> newRunAttributes = new Vector<>(attributeCount);
157 Vector<Object> newRunAttributeValues = new Vector<>(attributeCount);
158 runAttributes[0] = newRunAttributes;
159 runAttributeValues[0] = newRunAttributeValues;
160
161 Iterator<? extends Map.Entry<? extends Attribute, ?>> iterator = attributes.entrySet().iterator();
162 while (iterator.hasNext()) {
163 Map.Entry<? extends Attribute, ?> entry = iterator.next();
164 newRunAttributes.addElement(entry.getKey());
165 newRunAttributeValues.addElement(entry.getValue());
166 }
167 }
|