📄 textfield.java
字号:
/* * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.lwuit;import com.sun.lwuit.events.ActionEvent;import com.sun.lwuit.events.ActionListener;import com.sun.lwuit.events.DataChangedListener;import com.sun.lwuit.geom.Dimension;import com.sun.lwuit.layouts.GridLayout;import com.sun.lwuit.plaf.UIManager;import java.util.Hashtable;import java.util.Vector;/** * Allows in place editing using a lightweight API without necessarily moving to * the external native text box. The main drawback in this approach is that editing * can't support features such as T9 and might not have the same keymapping or * behavior of the native text input. * <p>Notice that due to limitations of text area and text field input modes in * text area aren't properly supported since they won't work properly across devices. * To limit input modes please use the setInputModeOrder method. All constants * declated in TextArea are ignored with the exception of PASSWORD. * * @author Shai Almog */public class TextField extends TextArea { private static boolean replaceMenuDefault = true; private long cursorBlinkTime = System.currentTimeMillis(); private boolean drawCursor = true; private int cursorCharPosition = -1; private boolean pressedAndNotReleased; private long pressTime; private boolean useSoftkeys = true; private long releaseTime; private String previousText; private int commitTimeout = 1000; private boolean pendingCommit; private int pressCount = 0; private int lastKeyCode; private int pressedKeyCode; private static String clearText = "Clear"; private static String t9Text = "T9"; private boolean longClick; private Command originalClearCommand; private static Hashtable inputModes; private String inputMode = "Abc"; private static String[] defaultInputModeOrder = {"Abc", "ABC", "abc", "123"}; private String[] inputModeOrder = defaultInputModeOrder; private static Vector firstUppercaseInputMode = new Vector(); private Vector listeners = null; private int blinkOnTime = 800; private int blinkOffTime = 200; private boolean qwerty; private boolean replaceMenu = replaceMenuDefault; private Command[] originalCommands; /** * Set the text that should appear on the clear softkey */ public static void setClearText(String text) { clearText = text; } /** * Set the text that should appear on the T9 softkey */ public static void setT9Text(String text) { t9Text = text; } private Command DELETE_COMMAND = new Command(clearText) { public void actionPerformed(ActionEvent ev) { deleteChar(); } }; private Command T9_COMMAND = new Command(t9Text) { public void actionPerformed(ActionEvent ev) { editString(); } }; private static final char[] DEFAULT_SYMBOL_TABLE = new char[] { '.', ',', '?', '!', '$', '@', '\'', '-', '_', ')', '(', ':', ';', '&', '/', '~', '\\', '%', '*', '#', '+', '>', '=', '<', '"' }; private static char[] symbolTable = DEFAULT_SYMBOL_TABLE; private static final String[] DEFAULT_KEY_CODES = { // 0 " 0", // 1 ".,?!'\"1-()@/:_", // 2 "ABC2", // 3 "DEF3", // 4 "GHI4", // 5 "JKL5", // 6 "MNO6", // 7 "PQRS7", // 8 "TUV8", // 9 "WXYZ9", }; /** * Default constructor */ public TextField() { super(1, 20); } /** * Construct a text field with space reserved for columns */ public TextField(int columns) { super(1, columns); } /** * Construct text field */ public TextField(String text) { super(text, 1, 20); } /** * Performs a backspace operation */ protected void deleteChar() { String text = getText(); if(text.length() > 0) { if(cursorCharPosition > 0) { cursorCharPosition--; } text = text.substring(0, cursorCharPosition) + text.substring(cursorCharPosition + 1, text.length()); super.setText(text); fireDataChanged(DataChangedListener.REMOVED, cursorCharPosition); } } /** * Construct text field */ public TextField(String text, int columns) { super(text, 1, columns); } /** * @inheritDoc */ protected String getUIID() { return "TextField"; } /** * Commit the changes made to the text field as a complete edit operation. This * is used in a numeric keypad to allow the user to repeatedly press a number * to change values. */ protected void commitChange() { pendingCommit = false; previousText = null; pressCount = 0; } /** * Returns true if the text field is waiting for a commit on editing */ public boolean isPendingCommit() { return pendingCommit; } /** * The amount of time in milliseconds it will take for a change to get commited into * the field. * * @param commitTimeout indicates the amount of time that should elapse for a commit * to automatically occur */ public void setCommitTimeout(int commitTimeout) { this.commitTimeout = commitTimeout; } /** * The amount of time in milliseconds it will take for a change to get commited into * the field. */ public int getCommitTimeout() { return commitTimeout; } /** * Sets the current selected input mode matching one of the existing input * modes * * @param inputMode the display name of the input mode by default the following modes * are supported: Abc, ABC, abc, 123 */ public void setInputMode(String inputMode) { this.inputMode = inputMode; repaint(); } /** * Returns the currently selected input mode * * @return the display name of the input mode by default the following modes * are supported: Abc, ABC, abc, 123 */ public String getInputMode() { return inputMode; } /** * Indicates whether the key changes the current input mode * * @return true for the hash (#) key code */ protected boolean isChangeInputMode(int keyCode) { return keyCode == '#'; } private static void initInputModes() { if(inputModes == null) { firstUppercaseInputMode.addElement("Abc"); inputModes = new Hashtable(); Hashtable upcase = new Hashtable(); for(int iter = 0 ; iter < DEFAULT_KEY_CODES.length ; iter++) { upcase.put(new Integer('0' + iter), DEFAULT_KEY_CODES[iter]); } inputModes.put("ABC", upcase); Hashtable lowcase = new Hashtable(); for(int iter = 0 ; iter < DEFAULT_KEY_CODES.length ; iter++) { lowcase.put(new Integer('0' + iter), DEFAULT_KEY_CODES[iter].toLowerCase()); } inputModes.put("abc", lowcase); Hashtable numbers = new Hashtable(); for(int iter = 0 ; iter < 10 ; iter++) { numbers.put(new Integer('0' + iter), "" + iter); } inputModes.put("123", numbers); } } /** * Adds a new inputmode hashtable with the given name and set of values * * @param name a unique display name for the input mode e.g. ABC, 123 etc... * @param values The key for the hashtable is an Integer keyCode and the value * is a String containing the characters to toggle between for the given keycode * @param firstUpcase indicates if this input mode in an input mode used for the special * case where the first letter is an upper case letter */ public static void addInputMode(String name, Hashtable values, boolean firstUpcase) { initInputModes(); inputModes.put(name, values); if(firstUpcase) { firstUppercaseInputMode.addElement(name); } } /** * Returns the order in which input modes are toggled */ public String[] getInputModeOrder() { return inputModeOrder; } /** * Sets the order in which input modes are toggled and allows disabling/hiding * an input mode * * @param order the order for the input modes in this field */ public void setInputModeOrder(String[] order) { inputModeOrder = order; inputMode = order[0]; } /** * Returns the order in which input modes are toggled by default */ public static String[] getDefaultInputModeOrder() { return defaultInputModeOrder; } /** * Sets the order in which input modes are toggled by default and allows * disabling/hiding an input mode * * @param order the order for the input modes in all future created fields */ public static void setDefaultInputModeOrder(String[] order) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -