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:
027: package com.sun.midp.chameleon.input;
028:
029: import com.sun.midp.io.Util;
030: import com.sun.midp.log.LogChannels;
031: import com.sun.midp.log.Logging;
032:
033: /**
034: * Implements PTIterator using machine-dependent KNI interface.
035: *
036: */
037: public class PTIteratorImpl implements PTIterator {
038: /**
039: * Internal array size to hold KNI word completion
040: */
041: static final int MAX_STRING = 128;
042:
043: /**
044: * Internal array to hold KNI word completion
045: */
046: private byte[] entry;
047:
048: /**
049: * current handle
050: */
051: private int handle;
052:
053: /**
054: * create a new iterator
055: * @param dictionary dictionary id
056: */
057: public PTIteratorImpl(int dictionary) {
058: entry = new byte[MAX_STRING];
059: handle = ptNewIterator0(dictionary);
060: }
061:
062: /**
063: * create a new handle and clear completion state by
064: * calling ptNewIterator0()
065: */
066: public void reset() {
067: ptClear0(handle);
068: }
069:
070: /**
071: * check if current handle is valid
072: * @return true is valid, false otherwise
073: */
074: public boolean isValid() {
075: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
076: Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI,
077: "isValid = " + (handle != 0));
078: }
079: return handle > 0;
080: }
081:
082: /**
083: * Adds a key to current completion string
084: * @param keyCode char in the range '0'-'9', '#', or '*'
085: */
086: public void nextLevel(int keyCode) {
087: if (isValid()) {
088: ptAddKey0(handle, keyCode);
089: }
090: }
091:
092: /**
093: * Backspace on key in current completion string.
094: */
095: public void prevLevel() {
096: if (isValid()) {
097: ptDeleteKey0(handle);
098: }
099: }
100:
101: /**
102: * Returns true if the iteration has more elements. (In other words,
103: * returns <code>true</code> if <code>next</code> would return an
104: * element rather than throwing an exception.)
105: *
106: * @return true if the iterator has more elements.
107: */
108: public boolean hasNext() {
109: boolean ret = false;
110: if (isValid()) {
111: ret = ptHasCompletionOption0(handle);
112: }
113: return ret;
114: }
115:
116: /**
117: * Reverts to first possible completion.
118: * If next() has been called uptil hasNext() returns false, then after
119: * calling reviewCompletionOptions(), calling next() will return
120: * the 1st completion
121: */
122: public void resetNext() {
123: if (isValid()) {
124: ptRenewCompletionOptions0(handle);
125: }
126: }
127:
128: /**
129: * Returns the next element in the iteration.
130: *
131: * @return next element in the iteration.
132: *
133: * @exception NoSuchElementException iteration has no more elements.
134: */
135: public String next() {
136: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
137: Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI,
138: "[iter.nextCompletionOption] >>");
139: }
140: String ret = null;
141:
142: if (isValid()) {
143: ret = ptNextCompletionOption0(handle, entry.length);
144: }
145:
146: if (ret == null)
147: ret = "";
148:
149: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
150: Logging.report(Logging.INFORMATION, LogChannels.LC_HIGHUI,
151: "[iter.nextCompletionOption] : " + ret);
152: }
153:
154: return ret;
155: }
156:
157: /**
158: * NATIVE CODE
159: */
160:
161: /**
162: * Create a new iterator instance
163: *
164: * @param dictionary library handle
165: * @return handle of new iterator.
166: */
167: private static native int ptNewIterator0(int dictionary);
168:
169: /**
170: * Clear all text from the predictive text iterator
171: *
172: * @param handle the handle of the iterator
173: * @return true if iterator has been cleared successfully otherwise false.
174: */
175: private static native boolean ptClear0(int handle);
176:
177: /**
178: * Advances the predictive text iterator using the next key code
179: *
180: * @param handle the handle of the iterator
181: * @param keyCode the next key ('0'-'9')
182: *
183: * @return true if key code has been added successfully otherwise false
184: */
185: private static native boolean ptAddKey0(int handle, int keyCode);
186:
187: /**
188: * Backspace the iterator one key
189: *
190: * @param handle the handle of the iterator
191: * @return true if key has been deleted successfully otherwise false.
192: */
193: private static native boolean ptDeleteKey0(int handle);
194:
195: /**
196: * reset completion options for for the current predictive text entry
197: * After this call, ptNextCompletionOption() will return all
198: * completion options starting from 1st one.
199: *
200: * @param handle the handle of the iterator
201: * @return true if iterator has been reset successfully otherwise false.
202: */
203: private static native boolean ptRenewCompletionOptions0(int handle);
204:
205: /**
206: * return the current predictive text completion option
207: *
208: * @param handle the handle of the iterator
209: * @param outMaxSize max size of the outArray
210: *
211: * @return next element in the iteration
212: */
213: private static native String ptNextCompletionOption0(int handle,
214: int outMaxSize);
215:
216: /**
217: * see if exist further completion options for the current
218: * predictive text entry
219: *
220: * @param handle the handle of the iterator
221: *
222: * @return true if more completion options exist, false otherwise
223: */
224: private static native boolean ptHasCompletionOption0(int handle);
225:
226: }
|