blob: fa59a783f5d9036d721c9193b757519e4fff17c4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package javax.swing.text;
26
27import java.awt.Color;
28import java.awt.Component;
29import java.awt.Toolkit;
30import javax.swing.Icon;
31
32/**
33 * <p>
34 * A collection of <em>well known</em> or common attribute keys
35 * and methods to apply to an AttributeSet or MutableAttributeSet
36 * to get/set the properties in a typesafe manner.
37 * <p>
38 * The paragraph attributes form the definition of a paragraph to be rendered.
39 * All sizes are specified in points (such as found in postscript), a
40 * device independent measure.
41 * </p>
42 * <p align=center><img src="doc-files/paragraph.gif"
43 * alt="Diagram shows SpaceAbove, FirstLineIndent, LeftIndent, RightIndent,
44 * and SpaceBelow a paragraph."></p>
45 * <p>
46 *
47 * @author Timothy Prinzing
48 */
49public class StyleConstants {
50
51 /**
52 * Name of elements used to represent components.
53 */
54 public static final String ComponentElementName = "component";
55
56 /**
57 * Name of elements used to represent icons.
58 */
59 public static final String IconElementName = "icon";
60
61 /**
62 * Attribute name used to name the collection of
63 * attributes.
64 */
65 public static final Object NameAttribute = new StyleConstants("name");
66
67 /**
68 * Attribute name used to identifiy the resolving parent
69 * set of attributes, if one is defined.
70 */
71 public static final Object ResolveAttribute = new StyleConstants("resolver");
72
73 /**
74 * Attribute used to identify the model for embedded
75 * objects that have a model view separation.
76 */
77 public static final Object ModelAttribute = new StyleConstants("model");
78
79 /**
80 * Returns the string representation.
81 *
82 * @return the string
83 */
84 public String toString() {
85 return representation;
86 }
87
88 // ---- character constants -----------------------------------
89
90 /**
91 * Bidirectional level of a character as assigned by the Unicode bidi
92 * algorithm.
93 */
94 public static final Object BidiLevel = new CharacterConstants("bidiLevel");
95
96 /**
97 * Name of the font family.
98 */
99 public static final Object FontFamily = new FontConstants("family");
100
101 /**
102 * Name of the font family.
103 *
104 * @since 1.5
105 */
106 public static final Object Family = FontFamily;
107
108 /**
109 * Name of the font size.
110 */
111 public static final Object FontSize = new FontConstants("size");
112
113 /**
114 * Name of the font size.
115 *
116 * @since 1.5
117 */
118 public static final Object Size = FontSize;
119
120 /**
121 * Name of the bold attribute.
122 */
123 public static final Object Bold = new FontConstants("bold");
124
125 /**
126 * Name of the italic attribute.
127 */
128 public static final Object Italic = new FontConstants("italic");
129
130 /**
131 * Name of the underline attribute.
132 */
133 public static final Object Underline = new CharacterConstants("underline");
134
135 /**
136 * Name of the Strikethrough attribute.
137 */
138 public static final Object StrikeThrough = new CharacterConstants("strikethrough");
139
140 /**
141 * Name of the Superscript attribute.
142 */
143 public static final Object Superscript = new CharacterConstants("superscript");
144
145 /**
146 * Name of the Subscript attribute.
147 */
148 public static final Object Subscript = new CharacterConstants("subscript");
149
150 /**
151 * Name of the foreground color attribute.
152 */
153 public static final Object Foreground = new ColorConstants("foreground");
154
155 /**
156 * Name of the background color attribute.
157 */
158 public static final Object Background = new ColorConstants("background");
159
160 /**
161 * Name of the component attribute.
162 */
163 public static final Object ComponentAttribute = new CharacterConstants("component");
164
165 /**
166 * Name of the icon attribute.
167 */
168 public static final Object IconAttribute = new CharacterConstants("icon");
169
170 /**
171 * Name of the input method composed text attribute. The value of
172 * this attribute is an instance of AttributedString which represents
173 * the composed text.
174 */
175 public static final Object ComposedTextAttribute = new StyleConstants("composed text");
176
177 /**
178 * The amount of space to indent the first
179 * line of the paragraph. This value may be negative
180 * to offset in the reverse direction. The type
181 * is Float and specifies the size of the space
182 * in points.
183 */
184 public static final Object FirstLineIndent = new ParagraphConstants("FirstLineIndent");
185
186 /**
187 * The amount to indent the left side
188 * of the paragraph.
189 * Type is float and specifies the size in points.
190 */
191 public static final Object LeftIndent = new ParagraphConstants("LeftIndent");
192
193 /**
194 * The amount to indent the right side
195 * of the paragraph.
196 * Type is float and specifies the size in points.
197 */
198 public static final Object RightIndent = new ParagraphConstants("RightIndent");
199
200 /**
201 * The amount of space between lines
202 * of the paragraph.
203 * Type is float and specifies the size as a factor of the line height
204 */
205 public static final Object LineSpacing = new ParagraphConstants("LineSpacing");
206
207 /**
208 * The amount of space above the paragraph.
209 * Type is float and specifies the size in points.
210 */
211 public static final Object SpaceAbove = new ParagraphConstants("SpaceAbove");
212
213 /**
214 * The amount of space below the paragraph.
215 * Type is float and specifies the size in points.
216 */
217 public static final Object SpaceBelow = new ParagraphConstants("SpaceBelow");
218
219 /**
220 * Alignment for the paragraph. The type is
221 * Integer. Valid values are:
222 * <ul>
223 * <li>ALIGN_LEFT
224 * <li>ALIGN_RIGHT
225 * <li>ALIGN_CENTER
226 * <li>ALIGN_JUSTIFED
227 * </ul>
228 *
229 */
230 public static final Object Alignment = new ParagraphConstants("Alignment");
231
232 /**
233 * TabSet for the paragraph, type is a TabSet containing
234 * TabStops.
235 */
236 public static final Object TabSet = new ParagraphConstants("TabSet");
237
238 /**
239 * Orientation for a paragraph.
240 */
241 public static final Object Orientation = new ParagraphConstants("Orientation");
242 /**
243 * A possible value for paragraph alignment. This
244 * specifies that the text is aligned to the left
245 * indent and extra whitespace should be placed on
246 * the right.
247 */
248 public static final int ALIGN_LEFT = 0;
249
250 /**
251 * A possible value for paragraph alignment. This
252 * specifies that the text is aligned to the center
253 * and extra whitespace should be placed equally on
254 * the left and right.
255 */
256 public static final int ALIGN_CENTER = 1;
257
258 /**
259 * A possible value for paragraph alignment. This
260 * specifies that the text is aligned to the right
261 * indent and extra whitespace should be placed on
262 * the left.
263 */
264 public static final int ALIGN_RIGHT = 2;
265
266 /**
267 * A possible value for paragraph alignment. This
268 * specifies that extra whitespace should be spread
269 * out through the rows of the paragraph with the
270 * text lined up with the left and right indent
271 * except on the last line which should be aligned
272 * to the left.
273 */
274 public static final int ALIGN_JUSTIFIED = 3;
275
276 // --- character attribute accessors ---------------------------
277
278 /**
279 * Gets the BidiLevel setting.
280 *
281 * @param a the attribute set
282 * @return the value
283 */
284 public static int getBidiLevel(AttributeSet a) {
285 Integer o = (Integer) a.getAttribute(BidiLevel);
286 if (o != null) {
287 return o.intValue();
288 }
289 return 0; // Level 0 is base level (non-embedded) left-to-right
290 }
291
292 /**
293 * Sets the BidiLevel.
294 *
295 * @param a the attribute set
296 * @param o the bidi level value
297 */
298 public static void setBidiLevel(MutableAttributeSet a, int o) {
299 a.addAttribute(BidiLevel, new Integer(o));
300 }
301
302 /**
303 * Gets the component setting from the attribute list.
304 *
305 * @param a the attribute set
306 * @return the component, null if none
307 */
308 public static Component getComponent(AttributeSet a) {
309 return (Component) a.getAttribute(ComponentAttribute);
310 }
311
312 /**
313 * Sets the component attribute.
314 *
315 * @param a the attribute set
316 * @param c the component
317 */
318 public static void setComponent(MutableAttributeSet a, Component c) {
319 a.addAttribute(AbstractDocument.ElementNameAttribute, ComponentElementName);
320 a.addAttribute(ComponentAttribute, c);
321 }
322
323 /**
324 * Gets the icon setting from the attribute list.
325 *
326 * @param a the attribute set
327 * @return the icon, null if none
328 */
329 public static Icon getIcon(AttributeSet a) {
330 return (Icon) a.getAttribute(IconAttribute);
331 }
332
333 /**
334 * Sets the icon attribute.
335 *
336 * @param a the attribute set
337 * @param c the icon
338 */
339 public static void setIcon(MutableAttributeSet a, Icon c) {
340 a.addAttribute(AbstractDocument.ElementNameAttribute, IconElementName);
341 a.addAttribute(IconAttribute, c);
342 }
343
344 /**
345 * Gets the font family setting from the attribute list.
346 *
347 * @param a the attribute set
348 * @return the font family, "Monospaced" as the default
349 */
350 public static String getFontFamily(AttributeSet a) {
351 String family = (String) a.getAttribute(FontFamily);
352 if (family == null) {
353 family = "Monospaced";
354 }
355 return family;
356 }
357
358 /**
359 * Sets the font attribute.
360 *
361 * @param a the attribute set
362 * @param fam the font
363 */
364 public static void setFontFamily(MutableAttributeSet a, String fam) {
365 a.addAttribute(FontFamily, fam);
366 }
367
368 /**
369 * Gets the font size setting from the attribute list.
370 *
371 * @param a the attribute set
372 * @return the font size, 12 as the default
373 */
374 public static int getFontSize(AttributeSet a) {
375 Integer size = (Integer) a.getAttribute(FontSize);
376 if (size != null) {
377 return size.intValue();
378 }
379 return 12;
380 }
381
382 /**
383 * Sets the font size attribute.
384 *
385 * @param a the attribute set
386 * @param s the font size
387 */
388 public static void setFontSize(MutableAttributeSet a, int s) {
389 a.addAttribute(FontSize, new Integer(s));
390 }
391
392 /**
393 * Checks whether the bold attribute is set.
394 *
395 * @param a the attribute set
396 * @return true if set else false
397 */
398 public static boolean isBold(AttributeSet a) {
399 Boolean bold = (Boolean) a.getAttribute(Bold);
400 if (bold != null) {
401 return bold.booleanValue();
402 }
403 return false;
404 }
405
406 /**
407 * Sets the bold attribute.
408 *
409 * @param a the attribute set
410 * @param b specifies true/false for setting the attribute
411 */
412 public static void setBold(MutableAttributeSet a, boolean b) {
413 a.addAttribute(Bold, Boolean.valueOf(b));
414 }
415
416 /**
417 * Checks whether the italic attribute is set.
418 *
419 * @param a the attribute set
420 * @return true if set else false
421 */
422 public static boolean isItalic(AttributeSet a) {
423 Boolean italic = (Boolean) a.getAttribute(Italic);
424 if (italic != null) {
425 return italic.booleanValue();
426 }
427 return false;
428 }
429
430 /**
431 * Sets the italic attribute.
432 *
433 * @param a the attribute set
434 * @param b specifies true/false for setting the attribute
435 */
436 public static void setItalic(MutableAttributeSet a, boolean b) {
437 a.addAttribute(Italic, Boolean.valueOf(b));
438 }
439
440 /**
441 * Checks whether the underline attribute is set.
442 *
443 * @param a the attribute set
444 * @return true if set else false
445 */
446 public static boolean isUnderline(AttributeSet a) {
447 Boolean underline = (Boolean) a.getAttribute(Underline);
448 if (underline != null) {
449 return underline.booleanValue();
450 }
451 return false;
452 }
453
454 /**
455 * Checks whether the strikethrough attribute is set.
456 *
457 * @param a the attribute set
458 * @return true if set else false
459 */
460 public static boolean isStrikeThrough(AttributeSet a) {
461 Boolean strike = (Boolean) a.getAttribute(StrikeThrough);
462 if (strike != null) {
463 return strike.booleanValue();
464 }
465 return false;
466 }
467
468
469 /**
470 * Checks whether the superscript attribute is set.
471 *
472 * @param a the attribute set
473 * @return true if set else false
474 */
475 public static boolean isSuperscript(AttributeSet a) {
476 Boolean superscript = (Boolean) a.getAttribute(Superscript);
477 if (superscript != null) {
478 return superscript.booleanValue();
479 }
480 return false;
481 }
482
483
484 /**
485 * Checks whether the subscript attribute is set.
486 *
487 * @param a the attribute set
488 * @return true if set else false
489 */
490 public static boolean isSubscript(AttributeSet a) {
491 Boolean subscript = (Boolean) a.getAttribute(Subscript);
492 if (subscript != null) {
493 return subscript.booleanValue();
494 }
495 return false;
496 }
497
498
499 /**
500 * Sets the underline attribute.
501 *
502 * @param a the attribute set
503 * @param b specifies true/false for setting the attribute
504 */
505 public static void setUnderline(MutableAttributeSet a, boolean b) {
506 a.addAttribute(Underline, Boolean.valueOf(b));
507 }
508
509 /**
510 * Sets the strikethrough attribute.
511 *
512 * @param a the attribute set
513 * @param b specifies true/false for setting the attribute
514 */
515 public static void setStrikeThrough(MutableAttributeSet a, boolean b) {
516 a.addAttribute(StrikeThrough, Boolean.valueOf(b));
517 }
518
519 /**
520 * Sets the superscript attribute.
521 *
522 * @param a the attribute set
523 * @param b specifies true/false for setting the attribute
524 */
525 public static void setSuperscript(MutableAttributeSet a, boolean b) {
526 a.addAttribute(Superscript, Boolean.valueOf(b));
527 }
528
529 /**
530 * Sets the subscript attribute.
531 *
532 * @param a the attribute set
533 * @param b specifies true/false for setting the attribute
534 */
535 public static void setSubscript(MutableAttributeSet a, boolean b) {
536 a.addAttribute(Subscript, Boolean.valueOf(b));
537 }
538
539
540 /**
541 * Gets the foreground color setting from the attribute list.
542 *
543 * @param a the attribute set
544 * @return the color, Color.black as the default
545 */
546 public static Color getForeground(AttributeSet a) {
547 Color fg = (Color) a.getAttribute(Foreground);
548 if (fg == null) {
549 fg = Color.black;
550 }
551 return fg;
552 }
553
554 /**
555 * Sets the foreground color.
556 *
557 * @param a the attribute set
558 * @param fg the color
559 */
560 public static void setForeground(MutableAttributeSet a, Color fg) {
561 a.addAttribute(Foreground, fg);
562 }
563
564 /**
565 * Gets the background color setting from the attribute list.
566 *
567 * @param a the attribute set
568 * @return the color, Color.black as the default
569 */
570 public static Color getBackground(AttributeSet a) {
571 Color fg = (Color) a.getAttribute(Background);
572 if (fg == null) {
573 fg = Color.black;
574 }
575 return fg;
576 }
577
578 /**
579 * Sets the background color.
580 *
581 * @param a the attribute set
582 * @param fg the color
583 */
584 public static void setBackground(MutableAttributeSet a, Color fg) {
585 a.addAttribute(Background, fg);
586 }
587
588
589 // --- paragraph attribute accessors ----------------------------
590
591 /**
592 * Gets the first line indent setting.
593 *
594 * @param a the attribute set
595 * @return the value, 0 if not set
596 */
597 public static float getFirstLineIndent(AttributeSet a) {
598 Float indent = (Float) a.getAttribute(FirstLineIndent);
599 if (indent != null) {
600 return indent.floatValue();
601 }
602 return 0;
603 }
604
605 /**
606 * Sets the first line indent.
607 *
608 * @param a the attribute set
609 * @param i the value
610 */
611 public static void setFirstLineIndent(MutableAttributeSet a, float i) {
612 a.addAttribute(FirstLineIndent, new Float(i));
613 }
614
615 /**
616 * Gets the right indent setting.
617 *
618 * @param a the attribute set
619 * @return the value, 0 if not set
620 */
621 public static float getRightIndent(AttributeSet a) {
622 Float indent = (Float) a.getAttribute(RightIndent);
623 if (indent != null) {
624 return indent.floatValue();
625 }
626 return 0;
627 }
628
629 /**
630 * Sets right indent.
631 *
632 * @param a the attribute set
633 * @param i the value
634 */
635 public static void setRightIndent(MutableAttributeSet a, float i) {
636 a.addAttribute(RightIndent, new Float(i));
637 }
638
639 /**
640 * Gets the left indent setting.
641 *
642 * @param a the attribute set
643 * @return the value, 0 if not set
644 */
645 public static float getLeftIndent(AttributeSet a) {
646 Float indent = (Float) a.getAttribute(LeftIndent);
647 if (indent != null) {
648 return indent.floatValue();
649 }
650 return 0;
651 }
652
653 /**
654 * Sets left indent.
655 *
656 * @param a the attribute set
657 * @param i the value
658 */
659 public static void setLeftIndent(MutableAttributeSet a, float i) {
660 a.addAttribute(LeftIndent, new Float(i));
661 }
662
663 /**
664 * Gets the line spacing setting.
665 *
666 * @param a the attribute set
667 * @return the value, 0 if not set
668 */
669 public static float getLineSpacing(AttributeSet a) {
670 Float space = (Float) a.getAttribute(LineSpacing);
671 if (space != null) {
672 return space.floatValue();
673 }
674 return 0;
675 }
676
677 /**
678 * Sets line spacing.
679 *
680 * @param a the attribute set
681 * @param i the value
682 */
683 public static void setLineSpacing(MutableAttributeSet a, float i) {
684 a.addAttribute(LineSpacing, new Float(i));
685 }
686
687 /**
688 * Gets the space above setting.
689 *
690 * @param a the attribute set
691 * @return the value, 0 if not set
692 */
693 public static float getSpaceAbove(AttributeSet a) {
694 Float space = (Float) a.getAttribute(SpaceAbove);
695 if (space != null) {
696 return space.floatValue();
697 }
698 return 0;
699 }
700
701 /**
702 * Sets space above.
703 *
704 * @param a the attribute set
705 * @param i the value
706 */
707 public static void setSpaceAbove(MutableAttributeSet a, float i) {
708 a.addAttribute(SpaceAbove, new Float(i));
709 }
710
711 /**
712 * Gets the space below setting.
713 *
714 * @param a the attribute set
715 * @return the value, 0 if not set
716 */
717 public static float getSpaceBelow(AttributeSet a) {
718 Float space = (Float) a.getAttribute(SpaceBelow);
719 if (space != null) {
720 return space.floatValue();
721 }
722 return 0;
723 }
724
725 /**
726 * Sets space below.
727 *
728 * @param a the attribute set
729 * @param i the value
730 */
731 public static void setSpaceBelow(MutableAttributeSet a, float i) {
732 a.addAttribute(SpaceBelow, new Float(i));
733 }
734
735 /**
736 * Gets the alignment setting.
737 *
738 * @param a the attribute set
739 * @return the value <code>StyleConstants.ALIGN_LEFT</code> if not set
740 */
741 public static int getAlignment(AttributeSet a) {
742 Integer align = (Integer) a.getAttribute(Alignment);
743 if (align != null) {
744 return align.intValue();
745 }
746 return ALIGN_LEFT;
747 }
748
749 /**
750 * Sets alignment.
751 *
752 * @param a the attribute set
753 * @param align the alignment value
754 */
755 public static void setAlignment(MutableAttributeSet a, int align) {
756 a.addAttribute(Alignment, new Integer(align));
757 }
758
759 /**
760 * Gets the TabSet.
761 *
762 * @param a the attribute set
763 * @return the <code>TabSet</code>
764 */
765 public static TabSet getTabSet(AttributeSet a) {
766 TabSet tabs = (TabSet)a.getAttribute(TabSet);
767 // PENDING: should this return a default?
768 return tabs;
769 }
770
771 /**
772 * Sets the TabSet.
773 *
774 * @param a the attribute set.
775 * @param tabs the TabSet
776 */
777 public static void setTabSet(MutableAttributeSet a, TabSet tabs) {
778 a.addAttribute(TabSet, tabs);
779 }
780
781 // --- privates ---------------------------------------------
782
783 static Object[] keys = {
784 NameAttribute, ResolveAttribute, BidiLevel,
785 FontFamily, FontSize, Bold, Italic, Underline,
786 StrikeThrough, Superscript, Subscript, Foreground,
787 Background, ComponentAttribute, IconAttribute,
788 FirstLineIndent, LeftIndent, RightIndent, LineSpacing,
789 SpaceAbove, SpaceBelow, Alignment, TabSet, Orientation,
790 ModelAttribute, ComposedTextAttribute
791 };
792
793 StyleConstants(String representation) {
794 this.representation = representation;
795 }
796
797 private String representation;
798
799 /**
800 * This is a typesafe enumeration of the <em>well-known</em>
801 * attributes that contribute to a paragraph style. These are
802 * aliased by the outer class for general presentation.
803 */
804 public static class ParagraphConstants extends StyleConstants
805 implements AttributeSet.ParagraphAttribute {
806
807 private ParagraphConstants(String representation) {
808 super(representation);
809 }
810 }
811
812 /**
813 * This is a typesafe enumeration of the <em>well-known</em>
814 * attributes that contribute to a character style. These are
815 * aliased by the outer class for general presentation.
816 */
817 public static class CharacterConstants extends StyleConstants
818 implements AttributeSet.CharacterAttribute {
819
820 private CharacterConstants(String representation) {
821 super(representation);
822 }
823 }
824
825 /**
826 * This is a typesafe enumeration of the <em>well-known</em>
827 * attributes that contribute to a color. These are aliased
828 * by the outer class for general presentation.
829 */
830 public static class ColorConstants extends StyleConstants
831 implements AttributeSet.ColorAttribute, AttributeSet.CharacterAttribute {
832
833 private ColorConstants(String representation) {
834 super(representation);
835 }
836 }
837
838 /**
839 * This is a typesafe enumeration of the <em>well-known</em>
840 * attributes that contribute to a font. These are aliased
841 * by the outer class for general presentation.
842 */
843 public static class FontConstants extends StyleConstants
844 implements AttributeSet.FontAttribute, AttributeSet.CharacterAttribute {
845
846 private FontConstants(String representation) {
847 super(representation);
848 }
849 }
850
851
852}