1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 19 public class ColoredString { 20 21 public static class Style { 22 private final String fForegroundColorName; 23 24 public Style(String foregroundColorName) { 25 fForegroundColorName= foregroundColorName; 26 } 27 28 public String getForegroundColorName() { 29 return fForegroundColorName; 30 } 31 } 32 33 public static final Style DEFAULT_STYLE= null; 34 35 private StringBuffer fBuffer; 36 private ArrayList fRanges; 37 38 public ColoredString() { 39 fBuffer= new StringBuffer (); 40 fRanges= null; 41 } 42 43 public ColoredString(String text) { 44 this(text, ColoredString.DEFAULT_STYLE); 45 } 46 47 public ColoredString(String text, Style style) { 48 this(); 49 append(text, style); 50 } 51 52 public String getString() { 53 return fBuffer.toString(); 54 } 55 56 public int length() { 57 return fBuffer.length(); 58 } 59 60 public Iterator getRanges() { 61 if (!hasRanges()) 62 return Collections.EMPTY_LIST.iterator(); 63 return getRangesList().iterator(); 64 } 65 66 public ColoredString append(String 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 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 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 ("Invalid offset (" + offset + ") or length (" + length + ")"); } 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 ("Overlapping ranges"); } 122 } 123 addRange(insertPos, new StyleRange(offset, length, style)); 124 } 125 126 129 public String 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 (); 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 getRangesList() { 157 if (fRanges == null) 158 fRanges= new ArrayList (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 |