?? textlist.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/TextList.java Version: ###VERSION### Date: ###DATE### Author(s): Artyomov Denis*******************************************************************************/package DrawControls;import java.util.Enumeration;import java.util.Hashtable;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import javax.microedition.lcdui.*;import DrawControls.VirtualList;import DrawControls.ListItem;class TextItem{ byte[] imgNumsAndTimes; Image[] image; String text; int color = 0; short font; short aniStepOrWidth; short timeCounterOrHeight; int getHeight(int fontSize) { if (image != null) return image[0].getHeight(); if (text == null) return 0; if (timeCounterOrHeight == 0) { Font font = Font.getFont(Font.FACE_SYSTEM, this.font, fontSize); timeCounterOrHeight = (short)font.getHeight(); } return timeCounterOrHeight; } int getWidth(int fontSize) { if (image != null) return image[0].getWidth(); if (text == null) return 0; if (aniStepOrWidth == 0) { Font font = Font.getFont(Font.FACE_SYSTEM, this.font, fontSize); aniStepOrWidth = (short)font.stringWidth(text); } return aniStepOrWidth; } int getColor() { return color&0x00FFFFFF; } void setColor(int value) { if ((color&0xFF000000) == 0) color = value; } int getFontStyle() { return font; } void setFontStyle(int value) { font = (short)value; } boolean replaceImage(Image from, Image to) { if (image == null) return false; if (image[0] == from) { image[0] = to; return true; } return false; } void setIndexedColor(int color, int value) { if ((this.color&0xFF000000) == color) this.color = color|value; }}class TextLine{ private Vector items = new Vector(); int height = -1; int bigTextIndex = -1; char last_charaster; TextItem elementAt(int index) { return (TextItem)items.elementAt(index); } void add(TextItem item) { items.addElement(item); } int getHeight(int fontSize) { if (height == -1) { height = fontSize; int currHeight; for (int i = items.size() - 1; i >= 0; i--) { currHeight = elementAt(i).getHeight(fontSize); if (currHeight > height) height = currHeight; } } return height; } int getWidth(int fontSize) { int width = 0; for (int i = items.size() - 1; i >= 0; i--) width += elementAt(i).getWidth(fontSize); return width; } void setItemColor(int value) { for (int i = items.size() - 1; i >= 0; i--) elementAt(i).setColor(value); } void paint(int xpos, int ypos, Graphics g, int fontSize, VirtualList vl, boolean nextAniStep) { int count = items.size(); int itemHeight = getHeight(fontSize); TextItem item; int drawYPos; int imgIndex; Image img; for (int i = 0; i < count; i++) { item = elementAt(i); drawYPos = ypos + (itemHeight - item.getHeight(fontSize))/2; if (item.image != null) { imgIndex = (item.imgNumsAndTimes == null) ? 0 : item.imgNumsAndTimes[2*item.aniStepOrWidth]; img = item.image[imgIndex]; if (g != null) g.drawImage(img, xpos, drawYPos, Graphics.TOP | Graphics.LEFT); if (nextAniStep && item.image.length > 1) { item.timeCounterOrHeight++; if (item.timeCounterOrHeight > item.imgNumsAndTimes[2*item.aniStepOrWidth+1]) { item.timeCounterOrHeight = 0; item.aniStepOrWidth++; if (item.aniStepOrWidth >= item.imgNumsAndTimes.length/2) item.aniStepOrWidth = 0; vl.getCanvas().repaint(xpos, drawYPos, img.getWidth(), img.getHeight()); } } } else if (item.text != null && !nextAniStep && g != null) { g.setColor(item.getColor()); g.setFont(vl.getQuickFont(item.getFontStyle())); g.drawString(item.text, xpos, drawYPos, Graphics.TOP | Graphics.LEFT); } xpos += item.getWidth(fontSize); } } int size() { return items.size(); } void readText(StringBuffer buffer) { for (int i = 0; i < items.size(); i++) buffer.append(elementAt(i).text); } boolean replaceImages(Image from, Image to) { boolean replaced = false; for (int i = items.size()-1; i >= 0; i--) replaced |= elementAt(i).replaceImage(from, to); return replaced; } void setIndexedColor(int color, int value) { for (int i = items.size()-1; i >= 0; i--) elementAt(i).setIndexedColor(color, value); } }//! Text list/*! This class store text and data of lines internally You may use it to show text with colorised lines :) */public class TextList extends VirtualList implements Runnable{ private boolean alwaysShowCursor; private boolean animated; private static TimerTask aniTimerTask; private static Timer aniTimer = new Timer(); private Vector lines = new Vector(); // Vector of lines. Each line contains cols. Col can be text or image private Hashtable indexedColors = new Hashtable(); //! Construct new text list public TextList ( String capt, //!< Caption of list int capTextColor, //!< Text color of caption int backColor, //!< Background color of list int fontSize, /*!< Font size for list lines and caption. Can be VirtualList.SMALL_FONT, VirtualList.MEDIUM_FONT or VirtualList.LARGE_FONT */ int cursorMode //!< Cursor mode. Can be VirtualList.SEL_INVERTED, VirtualList.SEL_DOTTED, VirtualList.SEL_NONE ) { super(capt, capTextColor, backColor, fontSize, cursorMode); } //! Construct new text list with default values of colors, font size etc... public TextList(String capt) { super(capt); } // protected int getSize() public int getSize() { if (lines.isEmpty()) return 0; int size = lines.size(); return (((TextLine) lines.lastElement()).size() == 0) ? size - 1 : size; } public void setAlwaysShowCursor(boolean value) { alwaysShowCursor = value; } private TextLine getLine(int index) { return (TextLine) lines.elementAt(index); } protected boolean isItemSelected(int index) { int selIndex = getCurrIndex(); int textIndex = (selIndex >= lines.size() || selIndex < 0) ? -1 : getLine(selIndex).bigTextIndex; if (textIndex == -1) return false; return (getLine(index).bigTextIndex == textIndex); } // protected void get(int index, ListItem item) protected void get(int index, ListItem item) { TextLine listItem = getLine(index); item.clear(); if (listItem.size() == 0) return; TextItem titem = listItem.elementAt(0); item.text = titem.text; item.color = titem.getColor(); item.fontStyle = titem.getFontStyle(); titem = null; } //! Remove all lines form list public void clear() { lines.removeAllElements(); setCurrentItem(0); animated = false; resetAnimationTask(); invalidate(); } //! Add new text item to list public void add(String text, //!< Text of new item int color, //!< Color of new item int imageIndex /*!< Index of image in images list. You must use setImageList to set images for list lines */ ) { internAdd(text, color, imageIndex, Font.STYLE_PLAIN, -1, true, '\0'); invalidate(); } //! Add new text item to list public void add(String text, //!< Text of new item int color, //!< Color of new item int imageIndex, /*!< Index of image in images list. You must use setImageList to set images for list lines */ int fontStyle //!< Text font style. See MID profile for details ) { internAdd(text, color, imageIndex, fontStyle, -1, true, '\0'); invalidate(); } private void internAdd(String text, int color, int imageIndex, int fontStyle, int textIndex, boolean doCRLF, char last_charaster) { TextItem newItem = new TextItem(); newItem.text = text; newItem.setColor(color); newItem.setFontStyle(fontStyle); if (lines.isEmpty()) lines.addElement(new TextLine()); TextLine textLine = (TextLine) lines.lastElement(); textLine.add(newItem); textLine.bigTextIndex = textIndex; if (doCRLF) { textLine.last_charaster = last_charaster; TextLine newLine = new TextLine(); newLine.bigTextIndex = textIndex; lines.addElement(newLine); } } //! Add new black text item to list public void add(String text //!< Text of new item ) { add(text, this.getTextColor(), -1); } public int getItemHeight(int itemIndex) { if (getCursorMode() != CURSOR_MODE_DISABLED) return super.getItemHeight(itemIndex); if (itemIndex >= lines.size()) return 1; return getLine(itemIndex).getHeight(getFontSize()); } // Overrides VirtualList.drawItemData protected void drawItemData(Graphics g, int index, int x1, int y1, int x2, int y2, int paintMode) { if (getCursorMode() != CURSOR_MODE_DISABLED) { super.drawItemData(g, index, x1, y1, x2, y2, paintMode); return; } TextLine line = getLine(index); line.paint(borderWidth, y1, g, getFontSize(), this, paintMode == DMS_CUSTOM); } // Overrides VirtualList.moveCursor protected void moveCursor(int step, boolean moveTop) { int size, currTextIndex, lastCurItem; int halfSize = (getDrawHeight()/getFontHeight())/2; switch (step) { case -1: case 1: lastCurItem = currItem; currTextIndex = getCurrTextIndex(); size = getSize(); storelastItemIndexes(); // Find next item TextLine item; for (; halfSize > 0; halfSize--) { currItem += step; if ((currItem < 0) || (currItem >= size)) break; item = getLine(currItem); if ((currTextIndex != item.bigTextIndex) && (item.bigTextIndex != -1)) { currTextIndex = item.bigTextIndex; break; } } // Find next item last line if ((halfSize != 0) && (currItem >= 0) && (currItem < size)) { for (; halfSize > 0; halfSize--) { currItem += step; if ((currItem < 0) || (currItem >= size)) { currItem -= step; break; } item = getLine(currItem); if (currTextIndex != item.bigTextIndex) { currItem -= step; break; } } } item = null; checkCurrItem(); checkTopItem(); checkTopItem(); if (alwaysShowCursor && getLine(currItem).bigTextIndex == -1) currItem = lastCurItem; repaintIfLastIndexesChanged(); break; default: super.moveCursor(step, moveTop); return; } }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -