⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 popmenu.java

📁 最强手机阅读器Anyview3.0版的界面代码
💻 JAVA
字号:
package com.ismyway.fairyui;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Graphics;import com.ismyway.anyview.Anyview;import com.ismyway.anyview.others.Settings;import com.ismyway.util.Theme;public class PopMenu extends Panel {	private static int itemHeight;	private MenuItem[] items = null;	public int currentIndex = 0;	public int topIndex = 0;	private int maxItem = 6;	private Callback callbackPop = null;	public static boolean alignLeft = true;	private static int itemspace = Anyview.hasPointerEvents ? 8 : 4;	public PopMenu(Callback panel, MenuItem[] items) {		callbackPop = panel;		this.items = items;	}	private static String getMenuTitle(MenuItem item) {		return item.getText();	}	public final static boolean hasSubMenu(MenuItem item) {		return item.getSubItems() != null;	}	/**	 * 取得当前菜单项最大的宽度	 * @return	 */	private int getMaxMenuWidth() {		int maxWidth = 0;		for (int i = 0; i < items.length; i++) {			MenuItem item = items[i];			String title = getMenuTitle(item);			boolean hasSubMenu = hasSubMenu(item);			int len = 0;			if (hasSubMenu) {				len = getFont().stringWidth(title) + 8;			} else {				len = getFont().stringWidth(title);			}			if (len > maxWidth) {				maxWidth = len;			}		}		int w = 0;		w = maxWidth + Settings.MENUSIDEBARWIDTH + Settings.MENUITEMRPAD;		return w;	}	public char getShortcutChar(int index) {		int num = index + 1;		if (num <= 9) {			return Integer.toString(num).charAt(0);		} else if (num == 10) {			return '*';		} else if (num == 11) {			return '#';		} else {			return ' ';		}	}	/**	 * 向缓冲中绘制	 * @param g	 * @param adjustx 左坐标的调整值	 * @param adjusty 顶点坐标的调整值	 */	public void paint(Graphics g, int adjustx, int adjusty) {		//System.out.println("popup menu " + adjustx + ", " + adjusty);		if (null == items) {			return;		}		g.setColor(Theme.Background);		g.fillRect(getLeft() - adjustx, getTop() - adjusty, getWidth(), getHeight());		g.setColor(Theme.BorderColor);		g.drawRect(getLeft() - adjustx, getTop() - adjusty, getWidth() - 1, getHeight() - 1);		g.setColor(Theme.ComponentLight);		g.fillRect(getLeft() - adjustx + 1, getTop() - adjusty + 1, Settings.MENUSIDEBARWIDTH, getHeight() - 2);		int menuHeight = itemHeight * items.length;		if (menuHeight > getHeight()) {			for (int i = 0; i < maxItem; i++) {				paintItem(g, i + topIndex, adjustx, adjusty - 5);			}			int tx = getLeft() + (getWidth() >> 1) - adjustx;			int ty = getTop() - adjusty;			if (topIndex != 0) {				Theme.drawArrow(g, Theme.Arrow, tx - 3, ty + 2, Theme.UP);			}			if (topIndex != items.length - maxItem) {				ty += getHeight();				Theme.drawArrow(g, Theme.Arrow, tx - 3, ty - 6, Theme.DOWN);			}		} else {			for (int i = 0; i < items.length; i++) {				paintItem(g, i, adjustx, adjusty);			}		}	}	private void paintItem(Graphics g, int itemindex, int adjustx, int adjusty) {		String suffix = "";		suffix = getShortcutChar(itemindex) + ".";		int left = getLeft() - adjustx;		int top = getTop() + itemHeight * (itemindex - topIndex) - adjusty;		String title = getMenuTitle(items[itemindex]);		g.setFont(getFont());		if (itemindex == currentIndex) {			Theme.drawSelectedBackground(g, left + 1, top + 1, getWidth() - 2, itemHeight - 2);			if (items[itemindex].isEnable()) {				g.setColor(Theme.TextLight);			} else {				g.setColor(Theme.ComponentLight);			}			g.drawString(suffix, left + 3, top + itemspace / 2, ANCHOR);			g.drawString(title, left + 3 + Settings.MENUSIDEBARWIDTH, top + itemspace / 2, ANCHOR);		} else {			if (items[itemindex].isEnable()) {				g.setColor(Theme.TextColor);			} else {				g.setColor(Theme.DisabledColor);			}			g.drawString(suffix, left + 3, top + itemspace / 2, ANCHOR);			g.drawString(title, left + 3 + Settings.MENUSIDEBARWIDTH, top + itemspace / 2, ANCHOR);		}		int hcenter = itemHeight >> 1;		if (hasSubMenu(items[itemindex])) {			Theme.drawArrow(g, Theme.Arrow, left + getWidth() - 7, top + hcenter - 3, Theme.RIGHT);		}	}	public void setMenus(MenuItem[] items) {		this.items = items;	}	/**	 * 计算当前菜单合适的高与宽	 */	public void validate() {		//System.out.println("popmenu.validate()");		itemHeight = getFont().getHeight() + itemspace;		MainCanvas canvas = MainCanvas.getInstance();		//		System.out.println("currentMenus = " + currentMenus.length);		int menuWidth = getMaxMenuWidth();		int menuHeight = itemHeight * items.length;		if (menuHeight > canvas.getHeight() - 20) {			maxItem = ((canvas.getHeight() - 30) / itemHeight);//10是预留给上下滚动箭头的绘制空间高度;上下各3个像素			menuHeight = itemHeight * maxItem + 10;		} else {			maxItem = items.length;		}		menuWidth = menuWidth < 60 ? 60 : menuWidth;		setWidth(menuWidth);		setHeight(menuHeight);		currentIndex = 0;		topIndex = 0;		//System.out.println(getLeft() + ", " + getTop());	}	public boolean keyRepeated(int keyCode) {		return keyReleased(keyCode);	}	public boolean keyReleased(int keyCode) {		if (keyCode == Settings.VKEY_RSOFT) {			MainCanvas.getInstance().closePopup();			return false;		}		int index = -1;		//System.out.println("keyCode = " + keyCode);		if (keyCode == Settings.VKEY_POUND) { //#			index = 11;		} else if (keyCode == Settings.VKEY_STAR) { //*			index = 10;		} else if (keyCode == Settings.VKEY_NUM1) { //1			index = 1;		} else if (keyCode == Settings.VKEY_NUM2) { //2			index = 2;		} else if (keyCode == Settings.VKEY_NUM3) { //3			index = 3;		} else if (keyCode == Settings.VKEY_NUM4) { //4			index = 4;		} else if (keyCode == Settings.VKEY_NUM5) { //5			index = 5;		} else if (keyCode == Settings.VKEY_NUM6) { //6			index = 6;		} else if (keyCode == Settings.VKEY_NUM7) { //7			index = 7;		} else if (keyCode == Settings.VKEY_NUM8) { //8			index = 8;		} else if (keyCode == Settings.VKEY_NUM9) { //9			index = 9;		}		if (index == -1 || index > items.length) {		} else {			currentIndex = index - 1;			if (items[currentIndex].isEnable()) {				if (hasSubMenu(items[currentIndex])) {					openPop(Settings.ANIMATE_NONE);				} else {					execute(currentIndex);				}			}			return false;		}		keyCode = Settings.mapKey(keyCode);		if (keyCode == Settings.VKEY_UP) {			currentIndex--;			if (currentIndex < 0) {				currentIndex = items.length - 1;				topIndex = items.length - maxItem;			}			if (currentIndex < topIndex) {				topIndex = currentIndex;			}		} else if (keyCode == Settings.VKEY_DOWN) {			currentIndex++;			if (currentIndex > items.length - 1) {				currentIndex = topIndex = 0;			}			if (currentIndex > topIndex + maxItem - 1) {				topIndex = currentIndex - maxItem + 1;			}		} else if (keyCode == Settings.VKEY_LEFT) {			MainCanvas.getInstance().closePopup();		} else if (keyCode == Settings.VKEY_RIGHT) {			if (hasSubMenu(items[currentIndex]) && items[currentIndex].isEnable()) {				openPop(Settings.ANIMATE_RIGHT);			}		} else if (keyCode == Settings.VKEY_FIRE) {			if (hasSubMenu(items[currentIndex])) {				openPop(Settings.ANIMATE_RIGHT);			} else {				execute(currentIndex);			}		} else if (keyCode == Settings.VKEY_LSOFT) {			if (hasSubMenu(items[currentIndex])) {				openPop(Settings.ANIMATE_RIGHT);			} else {				execute(currentIndex);			}		} else if (keyCode == Settings.VKEY_RSOFT) {			MainCanvas.getInstance().closePopup();		} else if (keyCode == Settings.VKEY_NUM0) {			MainCanvas.getInstance().closePopup();		} else if (keyCode == Settings.VKEY_NUM2) {			currentIndex--;			if (currentIndex < 0) {				currentIndex = items.length - 1;				topIndex = items.length - maxItem;			}			if (currentIndex < topIndex) {				topIndex = currentIndex;			}		} else if (keyCode == Settings.VKEY_NUM4) {			MainCanvas.getInstance().closePopup();		} else if (keyCode == Settings.VKEY_NUM6) {			if (hasSubMenu(items[currentIndex])) {				openPop(Settings.ANIMATE_RIGHT);			}		} else if (keyCode == Settings.VKEY_NUM8) {			currentIndex++;			if (currentIndex > items.length - 1) {				currentIndex = topIndex = 0;			}			if (currentIndex > topIndex + maxItem - 1) {				topIndex = currentIndex - maxItem + 1;			}		} else if (keyCode == Settings.VKEY_NUM5) {			if (hasSubMenu(items[currentIndex])) {				openPop(Settings.ANIMATE_RIGHT);			} else {				execute(currentIndex);			}		}		//repaint();		return true;	}	//弹出菜单	private void openPop(int method) {		MainCanvas mainCanvas = MainCanvas.getInstance();		MenuItem[] items = this.items[currentIndex].getSubItems();		PopMenu menu = new PopMenu(callbackPop, items);		menu.validate();		int x, y;		x = getLeft() + getWidth() - 10;		if (x + menu.getWidth() > mainCanvas.getWidth()) {			x = mainCanvas.getWidth() - menu.getWidth();		}		y = mainCanvas.getHeight() - menu.getHeight() - 20;		if (y > getTop() + itemHeight * currentIndex) {			y = getTop();		}		menu.setLocation(x, y);		mainCanvas.showPopup(menu, method);	}	private void execute(int index) {		if (null != callbackPop && items[index].isEnable()) {			mainCanvas.closePopMenu();			callbackPop.callback(this, new Command(items[index].getText(), Command.OK, 1), new Object[] { items[index]					.getObj() });		}	}	public boolean pointerPressed(int x, int y) {		//		System.out.println(x + ", " + y);		//		System.out.println("rect: " + getLeft() + ", " + getTop() + ", " + getWidth() + ", "		//				+ getHeight());		if (x > getLeft() && x < getLeft() + getWidth() && y > getTop() && y < getTop() + getHeight()) {			currentIndex = (y - getTop()) / itemHeight + topIndex;			//System.out.println(currentIndex);		}		lastPointerX = x;		lastPointerY = y;		return true;		/*if (items[currentIndex].isEnable()) {		 if (hasSubMenu(items[currentIndex])) {		 openPop(Settings.ANIMATE_NONE);		 } else {		 execute(currentIndex);		 }		 } */	}	public boolean pointerReleased(int x, int y) {		if (lastPointerX > getLeft() && lastPointerX < getLeft() + getWidth() && lastPointerY > getTop()				&& lastPointerY < getTop() + getHeight()) { //上次在菜单中			if (x > getLeft() && x < getLeft() + getWidth() && y > getTop() && y < getTop() + getHeight()) {				int index = (y - getTop()) / itemHeight + topIndex;				if (index == currentIndex) { //在同一item上按下和释放					if (items[currentIndex].isEnable()) {						if (hasSubMenu(items[currentIndex])) {							openPop(Settings.ANIMATE_NONE);						} else {							execute(currentIndex);						}					}				}				return false;			} else { //菜单之外,不处理				return false;			}		} else { //上次不在菜单中			if (x > getLeft() && x < getLeft() + getWidth() && y > getTop() && y < getTop() + getHeight()) {				currentIndex = (y - getTop()) / itemHeight + topIndex;				return true;			} else {				mainCanvas.closePopup();				return false;			}		}	}}

⌨️ 快捷键说明

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