textfieldlfimpl.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 2,002 行 · 第 1/5 页
JAVA
2,002 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program 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. * * This program 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 at /legal/license.txt). * * 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 or visit www.sun.com if you need additional * information or have any questions. */package javax.microedition.lcdui;import com.sun.midp.lcdui.*;import com.sun.midp.log.Logging;import com.sun.midp.log.LogChannels;import com.sun.midp.chameleon.*;import com.sun.midp.chameleon.input.*;import com.sun.midp.chameleon.layers.InputModeLayer;import com.sun.midp.chameleon.layers.PTILayer;import com.sun.midp.chameleon.layers.VirtualKeyboardLayer;import com.sun.midp.chameleon.layers.VirtualKeyListener;import com.sun.midp.chameleon.skins.ScreenSkin;import com.sun.midp.chameleon.skins.TextFieldSkin;import com.sun.midp.chameleon.skins.resources.TextFieldResources;import com.sun.midp.chameleon.skins.resources.PTIResources;import com.sun.midp.chameleon.skins.resources.InputModeResources;import com.sun.midp.configurator.Constants;import java.util.*;/** * This is the look &s; feel implementation for TextField. */class TextFieldLFImpl extends ItemLFImpl implements TextFieldLF, TextInputComponent, CommandListener, VirtualKeyListener{ /** TextField instance associated with this view */ protected TextField tf; /** cursor to keep track of where to draw the cursor */ protected TextCursor cursor; /** editable state of the text field */ protected boolean editable; /** flag indicating the first time traverse is executed */ protected boolean firstTimeInTraverse = true; /** The character set to use as the initial input mode, may be null */ protected String initialInputMode; /** The input mode cause the traverse out for the text component */ protected InputMode interruptedIM; /** * Cached input session instance requested from associated Display */ protected TextInputSession cachedInputSession; /** * This SubMenuCommand holds the set of InputModes available on the * InputMode pull-out menu */ protected SubMenuCommand inputMenu; /** * The set of InputModes available to process this text component */ protected InputMode[] inputModes; /** * A special "popup" layer that shows the user an indicator of * what the currently selected input mode is */ protected InputModeLayer inputModeIndicator; /** The state of the popup ChoiceGroup (false by default) */ private boolean pt_popupOpen; /** The state of the virtual keyboard popup (false by default) */ private boolean vkb_popupOpen; /** predictive text options */ String[] pt_matches; /** * A four dimensional array holding the anchor point, item * height, and space below the item which corresponds to the * parameters to the InputModeLayer's setAnchor() method */ protected int[] inputModeAnchor; /** * This is a flag to turn on the input mode indicator popup */ protected boolean showIMPopup; /** * true if the preferredX field in TextCursor should be updated with * the latest TextCursor.x coordinate */ protected boolean usePreferredX = true; /** * pixel offset to the start of the text field (for example, if * xScrollOffset is -60 it means means that the text in this * text field is scrolled 60 pixels left of the left edge of the * text field) */ protected int xScrollOffset; /** * Total width of the text contained in this TextField */ protected int textWidth; /** * Width of the scroll area for text */ protected int scrollWidth; /** A Timer which will handle firing repaints of the ScrollPainter */ protected static Timer textScrollTimer; /** A TimerTask which will repaint scrolling text on a repeated basis */ protected TextScrollPainter textScrollPainter; /** flag indicating the pointer press event is happened but release is still not handled */ private boolean pressedIn = false; /* * Cached display instance */ private Display oldDisplay = null; /** * Creates TextFieldLF for the passed in TextField. * @param tf The TextField associated with this TextFieldLF */ TextFieldLFImpl(TextField tf) { super(tf); TextFieldResources.load(); PTIResources.load(); InputModeResources.load(); this.tf = tf; // IMPL_NOTE: Input text session and popup layer for predictive // text input can't be initialized here since they belong to // Display instance that can be unavailable until Displayable // is set as the current one. cursor = new TextCursor(tf.buffer.length()); cursor.visible = false; xScrollOffset = 0; lSetConstraints(); if (textScrollTimer == null) { textScrollTimer = new Timer(); } inputModeIndicator = new InputModeLayer(); inputModeAnchor = new int[4]; } // ***************************************************** // Public methods defined in interfaces // ***************************************************** /** * Update the character buffer in TextField with pending user input. * Since Java TextField always keeps TextField.buffer up-to-date, * there is no pending user input. Do nothing but return false here. * @return true if there is new user input updated in the buffer. */ public boolean lUpdateContents() { return false; // nothing pending } /** * Notifies L&F of a content change in the corresponding TextField. */ public void lSetChars() { cursor.index = tf.buffer.length(); // cursor at the end cursor.option = Text.PAINT_USE_CURSOR_INDEX; if (!editable) { resetUneditable(); } lRequestPaint(); } /** * Notifies L&s;F of a character insertion in the corresponding * TextField. * @param data the source of the character data * @param offset the beginning of the region of characters copied * @param length the number of characters copied * @param position the position at which insertion occurred */ public void lInsert(char data[], int offset, int length, int position) { if (data == null) { return; // -au } if (position <= cursor.index) { cursor.index += length; cursor.option = Text.PAINT_USE_CURSOR_INDEX; } if (!editable) { resetUneditable(); } if (item.owner == null) { return; // because owner is null, we just return. } lRequestPaint(); } /** * Notifies L&amsp;F of character deletion in the corresponding * TextField. * @param offset the beginning of the deleted region * @param length the number of characters deleted */ public void lDelete(int offset, int length) { if (cursor.index >= offset) { int diff = cursor.index - offset; cursor.index -= (diff < length) ? diff : length; cursor.option = Text.PAINT_USE_CURSOR_INDEX; } if (!editable) { resetUneditable(); } if (item.owner == null) { return; // because owner is null, we just return. } lRequestPaint(); } /** * Notifies L&s;F of a maximum size change in the corresponding * TextField. * @param maxSize - the new maximum size */ public void lSetMaxSize(int maxSize) { int max = tf.getMaxSize(); if (cursor.index > max) { cursor.index = max; } lRequestInvalidate(true, true); } /** * Returns the available size (number of characters) that can be * stored in this <code>TextInputComponent</code>. * @return available size in characters */ public int getAvailableSize() { return tf.getMaxSize() - tf.buffer.length(); } /** * Gets the current input position. * @return the current caret position, <code>0</code> if at the beginning */ public int lGetCaretPosition() { return cursor.index; } /** * Update states associated with input constraints. * * @param autoScrolling true if auto scrolling is allowed * to show large uneditable contents */ void setConstraintsCommon(boolean autoScrolling) { // Cleanup old states that are constraints sensitive if (hasFocus && visible) { if (editable) { disableInput(); // IMPL_NOTE: problem with synchronization on layers and LCDUILock disableLayers(); } else if (autoScrolling) { stopScroll(); } } // Change editability editable = (tf.constraints & TextField.UNEDITABLE) != TextField.UNEDITABLE; // Setup new states that are constraints sensitive if (hasFocus && visible) { if (editable) { enableInput(); // IMPL_NOTE: problem with synchronization on layers and LCDUILock showIMPopup = true; enableLayers(); } else if (autoScrolling) { startScroll(); } } } /** * Notifies L&s;F that constraints have to be changed. */ public void lSetConstraints() { setConstraintsCommon(true); // The layout might change if the constraints does not match // the current text, causing the text to be set empty, // or changed to "password", causing it to change width // Request relayout to based on updated contentSize if (item.owner == null) { return; // because owner is null, we just return. } lRequestInvalidate(true, true); } /** * Validate a given character array against a constraints. * * @param buffer a character array * @param constraints text input constraints * @return true if constraints is met by the character array */ public boolean lValidate(DynamicCharacterArray buffer, int constraints) { return TextPolicy.isValidString(buffer, constraints); } /** * Notifies L&s;F that preferred initial input mode was changed. * @param characterSubset a string naming a Unicode character subset, * or <code>null</code> */ public void lSetInitialInputMode(String characterSubset) { this.initialInputMode = characterSubset; } /** * Notifies item that it has been recently deleted * Traverse out the textFieldLF. This implicitly remove the InputMode * indicator and possibly the Predictive Text Input indicator from the * screen. */ public void itemDeleted() { uCallTraverseOut(); } /** * Get input session instance from the associated display * @return TextInputSession instance common for * all clients of the associated Display * * IMPL_NOTE: Text field is supposed to be associated with only * one Display, that's why cached input session can be used. */ TextInputSession getInputSession() { if (cachedInputSession == null) { Display d = getCurrentDisplay(); if (d != null) { cachedInputSession = d.getInputSession(); } } return cachedInputSession; } // CommandListener interface /** * This CommandListener is only for the selection of Commands on * the Input sub-menu, which lists each of the available InputModes * for this text component. * @param c command * @param d displayable */ public void commandAction(Command c, Displayable d) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?