1. /*
  2. * @(#)InputMethodHighlight.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.im;
  8. import java.util.Map;
  9. /**
  10. * An InputMethodHighlight is used to describe the highlight
  11. * attributes of text being composed.
  12. * The description can be at two levels:
  13. * at the abstract level it specifies the conversion state and whether the
  14. * text is selected; at the concrete level it specifies style attributes used
  15. * to render the highlight.
  16. * An InputMethodHighlight must provide the description at the
  17. * abstract level; it may or may not provide the description at the concrete
  18. * level.
  19. * If no concrete style is provided, a renderer should use
  20. * {@link java.awt.Toolkit#mapInputMethodHighlight} to map to a concrete style.
  21. * <p>
  22. * The abstract description consists of three fields: <code>selected</code>,
  23. * <code>state</code>, and <code>variation</code>.
  24. * <code>selected</code> indicates whether the text range is the one that the
  25. * input method is currently working on, for example, the segment for which
  26. * conversion candidates are currently shown in a menu.
  27. * <code>state</code> represents the conversion state. State values are defined
  28. * by the input method framework and should be distinguished in all
  29. * mappings from abstract to concrete styles. Currently defined state values
  30. * are raw (unconverted) and converted.
  31. * These state values are recommended for use before and after the
  32. * main conversion step of text composition, say, before and after kana->kanji
  33. * or pinyin->hanzi conversion.
  34. * The <code>variation</code> field allows input methods to express additional
  35. * information about the conversion results.
  36. * <p>
  37. *
  38. * InputMethodHighlight instances are typically used as attribute values
  39. * returned from AttributedCharacterIterator for the INPUT_METHOD_HIGHLIGHT
  40. * attribute. They may be wrapped into {@link java.text.Annotation Annotation}
  41. * instances to indicate separate text segments.
  42. *
  43. * @version 1.19, 01/23/03
  44. * @see java.text.AttributedCharacterIterator
  45. * @since 1.2
  46. */
  47. public class InputMethodHighlight {
  48. /**
  49. * Constant for the raw text state.
  50. */
  51. public final static int RAW_TEXT = 0;
  52. /**
  53. * Constant for the converted text state.
  54. */
  55. public final static int CONVERTED_TEXT = 1;
  56. /**
  57. * Constant for the default highlight for unselected raw text.
  58. */
  59. public final static InputMethodHighlight UNSELECTED_RAW_TEXT_HIGHLIGHT =
  60. new InputMethodHighlight(false, RAW_TEXT);
  61. /**
  62. * Constant for the default highlight for selected raw text.
  63. */
  64. public final static InputMethodHighlight SELECTED_RAW_TEXT_HIGHLIGHT =
  65. new InputMethodHighlight(true, RAW_TEXT);
  66. /**
  67. * Constant for the default highlight for unselected converted text.
  68. */
  69. public final static InputMethodHighlight UNSELECTED_CONVERTED_TEXT_HIGHLIGHT =
  70. new InputMethodHighlight(false, CONVERTED_TEXT);
  71. /**
  72. * Constant for the default highlight for selected converted text.
  73. */
  74. public final static InputMethodHighlight SELECTED_CONVERTED_TEXT_HIGHLIGHT =
  75. new InputMethodHighlight(true, CONVERTED_TEXT);
  76. /**
  77. * Constructs an input method highlight record.
  78. * The variation is set to 0, the style to null.
  79. * @param selected Whether the text range is selected
  80. * @param state The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
  81. * @see InputMethodHighlight#RAW_TEXT
  82. * @see InputMethodHighlight#CONVERTED_TEXT
  83. * @exception IllegalArgumentException if a state other than RAW_TEXT or CONVERTED_TEXT is given
  84. */
  85. public InputMethodHighlight(boolean selected, int state) {
  86. this(selected, state, 0, null);
  87. }
  88. /**
  89. * Constructs an input method highlight record.
  90. * The style is set to null.
  91. * @param selected Whether the text range is selected
  92. * @param state The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
  93. * @param variation The style variation for the text range
  94. * @see InputMethodHighlight#RAW_TEXT
  95. * @see InputMethodHighlight#CONVERTED_TEXT
  96. * @exception IllegalArgumentException if a state other than RAW_TEXT or CONVERTED_TEXT is given
  97. */
  98. public InputMethodHighlight(boolean selected, int state, int variation) {
  99. this(selected, state, variation, null);
  100. }
  101. /**
  102. * Constructs an input method highlight record.
  103. * The style attributes map provided must be unmodifiable.
  104. * @param selected whether the text range is selected
  105. * @param state the conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
  106. * @param variation the variation for the text range
  107. * @param style the rendering style attributes for the text range, or null
  108. * @see InputMethodHighlight#RAW_TEXT
  109. * @see InputMethodHighlight#CONVERTED_TEXT
  110. * @exception IllegalArgumentException if a state other than RAW_TEXT or CONVERTED_TEXT is given
  111. * @since 1.3
  112. */
  113. public InputMethodHighlight(boolean selected, int state, int variation, Map style) {
  114. this.selected = selected;
  115. if (!(state == RAW_TEXT || state == CONVERTED_TEXT)) {
  116. throw new IllegalArgumentException("unknown input method highlight state");
  117. }
  118. this.state = state;
  119. this.variation = variation;
  120. this.style = style;
  121. }
  122. /**
  123. * Returns whether the text range is selected.
  124. */
  125. public boolean isSelected() {
  126. return selected;
  127. }
  128. /**
  129. * Returns the conversion state of the text range.
  130. * @return The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT.
  131. * @see InputMethodHighlight#RAW_TEXT
  132. * @see InputMethodHighlight#CONVERTED_TEXT
  133. */
  134. public int getState() {
  135. return state;
  136. }
  137. /**
  138. * Returns the variation of the text range.
  139. */
  140. public int getVariation() {
  141. return variation;
  142. }
  143. /**
  144. * Returns the rendering style attributes for the text range, or null.
  145. * @since 1.3
  146. */
  147. public Map getStyle() {
  148. return style;
  149. }
  150. private boolean selected;
  151. private int state;
  152. private int variation;
  153. private Map style;
  154. };