📄 listshow.java
字号:
package mome.ext;import java.util.Vector;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;/** * @author Sergio Morozov * @version 0.9 */public class ListShow{ /** * @since 0.9 */ public static final int DEFAULT_SELECTED_BGCOLOR = 0xFFFFFF; /** * @since 0.9 */ public static final int DEFAULT_SELECTED_FGCOLOR = 0x0; /** * @since 0.9 */ public static final int DEFAULT_FGCOLOR = 0xFFFFFF; public static final int DEFAULT_BGCOLOR = 0x0; private int x = 0; private int y = 0; private int width = 0; private int height = 0; private Vector items = new Vector(); private Vector itemFonts = new Vector(); private int selected = 0; private int bgColor = DEFAULT_BGCOLOR; private int fgColor = DEFAULT_FGCOLOR; private int selectedBGColor = DEFAULT_SELECTED_BGCOLOR; private int selectedFGColor = DEFAULT_SELECTED_FGCOLOR; private int startPageItem = 0; private int endPageItem = 0; private boolean paintDown = true; public ListShow(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public ListShow(int x, int y, int width, int height, String[] items, Font[] fonts) { this(x, y, width, height); if (items == null) throw new NullPointerException("items"); if (fonts == null) throw new NullPointerException("itemFonts"); int length = items.length; int fontsLength = fonts.length; if (fontsLength > length) throw new IllegalArgumentException( "size of fonts > size of items: " + fontsLength + " > " + length); for (int i = 0; i < length; i++) if (i < fontsLength) this.append(items[i], fonts[i]); else this.append(items[i]); } public void setBGColor(int color) { this.bgColor = color; } public void setFGColor(int color) { this.fgColor = color; } public void setSelectedBGColor(int color) { this.selectedBGColor = color; } public void setSelectedFGColor(int color) { this.selectedFGColor = color; } public void append(String item, Font font) { if (item == null) throw new NullPointerException("item"); if (font == null) font = Font.getDefaultFont(); this.items.addElement(item); this.itemFonts.addElement(font); } public void append(String item) { this.append(item, null); } public void delete(int pos) { this.items.removeElementAt(pos); this.itemFonts.removeElementAt(pos); if (pos <= this.startPageItem) { this.startPageItem--; this.paintDown = true; } if (pos <= this.selected) { this.selected--; this.paintDown = true; } if (pos <= this.endPageItem) { this.endPageItem--; this.paintDown = true; } } public void deleteAll() { this.items.removeAllElements(); this.itemFonts.removeAllElements(); this.selected = 0; this.startPageItem = 0; this.endPageItem = 0; this.paintDown = true; } public void set(int pos, String item, Font font) { if (item == null) throw new NullPointerException("item"); if (font == null) font = Font.getDefaultFont(); this.items.setElementAt(item, pos); this.itemFonts.setElementAt(font, pos); } public boolean isSelected(int pos) { return pos == this.selected; } public int getSelectedIndex() { return (this.items.size() > 0) ? this.selected : -1; } public String getSelected() { return (this.items.size() > 0) ? (String) this.items .elementAt(this.selected) : null; } public boolean next() { boolean res = false; if (this.selected < this.items.size() - 1) { res = true; if (++this.selected > this.endPageItem) { this.paintDown = false; this.endPageItem = this.selected; } } return res; } public boolean prev() { boolean res = false; if (this.selected > 0) { res = true; if (--this.selected < this.startPageItem) { this.paintDown = true; this.startPageItem = this.selected; } } return res; } public void previous() { if (this.selected > 0) this.selected--; } public void show(Graphics g) { int clipX = g.getClipX(); int clipY = g.getClipY(); int clipW = g.getClipWidth(); int clipH = g.getClipHeight(); g.setClip(this.x, this.y, this.width, this.height); int itemHeight; g.setColor(this.bgColor); g.fillRect(this.x, this.y, this.width, this.height); g.setColor(this.fgColor); if (this.paintDown) { int iLenght = this.items.size(); int curY = this.y; for (int i = this.startPageItem; (i < iLenght) && (curY - this.y + (itemHeight = ((Font) this.itemFonts.elementAt(i)).getHeight()) <= this.height); i++) { String item = (String) this.items.elementAt(i); Font font = (Font) this.itemFonts.elementAt(i); g.setFont(font); int itemWidth = font.stringWidth(item); if (i == this.selected) { g.setColor(this.selectedBGColor); g.fillRect(this.x, curY, itemWidth + 4, itemHeight); g.setColor(this.selectedFGColor); g.drawString(item, this.x + 2, curY, Graphics.TOP | Graphics.LEFT); g.setColor(this.fgColor); } else g.drawString((String) this.items.elementAt(i), this.x + 2, curY, Graphics.TOP | Graphics.LEFT); curY += itemHeight; this.endPageItem = i; } if (this.endPageItem < this.items.size() - 1) g.drawString( (String) this.items.elementAt(this.endPageItem + 1), this.x + 2, curY, Graphics.TOP | Graphics.LEFT); } else { int curY = this.y + this.height; for (int i = this.endPageItem; i >= 0 && curY - (itemHeight = ((Font) this.itemFonts.elementAt(i)).getHeight()) >= this.y; i--) { String item = (String) this.items.elementAt(i); Font font = (Font) this.itemFonts.elementAt(i); g.setFont(font); int itemWidth = font.stringWidth(item); if (i == this.selected) { g.setColor(this.selectedBGColor); g.fillRect(this.x, curY - itemHeight, itemWidth + 4, itemHeight); g.setColor(this.selectedFGColor); g.drawString((String) this.items.elementAt(i), this.x + 2, curY, Graphics.BOTTOM | Graphics.LEFT); g.setColor(this.fgColor); } else g.drawString((String) this.items.elementAt(i), this.x + 2, curY, Graphics.BOTTOM | Graphics.LEFT); curY -= itemHeight; this.startPageItem = i; } if (this.startPageItem > 0) g.drawString((String) this.items .elementAt(this.startPageItem - 1), this.x + 2, curY, Graphics.BOTTOM | Graphics.LEFT); } g.setClip(clipX, clipY, clipW, clipH); } /** * @return the y * @since 0.9 */ public int getY() { return this.y; } /** * @param y * the y to set * @since 0.9 */ public void setY(int y) { this.y = y; } /** * @return the height * @since 0.9 */ public int getHeight() { return this.height; } /** * @return the width * @since 0.9 */ public int getWidth() { return this.width; } /** * @return the x * @since 0.9 */ public int getX() { return this.x; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -