- /*
 - * Copyright 1999,2004 The Apache Software Foundation.
 - *
 - * Licensed under the Apache License, Version 2.0 (the "License");
 - * you may not use this file except in compliance with the License.
 - * You may obtain a copy of the License at
 - *
 - * http://www.apache.org/licenses/LICENSE-2.0
 - *
 - * Unless required by applicable law or agreed to in writing, software
 - * distributed under the License is distributed on an "AS IS" BASIS,
 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 - * See the License for the specific language governing permissions and
 - * limitations under the License.
 - */
 - package peaklau.eaglefund;
 - import javax.microedition.lcdui.CommandListener;
 - import javax.microedition.lcdui.Graphics;
 - /**
 - * ScrollScreen class
 - *
 - * This class can display text with vertical scroll bar.<br>
 - * The line numbers will show on the top title.<br>
 - *
 - * <br>
 - *
 - *
 - * @author peaklau <br>
 - * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
 - * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
 - * @version $Revision: 1.1 $ $Date: 2007/05/09 16:07:04 $
 - */
 - public class ScrollScreen extends TitleScreen implements CommandListener {
 - private int SCREEN_LINE_COUNT=0;
 - private String[] lines=null;
 - private int startLine=0;
 - private int lineCount=0;
 - private String title="";
 - /**
 - *
 - * @param title
 - * @param txt xxxx\r\n<RGB>xxxx\r\nbbbbbbbbb
 - * @param backDisplayable
 - * @param navigator
 - */
 - public ScrollScreen(String title,String txt,String backDisplayable,INavigator navigator){
 - super(title,backDisplayable,navigator);
 - this.title=title;
 - setTxt(txt);
 - SCREEN_LINE_COUNT=(height-2*Line_HIGHT-1)/FONT_HEIGHT;
 - }
 - public void setTxt(String txt){
 - if(txt!=null){
 - lineCount=1;
 - int index=0;
 - while((index=txt.indexOf("\r\n",index))>0){
 - index+=2;
 - lineCount++;
 - }
 - lines=new String[lineCount];
 - index=0;
 - int tmp=0;
 - for(int i=0;i<lineCount;i++){
 - tmp=txt.indexOf("\r\n",index);
 - if(tmp>0){
 - lines[i]=txt.substring(index,tmp);
 - }else{
 - lines[i]=txt.substring(index);
 - break;
 - }
 - index=tmp+2;
 - }
 - }else{
 - lineCount=0;
 - txt="";
 - }
 - repaint();
 - }
 - private int getLineColor(int index){
 - //System.out.println("getLineColor index="+index+" startLine="+startLine);
 - if((index+startLine)>=lineCount){
 - return 0x00000000;
 - }
 - String tmp=lines[index+startLine].toLowerCase();
 - if(tmp.indexOf("<")>=0){
 - int color=0;
 - for(int i=1;i<9;i++){
 - char c=tmp.charAt(i);
 - if((c>='a')&&(c<='f')){
 - color=color*16+(int)c-96+9;
 - }else{
 - color=color*16+(int)c-48;
 - }
 - }
 - //System.out.println(color);
 - return color;
 - }else{
 - return 0x00000000;
 - }
 - }
 - private String getLine(int index){
 - //System.out.println("getLine index="+index+" startLine="+startLine);
 - if((index+startLine)>=lineCount){
 - return "";
 - }
 - String tmp=lines[index+startLine];
 - if(tmp.indexOf("<")>=0){
 - tmp=tmp.substring(10);
 - }
 - return tmp;
 - }
 - public void showNotify(){
 - if(startLine>=lineCount){
 - startLine=0;
 - }
 - super.showNotify();
 - }
 - public void hideNotify(){
 - super.hideNotify();
 - }
 - protected void paint(Graphics g){
 - int sstartLine=startLine+1;
 - int eendLine=(startLine+1+SCREEN_LINE_COUNT)>lineCount?lineCount:(startLine+1+SCREEN_LINE_COUNT);
 - setTitle(title+"("+sstartLine+"/"+lineCount+"-"+eendLine+"/"+lineCount+")");
 - super.paint(g);
 - //txt
 - for(int i=0;i<SCREEN_LINE_COUNT;i++){
 - g.setColor(getLineColor(i));
 - g.drawString(getLine(i),0,Line_HIGHT*(i+1),Graphics.LEFT|Graphics.TOP);
 - }
 - }
 - protected void keyPressed(int keyCode) {
 - super.keyPressed(keyCode);
 - switch (getGameAction(keyCode)) {
 - case LEFT:
 - case UP:
 - if(startLine>0){
 - startLine--;
 - }
 - repaint();
 - break;
 - case RIGHT:
 - case DOWN:
 - if(startLine<(lineCount-2)){
 - startLine++;
 - }
 - repaint();
 - break;
 - default:
 - break;
 - }
 - }
 - }