📄 textbasic.java
字号:
/* * @(#)TextBasic.java 1.37 01/09/17 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package javax.microedition.lcdui;import com.sun.midp.lcdui.InputModeHelper;import com.sun.midp.lcdui.InputModeClient;/** * The basic support for a text component. This class is used * by TextField, TextBox, and TextEditor */class TextBasic { /** Buffer holding the data */ char buffer[]; /** The number of characters in the buffer */ int numChars; // = 0; /** The current position in the buffer */ int curPos; // = 0; /** The constraints applied to this text object */ int constraints; // = TextField.ANY; /** The policy applied to this text object */ Policy policy; /** The default mode of this text object */ String defaultMode; /** The allowed modes of this text object */ String allowedMode[]; /** * Construct a new TextBasic * * @param constraints The constraints to apply to this text object * @param maxSize The maximum size of this text object * @param text The initial text to set in this text object */ TextBasic(int constraints, int maxSize, String text) { if (text == null) { buffer = new char[0]; } else { buffer = text.toCharArray(); numChars = curPos = text.length(); } // getPolicy throws an IllegalArgumentException // if the constraints parameter is invalid policy = Policy.getPolicy(constraints); this.constraints = constraints; if (!policy.validateContent(buffer, 0, numChars)) { numChars = curPos = 0; throw new IllegalArgumentException(); } if (numChars > maxSize) { throw new IllegalArgumentException(); } setMaxSize(maxSize); } // util methods /** * Set the constraints of this text object * * @param constraints The new constraints * @return int The delta height caused by the constraint change */ int setConstraints(int constraints) { if (constraints == this.constraints) { return 0; } this.constraints = constraints; // policy is never null, as its set initially in the constructor int w = policy.width; int h = policy.height; // getPolicy throws an IllegalArgumentException // if the constraints parameter is invalid policy = Policy.getPolicy(constraints); if (!policy.validateContent(buffer, 0, numChars)) { numChars = curPos = 0; } // If the old policy hadn't been layed out yet, simply return if (w == -1) { return 0; } // return the delta height between the old and new policies return h - policy.setWidth(w, buffer, numChars, curPos); } /** * Set the maximum size of this text object (in chars) * * @param maxSize The maximum number of characters this text object * can hold * @return int The delta height change caused by this size change */ int setMaxSize(int maxSize) { if (maxSize <= 0) { throw new IllegalArgumentException(); } maxSize = policy.constrainedSize(maxSize, buffer); if (maxSize != buffer.length) { char[] newbuf = new char[maxSize]; if (maxSize < numChars) { numChars = maxSize; if (curPos > numChars) { curPos = numChars; } } System.arraycopy(buffer, 0, newbuf, 0, numChars); buffer = newbuf; } return policy.contentChanged(buffer, numChars, 0, curPos); } /** * Insert a sub string into a given position * * @param src The String to insert * @param position The index to insert the string * @return int The delta height change caused by this insertion */ int insert(String src, int position) { if (position < 0) { position = 0; } else if (position > numChars) { position = numChars; } // If src == null, // NullPointerException will be thrown in src.length() by VM int length = src.length(); if (numChars + length > buffer.length) { throw new IllegalArgumentException(); } System.arraycopy(buffer, position, buffer, position + length, numChars - position); src.getChars(0, length, buffer, position); numChars += length; if (!policy.validateContent(buffer, position, length)) { numChars -= length; System.arraycopy(buffer, position + length, buffer, position, numChars - position); throw new IllegalArgumentException(); } if (position <= curPos) { curPos += length; } return policy.contentChanged(buffer, numChars, position, curPos); } /** * Insert characters into this text object * * @param data The array of characters to insert * @param offset The start point of the array to insert * @param length The number of characters to insert out of the 'data' * array * @param position The position in this text object to insert the * new characters * @return int The delta height change caused by this insertion */ int insert(char[] data, int offset, int length, int position) { if (position < 0) { position = 0; } else if (position > numChars) { position = numChars; } if (numChars + length > buffer.length) { throw new IllegalArgumentException(); } // If data == null, // NullPointerException will be thrown in data.length by VM if ((offset < 0) || (length < 0) || (offset + length > data.length)) { throw new ArrayIndexOutOfBoundsException(); } System.arraycopy(buffer, position, buffer, position + length, numChars - position); System.arraycopy(data, offset, buffer, position, length); numChars += length; if (!policy.validateContent(buffer, position, length)) { numChars -= length; System.arraycopy(buffer, position + length, buffer, position, numChars - position); throw new IllegalArgumentException(); } if (position <= curPos) { curPos += length; } return policy.contentChanged(buffer, numChars, position, curPos); } /** * Delete characters from this text object * * @param offset The offset into the buffer to start deletion * @param length The number of characters to delete out of the buffer * @return int The delta height change caused by this deletion */ int deleteChars(int offset, int length) { if ((offset < 0 || length < 0) || (offset + length > numChars)) { throw new StringIndexOutOfBoundsException(); } System.arraycopy(buffer, offset + length, buffer, offset, numChars - offset - length); numChars -= length; if (offset < curPos) { curPos -= length;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -