001/* InputMethodHighlight.java -- highlights the current text selection
002   Copyright (C) 2002, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package java.awt.im;
039
040import java.awt.Toolkit;
041import java.text.Annotation;
042import java.text.AttributedCharacterIterator;
043import java.util.Map;
044import java.awt.font.TextAttribute;
045
046/**
047 * This describes the highlight attributes of text composed in an input method.
048 * The description includes an abstract level (whether text has been converted
049 * yet, and whether it is selected), and a concrete level (which style
050 * attributes are used in rendering). If no concrete level is defined, the
051 * renderer should use
052 * {@link Toolkit#mapInputMethodHighlight(InputMethodHighlight)}. An example
053 * of conversion state is kana -> kanji.
054 *
055 * <p>Instances of this class are typically used in
056 * AttributedCharacterIterators, and may be wrapped in Annotations to separate
057 * text segments.
058 *
059 * @author Eric Blake (ebb9@email.byu.edu)
060 * @see AttributedCharacterIterator
061 * @see Annotation
062 * @since 1.2
063 * @status updated to 1.4
064 */
065public class InputMethodHighlight
066{
067  /** Raw text state (before conversion). */
068  public static final int RAW_TEXT = 0;
069
070  /** Converted text state (after conversion). */
071  public static final int CONVERTED_TEXT = 1;
072
073  /** Default do-nothing highlighting for unselected raw text. */
074  public static final InputMethodHighlight UNSELECTED_RAW_TEXT_HIGHLIGHT
075    = new InputMethodHighlight(false, RAW_TEXT);
076
077  /** Default do-nothing highlighting for selected raw text. */
078  public static final InputMethodHighlight SELECTED_RAW_TEXT_HIGHLIGHT
079    = new InputMethodHighlight(true, RAW_TEXT);
080
081  /** Default do-nothing highlighting for unselected converted text. */
082  public static final InputMethodHighlight UNSELECTED_CONVERTED_TEXT_HIGHLIGHT
083    = new InputMethodHighlight(false, CONVERTED_TEXT);
084
085  /** Default do-nothing highlighting for selected converted text. */
086  public static final InputMethodHighlight SELECTED_CONVERTED_TEXT_HIGHLIGHT
087    = new InputMethodHighlight(true, CONVERTED_TEXT);
088
089  /** Whether the highlighting applies to selected text. */
090  private final boolean selected;
091
092  /** The state of highlighted text. */
093  private final int state;
094
095  /** Any variation on the highlighting style. */
096  private final int variation;
097
098  /** The unmodifiable map of rendering styles. */
099  private final Map<TextAttribute, ?> style;
100
101  /**
102   * Create an input method highlight style, with variation 0 and null style
103   * mapping.
104   *
105   * @param selected whether the text range is selected
106   * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
107   * @throws IllegalArgumentException if state is invalid
108   */
109  public InputMethodHighlight(boolean selected, int state)
110  {
111    this(selected, state, 0, null);
112  }
113
114  /**
115   * Create an input method highlight style, with null style mapping.
116   *
117   * @param selected whether the text range is selected
118   * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
119   * @param variation the style variation
120   * @throws IllegalArgumentException if state is invalid
121   */
122  public InputMethodHighlight(boolean selected, int state, int variation)
123  {
124    this(selected, state, variation, null);
125  }
126
127  /**
128   * Create an input method highlight style.
129   *
130   * @param selected whether the text range is selected
131   * @param state either {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
132   * @param variation the style variation
133   * @param style an unmodifiable map of rendering styles, or null
134   * @throws IllegalArgumentException if state is invalid
135   * @since 1.3
136   */
137  public InputMethodHighlight(boolean selected, int state, int variation,
138                              Map<TextAttribute, ?> style)
139  {
140    if (state != RAW_TEXT && state != CONVERTED_TEXT)
141      throw new IllegalArgumentException();
142    this.selected = selected;
143    this.state = state;
144    this.variation = variation;
145    this.style = style;
146  }
147
148  /**
149   * Return whether the highlighting applies to selected text.
150   *
151   * @return the selection status
152   */
153  public boolean isSelected()
154  {
155    return selected;
156  }
157
158  /**
159   * Return the conversion state of the highlighted text.
160   *
161   * @return one of {@link #RAW_TEXT} or {@link #CONVERTED_TEXT}
162   */
163  public int getState()
164  {
165    return state;
166  }
167
168  /**
169   * Return the highlighting style variation.
170   *
171   * @return the variation
172   */
173  public int getVariation()
174  {
175    return variation;
176  }
177
178  /**
179   * Return the rendering style attributes map, or null if it should be the
180   * default mapping.
181   *
182   * @return the style map
183   * @since 1.3
184   */
185  public Map<TextAttribute, ?> getStyle()
186  {
187    return style;
188  }
189} // class InputMethodHighlight