1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Michael Danilov
19 * @version $Revision$
20 */
21package java.awt.event;
22
23import java.awt.AWTEvent;
24import java.awt.Component;
25import java.awt.font.TextHitInfo;
26import java.text.AttributedCharacterIterator;
27
28import org.apache.harmony.awt.internal.nls.Messages;
29
30/**
31 * This class is not supported in Android 1.0. It is merely provided to maintain
32 * interface compatibility with desktop Java implementations.
33 *
34 * @since Android 1.0
35 */
36public class InputMethodEvent extends AWTEvent {
37
38    private static final long serialVersionUID = 4727190874778922661L;
39
40    public static final int INPUT_METHOD_FIRST = 1100;
41
42    public static final int INPUT_METHOD_TEXT_CHANGED = 1100;
43
44    public static final int CARET_POSITION_CHANGED = 1101;
45
46    public static final int INPUT_METHOD_LAST = 1101;
47
48    private AttributedCharacterIterator text;
49    private TextHitInfo visiblePosition;
50    private TextHitInfo caret;
51    private int committedCharacterCount;
52    private long when;
53
54    public InputMethodEvent(Component src, int id,
55                            TextHitInfo caret,
56                            TextHitInfo visiblePos) {
57        this(src, id, null, 0, caret, visiblePos);
58    }
59
60    public InputMethodEvent(Component src, int id,
61                            AttributedCharacterIterator text,
62                            int commitedCharCount,
63                            TextHitInfo caret,
64                            TextHitInfo visiblePos) {
65        this(src, id, 0l, text, commitedCharCount, caret, visiblePos);
66    }
67
68    public InputMethodEvent(Component src, int id, long when,
69                            AttributedCharacterIterator text,
70                            int committedCharacterCount,
71                            TextHitInfo caret,
72                            TextHitInfo visiblePos) {
73        super(src, id);
74
75        if ((id < INPUT_METHOD_FIRST) || (id > INPUT_METHOD_LAST)) {
76            // awt.18E=Wrong event id
77            throw new IllegalArgumentException(Messages.getString("awt.18E")); //$NON-NLS-1$
78        }
79        if ((id == CARET_POSITION_CHANGED) && (text != null)) {
80            // awt.18F=Text must be null for CARET_POSITION_CHANGED
81            throw new IllegalArgumentException(Messages.getString("awt.18F")); //$NON-NLS-1$
82        }
83        if ((text != null) &&
84                ((committedCharacterCount < 0) ||
85                 (committedCharacterCount >
86                        (text.getEndIndex() - text.getBeginIndex())))) {
87            // awt.190=Wrong committedCharacterCount
88            throw new IllegalArgumentException(Messages.getString("awt.190")); //$NON-NLS-1$
89        }
90
91        this.when = when;
92        this.text = text;
93        this.caret = caret;
94        this.visiblePosition = visiblePos;
95        this.committedCharacterCount = committedCharacterCount;
96    }
97
98    public TextHitInfo getCaret() {
99        return caret;
100    }
101
102    public int getCommittedCharacterCount() {
103        return committedCharacterCount;
104    }
105
106    public AttributedCharacterIterator getText() {
107        return text;
108    }
109
110    public TextHitInfo getVisiblePosition() {
111        return visiblePosition;
112    }
113
114    public long getWhen() {
115        return when;
116    }
117
118    @Override
119    public void consume() {
120        super.consume();
121    }
122
123    @Override
124    public boolean isConsumed() {
125        return super.isConsumed();
126    }
127
128    @Override
129    public String paramString() {
130        /* The format is based on 1.5 release behavior
131         * which can be revealed by the following code:
132         *
133         * InputMethodEvent e = new InputMethodEvent(new Component(){},
134         *          InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
135         *          TextHitInfo.leading(1), TextHitInfo.trailing(2));
136         * System.out.println(e);
137         */
138        String typeString = null;
139
140        switch (id) {
141        case INPUT_METHOD_TEXT_CHANGED:
142            typeString = "INPUT_METHOD_TEXT_CHANGED"; //$NON-NLS-1$
143            break;
144        case CARET_POSITION_CHANGED:
145            typeString = "CARET_POSITION_CHANGED"; //$NON-NLS-1$
146            break;
147        default:
148            typeString = "unknown type"; //$NON-NLS-1$
149        }
150
151        return typeString + ",text=" + text +  //$NON-NLS-1$
152                ",commitedCharCount=" + committedCharacterCount + //$NON-NLS-1$
153                ",caret=" + caret + ",visiblePosition=" + visiblePosition; //$NON-NLS-1$ //$NON-NLS-2$
154    }
155
156}
157