1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
/*
* @(#)TextStyle.java 9/6/2005
*
* Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
*/
package com.jidesoft.swing;
import java.awt.*;
/**
* A data structure represents a style for a range of text. There are two categories of styles that currently supports.
* One is the font style and color which includes bold, italic, superscript, subscript as well as the color of the text.
* The other one is line color and style. The line style could be straight line, dotted line, waved line or any
* customized style using Stroke. The line could be used as underline or strikethrough line.
* <p/>
* The name of StyleRange comes from SWT's StyleRange. We borrowed some design idea from it. StyledLabel is actually
* very similar to SWT's StyledText. Saying that, the features of the two components are not exactly the same since the
* purpose of the two components are quite different.
*/
public class StyleRange {
public static final int STYLE_STRIKE_THROUGH = 0x1;
public static final int STYLE_DOUBLE_STRIKE_THROUGH = STYLE_STRIKE_THROUGH << 1;
public static final int STYLE_WAVED = STYLE_DOUBLE_STRIKE_THROUGH << 1;
public static final int STYLE_UNDERLINED = STYLE_WAVED << 1;
public static final int STYLE_DOTTED = STYLE_UNDERLINED << 1;
public static final int STYLE_SUPERSCRIPT = STYLE_DOTTED << 1;
public static final int STYLE_SUBSCRIPT = STYLE_SUPERSCRIPT << 1;
private final int _fontStyle;
private final Color _fontColor;
private final Color _backgroundColor;
private final Color _lineColor;
private final Stroke _lineStroke;
private final int _additionalStyle;
private int _start;
private int _length;
private float _fontShrinkRatio = 1.5f;
/**
* Creates a style range with a specified font style.
*
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
*/
public StyleRange(int fontStyle) {
this(0, -1, fontStyle, null, 0, null, null);
}
/**
* Creates a style range with a specified font color.
*
* @param fontColor the color of the text
*/
public StyleRange(Color fontColor) {
this(0, -1, -1, fontColor, 0, null, null);
}
/**
* Creates a style range with a specified font style and font color.
*
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text
*/
public StyleRange(int fontStyle, Color fontColor) {
this(0, -1, fontStyle, fontColor, 0, null, null);
}
/**
* Creates a style range with a specified font style and additional style.
*
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to connect two or more styles as long as it makes
* sense.
*/
public StyleRange(int fontStyle, int additionalStyle) {
this(0, -1, fontStyle, null, additionalStyle, null, null);
}
/**
* Creates a style range with a specified font style and additional style.
*
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
*/
public StyleRange(int fontStyle, int additionalStyle, float fontShrinkRatio) {
this(0, -1, fontStyle, null, additionalStyle, null, null, fontShrinkRatio);
}
/**
* Creates a style range with a specified font style and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
*/
public StyleRange(int start, int length, int fontStyle) {
this(start, length, fontStyle, null, 0, null, null);
}
/**
* Creates a style range with a specified font style, font color and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor) {
this(start, length, fontStyle, fontColor, 0, null, null);
}
/**
* Creates a style range with a specified font color and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontColor the color of the text.
*/
public StyleRange(int start, int length, Color fontColor) {
this(start, length, Font.PLAIN, fontColor, 0, null, null);
}
/**
* Creates a style range with a specified font style, additional style and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
*/
public StyleRange(int start, int length, int fontStyle, int additionalStyle) {
this(start, length, fontStyle, null, additionalStyle, null, null);
}
/**
* Creates a style range with a specified font style, additional style and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
*/
public StyleRange(int start, int length, int fontStyle, int additionalStyle, float fontShrinkRatio) {
this(start, length, fontStyle, null, additionalStyle, null, null, fontShrinkRatio);
}
/**
* Creates a style range with a specified font style, font color, and additional style.
*
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
*/
public StyleRange(int fontStyle, Color fontColor, int additionalStyle, Color lineColor) {
this(0, -1, fontStyle, fontColor, additionalStyle, lineColor, null);
}
/**
* Creates a style range with a specified font style, font color, and additional style.
*
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param backgroundColor the background color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
*/
public StyleRange(int fontStyle, Color fontColor, Color backgroundColor, int additionalStyle, Color lineColor) {
this(0, -1, fontStyle, fontColor, backgroundColor, additionalStyle, lineColor, null);
}
/**
* Creates a style range with a specified font style, font color, additional style and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, int additionalStyle) {
this(start, length, fontStyle, fontColor, additionalStyle, null, null);
}
/**
* Creates a style range with a specified font style, font color, additional style and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param backgroundColor the background color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, Color backgroundColor, int additionalStyle) {
this(start, length, fontStyle, fontColor, backgroundColor, additionalStyle, null, null);
}
/**
* Creates a style range with a specified font style, font color, additional style, and line color.
*
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param lineColor the color of the line.
*/
public StyleRange(int fontStyle, Color fontColor, int additionalStyle, Color lineColor, Stroke lineStroke) {
this(0, -1, fontStyle, fontColor, additionalStyle, lineColor, lineStroke);
}
/**
* Creates a style range with a specified font style, font color, additional style, line color and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param lineColor the color of the line.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, int additionalStyle, Color lineColor) {
this(start, length, fontStyle, fontColor, additionalStyle, lineColor, null);
}
/**
* Creates a style range with a specified font style, font color, additional style, line color and a range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param backgroundColor the background color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param lineColor the color of the line.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, Color backgroundColor, int additionalStyle, Color lineColor) {
this(start, length, fontStyle, fontColor, backgroundColor, additionalStyle, lineColor, null);
}
/**
* Creates a style range with a specified font style, font color, additional style, line color, line stroke and a
* range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param lineColor the color of the line.
* @param lineStroke the stroke of the line.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, int additionalStyle, Color lineColor, Stroke lineStroke) {
this(start, length, fontStyle, fontColor, additionalStyle, lineColor, lineStroke, 1.5f);
}
/**
* Creates a style range with a specified font style, font color, additional style, line color, line stroke and a
* range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param backgroundColor the background color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param lineColor the color of the line.
* @param lineStroke the stroke of the line.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, Color backgroundColor, int additionalStyle, Color lineColor, Stroke lineStroke) {
this(start, length, fontStyle, fontColor, backgroundColor, additionalStyle, lineColor, lineStroke, 1.5f);
}
/**
* Creates a style range with a specified font style, font color, additional style, line color, line stroke and a
* range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use any | to concat two or more styles as long as it makes
* sense.
* @param lineColor the color of the line.
* @param lineStroke the stroke of the line.
* @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, int additionalStyle, Color lineColor, Stroke lineStroke, float fontShrinkRatio) {
this(start, length, fontStyle, fontColor, null, additionalStyle, lineColor, lineStroke, fontShrinkRatio);
}
/**
* Creates a style range exactly the same with the existing range.
*
* @param range the old range
* @since 3.2.1
*/
public StyleRange(StyleRange range) {
this(range.getStart(), range.getLength(), range.getFontStyle(), range.getFontColor(), range.getBackgroundColor(), range.getAdditionalStyle(), range.getLineColor(), range.getLineStroke(), range.getFontShrinkRatio());
}
/**
* Creates a style range with a specified font style, font color, additional style, line color, line stroke and a
* range.
*
* @param start the start index of the range in a string
* @param length the length of the range.
* @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
* @param fontColor the color of the text.
* @param backgroundColor the background color of the text.
* @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin
* with STYLE_. You can also use bitwise OR "|" to concat any two or more styles as long as
* it makes sense.
* @param lineColor the color of the line.
* @param lineStroke the stroke of the line.
* @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
*/
public StyleRange(int start, int length, int fontStyle, Color fontColor, Color backgroundColor, int additionalStyle, Color lineColor, Stroke lineStroke, float fontShrinkRatio) {
if (length == 0) {
throw new IllegalArgumentException("The length of StyleRange cannot be 0.");
}
_start = start;
_length = length;
_fontColor = fontColor;
_fontStyle = fontStyle;
_backgroundColor = backgroundColor;
_lineColor = lineColor;
_lineStroke = lineStroke;
_additionalStyle = additionalStyle;
_fontShrinkRatio = fontShrinkRatio;
}
/**
* Gets the start index of the range.
*
* @return the start index of the range.
*/
public int getStart() {
return _start;
}
/**
* Sets the start index of the range.
*
* @param start the start index of the range
*/
public void setStart(int start) {
_start = start;
}
/**
* Gets the length of the range.
*
* @return the length of the range.
*/
public int getLength() {
return _length;
}
/**
* Sets the length of the range.
*
* @param length the length of the range
*/
public void setLength(int length) {
_length = length;
}
/**
* Gets the font style. Possible values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
*
* @return the font style.
*/
public int getFontStyle() {
return _fontStyle;
}
/**
* Gets the font color.
*
* @return the font color.
*/
public Color getFontColor() {
return _fontColor;
}
/**
* Gets the background color.
*
* @return the background color.
*/
public Color getBackgroundColor() {
return _backgroundColor;
}
/**
* Gets the additional style. Possible additional styles are defined as constants in {@link StyleRange}. The names
* begin with STYLE_. The value could also be two or more styles concatenated by | as long as it makes sense. It
* could be more convenient to use methods {@link #isStrikethrough()}, {@link #isDoublestrikethrough()}, {@link
* #isDotted()}, {@link #isWaved()}, {@link #isUnderlined()}, {@link #isSubscript()}, {@link #isSuperscript()} to
* see what's the additional style.
*
* @return the additional style.
*/
public int getAdditionalStyle() {
return _additionalStyle;
}
/**
* Gets the line color.
*
* @return the line color.
*/
public Color getLineColor() {
return _lineColor;
}
/**
* Gets the line stroke.
*
* @return the line stroke.
*/
public Stroke getLineStroke() {
return _lineStroke;
}
/**
* Checks if the text has strike through style.
*
* @return true if the text has strike through style.
*/
public boolean isStrikethrough() {
return (_additionalStyle & STYLE_STRIKE_THROUGH) != 0;
}
/**
* Checks if the text has double strike through style.
*
* @return true if the text has double strike through style.
*/
public boolean isDoublestrikethrough() {
return (_additionalStyle & STYLE_DOUBLE_STRIKE_THROUGH) != 0;
}
/**
* Checks if the line has waved style.
*
* @return true if the line has waved style.
*/
public boolean isWaved() {
return (_additionalStyle & STYLE_WAVED) != 0;
}
/**
* Checks if the text has underlined style.
*
* @return true if the text has underlined style.
*/
public boolean isUnderlined() {
return (_additionalStyle & STYLE_UNDERLINED) != 0;
}
/**
* Checks if the line has dotted style.
*
* @return true if the line has dotted style.
*/
public boolean isDotted() {
return (_additionalStyle & STYLE_DOTTED) != 0;
}
/**
* Checks if the text is superscript.
*
* @return true if the text is superscript.
*/
public boolean isSuperscript() {
return (_additionalStyle & STYLE_SUPERSCRIPT) != 0;
}
/**
* Checks if the text is subscript.
*
* @return true if the text is subscript.
*/
public boolean isSubscript() {
return (_additionalStyle & STYLE_SUBSCRIPT) != 0;
}
/**
* Gets the font shrink ratio for superscript and subscript.
*
* @return the shrink ratio.
*/
public float getFontShrinkRatio() {
return _fontShrinkRatio;
}
}
|