Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.input;
027
028import java.io.IOException;
029import java.io.ObjectInputStream;
030import java.io.ObjectOutputStream;
031import java.util.ArrayList;
032import java.util.List;
033import javafx.collections.FXCollections;
034import javafx.collections.ObservableList;
035import javafx.event.EventTarget;
036import javafx.event.EventType;
037import javafx.scene.Node;
038
039/**
040 * An event which indicates that the underlying input method notifies its
041 * text change in a {@link Node}.
042 * <p>
043 * This event is delivered to the {@link Node} object that extends 
044 * {@link javafx.scene.control.TextInputControl}, when the text under composition 
045 * (composed text) is generated/changed/removed, the input method commits
046 * the result text, or the input method caret position changes.
047 * <p>
048 * On receiving this event, the application is supposed to display
049 * the composed text with any visual feedback attributes to the user.
050 * <p>
051 * Note: this is a conditional feature. See
052 * {@link javafx.application.ConditionalFeature#INPUT_METHOD ConditionalFeature.INPUT_METHOD}
053 * for more information.
054 */
055public final class InputMethodEvent extends InputEvent{
056
057    private static final long serialVersionUID = 20121107L;
058    
059    /**
060     * The only valid EventType for the InputMethodEvent.
061     */
062    public static final EventType<InputMethodEvent> INPUT_METHOD_TEXT_CHANGED =
063            new EventType<InputMethodEvent>(InputEvent.ANY, "INPUT_METHOD_TEXT_CHANGED");
064
065    /**
066     * Common supertype for all input method event types.
067     */
068    public static final EventType<InputMethodEvent> ANY = INPUT_METHOD_TEXT_CHANGED;
069
070    /**
071     * Constructs new InputMethodEvent event.
072     * @param source the source of the event. Can be null.
073     * @param target the target of the event. Can be null.
074     * @param eventType The type of the event.
075     * @param composed the text under composition
076     * @param committed the text that is committed as a result of composition
077     * @param caretPosition the current position of the caret.
078     */
079    public InputMethodEvent(Object source, EventTarget target, EventType<InputMethodEvent> eventType,
080            List<InputMethodTextRun> composed, String committed,
081            int caretPosition) {
082        super(source, target, eventType);
083        this.composed = FXCollections.unmodifiableObservableList(FXCollections.observableArrayList(composed));
084        this.committed = committed;
085        this.caretPosition = caretPosition;
086    }
087
088    /**
089     * Constructs new InputMethodEvent event with empty source and target.
090     * @param eventType The type of the event.
091     * @param composed the text under composition
092     * @param committed the text that is committed as a result of composition
093     * @param caretPosition the current position of the caret.
094     */
095    public InputMethodEvent(EventType<InputMethodEvent> eventType,
096            List<InputMethodTextRun> composed, String committed,
097            int caretPosition) {
098        this(null, null, eventType, composed, committed, caretPosition);
099    }
100    
101
102    /**
103     * The text under composition.  This text should be displayed with the
104     * appropriate visual feedback that represents the {@link InputMethodHighlight}s
105     * attached to each run.
106     *
107     * @defaultValue null
108     */
109    private transient ObservableList<InputMethodTextRun> composed;
110
111    /**
112     * Gets the text under composition.  This text should be displayed with the
113     * appropriate visual feedback that represents the {@link InputMethodHighlight}s
114     * attached to each run.
115     *
116     * @return The text under composition
117     */
118    public final ObservableList<InputMethodTextRun> getComposed() {
119        return composed;
120    }
121
122    /**
123     * The text that is committed by the input method as the result of the
124     * composition.
125     *
126     * @defaultValue empty string
127     */
128    private final String committed;
129
130    /**
131     * Gets the text that is committed by the input method as the result of the
132     * composition.
133     *
134     * @return The committed text
135     */
136    public final String getCommitted() {
137        return committed;
138    }
139
140    /**
141     * The input method caret position within the composed text.
142     * If the position is -1, the caret should be invisible.
143     *
144     * @defaultValue 0
145     */
146    private final int caretPosition;
147
148    /**
149     * The input method caret position within the composed text.
150     * If the position is -1, the caret should be invisible.
151     *
152     * @return The input method caret position within the composed text
153     */
154    public final int getCaretPosition() {
155        return caretPosition;
156    }
157
158    /**
159     * Returns a string representation of this {@code InputMethodEvent} object.
160     * @return a string representation of this {@code InputMethodEvent} object.
161     */ 
162    @Override public String toString() {
163        final StringBuilder sb = new StringBuilder("InputMethodEvent [");
164
165        sb.append("source = ").append(getSource());
166        sb.append(", target = ").append(getTarget());
167        sb.append(", eventType = ").append(getEventType());
168        sb.append(", consumed = ").append(isConsumed());
169
170        sb.append(", composed = ").append(getComposed());
171        sb.append(", committed = ").append(getCommitted());
172        sb.append(", caretPosition = ").append(getCaretPosition());
173
174        return sb.append("]").toString();
175    }
176
177    @Override
178    public InputMethodEvent copyFor(Object newSource, EventTarget newTarget) {
179        return (InputMethodEvent) super.copyFor(newSource, newTarget);
180    }
181
182    @Override
183    public EventType<InputMethodEvent> getEventType() {
184        return (EventType<InputMethodEvent>) super.getEventType();
185    }
186    
187    private void writeObject(ObjectOutputStream oos) throws IOException {
188        oos.defaultWriteObject();
189        oos.writeObject(new ArrayList(composed));
190    }
191
192    private void readObject(ObjectInputStream ois) throws IOException,
193            ClassNotFoundException {
194        ois.defaultReadObject();
195        ArrayList<InputMethodTextRun> o = (ArrayList)ois.readObject();
196        composed = FXCollections.unmodifiableObservableList(FXCollections.observableArrayList(o));
197    }
198
199}