textboxlfimpl.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 711 行 · 第 1/2 页
JAVA
711 行
/* * * * 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.CGraphicsUtil;import com.sun.midp.chameleon.input.*;import com.sun.midp.chameleon.skins.*;import com.sun.midp.chameleon.skins.resources.*;import com.sun.midp.chameleon.layers.ScrollBarLayer;/** * This is the look &s; feel implementation for TextBox. */class TextBoxLFImpl extends TextFieldLFImpl implements TextFieldLF { /** * Contains line-break information for a blob of text */ protected TextInfo myInfo; /** * A flag indicating the scroll indicator has been initialized * for this textbox. This happens only once when the textbox * first paints its contents. */ protected boolean scrollInitialized; /** * Creates TextFieldLF for the passed in TextField. * @param tf The TextField associated with this TextFieldLF */ TextBoxLFImpl(TextField tf) { super(tf); if (myInfo == null) { myInfo = new TextInfo(4); // IMPL NOTE: add initial size to skin } drawsTraversalIndicator = false; } // ***************************************************** // Public methods defined in interfaces // ***************************************************** /** * Notifies L&F of a content change in the corresponding TextBox. */ public void lSetChars() { cursor.index = tf.buffer.length(); // cursor at the end cursor.option = Text.PAINT_USE_CURSOR_INDEX; myInfo.scrollY = myInfo.isModified = true; updateTextInfo(); } /** * Update text info if required * */ private void updateTextInfo() { int w = contentBounds[WIDTH]; int h = contentBounds[HEIGHT]; // bounds are already initialized if (w > 0 && h > 0) { w -= 2 * TextFieldSkin.BOX_MARGIN + 2 * TextFieldSkin.PAD_H; h -= ((2 * TextFieldSkin.BOX_MARGIN) + (inputModeIndicator.getDisplayMode() != null ? Font.getDefaultFont().getHeight() : 0)); Text.updateTextInfo(tf.buffer.toString(), ScreenSkin.FONT_INPUT_TEXT, w, h, 0, Text.NORMAL, cursor, myInfo); if (setVerticalScroll()) { lRequestInvalidate(true,true); } else { lRequestPaint(); } } } /** * Set new cursor position. Update text info if cursor position is changed * @param pos new position */ protected void setCaretPosition(int pos) { int oldPos = cursor.index; super.setCaretPosition(pos); cursor.option = Text.PAINT_USE_CURSOR_INDEX; myInfo.isModified = myInfo.scrollY |= (oldPos != cursor.index); updateTextInfo(); } /** * Commit the given input to this TextInputComponent's buffer. * This call constitutes a change to the value of this TextInputComponent * and should result in any listeners being notified. * @param input text to commit */ public void commit(String input) { // keep the first visible line int oldTopVis = myInfo.topVis; super.commit(input); // try to restore the visible region myInfo.topVis = oldTopVis; myInfo.isModified = myInfo.scrollY = true; updateTextInfo(); } /** * Notifies L&s;F of a character insertion in the corresponding * TextBox. * @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) { if (editable) { if (position <= cursor.index) { cursor.index += length; cursor.option = Text.PAINT_USE_CURSOR_INDEX; } } myInfo.isModified = myInfo.scrollY = true; updateTextInfo(); } } /** * 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 * * @exception IllegalArgumentException if the resulting contents * would be illegal for the current * @exception StringIndexOutOfBoundsException if <code>offset</code> * and <code>length</code> do not * specify a valid range within the contents of the <code>TextField</code> */ public void lDelete(int offset, int length) { if (editable) { if (cursor.index >= offset) { int diff = cursor.index - offset; cursor.index -= (diff < length) ? diff : length; cursor.option = Text.PAINT_USE_CURSOR_INDEX; } } myInfo.isModified = myInfo.scrollY = true; updateTextInfo(); } /** * Notifies L&s;F of a maximum size change in the corresponding * TextBox. * @param maxSize - the new maximum size */ public void lSetMaxSize(int maxSize) { if (editable) { if (cursor.index >= maxSize) { cursor.index = maxSize; cursor.option = Text.PAINT_USE_CURSOR_INDEX; } } myInfo.isModified = myInfo.scrollY = true; updateTextInfo(); } /** * Notifies L&s;F that constraints have to be changed. */ public void lSetConstraints() { boolean wasEditable = editable; setConstraintsCommon(false); setVerticalScroll(); if (myInfo != null) { // reset cursor position if needed if (editable) { int pos = cursor.y / ScreenSkin.FONT_INPUT_TEXT.getHeight(); int newPos = pos; // if text box has been uneditable before to reset cursor // position to the top of the screen if (!wasEditable) { newPos = myInfo.topVis + 1; } else if (pos <= myInfo.topVis) { newPos = myInfo.topVis + 1; } else if (pos > myInfo.topVis + myInfo.visLines) { newPos = myInfo.topVis + myInfo.visLines; } if (newPos != pos) { cursor.y = newPos * ScreenSkin.FONT_INPUT_TEXT.getHeight(); cursor.option = Text.PAINT_GET_CURSOR_INDEX; myInfo.isModified = myInfo.scrollY = true; updateTextInfo(); } } else { myInfo.isModified = myInfo.scrollY = true; updateTextInfo(); } } lRequestPaint(); } /** * Paint the text, linewrapping when necessary * * @param g the Graphics to use to paint with. If g is null then * only the first four arguments are used and nothing is * painted. Use this to return just the displayed string * @param dca the text to paint * @param opChar if opChar > 0 then an optional character to paint. * @param constraints text constraints * @param font the font to use to paint the text * @param fgColor foreground color * @param w the available width for the text * @param h the available height for the text * @param offset the first line pixel offset * @param options any of Text.[NORMAL | INVERT | HYPERLINK | TRUNCATE] * @param cursor text cursor object to use to draw vertical bar * @param info TextInfo structure to use for paint */ public void paint(Graphics g, DynamicCharacterArray dca, char opChar, int constraints, Font font, int fgColor, int w, int h, int offset, int options, TextCursor cursor, TextInfo info) { if (opChar != 0) { cursor = new TextCursor(cursor); info.isModified = true; } String str = getDisplayString(dca, opChar, constraints, cursor, true); info.isModified |= !bufferedTheSameAsDisplayed(tf.constraints); Text.updateTextInfo(str, font, w, h, offset, options, cursor, info); Text.paintText(info, g, str, font, fgColor, 0xffffff - fgColor, w, h, offset, options, cursor); // just correct cursor index if the charracter has // been already committed if (str != null && str.length() > 0) { getBufferString(new DynamicCharacterArray(str), constraints, cursor, true); } // has to be moved to correct place. It's incorrect to change // the layer's dirty bounds in paint context showPTPopup((int)0, cursor, w, h); showKeyboardLayer(); } /** * Sets the content size in the passed in array. * Content is calculated based on the availableWidth. * size[WIDTH] and size[HEIGHT] should be set by this method. * @param size The array that holds Item content size and location * in Item internal bounds coordinate system. * @param availableWidth The width available for this Item */ void lGetContentSize(int size[], int availableWidth) { int oldWidth = size[WIDTH]; int oldHeight = size[HEIGHT]; try { // We size to the maximum allowed, minus the padding // defined in the skin. size[WIDTH] = ((DisplayableLFImpl)tf.owner.getLF()). getDisplayableWidth() - 2 * TextFieldSkin.BOX_MARGIN; // Note: tf.owner is the original TextBox for this LFImpl size[HEIGHT] = ((DisplayableLFImpl)tf.owner.getLF()). getDisplayableHeight() - 2 * TextFieldSkin.BOX_MARGIN; } catch (Throwable t) { // NOTE: the above call to getCurrent() will size the textbox // appropriately if there is a title, ticker, etc. Calling // this method depends on the textbox being current however. size[WIDTH] = 100; size[HEIGHT] = 100; // IMPL NOTE: Log this as an error } if (oldHeight != size[HEIGHT] || oldWidth != size[WIDTH]) { myInfo.scrollY = myInfo.isModified = true; updateTextInfo(); } } /** * Paints the content area of this TextField. * Graphics is translated to contents origin. * @param g The graphics where Item content should be painted * @param width The width available for the Item's content * @param height The height available for the Item's content */ void lPaintContent(Graphics g, int width, int height) { g.translate(TextFieldSkin.BOX_MARGIN, TextFieldSkin.BOX_MARGIN); width -= (2 * TextFieldSkin.BOX_MARGIN); height -= ((2 * TextFieldSkin.BOX_MARGIN) + (inputModeIndicator.getDisplayMode() != null ? Font.getDefaultFont().getHeight() : 0)); if (editable) { if (TextFieldSkin.IMAGE_BG != null) { CGraphicsUtil.draw9pcsBackground(g, 0, 0, width, height, TextFieldSkin.IMAGE_BG); } else { CGraphicsUtil.drawDropShadowBox(g, 0, 0, width, height, TextFieldSkin.COLOR_BORDER, TextFieldSkin.COLOR_BORDER_SHD, TextFieldSkin.COLOR_BG); } } else { if (TextFieldSkin.IMAGE_BG_UE != null) { CGraphicsUtil.draw9pcsBackground(g, 0, 0, width, height,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?