|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 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.jface.viewers; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.List; |
| 15 |
|
| 16 |
import org.eclipse.jface.preference.JFacePreferences; |
| 17 |
import org.eclipse.jface.resource.ColorRegistry; |
| 18 |
import org.eclipse.jface.resource.JFaceResources; |
| 19 |
import org.eclipse.swt.custom.StyleRange; |
| 20 |
import org.eclipse.swt.graphics.TextStyle; |
| 21 |
|
| 22 |
/** |
| 23 |
* Represents a string with styled ranges. All ranges mark substrings of the string and do not overlap. |
| 24 |
* Styles are represented by {@link Style}. |
| 25 |
* |
| 26 |
* The styled string can be modified: |
| 27 |
* <ul> |
| 28 |
* <li>new strings with styles can be appended</li> |
| 29 |
* <li>styles can by applied to to the existing string</li> |
| 30 |
* </ul> |
| 31 |
* |
| 32 |
* <p> |
| 33 |
* This class may be instantiated; it is not intended to be subclassed. |
| 34 |
* </p> |
| 35 |
* @since 3.4 |
| 36 |
*/ |
| 37 |
public class StyledString { |
| 38 |
|
| 39 |
/** |
| 40 |
* Represents a style that can be associated to one ore more ranges in the {@link StyledString} |
| 41 |
* |
| 42 |
*/ |
| 43 |
public static abstract class Style { |
| 44 |
|
| 45 |
/** |
| 46 |
* Applies the styles represented by this object to the given textStyle. |
| 47 |
* |
| 48 |
* @param textStyle the {@link TextStyle} to modify |
| 49 |
*/ |
| 50 |
public abstract void applyStyles(TextStyle textStyle); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* A built-in style using the {@link JFacePreferences#QUALIFIER_COLOR} |
| 56 |
* managed in the JFace color registry (See {@link JFaceResources#getColorRegistry()}). |
| 57 |
*/ |
| 58 |
public static final Style QUALIFIER_STYLE= createColorRegistryStyle(JFacePreferences.QUALIFIER_COLOR, null); |
| 59 |
|
| 60 |
/** |
| 61 |
* A built-in style using the {@link JFacePreferences#COUNTER_COLOR} |
| 62 |
* managed in the JFace color registry (See {@link JFaceResources#getColorRegistry()}). |
| 63 |
*/ |
| 64 |
public static final Style COUNTER_STYLE= createColorRegistryStyle(JFacePreferences.COUNTER_COLOR, null); |
| 65 |
|
| 66 |
/** |
| 67 |
* A built-in style using the {@link JFacePreferences#DECORATIONS_COLOR} |
| 68 |
* managed in the JFace color registry (See {@link JFaceResources#getColorRegistry()}). |
| 69 |
*/ |
| 70 |
public static final Style DECORATIONS_STYLE= createColorRegistryStyle(JFacePreferences.DECORATIONS_COLOR, null); |
| 71 |
|
| 72 |
/** |
| 73 |
* Creates a style that takes the given foreground and background colors from the JFace color registry. |
| 74 |
* |
| 75 |
* @param foregroundColorName the color name for the foreground color |
| 76 |
* @param backgroundColorName the color name for the background color |
| 77 |
* |
| 78 |
* @return the created style |
| 79 |
*/ |
| 80 |
public static Style createColorRegistryStyle(String foregroundColorName, String backgroundColorName) { |
| 81 |
return new DefaultStyle(foregroundColorName, backgroundColorName); |
| 82 |
} |
| 83 |
|
| 84 |
private static final StyleRange[] EMPTY= new StyleRange[0]; |
| 85 |
private StringBuffer fBuffer; |
| 86 |
private StyleRunList fStyleRuns; |
| 87 |
|
| 88 |
/** |
| 89 |
* Creates an empty {@link StyledString}. |
| 90 |
*/ |
| 91 |
public StyledString() { |
| 92 |
fBuffer= new StringBuffer(); |
| 93 |
fStyleRuns= null; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Creates an {@link StyledString} initialized with a string without a style associated. |
| 98 |
* |
| 99 |
* @param string the string |
| 100 |
*/ |
| 101 |
public StyledString(String string) { |
| 102 |
this(string, null); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Creates an {@link StyledString} initialized with a string and a style. |
| 107 |
* |
| 108 |
* @param string the string |
| 109 |
* @param style the style of the text or <code>null</code> to not associated a style. |
| 110 |
*/ |
| 111 |
public StyledString(String string, Style style) { |
| 112 |
this(); |
| 113 |
append(string, style); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the string of this {@link StyledString}. |
| 118 |
* |
| 119 |
* @return the current string of this {@link StyledString}. |
| 120 |
*/ |
| 121 |
public String getString() { |
| 122 |
return fBuffer.toString(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns the length of the string of this {@link StyledString}. |
| 127 |
* |
| 128 |
* @return the length of the current string |
| 129 |
*/ |
| 130 |
public int length() { |
| 131 |
return fBuffer.length(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Appends a string to the {@link StyledString}. The appended string will have no style associated. |
| 136 |
* |
| 137 |
* @param string the string to append. |
| 138 |
* @return returns a reference to this object. |
| 139 |
*/ |
| 140 |
public StyledString append(String string) { |
| 141 |
return append(string, null); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Appends a character to the {@link StyledString}. The appended character will have no style associated. |
| 146 |
* |
| 147 |
* @param ch the character to append. |
| 148 |
* @return returns a reference to this object. |
| 149 |
*/ |
| 150 |
public StyledString append(char ch) { |
| 151 |
return append(String.valueOf(ch), null); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Appends a string with styles to the {@link StyledString}. |
| 156 |
* |
| 157 |
* @param string the string to append. |
| 158 |
* @return returns a reference to this object. |
| 159 |
*/ |
| 160 |
public StyledString append(StyledString string) { |
| 161 |
if (string.length() == 0) { |
| 162 |
return this; |
| 163 |
} |
| 164 |
|
| 165 |
int offset= fBuffer.length(); |
| 166 |
fBuffer.append(string.getString()); |
| 167 |
|
| 168 |
List otherRuns= string.fStyleRuns; |
| 169 |
if (otherRuns != null && !otherRuns.isEmpty()) { |
| 170 |
for (int i= 0; i < otherRuns.size(); i++) { |
| 171 |
StyleRun curr= (StyleRun) otherRuns.get(i); |
| 172 |
if (i == 0 && curr.offset != 0) { |
| 173 |
appendStyleRun(null, offset); // appended string will start with the default color |
| 174 |
} |
| 175 |
appendStyleRun(curr.style, offset + curr.offset); |
| 176 |
} |
| 177 |
} else { |
| 178 |
appendStyleRun(null, offset); // appended string will start with the default color |
| 179 |
} |
| 180 |
return this; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Appends a character with a style to the {@link StyledString}. The appended character will |
| 185 |
* have the given style associated. |
| 186 |
* |
| 187 |
* @param ch the character to append. |
| 188 |
* @param style the style to of the character to append or <code>null</code> if no style should be |
| 189 |
* associated to the string. |
| 190 |
* @return returns a reference to this object. |
| 191 |
*/ |
| 192 |
public StyledString append(char ch, Style style) { |
| 193 |
return append(String.valueOf(ch), style); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Appends a string with a style to the {@link StyledString}. The appended string will |
| 198 |
* have the given style associated. |
| 199 |
* |
| 200 |
* @param string the string to append. |
| 201 |
* @param style the style to of the string to append or <code>null</code> if no style should be |
| 202 |
* associated to the string. |
| 203 |
* @return returns a reference to this object. |
| 204 |
*/ |
| 205 |
public StyledString append(String string, Style style) { |
| 206 |
if (string.length() == 0) |
| 207 |
return this; |
| 208 |
|
| 209 |
int offset= fBuffer.length(); // the length before appending |
| 210 |
fBuffer.append(string); |
| 211 |
appendStyleRun(style, offset); |
| 212 |
return this; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Sets a style to the given source range. The range must be subrange of actual string of this {@link StyledString}. |
| 217 |
* Styles previously set for that range will be overwritten. |
| 218 |
* |
| 219 |
* @param offset the start offset of the range |
| 220 |
* @param length the length of the range |
| 221 |
* @param style the style to set |
| 222 |
* |
| 223 |
* @throws StringIndexOutOfBoundsException if <code>start</code> is |
| 224 |
* less than zero, or if offset plus length is greater than the length of this object. |
| 225 |
*/ |
| 226 |
public void setStyle(int offset, int length, Style style) { |
| 227 |
if (offset < 0 || offset + length > fBuffer.length()) { |
| 228 |
throw new StringIndexOutOfBoundsException("Invalid offset (" + offset + ") or length (" + length + ")"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ |
| 229 |
} |
| 230 |
if (length == 0) { |
| 231 |
return; |
| 232 |
} |
| 233 |
if (!hasRuns() || getLastRun().offset <= offset) { |
| 234 |
appendStyleRun(style, offset); |
| 235 |
if (offset + length != fBuffer.length()) { |
| 236 |
appendStyleRun(null, offset + length); |
| 237 |
} |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
int endRun= findRun(offset + length); |
| 242 |
if (endRun >= 0) { |
| 243 |
// run with the same end index, nothing to change |
| 244 |
} else { |
| 245 |
endRun= -(endRun + 1); |
| 246 |
if (offset + length < fBuffer.length()) { |
| 247 |
Style prevStyle= endRun > 0 ? fStyleRuns.getRun(endRun - 1).style : null; |
| 248 |
fStyleRuns.add(endRun, new StyleRun(offset + length, prevStyle)); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
int startRun= findRun(offset); |
| 253 |
if (startRun >= 0) { |
| 254 |
// run with the same start index |
| 255 |
StyleRun styleRun= fStyleRuns.getRun(startRun); |
| 256 |
styleRun.style= style; |
| 257 |
} else { |
| 258 |
startRun= -(startRun + 1); |
| 259 |
|
| 260 |
Style prevStyle= startRun > 0 ? fStyleRuns.getRun(startRun - 1).style : null; |
| 261 |
if (isDifferentStyle(prevStyle, style) || (startRun == 0 && style != null)) { |
| 262 |
fStyleRuns.add(startRun, new StyleRun(offset, style)); |
| 263 |
endRun++; // endrun is moved one back |
| 264 |
} else { |
| 265 |
startRun--; // we use the previous |
| 266 |
} |
| 267 |
} |
| 268 |
if (startRun + 1 < endRun) { |
| 269 |
fStyleRuns.removeRange(startRun + 1, endRun); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Returns {@link StyleRange} for all applied styles to this string |
| 275 |
* |
| 276 |
* @return an array of all {@link StyleRange} applied to this string. |
| 277 |
*/ |
| 278 |
public StyleRange[] getStyleRanges() { |
| 279 |
if (hasRuns()) { |
| 280 |
ArrayList res= new ArrayList(); |
| 281 |
|
| 282 |
List styleRuns= getStyleRuns(); |
| 283 |
int offset= 0; |
| 284 |
Style style= null; |
| 285 |
for (int i= 0; i < styleRuns.size(); i++) { |
| 286 |
StyleRun curr= (StyleRun) styleRuns.get(i); |
| 287 |
if (isDifferentStyle(curr.style, style)) { |
| 288 |
if (curr.offset > offset && style != null) { |
| 289 |
res.add(createStyleRange(offset, curr.offset, style)); |
| 290 |
} |
| 291 |
offset= curr.offset; |
| 292 |
style= curr.style; |
| 293 |
} |
| 294 |
} |
| 295 |
if (fBuffer.length() > offset && style != null) { |
| 296 |
res.add(createStyleRange(offset, fBuffer.length(), style)); |
| 297 |
} |
| 298 |
return (StyleRange[]) res.toArray(new StyleRange[res.size()]); |
| 299 |
} |
| 300 |
return EMPTY; |
| 301 |
} |
| 302 |
|
| 303 |
private int findRun(int offset) { |
| 304 |
// method assumes that fStyleRuns is not null |
| 305 |
int low= 0; |
| 306 |
int high= fStyleRuns.size() - 1; |
| 307 |
while (low <= high) { |
| 308 |
int mid = (low + high) / 2; |
| 309 |
StyleRun styleRun= fStyleRuns.getRun(mid); |
| 310 |
if (styleRun.offset < offset) { |
| 311 |
low = mid + 1; |
| 312 |
} else if (styleRun.offset > offset) { |
| 313 |
high = mid - 1; |
| 314 |
} else { |
| 315 |
return mid; // key found |
| 316 |
} |
| 317 |
} |
| 318 |
return -(low + 1); // key not found. |
| 319 |
} |
| 320 |
|
| 321 |
private StyleRange createStyleRange(int start, int end, Style style) { |
| 322 |
StyleRange styleRange= new StyleRange(); |
| 323 |
styleRange.start= start; |
| 324 |
styleRange.length= end - start; |
| 325 |
style.applyStyles(styleRange); |
| 326 |
return styleRange; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
/* (non-Javadoc) |
| 331 |
* @see java.lang.Object#toString() |
| 332 |
*/ |
| 333 |
public String toString() { |
| 334 |
return fBuffer.toString(); |
| 335 |
} |
| 336 |
|
| 337 |
private boolean hasRuns() { |
| 338 |
return fStyleRuns != null && !fStyleRuns.isEmpty(); |
| 339 |
} |
| 340 |
|
| 341 |
private void appendStyleRun(Style style, int offset) { |
| 342 |
StyleRun lastRun= getLastRun(); |
| 343 |
if (lastRun != null && lastRun.offset == offset) { |
| 344 |
lastRun.style= style; |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
if (lastRun == null && style != null || lastRun != null && isDifferentStyle(style, lastRun.style)) { |
| 349 |
getStyleRuns().add(new StyleRun(offset, style)); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
private boolean isDifferentStyle(Style style1, Style style2) { |
| 354 |
if (style1 == null) { |
| 355 |
return style2 != null; |
| 356 |
} |
| 357 |
return !style1.equals(style2); |
| 358 |
} |
| 359 |
|
| 360 |
private StyleRun getLastRun() { |
| 361 |
if (fStyleRuns == null || fStyleRuns.isEmpty()) { |
| 362 |
return null; |
| 363 |
} |
| 364 |
return fStyleRuns.getRun(fStyleRuns.size() - 1); |
| 365 |
} |
| 366 |
|
| 367 |
private List getStyleRuns() { |
| 368 |
if (fStyleRuns == null) |
| 369 |
fStyleRuns= new StyleRunList(); |
| 370 |
return fStyleRuns; |
| 371 |
} |
| 372 |
|
| 373 |
private static class StyleRun { |
| 374 |
public int offset; |
| 375 |
public Style style; |
| 376 |
|
| 377 |
public StyleRun(int offset, Style style) { |
| 378 |
this.offset= offset; |
| 379 |
this.style= style; |
| 380 |
} |
| 381 |
|
| 382 |
public String toString() { |
| 383 |
return "Offset " + offset + ", style: " + style.toString(); //$NON-NLS-1$//$NON-NLS-2$ |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
private static class StyleRunList extends ArrayList { |
| 388 |
private static final long serialVersionUID= 123L; |
| 389 |
|
| 390 |
public StyleRunList() { |
| 391 |
super(3); |
| 392 |
} |
| 393 |
|
| 394 |
public StyleRun getRun(int index) { |
| 395 |
return (StyleRun) get(index); |
| 396 |
} |
| 397 |
|
| 398 |
public void removeRange(int fromIndex, int toIndex) { |
| 399 |
super.removeRange(fromIndex, toIndex); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
private static class DefaultStyle extends Style { |
| 404 |
private final String fForegroundColorName; |
| 405 |
private final String fBackgroundColorName; |
| 406 |
|
| 407 |
public DefaultStyle(String foregroundColorName, String backgroundColorName) { |
| 408 |
fForegroundColorName= foregroundColorName; |
| 409 |
fBackgroundColorName= backgroundColorName; |
| 410 |
} |
| 411 |
|
| 412 |
public void applyStyles(TextStyle textStyle) { |
| 413 |
ColorRegistry colorRegistry= JFaceResources.getColorRegistry(); |
| 414 |
if (fForegroundColorName != null) { |
| 415 |
textStyle.foreground= colorRegistry.get(fForegroundColorName); |
| 416 |
} |
| 417 |
if (fBackgroundColorName != null) { |
| 418 |
textStyle.background= colorRegistry.get(fBackgroundColorName); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
} |