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