001    /* InputMethodEvent.java -- events from a text input method
002       Copyright (C) 1999, 2002, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath 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 for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.awt.event;
040    
041    import java.awt.AWTEvent;
042    import java.awt.Component;
043    import java.awt.EventQueue;
044    import java.awt.font.TextHitInfo;
045    import java.io.IOException;
046    import java.io.ObjectInputStream;
047    import java.text.AttributedCharacterIterator;
048    
049    /**
050     * This class is for event generated by change in a text input method.
051     *
052     * @author Aaron M. Renn (arenn@urbanophile.com)
053     * @see InputMethodListener
054     * @since 1.2
055     * @status updated to 1.4
056     */
057    public class InputMethodEvent extends AWTEvent
058    {
059      /**
060       * Compatible with JDK 1.2+.
061       */
062      private static final long serialVersionUID = 4727190874778922661L;
063    
064      /** This is the first id in the range of event ids used by this class. */
065      public static final int INPUT_METHOD_FIRST = 1100;
066    
067      /** This event id indicates that the text in the input method has changed. */
068      public static final int INPUT_METHOD_TEXT_CHANGED = 1100;
069    
070      /** This event id indicates that the input method curor point has changed. */
071      public static final int CARET_POSITION_CHANGED = 1101;
072    
073      /** This is the last id in the range of event ids used by this class. */
074      public static final int INPUT_METHOD_LAST = 1101;
075    
076      /**
077       * The timestamp when this event was created.
078       *
079       * @serial the timestamp
080       * @since 1.4
081       */
082      private long when;
083    
084      /** The input method text. */
085      private final transient AttributedCharacterIterator text;
086    
087      /** The number of committed characters in the text. */
088      private final transient int committedCharacterCount;
089    
090      /** The caret. */
091      private final transient TextHitInfo caret;
092    
093      /** The most important position to be visible. */
094      private final transient TextHitInfo visiblePosition;
095    
096      /**
097       * Initializes a new instance of <code>InputMethodEvent</code> with the
098       * specified source, id, timestamp, text, char count, caret, and visible
099       * position.
100       *
101       * @param source the source that generated the event
102       * @param id the event id
103       * @param when the timestamp of the event
104       * @param text the input text
105       * @param committedCharacterCount the number of committed characters
106       * @param caret the caret position
107       * @param visiblePosition the position most important to make visible
108       * @throws IllegalArgumentException if source is null, id is invalid, id is
109       *         CARET_POSITION_CHANGED and text is non-null, or if
110       *         committedCharacterCount is out of range
111       * @since 1.4
112       */
113      public InputMethodEvent(Component source, int id, long when,
114                              AttributedCharacterIterator text,
115                              int committedCharacterCount, TextHitInfo caret,
116                              TextHitInfo visiblePosition)
117      {
118        super(source, id);
119        this.when = when;
120        this.text = text;
121        this.committedCharacterCount = committedCharacterCount;
122        this.caret = caret;
123        this.visiblePosition = visiblePosition;
124        if (id < INPUT_METHOD_FIRST || id > INPUT_METHOD_LAST
125            || (id == CARET_POSITION_CHANGED && text != null)
126            || committedCharacterCount < 0
127            || (committedCharacterCount
128                > (text == null ? 0 : text.getEndIndex() - text.getBeginIndex())))
129          throw new IllegalArgumentException();
130      }
131    
132      /**
133       * Initializes a new instance of <code>InputMethodEvent</code> with the
134       * specified source, id, text, char count, caret, and visible position.
135       *
136       * @param source the source that generated the event
137       * @param id the event id
138       * @param text the input text
139       * @param committedCharacterCount the number of committed characters
140       * @param caret the caret position
141       * @param visiblePosition the position most important to make visible
142       * @throws IllegalArgumentException if source is null, id is invalid, id is
143       *         CARET_POSITION_CHANGED and text is non-null, or if
144       *         committedCharacterCount is out of range
145       * @since 1.4
146       */
147      public InputMethodEvent(Component source, int id,
148                              AttributedCharacterIterator text,
149                              int committedCharacterCount, TextHitInfo caret,
150                              TextHitInfo visiblePosition)
151      {
152        this(source, id, EventQueue.getMostRecentEventTime(), text,
153             committedCharacterCount, caret, visiblePosition);
154      }
155    
156      /**
157       * Initializes a new instance of <code>InputMethodEvent</code> with the
158       * specified source, id, caret, and visible position, and with a null
159       * text and char count.
160       *
161       * @param source the source that generated the event
162       * @param id the event id
163       * @param caret the caret position
164       * @param visiblePosition the position most important to make visible
165       * @throws IllegalArgumentException if source is null or id is invalid
166       * @since 1.4
167       */
168      public InputMethodEvent(Component source, int id, TextHitInfo caret,
169                              TextHitInfo visiblePosition)
170      {
171        this(source, id, EventQueue.getMostRecentEventTime(), null, 0, caret,
172             visiblePosition);
173      }
174    
175      /**
176       * This method returns the input method text. This can be <code>null</code>,
177       * and will always be null for <code>CARET_POSITION_CHANGED</code> events.
178       * Characters from 0 to <code>getCommittedCharacterCount()-1</code> have
179       * been committed, the remaining characters are composed text.
180       *
181       * @return the input method text, or null
182       */
183      public AttributedCharacterIterator getText()
184      {
185        return text;
186      }
187    
188      /**
189       * Returns the number of committed characters in the input method text.
190       *
191       * @return the number of committed characters in the input method text
192       */
193      public int getCommittedCharacterCount()
194      {
195        return committedCharacterCount;
196      }
197    
198      /**
199       * Returns the caret position. The caret offset is relative to the composed
200       * text of the most recent <code>INPUT_METHOD_TEXT_CHANGED</code> event.
201       *
202       * @return the caret position, or null
203       */
204      public TextHitInfo getCaret()
205      {
206        return caret;
207      }
208    
209      /**
210       * Returns the position that is most important to be visible, or null if
211       * such a hint is not necessary. The caret offset is relative to the composed
212       * text of the most recent <code>INPUT_METHOD_TEXT_CHANGED</code> event.
213       *
214       * @return the position that is most important to be visible
215       */
216      public TextHitInfo getVisiblePosition()
217      {
218        return visiblePosition;
219      }
220    
221      /**
222       * This method consumes the event.  A consumed event is not processed
223       * in the default manner by the component that generated it.
224       */
225      public void consume()
226      {
227        consumed = true;
228      }
229    
230      /**
231       * This method tests whether or not this event has been consumed.
232       *
233       * @return true if the event has been consumed
234       */
235      public boolean isConsumed()
236      {
237        return consumed;
238      }
239    
240      /**
241       * Return the timestamp of this event.
242       *
243       * @return the timestamp
244       * @since 1.4
245       */
246      public long getWhen()
247      {
248        return when;
249      }
250    
251      /**
252       * This method returns a string identifying the event. This contains the
253       * event ID, the committed and composed characters separated by '+', the
254       * number of committed characters, the caret, and the visible position.
255       *
256       * @return a string identifying the event
257       */
258      public String paramString()
259      {
260        StringBuffer s
261          = new StringBuffer(80 + (text == null ? 0
262                                   : text.getEndIndex() - text.getBeginIndex()));
263        s.append(id == INPUT_METHOD_TEXT_CHANGED ? "INPUT_METHOD_TEXT_CHANGED, "
264                 : "CARET_POSITION_CHANGED, ");
265        if (text == null)
266          s.append("no text, 0 characters committed, caret: ");
267        else
268          {
269            s.append('"');
270            int i = text.getBeginIndex();
271            int j = committedCharacterCount;
272            while (--j >= 0)
273              s.append(text.setIndex(i++));
274            s.append("\" + \"");
275            j = text.getEndIndex() - i;
276            while (--j >= 0)
277              s.append(text.setIndex(i++));
278            s.append("\", ").append(committedCharacterCount)
279              .append(" characters committed, caret: ");          
280          }
281        s.append(caret == null ? (Object) "no caret" : caret).append(", ")
282          .append(visiblePosition == null ? (Object) "no visible position"
283                  : visiblePosition);
284        return s.toString();
285      }
286    
287      /**
288       * Reads in the object from a serial stream, updating when to
289       * {@link EventQueue#getMostRecentEventTime()} if necessary.
290       *
291       * @param s the stream to read from
292       * @throws IOException if deserialization fails
293       * @throws ClassNotFoundException if deserialization fails
294       * @serialData default, except for updating when
295       */
296      private void readObject(ObjectInputStream s)
297        throws IOException, ClassNotFoundException
298      {
299        s.defaultReadObject();
300        if (when == 0)
301          when = EventQueue.getMostRecentEventTime();
302      }
303    } // class InputMethodEvent