001: /*
002: * @(#)TextStyle.java 9/6/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import java.awt.*;
009:
010: /**
011: * A data structure represents a style for a range of text. There are two categories of styles
012: * that currently supports. One is the font style and color which includes bold, italic, superscript,
013: * subscript as well as the color of the text. The other one is line color and style. The line style could be
014: * straight line, dotted line, waved line or any customized style using Stroke. The line could be used as
015: * underline or strikethrough line.
016: * <p/>
017: * The name of StyleRange comes from SWT's StyleRange. We borrowed some design idea from it.
018: * StyledLabel is actually very similar to SWT's StyledText. Saying
019: * that, the features of the two components are not exactly the same since the purpose of the
020: * two components are quite different.
021: */
022: public class StyleRange {
023: public static final int STYLE_STRIKE_THROUGH = 0x1;
024: public static final int STYLE_DOUBLE_STRIKE_THROUGH = STYLE_STRIKE_THROUGH << 1;
025: public static final int STYLE_WAVED = STYLE_DOUBLE_STRIKE_THROUGH << 1;
026: public static final int STYLE_UNDERLINED = STYLE_WAVED << 1;
027: public static final int STYLE_DOTTED = STYLE_UNDERLINED << 1;
028: public static final int STYLE_SUPERSCRIPT = STYLE_DOTTED << 1;
029: public static final int STYLE_SUBSCRIPT = STYLE_SUPERSCRIPT << 1;
030:
031: private final int _fontStyle;
032: private final Color _fontColor;
033:
034: private final Color _backgroundColor;
035:
036: private final Color _lineColor;
037: private final Stroke _lineStroke;
038: private final int _additionalStyle;
039:
040: private final int _start;
041: private final int _length;
042:
043: private float _fontShrinkRatio = 1.5f;
044:
045: /**
046: * Creates a style range with a specified font style.
047: *
048: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
049: */
050: public StyleRange(int fontStyle) {
051: this (0, -1, fontStyle, null, 0, null, null);
052: }
053:
054: /**
055: * Creates a style range with a specified font color.
056: *
057: * @param fontColor the color of the text
058: */
059: public StyleRange(Color fontColor) {
060: this (0, -1, -1, fontColor, 0, null, null);
061: }
062:
063: /**
064: * Creates a style range with a specified font style and font color.
065: *
066: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
067: * @param fontColor the color of the text
068: */
069: public StyleRange(int fontStyle, Color fontColor) {
070: this (0, -1, fontStyle, fontColor, 0, null, null);
071: }
072:
073: /**
074: * Creates a style range with a specified font style and additional style.
075: *
076: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
077: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
078: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
079: */
080: public StyleRange(int fontStyle, int additionalStyle) {
081: this (0, -1, fontStyle, null, additionalStyle, null, null);
082: }
083:
084: /**
085: * Creates a style range with a specified font style and additional style.
086: *
087: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
088: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
089: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
090: * @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
091: */
092: public StyleRange(int fontStyle, int additionalStyle,
093: float fontShrinkRatio) {
094: this (0, -1, fontStyle, null, additionalStyle, null, null,
095: fontShrinkRatio);
096: }
097:
098: /**
099: * Creates a style range with a specified font style and a range.
100: *
101: * @param start the start index of the range in a string
102: * @param length the length of the range.
103: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
104: */
105: public StyleRange(int start, int length, int fontStyle) {
106: this (start, length, fontStyle, null, 0, null, null);
107: }
108:
109: /**
110: * Creates a style range with a specified font style, font color and a range.
111: *
112: * @param start the start index of the range in a string
113: * @param length the length of the range.
114: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
115: * @param fontColor the color of the text.
116: */
117: public StyleRange(int start, int length, int fontStyle,
118: Color fontColor) {
119: this (start, length, fontStyle, fontColor, 0, null, null);
120: }
121:
122: /**
123: * Creates a style range with a specified font color and a range.
124: *
125: * @param start the start index of the range in a string
126: * @param length the length of the range.
127: * @param fontColor the color of the text.
128: */
129: public StyleRange(int start, int length, Color fontColor) {
130: this (start, length, Font.PLAIN, fontColor, 0, null, null);
131: }
132:
133: /**
134: * Creates a style range with a specified font style, additional style and a range.
135: *
136: * @param start the start index of the range in a string
137: * @param length the length of the range.
138: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
139: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
140: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
141: */
142: public StyleRange(int start, int length, int fontStyle,
143: int additionalStyle) {
144: this (start, length, fontStyle, null, additionalStyle, null,
145: null);
146: }
147:
148: /**
149: * Creates a style range with a specified font style, additional style and a range.
150: *
151: * @param start the start index of the range in a string
152: * @param length the length of the range.
153: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
154: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
155: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
156: * @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
157: */
158: public StyleRange(int start, int length, int fontStyle,
159: int additionalStyle, float fontShrinkRatio) {
160: this (start, length, fontStyle, null, additionalStyle, null,
161: null, fontShrinkRatio);
162: }
163:
164: /**
165: * Creates a style range with a specified font style, font color, and additional style.
166: *
167: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
168: * @param fontColor the color of the text.
169: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
170: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
171: */
172: public StyleRange(int fontStyle, Color fontColor,
173: int additionalStyle, Color lineColor) {
174: this (0, -1, fontStyle, fontColor, additionalStyle, lineColor,
175: null);
176: }
177:
178: /**
179: * Creates a style range with a specified font style, font color, and additional style.
180: *
181: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
182: * @param fontColor the color of the text.
183: * @param backgroundColor the background color of the text.
184: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
185: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
186: */
187: public StyleRange(int fontStyle, Color fontColor,
188: Color backgroundColor, int additionalStyle, Color lineColor) {
189: this (0, -1, fontStyle, fontColor, backgroundColor,
190: additionalStyle, lineColor, null);
191: }
192:
193: /**
194: * Creates a style range with a specified font style, font color, additional style and a range.
195: *
196: * @param start the start index of the range in a string
197: * @param length the length of the range.
198: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
199: * @param fontColor the color of the text.
200: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
201: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
202: */
203: public StyleRange(int start, int length, int fontStyle,
204: Color fontColor, int additionalStyle) {
205: this (start, length, fontStyle, fontColor, additionalStyle,
206: null, null);
207: }
208:
209: /**
210: * Creates a style range with a specified font style, font color, additional style and a range.
211: *
212: * @param start the start index of the range in a string
213: * @param length the length of the range.
214: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
215: * @param fontColor the color of the text.
216: * @param backgroundColor the background color of the text.
217: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
218: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
219: */
220: public StyleRange(int start, int length, int fontStyle,
221: Color fontColor, Color backgroundColor, int additionalStyle) {
222: this (start, length, fontStyle, fontColor, backgroundColor,
223: additionalStyle, null, null);
224: }
225:
226: /**
227: * Creates a style range with a specified font style, font color, additional style, and line color.
228: *
229: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
230: * @param fontColor the color of the text.
231: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
232: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
233: * @param lineColor the color of the line.
234: */
235: public StyleRange(int fontStyle, Color fontColor,
236: int additionalStyle, Color lineColor, Stroke lineStroke) {
237: this (0, -1, fontStyle, fontColor, additionalStyle, lineColor,
238: lineStroke);
239: }
240:
241: /**
242: * Creates a style range with a specified font style, font color, additional style, line color and a range.
243: *
244: * @param start the start index of the range in a string
245: * @param length the length of the range.
246: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
247: * @param fontColor the color of the text.
248: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
249: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
250: * @param lineColor the color of the line.
251: */
252: public StyleRange(int start, int length, int fontStyle,
253: Color fontColor, int additionalStyle, Color lineColor) {
254: this (start, length, fontStyle, fontColor, additionalStyle,
255: lineColor, null);
256: }
257:
258: /**
259: * Creates a style range with a specified font style, font color, additional style, line color and a range.
260: *
261: * @param start the start index of the range in a string
262: * @param length the length of the range.
263: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
264: * @param fontColor the color of the text.
265: * @param backgroundColor the background color of the text.
266: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
267: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
268: * @param lineColor the color of the line.
269: */
270: public StyleRange(int start, int length, int fontStyle,
271: Color fontColor, Color backgroundColor,
272: int additionalStyle, Color lineColor) {
273: this (start, length, fontStyle, fontColor, backgroundColor,
274: additionalStyle, lineColor, null);
275: }
276:
277: /**
278: * Creates a style range with a specified font style, font color, additional style, line color, line stroke and a range.
279: *
280: * @param start the start index of the range in a string
281: * @param length the length of the range.
282: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
283: * @param fontColor the color of the text.
284: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
285: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
286: * @param lineColor the color of the line.
287: * @param lineStroke the stroke of the line.
288: */
289: public StyleRange(int start, int length, int fontStyle,
290: Color fontColor, int additionalStyle, Color lineColor,
291: Stroke lineStroke) {
292: this (start, length, fontStyle, fontColor, additionalStyle,
293: lineColor, lineStroke, 1.5f);
294: }
295:
296: /**
297: * Creates a style range with a specified font style, font color, additional style, line color, line stroke and a range.
298: *
299: * @param start the start index of the range in a string
300: * @param length the length of the range.
301: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
302: * @param fontColor the color of the text.
303: * @param backgroundColor the background color of the text.
304: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
305: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
306: * @param lineColor the color of the line.
307: * @param lineStroke the stroke of the line.
308: */
309: public StyleRange(int start, int length, int fontStyle,
310: Color fontColor, Color backgroundColor,
311: int additionalStyle, Color lineColor, Stroke lineStroke) {
312: this (start, length, fontStyle, fontColor, backgroundColor,
313: additionalStyle, lineColor, lineStroke, 1.5f);
314: }
315:
316: /**
317: * Creates a style range with a specified font style, font color, additional style, line color, line stroke and a range.
318: *
319: * @param start the start index of the range in a string
320: * @param length the length of the range.
321: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
322: * @param fontColor the color of the text.
323: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
324: * STYLE_. You can also use any | to concat two or more styles as long as it makes sense.
325: * @param lineColor the color of the line.
326: * @param lineStroke the stroke of the line.
327: * @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
328: */
329: public StyleRange(int start, int length, int fontStyle,
330: Color fontColor, int additionalStyle, Color lineColor,
331: Stroke lineStroke, float fontShrinkRatio) {
332: this (start, length, fontStyle, fontColor, null,
333: additionalStyle, lineColor, lineStroke, fontShrinkRatio);
334: }
335:
336: /**
337: * Creates a style range with a specified font style, font color, additional style, line color, line stroke and a range.
338: *
339: * @param start the start index of the range in a string
340: * @param length the length of the range.
341: * @param fontStyle Valid values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
342: * @param fontColor the color of the text.
343: * @param backgroundColor the background color of the text.
344: * @param additionalStyle Valid additional styles are defined as constants in {@link StyleRange}. The names begin with
345: * STYLE_. You can also use bitwise OR "|" to concat any two or more styles as long as it makes sense.
346: * @param lineColor the color of the line.
347: * @param lineStroke the stroke of the line.
348: * @param fontShrinkRatio the ratio that regular font size divides by subscript or superscript font size.
349: */
350: public StyleRange(int start, int length, int fontStyle,
351: Color fontColor, Color backgroundColor,
352: int additionalStyle, Color lineColor, Stroke lineStroke,
353: float fontShrinkRatio) {
354: if (length == 0) {
355: throw new IllegalArgumentException(
356: "The length of StyleRange cannot be 0.");
357: }
358:
359: _start = start;
360: _length = length;
361: _fontColor = fontColor;
362: _fontStyle = fontStyle;
363: _backgroundColor = backgroundColor;
364: _lineColor = lineColor;
365: _lineStroke = lineStroke;
366: _additionalStyle = additionalStyle;
367: _fontShrinkRatio = fontShrinkRatio;
368: }
369:
370: /**
371: * Gets the start index of the range.
372: *
373: * @return the start index of the range.
374: */
375: public int getStart() {
376: return _start;
377: }
378:
379: /**
380: * Gets the length of the range.
381: *
382: * @return the length of the range.
383: */
384: public int getLength() {
385: return _length;
386: }
387:
388: /**
389: * Gets the font style. Possible values are Font.PLAIN, Font.ITALIC, Font.BOLD or Font.BOLD | Font.ITALIC.
390: *
391: * @return the font style.
392: */
393: public int getFontStyle() {
394: return _fontStyle;
395: }
396:
397: /**
398: * Gets the font color.
399: *
400: * @return the font color.
401: */
402: public Color getFontColor() {
403: return _fontColor;
404: }
405:
406: /**
407: * Gets the background color.
408: *
409: * @return the background color.
410: */
411: public Color getBackgroundColor() {
412: return _backgroundColor;
413: }
414:
415: /**
416: * Gets the additional style. Possible additional styles are defined as constants in {@link StyleRange}.
417: * The names begin with STYLE_. The value could also be two or more styles concated by | as long as it makes sense.
418: * It could be more convenient to use methods {@link #isStrikethrough()}, {@link #isDoublestrikethrough()}, {@link #isDotted()}, {@link #isWaved()},
419: * {@link #isUnderlined()}, {@link #isSubscript()}, {@link #isSuperscript()} to see what's the additional style.
420: *
421: * @return the additional style.
422: */
423: public int getAdditionalStyle() {
424: return _additionalStyle;
425: }
426:
427: /**
428: * Gets the line color.
429: *
430: * @return the line color.
431: */
432: public Color getLineColor() {
433: return _lineColor;
434: }
435:
436: /**
437: * Gets the line stroke.
438: *
439: * @return the line stroke.
440: */
441: public Stroke getLineStroke() {
442: return _lineStroke;
443: }
444:
445: /**
446: * Checks if the text has strike through style.
447: *
448: * @return true if the text has strike through style.
449: */
450: public boolean isStrikethrough() {
451: return (_additionalStyle & STYLE_STRIKE_THROUGH) != 0;
452: }
453:
454: /**
455: * Checks if the text has double strike through style.
456: *
457: * @return true if the text has double strike through style.
458: */
459: public boolean isDoublestrikethrough() {
460: return (_additionalStyle & STYLE_DOUBLE_STRIKE_THROUGH) != 0;
461: }
462:
463: /**
464: * Checks if the line has waved style.
465: *
466: * @return true if the line has waved style.
467: */
468: public boolean isWaved() {
469: return (_additionalStyle & STYLE_WAVED) != 0;
470: }
471:
472: /**
473: * Checks if the text has underlined style.
474: *
475: * @return true if the text has underlined style.
476: */
477: public boolean isUnderlined() {
478: return (_additionalStyle & STYLE_UNDERLINED) != 0;
479: }
480:
481: /**
482: * Checks if the line has dotted style.
483: *
484: * @return true if the line has dotted style.
485: */
486: public boolean isDotted() {
487: return (_additionalStyle & STYLE_DOTTED) != 0;
488: }
489:
490: /**
491: * Checks if the text is superscript.
492: *
493: * @return true if the text is superscript.
494: */
495: public boolean isSuperscript() {
496: return (_additionalStyle & STYLE_SUPERSCRIPT) != 0;
497: }
498:
499: /**
500: * Checks if the text is subscript.
501: *
502: * @return true if the text is subscript.
503: */
504: public boolean isSubscript() {
505: return (_additionalStyle & STYLE_SUBSCRIPT) != 0;
506: }
507:
508: /**
509: * Gets the font shrink ratio for superscript and subscript.
510: *
511: * @return the shrink ratio.
512: */
513: public float getFontShrinkRatio() {
514: return _fontShrinkRatio;
515: }
516: }
|