001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program 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 version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.chameleon.input;
027:
028: import javax.microedition.lcdui.Canvas;
029: import javax.microedition.lcdui.Display;
030: import javax.microedition.lcdui.Displayable;
031: import javax.microedition.lcdui.TextField;
032: import com.sun.midp.i18n.Resource;
033: import com.sun.midp.i18n.ResourceConstants;
034:
035: /**
036: * An InputMode instance which processes the numeric 0-9 keys
037: * as their literal numeric values.
038: */
039: public class KeyboardInputMode implements InputMode {
040:
041: /** A holder for the keyCode which was last processed */
042: protected int lastKey = -1;
043:
044: /** The InputModeMediator for the current input session */
045: protected InputModeMediator mediator;
046:
047: /**
048: * This method is called to determine if this InputMode supports
049: * the given text input constraints. The semantics of the constraints
050: * value are defined in the javax.microedition.lcdui.TextField API.
051: * If this InputMode returns false, this InputMode must not be used
052: * to process key input for the selected text component.
053: *
054: * @param constraints text input constraints. The semantics of the
055: * constraints value are defined in the TextField API.
056: *
057: * @return true if this InputMode supports the given text component
058: * constraints, as defined in the MIDP TextField API
059: */
060: public boolean supportsConstraints(int constraints) {
061: return true;
062: }
063:
064: /**
065: * Returns the display name which will represent this InputMode to
066: * the user, such as in a selection list or the softbutton bar.
067: *
068: * @return the locale-appropriate name to represent this InputMode
069: * to the user
070: */
071: public String getName() {
072: return Resource.getString(ResourceConstants.LCDUI_TF_KEYBOARD);
073: }
074:
075: /**
076: * Returns the command name which will represent this InputMode in
077: * the input menu
078: *
079: * @return the locale-appropriate name to represent this InputMode
080: * to the user
081: */
082: public String getCommandName() {
083: return getName();
084: }
085:
086: /**
087: * This method will be called before any input keys are passed
088: * to this InputMode to allow the InputMode to perform any needed
089: * initialization. A reference to the InputModeMediator which is
090: * currently managing the relationship between this InputMode and
091: * the input session is passed in. This reference can be used
092: * by this InputMode to commit text input as well as end the input
093: * session with this InputMode. The reference is only valid until
094: * this InputMode's endInput() method is called.
095: *
096: * @param constraints text input constraints. The semantics of the
097: * constraints value are defined in the TextField API.
098: *
099: * @param mediator the InputModeMediator which is negotiating the
100: * relationship between this InputMode and the input session
101: *
102: * @param inputSubset current input subset
103: */
104: public void beginInput(InputModeMediator mediator,
105: String inputSubset, int constraints) {
106: validateState(false);
107: this .mediator = mediator;
108: }
109:
110: /**
111: * Returns true if input mode is using its own displayable, false ifinput
112: * mode does not require the speial displayable for its representation.
113: * By default - false
114: * @return true if input mode is using its own displayable, otherwise false
115: */
116: public boolean hasDisplayable() {
117: return false;
118: }
119:
120: /**
121: * Process the given key code as input.
122: *
123: * This method will return true if the key was processed successfully,
124: * false otherwise.
125: *
126: * @param keyCode the keycode of the key which was input
127: * @param longPress return true if it's long key press otherwise false
128: * @return true if the key was processed by this InputMode, false
129: * otherwise.
130: */
131: public int processKey(int keyCode, boolean longPress) {
132: int ret = KEYCODE_NONE;
133: validateState(true);
134: // if the key is printable one
135: if (mediator != null && !longPress && keyCode >= ' '
136: && keyCode < 127) {
137: mediator.commit("" + (char) keyCode);
138: }
139: return ret;
140: }
141:
142: /**
143: * return the pending char
144: * used to bypass the asynchronous commit mechanism
145: * e.g. to immediately commit a char before moving the cursor
146: * @return return the pending char
147: */
148: public char getPendingChar() {
149: return 0;
150: }
151:
152: /**
153: * Return the next possible match for the key input processed thus
154: * far by this InputMode. A call to this method should be preceeded
155: * by a check of hasMoreMatches(). If the InputMode has more available
156: * matches for the given input, this method will return them one by one.
157: *
158: * @return a String representing the next available match to the key
159: * input thus far
160: */
161: public String getNextMatch() {
162: return null;
163: }
164:
165: /**
166: * True, if after processing a key, there is more than one possible
167: * match to the input. If this method returns true, the getNextMatch()
168: * method can be called to return the value.
169: *
170: * @return true if after processing a key, there is more than the one
171: * possible match to the given input
172: */
173: public boolean hasMoreMatches() {
174: return false;
175: }
176:
177: /**
178: * Gets the possible string matches
179: *
180: * @return returns the set of options.
181: */
182: public String[] getMatchList() {
183: return new String[0];
184: }
185:
186: /**
187: * Mark the end of this InputMode's processing. The only possible call
188: * to this InputMode after a call to endInput() is a call to beginInput()
189: * to begin a new input session.
190: */
191: public void endInput() {
192: validateState(true);
193: this .mediator = null;
194: }
195:
196: /**
197: * By default the regular input method has no specific displayable
198: * representation so it returns null.
199: * @return null by default
200: */
201: public Displayable getDisplayable() {
202: return null;
203: }
204:
205: /**
206: * This method will validate the state of this InputMode. If this
207: * is a check for an "active" operation, the TextInputMediator must
208: * be non-null or else this method will throw an IllegalStateException.
209: * If this is a check for an "inactive" operation, then the
210: * TextInputMediator should be null.
211: *
212: * @param activeOperation true if any operation is active otherwise false.
213: */
214: protected void validateState(boolean activeOperation) {
215: if (activeOperation && this .mediator == null) {
216: throw new IllegalStateException(
217: "Illegal operation on an input session already in progress");
218: } else if (!activeOperation && this .mediator != null) {
219: throw new IllegalStateException(
220: "Illegal operation on an input session which is not in progress");
221: }
222: }
223:
224: /** input subset x constraint map */
225: private static final boolean[][] isMap = {
226: // |ANY|EMAILADDR|NUMERIC|PHONENUMBER|URL|DECIMAL
227: { false, false, false, false, false, false }, // IS_FULLWIDTH_DIGITS
228: { false, false, false, false, false, false }, // IS_FULLWIDTH_LATIN
229: { true, true, false, false, true, false }, // IS_HALFWIDTH_KATAKANA
230: { true, true, false, false, true, false }, // IS_HANJA
231: { true, true, false, false, true, false }, // IS_KANJI
232: { false, false, false, false, false, false }, // IS_LATIN
233: { false, false, false, false, false, false }, // IS_LATIN_DIGITS
234: { true, true, false, false, true, false }, // IS_SIMPLIFIED_HANZI
235: { true, true, false, false, true, false }, // IS_TRADITIONAL_HANZI
236: { false, false, false, false, false, false }, // MIDP_UPPERCASE_LATIN
237: { false, false, false, false, false, false }, // MIDP_LOWERCASE_LATIN
238: { true, true, false, false, true, false } // NULL
239: };
240:
241: /**
242: * Returns the map specifying this input mode is proper one for the
243: * particular pair of input subset and constraint. The form of the map is
244: *
245: * |ANY|EMAILADDR|NUMERIC|PHONENUMBER|URL|DECIMAL|
246: * ---------------------------------------------------------------------
247: * IS_FULLWIDTH_DIGITS |t|f| t|f | t|f | t|f |t|f| t|f |
248: * IS_FULLWIDTH_LATIN |t|f| t|f | t|f | t|f |t|f| t|f |
249: * IS_HALFWIDTH_KATAKANA |t|f| t|f | t|f | t|f |t|f| t|f |
250: * IS_HANJA |t|f| t|f | t|f | t|f |t|f| t|f |
251: * IS_KANJI |t|f| t|f | t|f | t|f |t|f| t|f |
252: * IS_LATIN |t|f| t|f | t|f | t|f |t|f| t|f |
253: * IS_LATIN_DIGITS |t|f| t|f | t|f | t|f |t|f| t|f |
254: * IS_SIMPLIFIED_HANZI |t|f| t|f | t|f | t|f |t|f| t|f |
255: * IS_TRADITIONAL_HANZI |t|f| t|f | t|f | t|f |t|f| t|f |
256: * MIDP_UPPERCASE_LATIN |t|f| t|f | t|f | t|f |t|f| t|f |
257: * MIDP_LOWERCASE_LATIN |t|f| t|f | t|f | t|f |t|f| t|f |
258: * NULL |t|f| t|f | t|f | t|f |t|f| t|f |
259: *
260: * @return input subset x constraint map
261: */
262: public boolean[][] getIsConstraintsMap() {
263: return isMap;
264: }
265: }
|