libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / awt / event / InputMethodEvent.java
blobe0be40d5a6708d946e0f2394c3a393b55e7c20fe
1 /* InputMethodEvent.java -- events from a text input method
2 Copyright (C) 1999, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.event;
41 import gnu.java.lang.CPStringBuilder;
43 import java.awt.AWTEvent;
44 import java.awt.Component;
45 import java.awt.EventQueue;
46 import java.awt.font.TextHitInfo;
47 import java.io.IOException;
48 import java.io.ObjectInputStream;
49 import java.text.AttributedCharacterIterator;
51 /**
52 * This class is for event generated by change in a text input method.
54 * @author Aaron M. Renn (arenn@urbanophile.com)
55 * @see InputMethodListener
56 * @since 1.2
57 * @status updated to 1.4
59 public class InputMethodEvent extends AWTEvent
61 /**
62 * Compatible with JDK 1.2+.
64 private static final long serialVersionUID = 4727190874778922661L;
66 /** This is the first id in the range of event ids used by this class. */
67 public static final int INPUT_METHOD_FIRST = 1100;
69 /** This event id indicates that the text in the input method has changed. */
70 public static final int INPUT_METHOD_TEXT_CHANGED = 1100;
72 /** This event id indicates that the input method curor point has changed. */
73 public static final int CARET_POSITION_CHANGED = 1101;
75 /** This is the last id in the range of event ids used by this class. */
76 public static final int INPUT_METHOD_LAST = 1101;
78 /**
79 * The timestamp when this event was created.
81 * @serial the timestamp
82 * @since 1.4
84 private long when;
86 /** The input method text. */
87 private final transient AttributedCharacterIterator text;
89 /** The number of committed characters in the text. */
90 private final transient int committedCharacterCount;
92 /** The caret. */
93 private final transient TextHitInfo caret;
95 /** The most important position to be visible. */
96 private final transient TextHitInfo visiblePosition;
98 /**
99 * Initializes a new instance of <code>InputMethodEvent</code> with the
100 * specified source, id, timestamp, text, char count, caret, and visible
101 * position.
103 * @param source the source that generated the event
104 * @param id the event id
105 * @param when the timestamp of the event
106 * @param text the input text
107 * @param committedCharacterCount the number of committed characters
108 * @param caret the caret position
109 * @param visiblePosition the position most important to make visible
110 * @throws IllegalArgumentException if source is null, id is invalid, id is
111 * CARET_POSITION_CHANGED and text is non-null, or if
112 * committedCharacterCount is out of range
113 * @since 1.4
115 public InputMethodEvent(Component source, int id, long when,
116 AttributedCharacterIterator text,
117 int committedCharacterCount, TextHitInfo caret,
118 TextHitInfo visiblePosition)
120 super(source, id);
121 this.when = when;
122 this.text = text;
123 this.committedCharacterCount = committedCharacterCount;
124 this.caret = caret;
125 this.visiblePosition = visiblePosition;
126 if (id < INPUT_METHOD_FIRST || id > INPUT_METHOD_LAST
127 || (id == CARET_POSITION_CHANGED && text != null)
128 || committedCharacterCount < 0
129 || (committedCharacterCount
130 > (text == null ? 0 : text.getEndIndex() - text.getBeginIndex())))
131 throw new IllegalArgumentException();
135 * Initializes a new instance of <code>InputMethodEvent</code> with the
136 * specified source, id, text, char count, caret, and visible position.
138 * @param source the source that generated the event
139 * @param id the event id
140 * @param text the input text
141 * @param committedCharacterCount the number of committed characters
142 * @param caret the caret position
143 * @param visiblePosition the position most important to make visible
144 * @throws IllegalArgumentException if source is null, id is invalid, id is
145 * CARET_POSITION_CHANGED and text is non-null, or if
146 * committedCharacterCount is out of range
147 * @since 1.4
149 public InputMethodEvent(Component source, int id,
150 AttributedCharacterIterator text,
151 int committedCharacterCount, TextHitInfo caret,
152 TextHitInfo visiblePosition)
154 this(source, id, EventQueue.getMostRecentEventTime(), text,
155 committedCharacterCount, caret, visiblePosition);
159 * Initializes a new instance of <code>InputMethodEvent</code> with the
160 * specified source, id, caret, and visible position, and with a null
161 * text and char count.
163 * @param source the source that generated the event
164 * @param id the event id
165 * @param caret the caret position
166 * @param visiblePosition the position most important to make visible
167 * @throws IllegalArgumentException if source is null or id is invalid
168 * @since 1.4
170 public InputMethodEvent(Component source, int id, TextHitInfo caret,
171 TextHitInfo visiblePosition)
173 this(source, id, EventQueue.getMostRecentEventTime(), null, 0, caret,
174 visiblePosition);
178 * This method returns the input method text. This can be <code>null</code>,
179 * and will always be null for <code>CARET_POSITION_CHANGED</code> events.
180 * Characters from 0 to <code>getCommittedCharacterCount()-1</code> have
181 * been committed, the remaining characters are composed text.
183 * @return the input method text, or null
185 public AttributedCharacterIterator getText()
187 return text;
191 * Returns the number of committed characters in the input method text.
193 * @return the number of committed characters in the input method text
195 public int getCommittedCharacterCount()
197 return committedCharacterCount;
201 * Returns the caret position. The caret offset is relative to the composed
202 * text of the most recent <code>INPUT_METHOD_TEXT_CHANGED</code> event.
204 * @return the caret position, or null
206 public TextHitInfo getCaret()
208 return caret;
212 * Returns the position that is most important to be visible, or null if
213 * such a hint is not necessary. The caret offset is relative to the composed
214 * text of the most recent <code>INPUT_METHOD_TEXT_CHANGED</code> event.
216 * @return the position that is most important to be visible
218 public TextHitInfo getVisiblePosition()
220 return visiblePosition;
224 * This method consumes the event. A consumed event is not processed
225 * in the default manner by the component that generated it.
227 public void consume()
229 consumed = true;
233 * This method tests whether or not this event has been consumed.
235 * @return true if the event has been consumed
237 public boolean isConsumed()
239 return consumed;
243 * Return the timestamp of this event.
245 * @return the timestamp
246 * @since 1.4
248 public long getWhen()
250 return when;
254 * This method returns a string identifying the event. This contains the
255 * event ID, the committed and composed characters separated by '+', the
256 * number of committed characters, the caret, and the visible position.
258 * @return a string identifying the event
260 public String paramString()
262 CPStringBuilder s
263 = new CPStringBuilder(80 + (text == null ? 0
264 : text.getEndIndex() - text.getBeginIndex()));
265 s.append(id == INPUT_METHOD_TEXT_CHANGED ? "INPUT_METHOD_TEXT_CHANGED, "
266 : "CARET_POSITION_CHANGED, ");
267 if (text == null)
268 s.append("no text, 0 characters committed, caret: ");
269 else
271 s.append('"');
272 int i = text.getBeginIndex();
273 int j = committedCharacterCount;
274 while (--j >= 0)
275 s.append(text.setIndex(i++));
276 s.append("\" + \"");
277 j = text.getEndIndex() - i;
278 while (--j >= 0)
279 s.append(text.setIndex(i++));
280 s.append("\", ").append(committedCharacterCount)
281 .append(" characters committed, caret: ");
283 s.append(caret == null ? (Object) "no caret" : caret).append(", ")
284 .append(visiblePosition == null ? (Object) "no visible position"
285 : visiblePosition);
286 return s.toString();
290 * Reads in the object from a serial stream, updating when to
291 * {@link EventQueue#getMostRecentEventTime()} if necessary.
293 * @param s the stream to read from
294 * @throws IOException if deserialization fails
295 * @throws ClassNotFoundException if deserialization fails
296 * @serialData default, except for updating when
298 private void readObject(ObjectInputStream s)
299 throws IOException, ClassNotFoundException
301 s.defaultReadObject();
302 if (when == 0)
303 when = EventQueue.getMostRecentEventTime();
305 } // class InputMethodEvent