softbuttonlayer.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 940 行 · 第 1/2 页

JAVA
940
字号
        // SoftButtonLayer always swallows any pointer input, as there is no        // need to forward the press on to any other layer.        return true;    }    /**     * Selects a command.     *     * @param cmd the command selected     */    public void commandSelected(Command cmd) {        if (cmd == null) {            return;        }        dismissMenu();        processCommand(cmd);    }    /**     * Toggles the alert. Grabs the background and requests a     * repaint.     *     * @param alertUp flag indicating the alaert has expired     */    public void toggleAlert(boolean alertUp) {        alertUP = alertUp;        setBackground();        requestRepaint();    }    /**     * Sets the background based on current menu and alert     * settings.     */    private void setBackground() {        if (menuUP) {            setBackground(SoftButtonSkin.IMAGE_MU_BG,                    SoftButtonSkin.COLOR_MU_BG);        } else if (alertUP) {            setBackground(SoftButtonSkin.IMAGE_AU_BG,                    SoftButtonSkin.COLOR_AU_BG);        } else {            setBackground(SoftButtonSkin.IMAGE_BG,                    SoftButtonSkin.COLOR_BG);        }    }    /**     * Toggles the current menu selection.     * Grabs the background and requests a repaint.     *     * @param menuUp the flag indicating the menu selection     */    public void toggleMenu(boolean menuUp) {        if (owner instanceof MIDPWindow) {            ((MIDPWindow) owner).paintWash(menuUp);        }        setBackground();        requestRepaint();    }    /**     * Sets the anchor constraints for rendering operation.     */    public void setAnchor() {        int anchor_x;        if (owner == null)            return;        bounds[X] = 0;        for (int i = 0; i < SoftButtonSkin.NUM_BUTTONS; i++) {            anchor_x = SoftButtonSkin.BUTTON_ANCHOR_X[i];            if (anchor_x < 0) {                anchor_x += owner.bounds[W];            }            cached_button_anchor_x[i] = anchor_x;        }        bounds[Y] = owner.bounds[H] - SoftButtonSkin.HEIGHT;        bounds[W] = owner.bounds[W];        bounds[H] = SoftButtonSkin.HEIGHT;    }    /**     * Switch based on soft button pressed.     *     * @param buttonID the button pushed     */    protected void softPress(int buttonID) {        switch (buttonID) {            case 0:                soft1();                break;            case 1:                soft2();                break;        }    }    /**     * Soft button 1 handler.     */    protected void soft1() {        if (menuUP) {            dismissMenu();            subMenu = null;            requestRepaint();            setButtonLabels();            setInteractive(false);        } else {            processCommand(soft1);        }    }    /**     * Initializes the menu from menu resources.     */    protected void initMenu() {        MenuResources.load();        menuLayer = new MenuLayer();        Command menuClose = new Command(SoftButtonSkin.TEXT_BACKCMD,                Command.BACK, 1);        menuLayer.setCommands(new Command[]{menuClose});        menuLayer.setCommandListener(this);    }    /**     * Soft button 2 handler.     */    protected void soft2() {        if (soft2 != null) {            if (soft2.length == 1 &&                soft2[0] instanceof SubMenuCommand) {                subMenu = (SubMenuCommand) soft2[0];            }                        if (soft2.length > 1 ||                subMenu != null) {                if (menuLayer == null) {                    initMenu();                }                menuLayer.setMenuCommands(soft2, this, 0);                                menuUP = true;                toggleMenu(menuUP);                                // Show the menu                if (owner != null) {                    owner.addLayer(menuLayer);		    menuLayer.alignMenu();           		    menuLayer.requestRepaint();                    menuLayer.setScrollInd(ScrollIndLayer.getInstance(ScrollIndSkin.MODE));                }                                requestRepaint();            } else if (soft2.length == 1) {                // command action                processCommand(soft2[0]);            }        } else {            setInteractive(false);        }    }    /**     * Determines if soft button 2 will be processed by the layer.     * Called by keyInput to determine if the corresponding key event      * should be absorbed by SoftButtonLayer.     *     * @return true if soft2 command can be processed, false otherwise     */    protected boolean isSoft2Active() {        // when MIDPWindow is not in full screen mode, we absorb        // all key events reserved for the delivery of commands        if (!((MIDPWindow)owner).isInFullScreenMode()) {            return true;        }        // for full screen mode we should check if soft key is useful        if (menuUP) {            return true;        } else if (soft2 != null) {            if (soft2.length == 1 &&                soft2[0] instanceof SubMenuCommand) {                return true;            }            // search for at least one active command            for (int i = 0; i < soft2.length; i++) {                if (isCommandActive(soft2[i])) {                    return true;                }            }        }        return false;    }    /**     * Determines if soft button 1 will be processed by the layer.     * Called by keyInput to determine if the corresponding key event      * should be absorbed by SoftButtonLayer.     *     * @return true if soft1 command can be processed, false otherwise     */    protected boolean isSoft1Active() {        // when MIDPWindow is not in full screen mode, we absorb        // all key events reserved for the delivery of commands        if (!((MIDPWindow)owner).isInFullScreenMode()) {            return true;        }        // for full screen mode we should check if soft key is useful        if (menuUP) {            return true;        } else {            return isCommandActive(soft1);        }    }    /**     * Determines if it is possible to process the command.     *     * @param cmd the command to check     * @return true if command can be processed, false otherwise     */    protected boolean isCommandActive(Command cmd) {        if (tunnel == null || cmd == null) {            return false;        }        if ((isItemCommand(cmd) && (itemListener != null))             || (scrListener != null)) {            return true;        }        return false;    }    /**     * Processes commands.     *     * @param cmd the selected command     */    protected void processCommand(Command cmd) {        setInteractive(false);        if (tunnel == null || cmd == null) {            return;        }        if (subMenu != null) {            subMenu.notifyListener(cmd);            subMenu = null;        } else if (isItemCommand(cmd)) {            tunnel.callItemListener(cmd, itemListener);        } else {            tunnel.callScreenListener(cmd, scrListener);        }    }    /**     * Sets the button labels.     */    protected void setButtonLabels() {        // reset all the labels        for (int i = 0; i < SoftButtonSkin.NUM_BUTTONS; i++) {            labels[i] = null;        }        // Port me : If a port had more than 2 soft buttons, adjust        // the behavior here for extra buttons        labels[0] = (soft1 == null) ? null : soft1.getLabel();        if (soft2 == null) {            labels[1] = null;        } else if (soft2.length == 1) {            labels[1] = soft2[0].getLabel();        } else {            labels[1] = SoftButtonSkin.TEXT_MENUCMD;        }        if (isNativeLayer) {            // paint buttons on native layer instead of java's SFBLayer            setNativeSoftButtonLabel0 (labels[0],0);            setNativeSoftButtonLabel0 (labels[1],1);        } else {            addDirtyRegion();            requestRepaint();        }    }    /**     * Rearranges the commands based on weights and priority.     *     * @param cmds the commands to be sorted     * @param num  the number of commands to check     */    protected void sortCommands(Command[] cmds, int num) {        // The number of commands is small, so we use a simple        // Insertion sort that requires little heap                for (int i = 1; i < num; i++) {            for (int j = i; j > 0; j--) {               if (compare(cmds[j], cmds[j - 1]) < 0) {                    swap = cmds[j];                    cmds[j] = cmds[j - 1];                    cmds[j - 1] = swap;                } else break;            }        }    }    /**     * Compares two commands.     *     * @param a first command for comparison     * @param b second command for comparison     * @return 0 if commands are the same; negative     *         if the first object is lower; positive is first     *         command is higher.     */    protected int compare(Command a, Command b) {        if (a == null || b == null) {            return 0;        }        typeX = a.getCommandType();        typeY = b.getCommandType();        if (typeX != typeY) {            return cmdWeights[typeX] - cmdWeights[typeY];        } else {            return a.getPriority() - b.getPriority();        }    }    /**     * Checks if the item is a command.     *     * @param cmd the item to be checked     * @return true if the command is found in the     *         list of item commands     */    protected boolean isItemCommand(Command cmd) {        if (itmCmds.length == 0) {            return false;        }        for (int i = 0; i < itmCmds.length; i++) {            if (itmCmds[i] == cmd) {                return true;            }        }        return false;    }/*** Initializes the soft button layer.*/    protected void initialize() {        super.initialize();        cached_button_anchor_x = new int[SoftButtonSkin.NUM_BUTTONS];        setAnchor();    }    /**     * Renders the soft button layer.     *     * @param g the graphics context to be updated     */    protected void paintBody(Graphics g) {        //paint nothing when using native layer        if (isNativeLayer) {            return;        }        g.setFont(SoftButtonSkin.FONT);        for (int i = 0; i < SoftButtonSkin.NUM_BUTTONS; i++) {            if (labels[i] == null) {                continue;            }            buttonw = SoftButtonSkin.FONT.stringWidth(labels[i]);            if (buttonw > SoftButtonSkin.BUTTON_MAX_WIDTH[i]) {                buttonw = SoftButtonSkin.BUTTON_MAX_WIDTH[i];            }            switch (SoftButtonSkin.BUTTON_ALIGN_X[i]) {                case Graphics.HCENTER:                    buttonx =                            cached_button_anchor_x[i] - (buttonw / 2);                    break;                case Graphics.RIGHT:                    buttonx = cached_button_anchor_x[i] - buttonw;                    break;                case Graphics.LEFT:                default:                    buttonx = cached_button_anchor_x[i];                    break;            }            buttony = SoftButtonSkin.BUTTON_ANCHOR_Y[i];            g.translate(buttonx, buttony);            Text.drawTruncStringShadowed(g, labels[i], SoftButtonSkin.FONT,                    SoftButtonSkin.COLOR_FG, SoftButtonSkin.COLOR_FG_SHD,                    SoftButtonSkin.BUTTON_SHD_ALIGN, buttonw);            g.translate(-buttonx, -buttony);        }    }    /**     * Returns the left soft button (one).     *     * @return the command that's tied to the left soft button     */    public Command getSoftOne() {        return soft1;    }    /**     * Returns the command array tied to the right soft button (two).     *     * @return the command array that's tied to the right soft button     */    public Command[] getSoftTwo() {        return soft2;    }    /**     * Returns true if the point lies in the bounds of commnad layers     * subset like buttons, menu, submenu      * @param x the "x" coordinate of the point     * @param y the "y" coordinate of the point     * @return true if the point lies in the bounds of commnad layers     * subset     */    public boolean belongToCmdLayers(int x, int y) {        return containsPoint(x,y) ||            (menuLayer != null &&              (menuLayer.containsPoint(x,y) ||              (menuLayer.cascadeMenu != null &&               menuLayer.cascadeMenu.containsPoint(x,y))              )             );    }            /**     * Update bounds of layer     *     * @param layers - current layer can be dependant on this parameter     */    public void update(CLayer[] layers) {        super.update(layers);        setAnchor();        if (null != menuLayer) {            menuLayer.update(layers);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?