InputMethodHighlight.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* InputMethodHighlight.java -- highlights the current text selection
  2. Copyright (C) 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt.im;
  32. import java.util.Map;
  33. /**
  34. * This describes the highlight attributes of text composed in an input method.
  35. * The description includes an abstract level (whether text has been converted
  36. * yet, and whether it is selected), and a concrete level (which style
  37. * attributes are used in rendering). If no concrete level is defined, the
  38. * renderer should use
  39. * {@link Toolkit#mapInputMethodHighlight(InputMethodHighlight)}. An example
  40. * of conversion state is kana -> kanji.
  41. *
  42. * <p>Instances of this class are typically used in
  43. * AttributedCharacterIterators, and may be wrapped in Annotations to separate
  44. * text segments.
  45. *
  46. * @author Eric Blake <ebb9@email.byu.edu>
  47. * @see AttributedCharacterIterators
  48. * @see Annotation
  49. * @since 1.2
  50. * @status updated to 1.4
  51. */
  52. public class InputMethodHighlight
  53. {
  54. /** Raw text state (before conversion). */
  55. public static final int RAW_TEXT = 0;
  56. /** Converted text state (after conversion). */
  57. public static final int CONVERTED_TEXT = 1;
  58. /** Default do-nothing highlighting for unselected raw text. */
  59. public static final InputMethodHighlight UNSELECTED_RAW_TEXT_HIGHLIGHT
  60. = new InputMethodHighlight(false, RAW_TEXT);
  61. /** Default do-nothing highlighting for selected raw text. */
  62. public static final InputMethodHighlight SELECTED_RAW_TEXT_HIGHLIGHT
  63. = new InputMethodHighlight(true, RAW_TEXT);
  64. /** Default do-nothing highlighting for unselected converted text. */
  65. public static final InputMethodHighlight UNSELECTED_CONVERTED_TEXT_HIGHLIGHT
  66. = new InputMethodHighlight(false, CONVERTED_TEXT);
  67. /** Default do-nothing highlighting for selected converted text. */
  68. public static final InputMethodHighlight SELECTED_CONVERTED_TEXT_HIGHLIGHT
  69. = new InputMethodHighlight(true, CONVERTED_TEXT);
  70. /** Whether the highlighting applies to selected text. */
  71. private final boolean selected;
  72. /** The state of highlighted text. */
  73. private final int state;
  74. /** Any variation on the highlighting style. */
  75. private final int variation;
  76. /** The unmodifiable map of rendering styles. */
  77. private final Map style;
  78. /**
  79. * Create an input method highlight style, with variation 0 and null style
  80. * mapping.
  81. *
  82. * @param selected whether the text range is selected
  83. * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
  84. * @throws IllegalArgumentException if state is invalid
  85. */
  86. public InputMethodHighlight(boolean selected, int state)
  87. {
  88. this(selected, state, 0, null);
  89. }
  90. /**
  91. * Create an input method highlight style, with null style mapping.
  92. *
  93. * @param selected whether the text range is selected
  94. * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
  95. * @param variation the style variation
  96. * @throws IllegalArgumentException if state is invalid
  97. */
  98. public InputMethodHighlight(boolean selected, int state, int variation)
  99. {
  100. this(selected, state, variation, null);
  101. }
  102. /**
  103. * Create an input method highlight style.
  104. *
  105. * @param selected whether the text range is selected
  106. * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
  107. * @param variation the style variation
  108. * @param style an unmodifiable map of rendering styles, or null
  109. * @throws IllegalArgumentException if state is invalid
  110. * @since 1.3
  111. */
  112. public InputMethodHighlight(boolean selected, int state, int variation,
  113. Map style)
  114. {
  115. if (state != RAW_TEXT && state != CONVERTED_TEXT)
  116. throw new IllegalArgumentException();
  117. this.selected = selected;
  118. this.state = state;
  119. this.variation = variation;
  120. this.style = style;
  121. }
  122. /**
  123. * Return whether the highlighting applies to selected text.
  124. *
  125. * @return the selection status
  126. */
  127. public boolean isSelected()
  128. {
  129. return selected;
  130. }
  131. /**
  132. * Return the conversion state of the highlighted text.
  133. *
  134. * @return one of {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
  135. */
  136. public int getState()
  137. {
  138. return state;
  139. }
  140. /**
  141. * Return the highlighting style variation.
  142. *
  143. * @return the variation
  144. */
  145. public int getVariation()
  146. {
  147. return variation;
  148. }
  149. /**
  150. * Return the rendering style attributes map, or null if it should be the
  151. * default mapping.
  152. *
  153. * @return the style map
  154. * @since 1.3
  155. */
  156. public Map getStyle()
  157. {
  158. return style;
  159. }
  160. } // class InputMethodHighlight