KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > im > InputMethodHighlight


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
8 package java.awt.im;
9
10 import java.awt.font.TextAttribute JavaDoc;
11 import java.util.Map JavaDoc;
12
13 /**
14 * An InputMethodHighlight is used to describe the highlight
15 * attributes of text being composed.
16 * The description can be at two levels:
17 * at the abstract level it specifies the conversion state and whether the
18 * text is selected; at the concrete level it specifies style attributes used
19 * to render the highlight.
20 * An InputMethodHighlight must provide the description at the
21 * abstract level; it may or may not provide the description at the concrete
22 * level.
23 * If no concrete style is provided, a renderer should use
24 * {@link java.awt.Toolkit#mapInputMethodHighlight} to map to a concrete style.
25 * <p>
26 * The abstract description consists of three fields: <code>selected</code>,
27 * <code>state</code>, and <code>variation</code>.
28 * <code>selected</code> indicates whether the text range is the one that the
29 * input method is currently working on, for example, the segment for which
30 * conversion candidates are currently shown in a menu.
31 * <code>state</code> represents the conversion state. State values are defined
32 * by the input method framework and should be distinguished in all
33 * mappings from abstract to concrete styles. Currently defined state values
34 * are raw (unconverted) and converted.
35 * These state values are recommended for use before and after the
36 * main conversion step of text composition, say, before and after kana->kanji
37 * or pinyin->hanzi conversion.
38 * The <code>variation</code> field allows input methods to express additional
39 * information about the conversion results.
40 * <p>
41 *
42 * InputMethodHighlight instances are typically used as attribute values
43 * returned from AttributedCharacterIterator for the INPUT_METHOD_HIGHLIGHT
44 * attribute. They may be wrapped into {@link java.text.Annotation Annotation}
45 * instances to indicate separate text segments.
46 *
47 * @version 1.21, 05/05/04
48 * @see java.text.AttributedCharacterIterator
49 * @since 1.2
50 */

51
52 public class InputMethodHighlight {
53
54     /**
55      * Constant for the raw text state.
56      */

57     public final static int RAW_TEXT = 0;
58
59     /**
60      * Constant for the converted text state.
61      */

62     public final static int CONVERTED_TEXT = 1;
63
64
65     /**
66      * Constant for the default highlight for unselected raw text.
67      */

68     public final static InputMethodHighlight JavaDoc UNSELECTED_RAW_TEXT_HIGHLIGHT =
69         new InputMethodHighlight JavaDoc(false, RAW_TEXT);
70
71     /**
72      * Constant for the default highlight for selected raw text.
73      */

74     public final static InputMethodHighlight JavaDoc SELECTED_RAW_TEXT_HIGHLIGHT =
75         new InputMethodHighlight JavaDoc(true, RAW_TEXT);
76
77     /**
78      * Constant for the default highlight for unselected converted text.
79      */

80     public final static InputMethodHighlight JavaDoc UNSELECTED_CONVERTED_TEXT_HIGHLIGHT =
81         new InputMethodHighlight JavaDoc(false, CONVERTED_TEXT);
82
83     /**
84      * Constant for the default highlight for selected converted text.
85      */

86     public final static InputMethodHighlight JavaDoc SELECTED_CONVERTED_TEXT_HIGHLIGHT =
87         new InputMethodHighlight JavaDoc(true, CONVERTED_TEXT);
88
89
90     /**
91      * Constructs an input method highlight record.
92      * The variation is set to 0, the style to null.
93      * @param selected Whether the text range is selected
94      * @param state The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
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) {
100         this(selected, state, 0, null);
101     }
102
103     /**
104      * Constructs an input method highlight record.
105      * The style is set to null.
106      * @param selected Whether the text range is selected
107      * @param state The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
108      * @param variation The style variation for the text range
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      */

113     public InputMethodHighlight(boolean selected, int state, int variation) {
114         this(selected, state, variation, null);
115     }
116
117     /**
118      * Constructs an input method highlight record.
119      * The style attributes map provided must be unmodifiable.
120      * @param selected whether the text range is selected
121      * @param state the conversion state for the text range - RAW_TEXT or CONVERTED_TEXT
122      * @param variation the variation for the text range
123      * @param style the rendering style attributes for the text range, or null
124      * @see InputMethodHighlight#RAW_TEXT
125      * @see InputMethodHighlight#CONVERTED_TEXT
126      * @exception IllegalArgumentException if a state other than RAW_TEXT or CONVERTED_TEXT is given
127      * @since 1.3
128      */

129     public InputMethodHighlight(boolean selected, int state, int variation,
130                 Map JavaDoc<TextAttribute JavaDoc,?> style)
131     {
132         this.selected = selected;
133         if (!(state == RAW_TEXT || state == CONVERTED_TEXT)) {
134             throw new IllegalArgumentException JavaDoc("unknown input method highlight state");
135         }
136         this.state = state;
137         this.variation = variation;
138         this.style = style;
139     }
140
141     /**
142      * Returns whether the text range is selected.
143      */

144     public boolean isSelected() {
145         return selected;
146     }
147     
148     /**
149      * Returns the conversion state of the text range.
150      * @return The conversion state for the text range - RAW_TEXT or CONVERTED_TEXT.
151      * @see InputMethodHighlight#RAW_TEXT
152      * @see InputMethodHighlight#CONVERTED_TEXT
153      */

154     public int getState() {
155         return state;
156     }
157
158     /**
159      * Returns the variation of the text range.
160      */

161     public int getVariation() {
162         return variation;
163     }
164     
165     /**
166      * Returns the rendering style attributes for the text range, or null.
167      * @since 1.3
168      */

169     public Map JavaDoc<TextAttribute JavaDoc,?> getStyle() {
170         return style;
171     }
172
173     private boolean selected;
174     private int state;
175     private int variation;
176     private Map JavaDoc style;
177
178 };
179
Popular Tags