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