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