package me.com.kaikai.load;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.geom.Rectangle;
import com.sun.lwuit.plaf.Style;
public class Text_Label extends Component
{
 
   
     /*  标题白色:rgb = 255 255 255
         正文黑色:rgb = 86 86 86  //565656
         正文蓝色:rgb = 63  107 169//3f6ba9
         正文灰色:rgb = 189 189 189//bdbdbd
         分割线颜色:rgb = 224 224 224//e0e0e0
       */
 
  /******************TextArea用到的字段****************************/
     private String text;                //存储当前的文本内容
     private int rowsGap = 2;   
     private Font font =null;   
    /*******************************用户信息************************************/
   
    
       private String name="sdfsdfdsfds";
       private int nameColor=0X3f6ba9;
      
       private String title="sdfdsfsdfsd";
       private int titleColor=0X565656;
      
       private String tail="sdffssd";
       private int tailColor=0Xbdbdbd;      
       private Image pic; //图片绘制
     
       private Vector vec=new Vector();
       private Node temp=null;
   
       int leftPadding =this.getStyle().getPadding(isRTL(), Component.LEFT); 
       int rightPadding =this.getStyle().getPadding(isRTL(), Component.RIGHT);
       int topPadding =this.getStyle().getPadding(false, Component.TOP);
     
       int oWidth=0;     //宽度 剪切板
       int oX=0;
       int oY=0;
       int oHight=0;
      
       int  rowcount=0;   //可以自动换行的变量行号
    int rowwOrection=0;//移动的方向,也代表动态提取的数据
    boolean Next=true;//向下翻页时用到此变量
   
     public Text_Label(){
         this("",Display.getInstance().getDisplayWidth());       
      }
    
     public Text_Label(String text){
        this(text,Display.getInstance().getDisplayWidth()); 
      
     }
     public Text_Label(String text,int width,int maxRow){
      this(text,width); 
            
     }    
     public Text_Label(String text,int width){              //这个是基类         
      this.setFocus(true);                      //获得焦点
         this.getStyle().setBorder(null);                   //没有边框
         this.setScrollSize(new Dimension(0,0));                  //设置无滚动条
         this.getStyle().setPadding(0, 0, 4, 15);
         try {
    pic=Image.createImage("/img/girl_001.gif");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } //图片绘制
   
   String[]a=new String[]{"赵海洋","张磊","刘彩霞","慌忙海鸥岛","山口野狼"};
   String[]context=new String[]{"今天太阳出来的很早","黄宏伟今天又迟到了,他老是这样子,真是受不了他。要屏蔽他很难南","天啊,真的有这等是,哈哈哈接视频搜水电费等额问题过热隔热而过热二氧化碳日一天一天如图菊花台有沿途教育厅","问题过热隔热而过热二氧化碳日一天一天如图菊花台有沿途教育厅"};
   String[]tail=new String[]{"2010年20月10号@金鸡湖大道","2010年20月13号","2010年23月13号@金鸡湖大道","金鸡湖大道"};
   for(int i=0;i<20;i++)
   {
    temp=new Node();
    temp.setName(a[i%a.length]+i);
    temp.setContext(context[i%context.length]);
    temp.setTime(tail[i%tail.length]);
    vec.addElement(temp);
   }
   
     } 
     public void setText(String t)                               //为当前文本域赋值
     {
         this.text = (t != null) ? t : "";                  
     }   
     public int getRowsGap()
     {
      return rowsGap;
     }
    
     public void paint(Graphics g) {       
      //rowcount=0;
      
         //int typeCOunt=0;//翻页效果的实现,即如果没有实现最大花的话则不翻页
      
       if(rowwOrection<5)
       {
         for(int i=0,charTempCount=0;i<4;i++)//vec.size()
             {
             temp=(Node)vec.elementAt(i) ;
             charTempCount+=drawTextArea(g,temp.getName(),temp.getContext(),temp.getTime(),charTempCount); 
             }    
        
       }
       else
       {        
         for(int i=rowwOrection-4,charTempCount=0;i<rowwOrection;i++)//vec.size()
            {
            temp=(Node)vec.elementAt(i) ;
            charTempCount+=drawTextArea(g,temp.getName(),temp.getContext(),temp.getTime(),charTempCount); 
            }    
       }
     }  
    public int drawTextArea(Graphics g,String name,String title,String tail,int rowcount)
    { 
     int temprowCount=1;//默认的为一行    
     if(oWidth==0)  
       oWidth=g.getClipWidth(); 
     if(oX==0)
      oX=g.getClipX();
     if(oY==0)
      oY=g.getClipY();
     if(oHight==0)
      oHight=g.getClipHeight();
    
       g.setFont(Font.createSystemFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM));        
          font = getStyle().getFont();        
         int fontHeight = font.getHeight();       
                       //定义行号       
         int x=getX()+leftPadding;//字体的左边界x坐标位置,是个常量
         int y =getY()+topPadding +(getRowsGap() + fontHeight) * rowcount;     
         if(Rectangle.intersects(x-pic.getWidth()-7, y,getWidth(),fontHeight,oX,oY,oWidth,oHight)) {
       
            /************绘制头像*******************/
                 if(pic!=null)
                 g.drawImage(pic,1,y);  
            /***************************/
                
        /********************画文字******************************/
             /*******************画姓名***********************/          
             x+=pic.getWidth()+7;            
             g.setColor(nameColor);
           g.drawString(name,x ,y);
       /*********************over*************************/
        
      /******************画标题内容********************/
             g.setColor(titleColor);            
           int TextWidth=oWidth-pic.getWidth()-7-leftPadding;  //文字的宽度  -rightPadding右边距没有截掉
         // System.out.println("-----实际面板宽度:-"+oWidth);
          // System.out.println("-----实际可用字段的宽度:-"+TextWidth);
       if(x+font.charWidth('海')+font.stringWidth(name)+font.stringWidth(title)>TextWidth)//如果超出界限
       {
          char[]sub=title.toCharArray();         
           int tempCharCountWidth=0;      //字符的总宽度
           int tempCharStart=0;          //字符数组的下标初始位置
          
           for(int i=0;i<sub.length-1;i++)
            {
            tempCharCountWidth+=font.charWidth(sub[i]);
          
            if(tempCharStart==0)
             {
             if(tempCharCountWidth<TextWidth-font.stringWidth(name)&&tempCharCountWidth+font.charWidth(sub[i+1])>TextWidth-font.stringWidth(name))
             {
              g.drawString(title.substring(tempCharStart,i),x+font.charWidth('海')+font.stringWidth(name),y); 
               tempCharCountWidth=0;                      //字体总高度设置为0
               tempCharStart=i;
               continue;
             }
                }
          
            if(tempCharCountWidth<=TextWidth&&tempCharCountWidth+font.charWidth(sub[i+1])>TextWidth)
           {
                g.drawString(title.substring(tempCharStart,i),x,y+=(getRowsGap() + fontHeight));             
             tempCharCountWidth=0;//字体总高度设置为0
             tempCharStart=i;
            temprowCount++;
             continue;
           }
           
             if(i==sub.length-2)
           {
                    temprowCount++;
              g.drawString(title.substring(tempCharStart,i+2),x,y+=(getRowsGap() + fontHeight)); 
           }       
            }            
          }
         else//如果没有超出界限
           g.drawString(title,x+font.charWidth('海')+font.stringWidth(name),y);       
       
        g.setFont(Font.createSystemFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL));
       
         font=g.getFont();
        fontHeight = font.getHeight();   
      
        g.setColor(tailColor);
          g.drawString(tail,x,y+=(getRowsGap()+fontHeight));            
         
          g.setColor(0Xe0e0e0);
          g.drawLine(0,y+=(getRowsGap()+fontHeight), getX()+oWidth,y);
         
          temprowCount+=2;
         }   
         //System.out.println("行数:"+temprowCount);
        
         return temprowCount;
     }
    
     public void keyPressed(int code)
  {
      if(code==-1&&rowwOrection>0)
      {
       rowcount++;      
       rowwOrection--;
       Next=false;
      }
      else if(code==-2&&rowwOrection<vec.size())
      {
        rowcount--;       
        rowwOrection++;
        System.out.println("row:"+rowcount+";  rowOrection:"+rowwOrection);
      
      }      
      }
}
class Node
{
 private boolean isReply=false;//是否为回复
 private Image selfImage=null;//自己的头像
 private String context=null;//当前的内容
 private String Name=null;//自己的名字
 private String addRess=null;//地址
 private String time=null;//时间
 public String getContext() {
  return context;
 }
 public void setContext(String context) {
  this.context = context;
 }
 public boolean isReply() {
  return isReply;
 }
 public void setReply(boolean isReply) {
  this.isReply = isReply;
 }
 public String getName() {
  return Name;
 }
 public void setName(String name) {
  Name = name;
 }
 public Image getSelfImage() {
  return selfImage;
 }
 public void setSelfImage(Image selfImage) {
  this.selfImage = selfImage;
 }
 public String getTime() {
  return time;
 }
 public void setTime(String time) {
  this.time = time;
 }
}