midpwindow.java

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

JAVA
851
字号
/* *   * * 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;import com.sun.midp.chameleon.layers.*;import com.sun.midp.chameleon.skins.*;import com.sun.midp.log.Logging;import com.sun.midp.log.LogChannels;import javax.microedition.lcdui.*;/** * The MIDPWindow class is a concrete instance of a CWindow which * implements the MIDP specification and its graphical elements, * such as a title bar, soft buttons, ticker, etc. */public class MIDPWindow extends CWindow {    // The order of layers id is impotant during creation and updating    /** Id of layer containing the alert wash */    public static final int ALERT_WASH_LAYER = 0;    /** Id of layer containing the alert displayable */    public static final int ALERT_LAYER = 1;    /** Id of layer containing the mail */    public static final int WASH_LAYER = 2;        /** Id of layer rendering the soft button controls */    public static final int BTN_LAYER = 3;    /** Id of layer containing the ticker of the current displayable */    public static final int TICKER_LAYER = 4;    /** Id of layer containing the title of the current displayable */    public static final int TITLE_LAYER = 5;    /** Id of layer containing the pti contents */    public static final int PTI_LAYER = 6;    /** Id of layer containing the virtual keyboard contents */    public static final int KEYBOARD_LAYER = 7;    /** Id of layer containing the current displayable's contents */    public static final int BODY_LAYER = 8;    /** Number of main layers*/                                               public static final int LAST_LAYER = 9;    /** Used to call back into the Display class from this package */    ChamDisplayTunnel tunnel;    /** Cached typed references to the namded layers */    private WashLayer washLayer;    private WashLayer alertWashLayer;    private AlertLayer alertLayer;    private TitleLayer titleLayer;    private TickerLayer tickerLayer;    private SoftButtonLayer buttonLayer;    private PTILayer ptiLayer;    private VirtualKeyboardLayer keyboardLayer;    private BodyLayer bodyLayer;    // layout modes    /**     * Normal screen mode     */    private static final int NORMAL_MODE         = 0;    /**     * Full screen mode when the current displayable     * is occupying as much screen as possible     */    private static final int FULL_SCR_MODE       = 1;    /**     * Current screen mode     */    int screenMode;    /** Cache of screen commands */    Command[] scrCmdCache;    /** Number of screen commands in the cache */    int scrCmdCount;    /** Listener to notify when a screen command is selected */    CommandListener scrCmdListener;    /** Cache of selected item commands */    Command[] itemCmdCache;    /** Number of item commands in the cache */    int itemCmdCount;    /** Listener to notify when an item command is selected */    ItemCommandListener itemCmdListener;    CLayer[] mainLayers = new CLayer[LAST_LAYER];    /** Determines whether area of the window has been changed */    boolean sizeChangedOccured = false;    /** Indicates if body layer was checked for optimized Canvas painting */    boolean bodyChecked = false;    /** Indicates wheher body layer is overlapped with a visible layer */    boolean bodyOverlapped = false;    /**     * Construct a new MIDPWindow given the tunnel to the desired     * MIDP Display instance     *     * @param tunnel the "tunnel" to make calls from this java package     *               back into the Display object in another package     */    public MIDPWindow(ChamDisplayTunnel tunnel) {        super(ScreenSkin.IMAGE_BG, ScreenSkin.COLOR_BG, 	      tunnel.getDisplayWidth(), tunnel.getDisplayHeight());        this.tunnel = tunnel;        for (int i = LAST_LAYER - 1; i >= 0; i-- ) {            createLayer(i);        }    }    /**     * Request a repaint. This method does not require any bounds     * information as it is contained in each of the Chameleon layers.     * This method simply results in a repaint event being placed in     * the event queue for a future callback.     */    public void requestRepaint() {        if (tunnel != null) {            tunnel.scheduleRepaint();        }    }    /**     * Set the title of this MIDPWindow. This would typically     * correspond to the title of the current displayable, and     * may result in the title layer appearing or disappearing.     *     * @param title the value of the title. null indicates there     *              is no title.     */    public void setTitle(String title) {        if (titleLayer.setTitle(title)) {            resize();        }        requestRepaint();    }    /**     * Set the ticker of this MIDPWindow. This would typically     * correspond to the ticker of the current displayable, and     * may result in the ticker layer appearing or disappearing.     *     * @param ticker the current Ticker object. null indicates there     *              is no ticker.     */    public void setTicker(Ticker ticker) {        if (tickerLayer.setText((ticker != null) ? ticker.getString() : null)) {            resize();        }        requestRepaint();    }    /**     * Alert this MIDPWindow that the given displayable is now current     * and should be shown on the screen.     *     * This will establish the given displayable on the screen,     * as well as reflect the displayable's title and ticker (if any).     * Special circumstances may occur if the displayable is an Alert,     * such as maintaining the current screen contents and showing the     * Alert in a popup.     *     * @param displayable the newly current displayable to show     * @param height the preferred height of the new displayable     */    public void showDisplayable(Displayable displayable, int height) {        bodyLayer.opaque =  (displayable instanceof Canvas);        Ticker t = displayable.getTicker();        tickerLayer.setText((t != null) ? t.getString() : null);        if (displayable instanceof Alert) {            tickerLayer.toggleAlert(true);            buttonLayer.toggleAlert(true);                        // alert does not use title layer. The title is a part of content             titleLayer.setTitle(null);            alertLayer.setAlert(true, (Alert)displayable, height);                        paintWash(false);            addLayer(alertLayer);        } else {            titleLayer.setTitle(displayable.getTitle());	    bodyLayer.setVisible(true);        }        addLayer(tickerLayer);        resize();        requestRepaint();    }    /**     * Alert this MIDPWindow that the given displayable is no longer     * current and should be removed from the screen.     *     * Special circumstances may occur if the displayable is an Alert,     * such as removing the popup and re-instating the previous     * displayable which was visible before the Alert popped up.     *     * @param displayable the newly current displayable to show     */    public void hideDisplayable(Displayable displayable) {        if (displayable instanceof Alert) {            buttonLayer.toggleAlert(false);            tickerLayer.toggleAlert(false);            paintWash(false);            alertLayer.setAlert(false, null, 0);            removeLayer(alertLayer);        } else {            bodyLayer.setVisible(false);        }                removeLayer(tickerLayer);                buttonLayer.dismissMenu();        // Make sure that not of the popups are shown        clearPopups();    }    /**     * Determines if the system menu is currently visible. This can be useful     * in determining the current isShown() status of the displayable.     *     * @return true if the system menu is up     */    public boolean systemMenuUp() {        return buttonLayer.systemMenuUp();    }    /**     * Request a repaint of a region of the current displayable.     * This method specifically marks a region of the body layer     * (which renders the displayable's contents) as dirty and     * results in a repaint request being scheduled. The coordinates     * are in the space of the displayable itself - that is, 0,0     * represents the top left corner of the body layer.     *     * @param x the x coordinate of the dirty region     * @param y the y coordinate of the dirty region     * @param w the width of the dirty region     * @param h the height of the dirty region     */    public void repaintDisplayable(int x, int y, int w, int h) {        // We mark the body layer as dirty        if (alertLayer.visible) {            alertLayer.addDirtyRegion(x, y, w, h);        } else {            bodyLayer.addDirtyRegion(x, y, w, h);        }        requestRepaint();    }    /**     * Add the given layer to this window. This method is     * overridden from CWindow in order to special case     * popup layers. Popup layers can have their own commands     * which supercede those of the current displayable.     *     * @param layer the CLayer to add to this window     * @return true if new layer was added, false otherwise     */    public boolean addLayer(CLayer layer) {        boolean added = super.addLayer(layer);	if (added) {	    if (layer instanceof PopupLayer) {		PopupLayer popup = (PopupLayer)layer;		popup.setDirty();		popup.visible = true;				Command[] cmds = popup.getCommands();		if (cmds != null) {		    buttonLayer.updateCommandSet(						 null, 0, null, cmds, cmds.length,						 popup.getCommandListener());		}	    }	    	    if (layer instanceof PTILayer) {		ptiLayer = (PTILayer)layer;		mainLayers[PTI_LAYER] = layer;		resize();	    } else if (layer instanceof VirtualKeyboardLayer) {		keyboardLayer = (VirtualKeyboardLayer)layer;		mainLayers[KEYBOARD_LAYER] = layer;		resize();	    } else {		layer.update(mainLayers);	    }	}        if (added && layer instanceof VirtualKeyboardLayer) {            keyboardLayer = (VirtualKeyboardLayer)layer;            mainLayers[KEYBOARD_LAYER] = layer;            resize();        }        return added;    }    /**     * Remove the given layer from this window. This method is     * overridden from CWindow in order to special case popup     * layers. Popup layers can have their own commands which     * supercede those of the current displayable. In this case,     * the popup is removed and the commands in the soft button     * bar are restored to either the next top-most popup layer     * or the current displayable itself.     *     * @param layer the CLayer to remove from this window     * @return true if the layer was able to be removed     */    public boolean removeLayer(CLayer layer) {        if (super.removeLayer(layer)) {            if (layer instanceof PopupLayer) {                if (layer == mainLayers[PTI_LAYER]) {                    ptiLayer = null;                    mainLayers[PTI_LAYER] = null;                    resize();                }                if (layer == mainLayers[KEYBOARD_LAYER]) {                    keyboardLayer = null;                    mainLayers[KEYBOARD_LAYER] = null;                    resize();                }                // Now we update the command set with either the                // next top most popup or the original cached commands                PopupLayer p = getTopMostPopup();                if (p != null && p.getCommands() != null) {                    Command[] cmds = p.getCommands();                    buttonLayer.updateCommandSet(                        null, 0, null, cmds, cmds.length, p.getCommandListener());                } else {                    buttonLayer.updateCommandSet(                        itemCmdCache, itemCmdCount, itemCmdListener,                        scrCmdCache, scrCmdCount, scrCmdListener);                }            } // instanceof            return true;        } // removeLayer        return false;    }    /**     * Return bounds of BodyLayer currently     * @return array of bounds     */    public int[] getBodyLayerBounds() {        int[] innerBounds = new int[4];        System.arraycopy(bodyLayer.bounds,0,innerBounds,0,4);        return innerBounds;    }    /**     * Update this MIDPWindow's current command set to match the     * current displayable and possibly item selection.     *     * @param itemCommands the set of item specific commands     * @param itemCmdCount the number of item commands     * @param itemCmdListener the notification listener for item commands     * @param scrCommands the set of screen specific commands     * @param scrCmdCount the number of screen commands     * @param scrCmdListener the notification listener for screen commands     */    public void updateCommandSet(Command[] itemCommands,                                 int itemCmdCount,                                 ItemCommandListener itemCmdListener,                                 Command[] scrCommands,                                 int scrCmdCount,                                 CommandListener scrCmdListener)    {        // We cache commands to easily reset them when a        // popup takes precedence and then is dismissed        this.itemCmdCache = itemCommands;        this.itemCmdCount = itemCmdCount;        this.itemCmdListener = itemCmdListener;        this.scrCmdCache = scrCommands;        this.scrCmdCount = scrCmdCount;        this.scrCmdListener = scrCmdListener;        buttonLayer.updateCommandSet(itemCommands, itemCmdCount,

⌨️ 快捷键说明

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