1. /*
  2. * Copyright 1999,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package peaklau.eaglefund;
  17. import javax.microedition.lcdui.CommandListener;
  18. import javax.microedition.lcdui.Graphics;
  19. /**
  20. * ScrollScreen class
  21. *
  22. * This class can display text with vertical scroll bar.<br>
  23. * The line numbers will show on the top title.<br>
  24. *
  25. * <br>
  26. *
  27. *
  28. * @author peaklau <br>
  29. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  30. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  31. * @version $Revision: 1.1 $ $Date: 2007/05/09 16:07:04 $
  32. */
  33. public class ScrollScreen extends TitleScreen implements CommandListener {
  34. private int SCREEN_LINE_COUNT=0;
  35. private String[] lines=null;
  36. private int startLine=0;
  37. private int lineCount=0;
  38. private String title="";
  39. /**
  40. *
  41. * @param title
  42. * @param txt xxxx\r\n<RGB>xxxx\r\nbbbbbbbbb
  43. * @param backDisplayable
  44. * @param navigator
  45. */
  46. public ScrollScreen(String title,String txt,String backDisplayable,INavigator navigator){
  47. super(title,backDisplayable,navigator);
  48. this.title=title;
  49. setTxt(txt);
  50. SCREEN_LINE_COUNT=(height-2*Line_HIGHT-1)/FONT_HEIGHT;
  51. }
  52. public void setTxt(String txt){
  53. if(txt!=null){
  54. lineCount=1;
  55. int index=0;
  56. while((index=txt.indexOf("\r\n",index))>0){
  57. index+=2;
  58. lineCount++;
  59. }
  60. lines=new String[lineCount];
  61. index=0;
  62. int tmp=0;
  63. for(int i=0;i<lineCount;i++){
  64. tmp=txt.indexOf("\r\n",index);
  65. if(tmp>0){
  66. lines[i]=txt.substring(index,tmp);
  67. }else{
  68. lines[i]=txt.substring(index);
  69. break;
  70. }
  71. index=tmp+2;
  72. }
  73. }else{
  74. lineCount=0;
  75. txt="";
  76. }
  77. repaint();
  78. }
  79. private int getLineColor(int index){
  80. //System.out.println("getLineColor index="+index+" startLine="+startLine);
  81. if((index+startLine)>=lineCount){
  82. return 0x00000000;
  83. }
  84. String tmp=lines[index+startLine].toLowerCase();
  85. if(tmp.indexOf("<")>=0){
  86. int color=0;
  87. for(int i=1;i<9;i++){
  88. char c=tmp.charAt(i);
  89. if((c>='a')&&(c<='f')){
  90. color=color*16+(int)c-96+9;
  91. }else{
  92. color=color*16+(int)c-48;
  93. }
  94. }
  95. //System.out.println(color);
  96. return color;
  97. }else{
  98. return 0x00000000;
  99. }
  100. }
  101. private String getLine(int index){
  102. //System.out.println("getLine index="+index+" startLine="+startLine);
  103. if((index+startLine)>=lineCount){
  104. return "";
  105. }
  106. String tmp=lines[index+startLine];
  107. if(tmp.indexOf("<")>=0){
  108. tmp=tmp.substring(10);
  109. }
  110. return tmp;
  111. }
  112. public void showNotify(){
  113. if(startLine>=lineCount){
  114. startLine=0;
  115. }
  116. super.showNotify();
  117. }
  118. public void hideNotify(){
  119. super.hideNotify();
  120. }
  121. protected void paint(Graphics g){
  122. int sstartLine=startLine+1;
  123. int eendLine=(startLine+1+SCREEN_LINE_COUNT)>lineCount?lineCount:(startLine+1+SCREEN_LINE_COUNT);
  124. setTitle(title+"("+sstartLine+"/"+lineCount+"-"+eendLine+"/"+lineCount+")");
  125. super.paint(g);
  126. //txt
  127. for(int i=0;i<SCREEN_LINE_COUNT;i++){
  128. g.setColor(getLineColor(i));
  129. g.drawString(getLine(i),0,Line_HIGHT*(i+1),Graphics.LEFT|Graphics.TOP);
  130. }
  131. }
  132. protected void keyPressed(int keyCode) {
  133. super.keyPressed(keyCode);
  134. switch (getGameAction(keyCode)) {
  135. case LEFT:
  136. case UP:
  137. if(startLine>0){
  138. startLine--;
  139. }
  140. repaint();
  141. break;
  142. case RIGHT:
  143. case DOWN:
  144. if(startLine<(lineCount-2)){
  145. startLine++;
  146. }
  147. repaint();
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. }