stringitemlfimpl.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 550 行 · 第 1/2 页
JAVA
550 行
/* * * * 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.Text;import com.sun.midp.configurator.Constants;import com.sun.midp.chameleon.CGraphicsUtil;import com.sun.midp.chameleon.skins.ScreenSkin;import com.sun.midp.chameleon.skins.StringItemSkin;import com.sun.midp.chameleon.skins.resources.StringItemResources;/*** This is the look &s; feel implementation for StringItem.*/class StringItemLFImpl extends ItemLFImpl implements StringItemLF { /** * Creates a look&s;feel for a StringItem * @param strItem The StringItem associated with this look&s;feel */ StringItemLFImpl(StringItem strItem) { super(strItem); this.strItem = strItem; StringItemResources.load(); checkTraverse(); // when no commands are added actual appearance // is PLAIN; actual appearance will be the same // as appearance set in StringItem if a command is added // this StringItem this.appearanceMode = Item.PLAIN; } // ***************************************************** // Public methods (StringItemLF interface impl) // ***************************************************** /** * Get the preferred width of this Item * * @param h the tentative content height in pixels, or -1 if a * tentative height has not been computed * @return the preferred width */ public int lGetPreferredWidth(int h) { // IMPL NOTE: we ignore the 'h' value and just return // a basic width based on our contents. // In BUTTON mode internal layout and sizing is done in // ItemLFImpl if (appearanceMode == Item.BUTTON) { return super.lGetPreferredWidth(h); } // In PLAIN and HYPERLINK modes label and content string are // almost concatenated together and wrapped together // (if both are not empty there is a horizontal padding between them) int size[] = contentBounds; Text.getTwoStringsSize(size, strItem.label, strItem.str, ScreenSkin.FONT_LABEL, strItem.font, lGetAvailableWidth(), getHorizontalPad()); return size[WIDTH]; } /** * Get the preferred height of this Item * * @param w the tentative content width in pixels, or -1 if a * tentative width has not been computed * @return the preferred height */ public int lGetPreferredHeight(int w) { // In BUTTON and HIPERLINK mode internal layout and sizing is done in // ItemLFImpl if (appearanceMode == Item.BUTTON || appearanceMode == Item.HYPERLINK) { return super.lGetPreferredHeight(w); } // In PLAIN and HYPERLINK modes label and content string are // almost concatenated together and wrapped together // (almost because there is a horizontal padding between them) int size[] = contentBounds; Text.getTwoStringsSize(size, strItem.label, strItem.str, ScreenSkin.FONT_LABEL, strItem.font, w == -1 ? lGetAvailableWidth() : w, getHorizontalPad()); return size[HEIGHT]; } /** * Get the minimum width of this Item. * Calculate the minimum width as the width of double "W". If the calculated * width is greater than available width just return available width. * * @return the minimum width */ public int lGetMinimumWidth() { int minWidth = strItem.font.charWidth('W') * 2; int availableWidth = lGetAvailableWidth(); return (minWidth > availableWidth ? availableWidth : minWidth); } /** * Get the minimum height of this Item. * Calculate the minimum height as the height of the font. * * @return the minimum height */ public int lGetMinimumHeight() { return strItem.font.getHeight(); } /** * Notifies L&s;F of a command addition in the corresponding StringItem. * @param cmd the newly added command * @param i the index of the added command in the StringItem's * commands[] array */ public void lAddCommand(Command cmd, int i) { super.lAddCommand(cmd, i); // restore the value of the original appearanceMode if ((strItem.numCommands >= 1) && (appearanceMode == Item.PLAIN)) { appearanceMode = strItem.appearanceMode == Item.BUTTON ? Item.BUTTON : Item.HYPERLINK; lRequestInvalidate(true, true); } // checkTraverse(); right now traversability is not command dependent // lRequestInvalidate(true, true); } /** * Notifies L&s;F of a command removal in the corresponding StringItem. * @param cmd the newly removed command * @param i the index of the removed command in the StringItem's * commands[] array */ public void lRemoveCommand(Command cmd, int i) { super.lRemoveCommand(cmd, i); // default to PLAIN appearance if there are no commands left if (strItem.numCommands < 1) { appearanceMode = Item.PLAIN; lRequestInvalidate(true, true); } // checkTraverse(); right now traversability is not command dependent // lRequestInvalidate(true, true); } /** * Notifies L&s;F of a string change in the corresponding StringItem. * @param str - the new string set in the StringItem */ public void lSetText(String str) { checkTraverse(); lRequestInvalidate(true, true); } /** * Notifies L&s;F of a font change in the corresponding StringItem. * @param font - the new font set in the StringItem */ public void lSetFont(Font font) { lRequestInvalidate(true, true); } /** * Gets default font to render text in StringItem if it was not * set by the application. * @return - the font to render text if it was not set by the app */ public Font getDefaultFont() { return getTextFont(appearanceMode); } // ***************************************************** // Package private methods // ***************************************************** /** * Returns the font to render text in StringItem. * @param appearance The appearance mode of the StringItem * @return the font to render text in StringItem. */ static Font getTextFont(int appearance) { switch (appearance) { case Item.PLAIN: return StringItemSkin.FONT; case Item.HYPERLINK: return StringItemSkin.FONT_LINK; default: // BUTTON return StringItemSkin.FONT_BUTTON; } } /** * Returns the foreground color to render text in StringItem. * @param appearance The appearance mode of the StringItem * @return the foreground color per appearance mode */ static int getForeground(int appearance) { switch (appearance) { case Item.PLAIN: return ScreenSkin.COLOR_FG; case Item.HYPERLINK: return StringItemSkin.COLOR_FG_LINK; default: // BUTTON return StringItemSkin.COLOR_FG_BUTTON; } } /** * Returns the foreground color to render text in StringItem. * @param appearance The appearance mode of the StringItem * @return the foreground color per appearance mode */ static int getForegroundHilight(int appearance) { switch (appearance) { case Item.HYPERLINK: return StringItemSkin.COLOR_FG_LINK_FOCUS; case Item.PLAIN: default: // BUTTON return ScreenSkin.COLOR_FG; } } /** * 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 w The width available for this Item */ void lGetContentSize(int size[], int w) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?