?? virtuallist.java.svn-base
字號:
/******************************************************************************* Library of additional graphical screens for J2ME applications Copyright (C) 2003-08 Jimm Project This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA******************************************************************************** File: src/DrawControls/VirtualList.java Version: ###VERSION### Date: ###DATE### Author(s): Artyomov Denis, Igor Palkin*******************************************************************************/package DrawControls;import javax.microedition.lcdui.*;import java.lang.Math;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import DrawControls.ListItem;import DrawControls.VirtualListCommands;class VirtualCanvas extends Canvas implements Runnable{ VirtualList currentControl; private Timer repeatTimer = new Timer(); private TimerTask timerTask; private int lastKeyKode; private Display display; public void setDisplay(Display display) { this.display = display; } public Display getDisplay() { return display; } public VirtualCanvas() { //#sijapp cond.if target="MIDP2" | target="MOTOROLA" | target="SIEMENS2"# setFullScreenMode(true); //#sijapp cond.elseif target="RIM"# setFullScreenMode(false); //#sijapp cond.end# } protected void paint(Graphics g) { if (currentControl != null) currentControl.paint(g); } protected void showNotify() { cancelKeyRepeatTask(); //#sijapp cond.if target="MIDP2" | target="MOTOROLA" | target="SIEMENS2"# setFullScreenMode(true); //#sijapp cond.elseif target="RIM"# setFullScreenMode(false); //#sijapp cond.end# if (currentControl != null) currentControl.showNotify(); } protected void hideNotify() { cancelKeyRepeatTask();//#sijapp cond.if target!="RIM" & target!="DEFAULT"# currentControl.resetUiState();//#sijapp cond.end# currentControl.onHide(); } public void run() { if (timerTask == null) return; currentControl.keyRepeated(lastKeyKode); } protected void keyPressed(int keyCode) { cancelKeyRepeatTask(); if (currentControl != null) currentControl.keyPressed(keyCode); lastKeyKode = keyCode; timerTask = new TimerTask() { public void run() { display.callSerially(VirtualCanvas.this); } }; repeatTimer.schedule(timerTask, 500, 50); } protected void keyReleased(int keyCode) { if (currentControl != null) currentControl.keyReleased(keyCode); cancelKeyRepeatTask(); } void cancelKeyRepeatTask() { if (timerTask != null) timerTask.cancel(); lastKeyKode = 0; timerTask = null; } //#sijapp cond.if target is "MIDP2"# protected void pointerDragged(int x, int y) { if (currentControl != null) currentControl.pointerDragged(x, y); } protected void pointerPressed(int x, int y) { if (currentControl != null) currentControl.pointerPressed(x, y); } protected void pointerReleased(int x, int y) { if (currentControl != null) currentControl.pointerReleased(x, y); } //#sijapp cond.end#}public abstract class VirtualList{ private static VirtualCanvas virtualCanvas = new VirtualCanvas(); /*! Use dotted mode of cursor. If item of list is selected, dotted rectangle drawn around it*/ public final static int CURSOR_MODE_ENABLED = 2; /*! Does't show cursor at selected item. */ public final static int CURSOR_MODE_DISABLED = 3; /*! Constant for medium sized font of caption and item text */ public final static int MEDIUM_FONT = Font.SIZE_MEDIUM; /*! Constant for large sized font of caption and item text */ public final static int LARGE_FONT = Font.SIZE_LARGE; /*! Constant for small sized font of caption and item text */ public final static int SMALL_FONT = Font.SIZE_SMALL; // Key event type public final static int KEY_PRESSED = 1; public final static int KEY_REPEATED = 2; public final static int KEY_RELEASED = 3; // Mode for background image private static boolean caveBgImage = true; // Commands to react to VL events private VirtualListCommands vlCommands; // Used by "Invalidate" method to prevent invalidate when locked private boolean dontRepaint = false; // Used for passing params of items when painting final static protected ListItem paintedItem; // Used to catch changes to repaint data private int lastCurrItem = 0, lastTopItem = 0; private static int curMenuItemIndex; private static int[] curXVals = new int[2]; private static final int KEY_CODE_LEFT_MENU = 1000001; private static final int KEY_CODE_RIGHT_MENU = 1000002; private static final int KEY_CODE_BACK_BUTTON = 1000003; private static final int KEY_CODE_UNKNOWN = 1000004; // Individual UI stuff protected int currItem = 0; private Image capImages[];//#sijapp cond.if target!="DEFAULT"# private static Image backImage;//#sijapp cond.end# protected boolean cyclingCursor = false; private boolean fullScreen = false; protected int borderWidth = 0; protected int curFrameWidth = 1; private int topItem = 0; // Index of top visilbe item private int fontSize = MEDIUM_FONT; // Current font size of VL private int bkgrndColor = 0xFFFFFF; // bk color of VL private int cursorColor = 0x808080; // Used when drawing focus rect. private int cursorFrameColor = 0xFF; // Used when drawing focus rect. private int textColor = 0x000000; // Default text color. private int capBkCOlor = 0xC0C0C0; private int capTxtColor = 0x00; // Color of caprion text private int cursorMode = CURSOR_MODE_ENABLED; // Cursor mode private String caption; private int fontHeight; private int cursorAlpha = 255; private int menuAlpha = 255; protected Font FONT_STYLE_BOLD; protected Font FONT_STYLE_PLAIN; protected Font FONT_STYLE_ITALIC; // Common UI stuff private static Font capAndMenuFont; private static boolean mirrorMenu = false; private static int capOffset = 0; private static String bottomText = null; protected static final int scrollerWidth; private static boolean mpbEnable = false; private static int mpbPercent = 0; static { capAndMenuFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL); int width = capAndMenuFont.getHeight() / 3; scrollerWidth = width > 4 ? width : 4; paintedItem = new ListItem(); } /** * Sets mirror-style for menues. If value is true, left and right menus is changed over * @param value values for mirror-style of menus */ public static void setMirrorMenu(boolean value) { mirrorMenu = value; } public static void setCapOffset(int value) { capOffset = value; } public static void setMiniProgressBar(boolean value) { mpbEnable = value; System.out.println ("mpbEnable = " + value); } public static boolean getMpbState() { return mpbEnable; } public static void setMpbPercent(int value) { mpbPercent = value; if (virtualCanvas.currentControl != null) virtualCanvas.currentControl.repaint(); System.out.println ("mpbPercent = " + value + "%"); } /** * Sets display reference for all VirtualList's instances. You have to set display * before usage of VirtualList objects * @param display reference to midlet's display */ static public void setDisplay(Display display) { virtualCanvas.setDisplay(display); } public void setFullScreen(boolean value) { if (fullScreen == value) return; fullScreen = value; if (isActive()) virtualCanvas.repaint(); } public static void setFullScreenForCurrent(boolean value) { if (virtualCanvas.currentControl != null) virtualCanvas.currentControl.setFullScreen(value); } //! Create new virtual list with default values public VirtualList(String capt //!< Caption text of new virtual list ) { super(); setCaption(capt); //#sijapp cond.if target is "SIEMENS2"# //# this.fontSize = Font.SIZE_SMALL; //#sijapp cond.else# this.fontSize = Font.SIZE_MEDIUM; //#sijapp cond.end# this.cursorMode = CURSOR_MODE_ENABLED; initVirtualList(); } // public VirtualList public VirtualList(String capt, //!< Caption text of new virtual list int capTextColor, //!< Caption text color int backColor, //!< Control back color int fontSize, /*!< Control font size. This font size if used both for caption and text in tree nodes */ int cursorMode /*!< Cursor mode. Can be VirtualList.SEL_DOTTED or VirtualList.SEL_INVERTED */ ) { super(); setCaption(capt); this.capTxtColor = capTextColor; this.bkgrndColor = backColor; this.fontSize = fontSize; this.cursorMode = cursorMode; initVirtualList(); } private void initVirtualList() { initFonts(); fontHeight = getQuickFont(Font.STYLE_PLAIN).getHeight(); curFrameWidth = (fontHeight > 16) ? 2 : 1; borderWidth = fontHeight/6+1; } //! Request number of list elements to be shown in list /*! You must return number of list elements in successtor of VirtualList. Class calls method "getSize" each time before it drawn */ abstract protected int getSize(); //! Request of data of one list item /*! You have to reload this method. With help of method "get" class finds out data of each item. Method "get" is called each time when list item is drawn */ abstract protected void get(int index, //!< Number of requested list item ListItem item //!< Data of list item. Fill this object with item data. ); Font getQuickFont(int style) { switch (style) { case Font.STYLE_BOLD: return FONT_STYLE_BOLD; case Font.STYLE_PLAIN: return FONT_STYLE_PLAIN; case Font.STYLE_ITALIC: return FONT_STYLE_ITALIC; } return Font.getFont(Font.FACE_SYSTEM, style, fontSize); } // returns height of draw area in pixels protected int getDrawHeight() { int menuBartHeight;//#sijapp cond.if target="RIM" | target="DEFAULT"# menuBartHeight = 0;//#sijapp cond.else# menuBartHeight = getMenuBarHeight();//#sijapp cond.end# return getHeightInternal() - getCapHeight() - menuBartHeight; } //! Init static Fonts private void initFonts() { FONT_STYLE_BOLD = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, fontSize); FONT_STYLE_PLAIN = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, fontSize); FONT_STYLE_ITALIC = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, fontSize); } //! Sets new font size and invalidates items public void setFontSize(int value) { if (fontSize == value) return; fontSize = value; initFonts(); fontHeight = FONT_STYLE_PLAIN.getHeight(); checkTopItem(); invalidate(); } public void setCyclingCursor(boolean value) { cyclingCursor = value; } public int getGameAction(int keyCode) { return virtualCanvas.getGameAction(keyCode); } public void repaint() { if (isActive()) virtualCanvas.repaint(); } int capImagesHeight = 0; public void setCapImage(Image[] images) { if (capImages == images) return; capImages = images; if (images == null) capImagesHeight = 0; else { capImagesHeight = 0; for (int i = 0; i < images.length; i++) { if (images[i] == null) continue; int ih = images[i].getHeight(); if (ih > capImagesHeight) capImagesHeight = ih; } } invalidate(0, 0, getWidth(), getCapHeight()); }//#sijapp cond.if target!="DEFAULT"# public static void setBackImage(Image image, boolean cave) { if (backImage == image) return; backImage = image; caveBgImage = cave; if (backImage == null) return; int width = image.getWidth(); int height = image.getHeight(); int[] pixels = new int[width*height]; int size = (width < height) ? width : height; if (size == 0) return; image.getRGB(pixels, 0, width, 0, 0, width, height); long total = 0; for (int i = 0; i < size; i++) { int pixel = pixels[i*width+i]; int r = pixel&0xFF; int g = (pixel >> 8)&0xFF; int b = (pixel >> 16)&0xFF; total += (r+g+b); } total /= size; backImageInvColor = (total > 300) ? 0x000000 : 0xFFFFFF; } public static Image getBackImage() { return backImage; } static private int backImageInvColor; //#sijapp cond.end# static public int checkTextColor(int color) {//#sijapp cond.if target!="DEFAULT"# if (backImage == null) return color; if (getInverseColor(color) == backImageInvColor) return backImageInvColor;//#sijapp cond.end# return color; } public void setVLCommands(VirtualListCommands vlCommands) { this.vlCommands = vlCommands; } public static VirtualList getCurrent() { return virtualCanvas.isShown() ? virtualCanvas.currentControl : null; } public void setColors(int capTxt, int capbk, int bkgrnd, int cursor, int text, int crsFrame, int cursorAlpha, int menuAlpha) { this.capBkCOlor = capbk; this.capTxtColor = capTxt; this.bkgrndColor = bkgrnd; this.cursorColor = cursor;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -