KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > ColoredString


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.viewsupport;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18
19 public class ColoredString {
20     
21     public static class Style {
22         private final String JavaDoc fForegroundColorName;
23
24         public Style(String JavaDoc foregroundColorName) {
25             fForegroundColorName= foregroundColorName;
26         }
27         
28         public String JavaDoc getForegroundColorName() {
29             return fForegroundColorName;
30         }
31     }
32     
33     public static final Style DEFAULT_STYLE= null;
34     
35     private StringBuffer JavaDoc fBuffer;
36     private ArrayList JavaDoc fRanges;
37     
38     public ColoredString() {
39         fBuffer= new StringBuffer JavaDoc();
40         fRanges= null;
41     }
42     
43     public ColoredString(String JavaDoc text) {
44         this(text, ColoredString.DEFAULT_STYLE);
45     }
46     
47     public ColoredString(String JavaDoc text, Style style) {
48         this();
49         append(text, style);
50     }
51     
52     public String JavaDoc getString() {
53         return fBuffer.toString();
54     }
55     
56     public int length() {
57         return fBuffer.length();
58     }
59     
60     public Iterator JavaDoc getRanges() {
61         if (!hasRanges())
62             return Collections.EMPTY_LIST.iterator();
63         return getRangesList().iterator();
64     }
65     
66     public ColoredString append(String JavaDoc text) {
67         return append(text, DEFAULT_STYLE);
68     }
69     
70     public ColoredString append(char ch) {
71         return append(String.valueOf(ch), DEFAULT_STYLE);
72     }
73     
74     public ColoredString append(ColoredString string) {
75         int offset= fBuffer.length();
76         fBuffer.append(string.getString());
77         for (Iterator JavaDoc iterator= string.getRanges(); iterator.hasNext();) {
78             StyleRange curr= (StyleRange) iterator.next();
79             addRange(new StyleRange(offset + curr.offset, curr.length, curr.style));
80         }
81         return this;
82     }
83         
84     public ColoredString append(String JavaDoc text, Style style) {
85         if (text.length() == 0)
86             return this;
87         
88         int offset= fBuffer.length();
89         fBuffer.append(text);
90         if (style != null) {
91             int nRanges= getNumberOfRanges();
92             if (nRanges > 0) {
93                 StyleRange last= getRange(nRanges - 1);
94                 if (last.offset + last.length == offset && style.equals(last.style)) {
95                     last.length += text.length();
96                     return this;
97                 }
98             }
99             addRange(new StyleRange(offset, text.length(), style));
100         }
101         return this;
102     }
103     
104     public void colorize(int offset, int length, Style style) {
105         if (offset < 0 || offset + length > fBuffer.length()) {
106             throw new IllegalArgumentException JavaDoc("Invalid offset (" + offset + ") or length (" + length + ")"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
107
}
108         
109         int insertPos= 0;
110         int nRanges= getNumberOfRanges();
111         for (int i= 0; i < nRanges; i++) {
112             StyleRange curr= getRange(i);
113             if (curr.offset + curr.length <= offset) {
114                 insertPos= i + 1;
115             }
116         }
117         if (insertPos < nRanges) {
118             StyleRange curr= getRange(insertPos);
119             if (curr.offset > offset + length) {
120                 throw new IllegalArgumentException JavaDoc("Overlapping ranges"); //$NON-NLS-1$
121
}
122         }
123         addRange(insertPos, new StyleRange(offset, length, style));
124     }
125     
126     /* (non-Javadoc)
127      * @see java.lang.Object#toString()
128      */

129     public String JavaDoc toString() {
130         return fBuffer.toString();
131     }
132     
133     private boolean hasRanges() {
134         return fRanges != null && !fRanges.isEmpty();
135     }
136     
137     private int getNumberOfRanges() {
138         return fRanges == null ? 0 : fRanges.size();
139     }
140     
141     private StyleRange getRange(int index) {
142         if (fRanges != null) {
143             return (StyleRange) fRanges.get(index);
144         }
145         throw new IndexOutOfBoundsException JavaDoc();
146     }
147     
148     private void addRange(StyleRange range) {
149         getRangesList().add(range);
150     }
151     
152     private void addRange(int index, StyleRange range) {
153         getRangesList().add(index, range);
154     }
155     
156     private List JavaDoc getRangesList() {
157         if (fRanges == null)
158             fRanges= new ArrayList JavaDoc(2);
159         return fRanges;
160     }
161     
162     public static class StyleRange {
163         public int offset;
164         public int length;
165         public Style style;
166         
167         public StyleRange(int offset, int length, Style style) {
168             this.offset= offset;
169             this.length= length;
170             this.style= style;
171         }
172     }
173 }
174
Popular Tags