001: /*
002: * @(#)InputMethodHighlight.java 1.19 03/01/23
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package java.awt.im;
028:
029: import java.util.Map;
030:
031: /**
032: * An InputMethodHighlight is used to describe the highlight
033: * attributes of text being composed.
034: * The description can be at two levels:
035: * at the abstract level it specifies the conversion state and whether the
036: * text is selected; at the concrete level it specifies style attributes used
037: * to render the highlight.
038: * An InputMethodHighlight must provide the description at the
039: * abstract level; it may or may not provide the description at the concrete
040: * level.
041: * If no concrete style is provided, a renderer should use
042: * {@link java.awt.Toolkit#mapInputMethodHighlight} to map to a concrete style.
043: * <p>
044: * The abstract description consists of three fields: <code>selected</code>,
045: * <code>state</code>, and <code>variation</code>.
046: * <code>selected</code> indicates whether the text range is the one that the
047: * input method is currently working on, for example, the segment for which
048: * conversion candidates are currently shown in a menu.
049: * <code>state</code> represents the conversion state. State values are defined
050: * by the input method framework and should be distinguished in all
051: * mappings from abstract to concrete styles. Currently defined state values
052: * are raw (unconverted) and converted.
053: * These state values are recommended for use before and after the
054: * main conversion step of text composition, say, before and after kana->kanji
055: * or pinyin->hanzi conversion.
056: * The <code>variation</code> field allows input methods to express additional
057: * information about the conversion results.
058: * <p>
059: *
060: * InputMethodHighlight instances are typically used as attribute values
061: * returned from AttributedCharacterIterator for the INPUT_METHOD_HIGHLIGHT
062: * attribute. They may be wrapped into {@link java.text.Annotation Annotation}
063: * instances to indicate separate text segments.
064: *
065: * @version 1.19, 01/23/03
066: * @see java.text.AttributedCharacterIterator
067: * @since 1.2
068: */
069:
070: public class InputMethodHighlight {
071:
072: /**
073: * Constant for the raw text state.
074: */
075: public final static int RAW_TEXT = 0;
076:
077: /**
078: * Constant for the converted text state.
079: */
080: public final static int CONVERTED_TEXT = 1;
081:
082: /**
083: * Constant for the default highlight for unselected raw text.
084: */
085: public final static InputMethodHighlight UNSELECTED_RAW_TEXT_HIGHLIGHT = new InputMethodHighlight(
086: false, RAW_TEXT);
087:
088: /**
089: * Constant for the default highlight for selected raw text.
090: */
091: public final static InputMethodHighlight SELECTED_RAW_TEXT_HIGHLIGHT = new InputMethodHighlight(
092: true, RAW_TEXT);
093:
094: /**
095: * Constant for the default highlight for unselected converted text.
096: */
097: public final static InputMethodHighlight UNSELECTED_CONVERTED_TEXT_HIGHLIGHT = new InputMethodHighlight(
098: false, CONVERTED_TEXT);
099:
100: /**
101: * Constant for the default highlight for selected converted text.
102: */
103: public final static InputMethodHighlight SELECTED_CONVERTED_TEXT_HIGHLIGHT = new InputMethodHighlight(
104: true, CONVERTED_TEXT);
105:
106: /**
107: * Constructs an input method highlight record.
108: * The variation is set to 0, the style to null.
109: * @param selected Whether the text range is selected
110: * @param state The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
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: */
115: public InputMethodHighlight(boolean selected, int state) {
116: this (selected, state, 0);
117: }
118:
119: /**
120: * Constructs an input method highlight record.
121: * The style is set to null.
122: * @param selected Whether the text range is selected
123: * @param state The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
124: * @param variation The style variation for the text range
125: * @see InputMethodHighlight#RAW_TEXT
126: * @see InputMethodHighlight#CONVERTED_TEXT
127: * @exception IllegalArgumentException if a state other than RAW_TEXT or CONVERTED_TEXT is given
128: */
129: public InputMethodHighlight(boolean selected, int state,
130: int variation) {
131: this .selected = selected;
132: if (!(state == RAW_TEXT || state == CONVERTED_TEXT)) {
133: throw new IllegalArgumentException(
134: "unknown input method highlight state");
135: }
136: this .state = state;
137: this .variation = variation;
138: }
139:
140: /**
141: * Constructs an input method highlight record.
142: * The style attributes map provided must be unmodifiable.
143: * @param selected whether the text range is selected
144: * @param state the conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
145: * @param variation the variation for the text range
146: * @param style the rendering style attributes for the text range, or null
147: * @see InputMethodHighlight#RAW_TEXT
148: * @see InputMethodHighlight#CONVERTED_TEXT
149: * @exception IllegalArgumentException if a state other than RAW_TEXT or CONVERTED_TEXT is given
150: * @since 1.3
151: */
152: /*
153: public InputMethodHighlight(boolean selected, int state, int variation, Map style) {
154: this.selected = selected;
155: if (!(state == RAW_TEXT || state == CONVERTED_TEXT)) {
156: throw new IllegalArgumentException("unknown input method highlight state");
157: }
158: this.state = state;
159: this.variation = variation;
160: this.style = style;
161: }
162: */
163:
164: /**
165: * Returns whether the text range is selected.
166: */
167: public boolean isSelected() {
168: return selected;
169: }
170:
171: /**
172: * Returns the conversion state of the text range.
173: * @return The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT.
174: * @see InputMethodHighlight#RAW_TEXT
175: * @see InputMethodHighlight#CONVERTED_TEXT
176: */
177: public int getState() {
178: return state;
179: }
180:
181: /**
182: * Returns the variation of the text range.
183: */
184: public int getVariation() {
185: return variation;
186: }
187:
188: /**
189: * Returns the rendering style attributes for the text range, or null.
190: * @since 1.3
191: */
192: /*
193: public Map getStyle() {
194: return style;
195: }
196: */
197:
198: private boolean selected;
199: private int state;
200: private int variation;
201: private Map style;
202:
203: };
|