softbuttonlayer.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 940 行 · 第 1/2 页
JAVA
940 行
/* * * * 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 com.sun.midp.chameleon.layers;import com.sun.midp.chameleon.*;import com.sun.midp.lcdui.Text;import javax.microedition.lcdui.*;import com.sun.midp.chameleon.skins.SoftButtonSkin;import com.sun.midp.chameleon.skins.ScrollIndSkin;import com.sun.midp.chameleon.skins.resources.MenuResources;// EventConstants defines some constant values, such as// key press, release, soft button codes, etc.import com.sun.midp.lcdui.EventConstants;import com.sun.midp.i18n.Resource;import com.sun.midp.i18n.ResourceConstants;/** * Soft button layer. */public class SoftButtonLayer extends CLayer implements CommandListener { /** * Labels for each of the softbuttons. */ protected String[] labels; /** * A cached copy of the set of screen commands sent from * the Display. */ protected Command[] scrCmds; /** * The CommandListener to notify for any screen commands executed. */ protected CommandListener scrListener; /** * A cached copy of the set of item commands sent from * the Display. */ protected Command[] itmCmds; /** * The ItemCommandListener to notify for any item commands executed. */ protected ItemCommandListener itemListener; /** * The command associated with soft button #1. * null if there is no command associated with button #1. */ protected Command soft1; /** * The set of commands associated with soft button #2. * null if there are no commands associated with button #2. * If there is only one element in this array, there is no need * for a menu and soft button #2 will behave similar to soft * button #1 and invoke the listener directly upon button press. */ protected Command[] soft2; /** * When a sub menu is currently active, this reference is non-null. */ protected SubMenuCommand subMenu; private int[] cached_button_anchor_x; /** * A set of weights assigned to each of the types of Commands. * The array is set up to return the weight of the Command type * for each index, ie. Command.BACK has a defined value of 2 from * the MIDP specification, cmdWieghts[Command.BACK] == 4, that is, * there are three other Command types which sort higher than BACK * (ITEM, SCREEN, and OK). The weighting is specified in the * MIDP Human Interface Specification (Sun Internal). */ static protected final int[] cmdWeights = { -1, // 0 is no value 2, // Screen 4, // Back 6, // Cancel 3, // Ok 8, // Help 7, // Stop 5, // Exit 1, // Item 9, // Virtual }; /** * A System menu to popup and display when a set of commands * needs to be displayed. */ protected MenuLayer menuLayer; /** * A flag indicating the system menu is up. */ protected boolean menuUP; /** * A flag indicating the alert is up. */ protected boolean alertUP; /** * A tunnel to utilize to notify of command invocation. */ protected ChamDisplayTunnel tunnel; /** * An internal variable defined once to avoid costly heap thrashing. */ protected Command swap; /** * Internal variables defined once to avoid costly heap thrashing. */ protected int typeX, typeY; /** * Internal variables for the paint loop. */ protected int buttonx, buttony, buttonw, buttonh; /** * True if user is interacting with the layer */ private boolean isInteractive ; // = false; /** * True if using native soft button layer */ private boolean isNativeLayer = false; /** * Construct a SoftButtonLayer. The layer's background image and color * information is obtained directly from the SoftButtonSkin class, * such as background image, tile, and/or background color. * * @param tunnel channel for command notifications */ public SoftButtonLayer(ChamDisplayTunnel tunnel) { super(SoftButtonSkin.IMAGE_BG, SoftButtonSkin.COLOR_BG); super.setSupportsInput(true); super.setVisible(true); this.tunnel = tunnel; isNativeLayer = isNativeSoftButtonLayerSupported0(); labels = new String[SoftButtonSkin.NUM_BUTTONS]; } static public native boolean isNativeSoftButtonLayerSupported0(); private native void setNativeSoftButtonLabel0(String label, int softButtonIndex); /** * Returnes true if user is interacting with the layer, * false otherwise. * * used by MIDPWindow to check if the layer should become * visible. */ public boolean isInteractive() { return isInteractive; } /** * Assigns new value to isInteractive and signals MIDPWindow. */ private void setInteractive(boolean interactive) { if (isInteractive != interactive) { isInteractive = interactive; if (owner instanceof MIDPWindow) { ((MIDPWindow)owner).onSoftButtonInteractive(isInteractive); } } } /** * Commandlistener interface implementation. Handle softbuton commands. * * @param c command * @param d displayable */ public void commandAction(Command c, Displayable d) { dismissMenu(); } /** * Dismiss menu layer. */ public void dismissMenu() { if (menuUP) { menuUP = false; menuLayer.dismiss(); if (owner != null) { menuLayer.setScrollInd(null); owner.removeLayer(menuLayer); } } toggleMenu(menuUP); } /** * Returns true if system menu is currently up, false otherwise. * * @return true if system menu is up, false otherwise */ public boolean systemMenuUp() { return menuUP; } /** * Called by the system to update the set of commands associated * with this button bar and its subsequent system menu. * * @param itemCmds an array of item specific commands * @param numI the number of item specific commands * @param itemListener the ItemCommandListener to notify if any * item commands are selected * @param screenCmds an array of screen specific commands * @param numS the number of screen specific commands * @param scrListener the CommandListener to notify if any * screen commands are selected */ public void updateCommandSet(Command[] itemCmds, int numI, ItemCommandListener itemListener, Command[] screenCmds, int numS, CommandListener scrListener) { // Cache the values for later this.itmCmds = new Command[numI]; if (numI > 0) { System.arraycopy(itemCmds, 0, this.itmCmds, 0, numI); } this.itemListener = itemListener; this.scrCmds = new Command[numS]; if (numS > 0) { System.arraycopy(screenCmds, 0, this.scrCmds, 0, numS); } this.scrListener = scrListener; // reset the commands soft1 = null; soft2 = null; if (numS > 0) { int index = -1; int type = -1; for (int i = 0; i < numS; i++) { if (!(this.scrCmds[i] instanceof SubMenuCommand)) { switch (this.scrCmds[i].getCommandType()) { case Command.BACK: index = i; type = Command.BACK; break; case Command.EXIT: if (type != Command.BACK) { index = i; type = Command.EXIT; } break; case Command.CANCEL: if (type != Command.BACK && type != Command.EXIT) { index = i; type = Command.CANCEL; } break; case Command.STOP: if (type != Command.BACK && type != Command.EXIT && type != Command.CANCEL) { index = i; type = Command.STOP; } break; default: break; } } // if // We can short circuit the search if we find // a BACK command, because that is the highest weighted // Command for the left soft button if (type == Command.BACK) { break; } } // for // If we have a command for the left button, we pop it out // of the array and decrement the number in the array - because // we'll sort them all together to form the right button (or menu) if (type > -1) { numS--; soft1 = this.scrCmds[index]; System.arraycopy(screenCmds, index + 1, scrCmds, index, numS - index); } } // Now fill in the 'right' soft button, possibly with a menu // of Commands switch (numI + numS) { case 0: soft2 = null; break; case 1: if (soft1 == null) { soft1 = (numI > 0) ? this.itmCmds[0] : this.scrCmds[0]; } else { soft2 = new Command[1]; soft2[0] = (numI > 0) ? this.itmCmds[0] : this.scrCmds[0]; } break; default: soft2 = new Command[numI + numS]; if (setCommands(numI, 0, this.itmCmds)) { numI--; } setCommands(numS, numI, this.scrCmds); break; } setButtonLabels(); } /** * Assigns the commands to soft1 and soft2 buttons . * * @param cmdNum Number of commands * @param start Start index for soft2 commands array * @param cmds Source commands array * @return if any commnad has been assign to soft1 returns true, * false otherwise */ private boolean setCommands(int cmdNum, int start, Command[] cmds) { int setSoft1 = 0; if (cmdNum > 0) { sortCommands(cmds, cmdNum); for (int i = 0; i < cmdNum; i++) { if (soft1 == null && !(cmds[i] instanceof SubMenuCommand)) { soft1 = cmds[i]; setSoft1++; } else { soft2[start + i - setSoft1] = cmds[i]; } } if (setSoft1 > 0) { // decrement the soft2 array length Command[] soft2temp = new Command[soft2.length - 1]; System.arraycopy(soft2, 0, soft2temp, 0, soft2temp.length); soft2 = soft2temp; } } return setSoft1 > 0; } /** * Handle key input from a keypad. Parameters describe * the type of key event and the platform-specific * code for the key. (Codes are translated using the * lcdui.Canvas) * * @param type the type of key event * @param keyCode the numeric code assigned to the key * @return true if this method processed the input, * otherwise false. */ public boolean keyInput(int type, int keyCode) { // SoftButtonLayer absorbs soft button // event only if corresponding soft button is "active". // For further clarification please refer to // isSoft1Active() and isSoft2Active() methods. boolean ret = false; if (keyCode == EventConstants.SOFT_BUTTON1) { if (isSoft1Active()) { if (type == EventConstants.PRESSED) { setInteractive(true); ret = true; } else if (type == EventConstants.RELEASED) { soft1(); ret = true; } } } else if (keyCode == EventConstants.SOFT_BUTTON2) { if (isSoft2Active()) { if (type == EventConstants.PRESSED) { setInteractive(true); ret = true; } else if (type == EventConstants.RELEASED) { soft2(); ret = true; } } } return ret; } /** * Handles pointer input events. * * @param type the event type for this input event * @param x the x coordinate of the input event * @param y the y coordinate of the input event * @return true always */ public boolean pointerInput(int type, int x, int y) { if (type != EventConstants.PRESSED) { return true; } for (int i = 0; i < SoftButtonSkin.NUM_BUTTONS; i++) { switch (SoftButtonSkin.BUTTON_ALIGN_X[i]) { case Graphics.LEFT: if (x < cached_button_anchor_x[i] || (x > cached_button_anchor_x[i] + SoftButtonSkin.BUTTON_MAX_WIDTH[i])) { continue; } break; case Graphics.RIGHT: if (x > cached_button_anchor_x[i] || (x < cached_button_anchor_x[i] - SoftButtonSkin.BUTTON_MAX_WIDTH[i])) { continue; } break; default: continue; } if (y >= SoftButtonSkin.BUTTON_ANCHOR_Y[i] && y <= bounds[H]) { softPress(i); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?