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

📄 panel.java

📁 最强手机阅读器Anyview3.0版的界面代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.ismyway.fairyui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.ismyway.anyview.others.Configure;
import com.ismyway.anyview.others.Settings;
import com.ismyway.anyview.win.AlertPanel;
import com.ismyway.util.ArrayList;
import com.ismyway.util.Theme;

public abstract class Panel extends Component implements CommandListener, Callback {
	//protected ArrayList components = new ArrayList(); //当前窗口内所有的ROW控件
	protected ArrayList hotpots = new ArrayList(); //当前窗口内所有可点击的热点
	protected Command leftCmd = null, rightCmd = null; //左、右软键对应的事件
	protected Panel leftPopup = null, rightPopup = null; //左、右软键对应的弹出菜单,可为NULL
	protected String title = ""; //窗口的标题
	protected boolean showTitle = true; //是否显示标题
	protected boolean showBottom = true; //是否显示命令栏
	protected Image logo = null; //标题栏上显示的logo
	protected static MainCanvas mainCanvas = null;
	protected int internalHeight = 0; //当前视图的实际高度
	protected int verticalOffset = 0; //当前Y坐标的偏移
	protected int viewportHeight = 0; //当前panel的高度
	protected boolean showscrollbar = false; //是否显示滚动条
	protected int bgColor = Theme.Background; //当前窗口的背景颜色,默认为白色
	protected int animateDir = Settings.ANIMATE_NONE; //当前窗口平滑滚动的方向
	private int scrollCount = 0; //平滑滚动的帧数
	public int scrollbarHeight = 16; //滚动条的高度
	protected int currentPointer = -1; //当前所停留的热点	
	private int scrollbartop = 0; //当前滚动条停留的Y坐标
	private boolean recycle = false; //是否允许热点循环
	protected String rightString = ""; //显示在右软键位置的文字
	protected Callback callback = null; //回调
	//protected Panel from = null; //从哪个Panel来的
	protected AlertPanel alert = null; //警告的panel
	//protected int adjustX = 0, adjuxtY = 0; //调整的X、Y值

	//触摸屏使用的变量
	protected Component pressedComponent = null; //触摸屏按下时点击到的控件
	protected boolean pointerPressed = false; //触摸屏按下时
	protected long pointerPressedtime = 0; //触摸屏按下时的时间
	protected boolean pointerInscrollbar = false; //是否在滚动条上
	protected int lastPointerX = 0, lastPointerY = 0; //屏幕最后停留的X,Y坐标

	public Panel() {
		this(null, null);
	}

	public Panel(Callback callback) {
		this(callback, null);
	}

	public Panel(Callback callback, Panel from) {
		this.callback = callback;
		//this.from = from;
		mainCanvas = MainCanvas.getInstance();
		setLeft(0);
		setTop(0);
		setWidth(mainCanvas.getWidth());
		setHeight(mainCanvas.getHeight());
		setCommandListener(this);
	}

	public boolean isShowBottom() {
		return showBottom;
	}

	public void setShowBottom(boolean showBottom) {
		this.showBottom = showBottom;
	}

	public boolean isShowTitle() {
		return showTitle;
	}

	public void setShowTitle(boolean showTitle) {
		this.showTitle = showTitle;
	}

	/**
	 * 设置窗口的背景颜色
	 * @param bgColor
	 */
	public void setBgColor(int bgColor) {
		this.bgColor = bgColor;
	}

	protected void paint(Graphics g, int adjustx, int adjusty) {
		g.setColor(bgColor);

		int fillx = getLeft() - adjustx;
		fillx = fillx < 0 ? 0 : fillx;
		if (fillx > mainCanvas.getWidth()) { //已出界,无需绘图
			return;
		}

		int filly = getTop() - adjusty;
		filly = filly < 0 ? 0 : filly;
		if (filly > mainCanvas.getHeight()) { //已出界,无需绘图
			return;
		}

		int fillw = mainCanvas.getWidth() - fillx;
		fillw = fillw < getWidth() ? fillw : getWidth();

		int fillh = mainCanvas.getHeight() - filly;
		fillh = fillh < getHeight() ? fillh : getHeight();

		//		System.out.println("fill[x ,y, w, h] = (" + fillx + ", " + filly + ", "
		//				+ fillw + ", " + fillh + "), internalHeight = "
		//				+ internalHeight + ", adjusty = " + adjusty
		//				+ ", getHeight() = " + getHeight());
		g.fillRect(fillx, filly, fillw, fillh);

		//绘制滚动条
		if (showscrollbar) {
			drawScrollbar(g, adjustx, adjusty);
		}

		//		int start = findSuitableRow(getTop() + adjusty);
		//		if (start > -1) {
		//			System.out.println("start = " + start);
		//		}

		for (int i = 0; i < components.size(); ++i) {
			Row row = (Row) components.get(i);

			if (row.getInnerTop() + row.getHeight() < adjusty) {
				continue;
			}
			if (row.getInnerTop() - adjusty > getHeight() - getBottomHeight()) {
				break;
			}
			//			System.out.println("row[" + i + "]:" + row.getClass().getName()
			//					+ ", " + (adjusty + verticalOffset));
			row.paint(g, getLeft() + adjustx, getTop() + adjusty);
		}

		paintTitleBottom(g, getLeft() + adjustx, getTop() + adjusty);
	}

	//	public int findSuitableRow(int height) { //二分法查找
	//		int index = 0; //相当于指针的东西
	//		int iStart = 0; //
	//		int iEnd = rows.size() - 1;
	//		while (true) {
	//			index = (iStart + iEnd) / 2;
	//			Row row = (Row) rows.get(index);
	//			if (row.getTop() + row.getHeight() < height) {
	//				iStart = index;
	//			} else if (row.getTop() > height + viewportHeight) {
	//				iEnd = index;
	//			} else {
	//				break;
	//			}
	//		}
	//		return index;
	//	}

	public void paint(Graphics g) {
		paint(g, 0, verticalOffset);
	}

	public void paintTitleBottom(Graphics g, int adjustx, int adjusty) {
		int bottomOffset = getBottomHeight();
		//补画被挡住的状态栏
		g.setFont(getFont());
		if (showTitle) {
			int textyoff = getTop() + (getTopHeight() - getFont().getHeight()) / 2;
			Theme.drawTitleBar(g, getLeft(), getTop(), mainCanvas.getWidth());
			int textxoff = 2;
			if (null != logo) {
				g.drawImage(logo, getLeft() + textxoff, getTop() + 2, LEFTTOP);
				textxoff += 18;
			}
			g.setColor(Theme.TextLight);
			g.drawString(title, getLeft() + textxoff, textyoff, ANCHOR);
		}

		if (showBottom) {
			int yoffset = getHeight() - bottomOffset;
			int textyoff = getTop() + yoffset + (bottomOffset - getFont().getHeight()) / 2;
			Theme.drawTitleBar(g, getLeft(), getTop() + yoffset, mainCanvas.getWidth());
			if (leftCmd != null) {
				String str = leftCmd.getLabel();
				g.setColor(Theme.TextLight);
				g.drawString(str, getLeft() + 2, textyoff, ANCHOR);
				//g.drawImage(Theme.BarShade, getFont().stringWidth(str) + 4, yoffset + 1, LEFTTOP);
				/*if (Anyview.hasPointerEvents && null != rightCmd) {
				 g.setColor(Theme.TextLight);
				 g.drawLine(getWidth() / 2, yoffset + 2, getWidth() / 2, yoffset + 16);
				 }*/
			}

			g.setColor(Theme.TextLight);
			g.drawString(rightString, mainCanvas.getWidth() - getFont().stringWidth(rightString) - 3, textyoff, ANCHOR);
			//				g.drawImage(Theme.BarShade, MainCanvas.getInstance().getWidth()
			//						- getFont().stringWidth(str) - 5, textyoff, LEFTTOP);
		}
	}

	public synchronized int add(Row row) {
		row.setParent(this);
		components.add(row);
		int id = components.size() - 1;
		row.set_componentID(id);
		return id;
	}

	public boolean remove(Row cmp) {
		currentPointer = -1;
		boolean found = components.remove(cmp);
		if (found) {
			internalHeight -= cmp.getHeight();
		}
		return found;
	}

	public void remove(int pos) {
		currentPointer = -1;
		if (pos >= 0 && pos < components.size()) {
			Row c = (Row) components.get(pos);
			internalHeight -= c.getHeight();
			components.remove(pos);
		}
	}

	public boolean keyRepeated(int key) {
		System.out.println("keyRepeated = " + key);

		//转换按键
		key = Settings.mapKey(key);

		boolean repaint = false;

		if (key == Settings.VKEY_UP || key == Settings.VKEY_NUM2) { //上
			if (1 > currentPointer) {
				//分两种情况判断
				if (hotpots.size() == 0 || !recycle) {//1、整个panel根本就没有热点或不允许热点循环
					if (showscrollbar) {
						verticalOffset -= getFont().getHeight() + Component.HORPADDING;
						verticalOffset = verticalOffset < 0 ? 0 : verticalOffset;
					}
				} else { //当前热点在第一项上
					int n = hotpots.size() - 1;
					Component c = (Component) hotpots.get(n);
					((Component) hotpots.get(currentPointer)).setSelected(false);
					c.setSelected(true);
					currentPointer = n;

					if (!componentInScreen(c) || c.getTop() < getTop() + verticalOffset + getTopHeight()) { //不在当前屏幕内
						verticalOffset = c.getTop() + c.getHeight() - viewportHeight - getTopHeight();
						//System.out.println("verticalOffset = " + verticalOffset + ", c.getHeight() = " + c.getHeight());
						verticalOffset = verticalOffset < 0 ? 0 : verticalOffset;
					}
				}
				return true;
			} else {
				int n = currentPointer - 1;
				Component c = (Component) hotpots.get(n);
				if (!componentInScreen(c) || c.getTop() < getTop() + verticalOffset + getTopHeight()) { //不在当前屏幕内,不管
					verticalOffset -= getFont().getHeight() + Component.HORPADDING;
					verticalOffset = verticalOffset < 0 ? 0 : verticalOffset;
					findPointerInCurrentScreen(false);
				} else {
					((Component) hotpots.get(currentPointer)).setSelected(false);
					c.setSelected(true);
					currentPointer = n;
					if (showscrollbar && verticalOffset > c.getTop()) {
						verticalOffset = c.getTop();
					}
				}
				return true;
			}
		} else if (key == Settings.VKEY_DOWN || key == Settings.VKEY_NUM8) { //下
			if (currentPointer > hotpots.size() - 2) { //整个panel根本就没有热点
				//分两种情况判断
				if (hotpots.size() == 0 || !recycle) { //
					if (showscrollbar) {
						verticalOffset += getFont().getHeight() + Component.HORPADDING;
						verticalOffset = verticalOffset > internalHeight - viewportHeight ? internalHeight
								- viewportHeight : verticalOffset;
					}
				} else {
					int n = 0;
					Component c = (Component) hotpots.get(n);
					((Component) hotpots.get(currentPointer)).setSelected(false);
					c.setSelected(true);
					currentPointer = n;

					verticalOffset = 0;
					//					if (showscrollbar) {
					//						verticalOffset = c.getTop();
					//						if (internalHeight - c.getTop() < viewportHeight) {
					//							verticalOffset = internalHeight - viewportHeight;
					//						}
					//					} else {
					//						verticalOffset = 0;
					//					}
				}
				return true;
			} else {
				int n = currentPointer + 1;
				Component c = (Component) hotpots.get(n);
				if (!componentInScreen(c)) { //不在当前屏幕内,不管
					verticalOffset += getFont().getHeight() + Component.HORPADDING;
					verticalOffset = verticalOffset > internalHeight - viewportHeight ? internalHeight - viewportHeight
							: verticalOffset;
					findPointerInCurrentScreen(true);

				} else {
					((Component) hotpots.get(currentPointer)).setSelected(false);
					c.setSelected(true);
					currentPointer = n;
				}
				return true;
			}
		} else if (key == Settings.VKEY_FIRE || key == Settings.VKEY_NUM5) {
			if (currentPointer == -1) {
				return false;
			}
			Component c = (Component) hotpots.get(currentPointer);
			c.setCommandListener(this);
			if (c instanceof SpinButton) {
				return c.keyRepeated(key);
			}
			return false;
		} else if (key == Settings.VKEY_LEFT || key == Settings.VKEY_NUM4) {
			if (animateDir == Settings.ANIMATE_NONE) {
				if (viewportHeight < internalHeight) {
					scrollCount = Settings.SCROLL_COUNT;
					animateDir = Settings.ANIMATE_UP;
				}
			}
		} else if (key == Settings.VKEY_RIGHT || key == Settings.VKEY_NUM6) {
			//System.out.println("here......");
			if (animateDir == Settings.ANIMATE_NONE) {
				if (viewportHeight < internalHeight) {
					scrollCount = Settings.SCROLL_COUNT;
					animateDir = Settings.ANIMATE_DOWN;
				}
			}
		} else if (key == Settings.VKEY_LSOFT || key == Settings.VKEY_STAR) {

		} else if (key == Settings.VKEY_RSOFT || key == Settings.VKEY_POUND) {

		} else if (key == Settings.VKEY_RETURN) {
			vkeyReturn();
		} else if (key == Settings.VKEY_DELETE) {
			vkeyDelete();
		}
		return repaint;
	}

	/**
	 * 返回true表示当前panel已经处理了该按键,并且要求重绘
	 */
	public boolean keyReleased(int key) {
		System.out.println("panel key: " + key + ", hotpots = " + hotpots.size());
		//		if (rows.size() < 1) {
		//			return true;
		//		}

		if (executeShortCut(key)) {
			return true;
		}

		//转换按键
		key = Settings.mapKey(key);

		boolean repaint = false;

		if (key == Settings.VKEY_UP || key == Settings.VKEY_NUM2) { //上

			if (1 > currentPointer) {
				//分两种情况判断
				if (hotpots.size() == 0 || !recycle) {//1、整个panel根本就没有热点或不允许热点循环
					if (showscrollbar) {
						verticalOffset -= getFont().getHeight() + Component.HORPADDING;
						verticalOffset = verticalOffset < 0 ? 0 : verticalOffset;
					}
				} else { //当前热点在第一项上
					int n = hotpots.size() - 1;
					Component c = (Component) hotpots.get(n);

					((Component) hotpots.get(currentPointer)).setSelected(false);
					c.setSelected(true);
					currentPointer = n;

					if (!componentInScreen(c) || c.getTop() < getTop() + verticalOffset + getTopHeight()) { //不在当前屏幕内
						verticalOffset = c.getTop() + c.getHeight() - viewportHeight - getTopHeight();
						//System.out.println("verticalOffset = " + verticalOffset + ", c.getHeight() = " + c.getHeight());
						verticalOffset = verticalOffset < 0 ? 0 : verticalOffset;
					}
				}
				return true;
			} else {

⌨️ 快捷键说明

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