Back to index

wims  3.65+svn20090927
Line.java
Go to the documentation of this file.
00001 package rene.viewer;
00002 
00003 import java.awt.*;
00004 
00005 public class Line
00006 {   TextDisplay TD;
00007        boolean Chosen;
00008        int Pos,Posend;
00009        int Block; // block state
00010        static final int NONE=0,START=1,END=2,FULL=4; // block states (logically or'ed)
00011        Color C,IC; // Color of line and high key color of the line
00012        char a[]; // Contains the characters of this line
00013        
00014        public Line (String s, TextDisplay td)
00015        {      this(s,td,Color.black);
00016        }
00017        
00025        public Line (String s, TextDisplay td, Color c)
00026     {  TD=td;
00027        C=c;
00028        // Create a color that is very bright, but resembles the line color
00029        IC=new Color(C.getRed()/4+192,C.getGreen()/4+192,C.getBlue()/4+192);
00030        Block=NONE;
00031        a=s.toCharArray();
00032     }
00033     
00034     public void expandTabs (int tabwidth)
00035     {  int pos=0;
00036        for (int i=0; i<a.length; i++)
00037        {      pos++;
00038               if (a[i]=='\t') pos=(pos/tabwidth+1)*tabwidth;
00039        }
00040        char b[]=new char[pos];
00041        pos=0;
00042        for (int i=0; i<a.length; i++)
00043        {      if (a[i]=='\t')
00044               {      int newpos=((pos+1)/tabwidth+1)*tabwidth;
00045                      for (int k=pos; k<newpos; k++) b[k]=' ';
00046                      pos=newpos;
00047               }
00048               else b[pos++]=a[i];
00049        }
00050        a=b;   
00051     }
00052     int length ()
00053     {  return a.length;
00054     }
00055     int getpos (int x, int offset)
00056     {  int l[]=TD.getwidth(a);
00057        int h=offset-TD.Offset*TD.FM.charWidth(' ');
00058        if (x<h) return 0;
00059        int i=0;
00060        while (x>h && i<a.length)
00061        {      h+=l[i];
00062               i++;
00063        }
00064        return i;
00065     }
00066     
00076        public void draw (Graphics g, int x, int y)
00077     {  int i1=0,i2,p=0;
00078        x-=TD.Offset*TD.FM.charWidth(' ');
00079        if (Chosen) // Complete line is chosen (in Lister)
00080        {      // To see, if the display has the focus:
00081               if (TD.hasFocus()) g.setColor(Color.darkGray);
00082               else g.setColor(Color.gray);
00083               g.fillRect(0,y-TD.Ascent,TD.getSize().width,TD.Height);
00084               g.setColor(IC); // draw in light color
00085                      TD.antialias(false);
00086               g.drawChars(a,0,a.length,x,y);
00087                      TD.antialias(true);
00088        }
00089        else if ((Block&FULL)!=0) // Line in full block (in Viewer)
00090        {      g.setColor(Color.darkGray);
00091               g.fillRect(x,y-TD.Ascent,TD.FM.charsWidth(a,0,a.length),TD.Height);
00092               g.setColor(Color.white);
00093               TD.antialias(false);
00094               g.drawChars(a,0,a.length,x,y);
00095               TD.antialias(true);
00096        }
00097        else if ((Block&START)!=0)
00098        {      if (Pos>0) // Draw text before block
00099               {      g.setColor(C);
00100                      g.drawChars(a,0,Pos,x,y);
00101                      x+=TD.FM.charsWidth(a,0,Pos);
00102               }
00103               if ((Block&END)!=0) // draw text in block
00104               {      if (Posend>Pos)
00105                      {      int h=TD.FM.charsWidth(a,Pos,Posend-Pos);
00106                             g.setColor(Color.darkGray);
00107                             g.fillRect(x,y-TD.Ascent,h,TD.Height);
00108                             g.setColor(Color.white);
00109                                    TD.antialias(false);
00110                             g.drawChars(a,Pos,Posend-Pos,x,y);
00111                                    TD.antialias(true);
00112                             g.setColor(C);
00113                             x+=h;
00114                             if (a.length>Posend)
00115                             {      g.drawChars(a,Posend,a.length-Posend,x,y);
00116                             }
00117                      }
00118                      else g.drawChars(a,Pos,a.length-Pos,x,y);
00119               }
00120               else // draw the rest of the line in block 
00121               {      int h=TD.FM.charsWidth(a,Pos,a.length-Pos);
00122                      g.setColor(Color.darkGray);
00123                      g.fillRect(x,y-TD.Ascent,h,TD.Height);
00124                      g.setColor(Color.white);
00125                             TD.antialias(false);
00126                      g.drawChars(a,Pos,a.length-Pos,x,y);      
00127                             TD.antialias(true);
00128               }
00129        }
00130        else if ((Block&END)!=0)
00131        {      int h=TD.FM.charsWidth(a,0,Posend);
00132               g.setColor(Color.darkGray);
00133                      g.fillRect(x,y-TD.Ascent,h,TD.Height);
00134                      g.setColor(Color.white);
00135                      TD.antialias(false);
00136               g.drawChars(a,0,Posend,x,y);       
00137                      TD.antialias(true);
00138               g.setColor(C);
00139               x+=h;
00140               if (a.length>Posend)
00141               {      g.drawChars(a,Posend,a.length-Posend,x,y);
00142               }
00143        }
00144        else
00145        {      g.setColor(C);
00146               g.drawChars(a,0,a.length,x,y);
00147        }
00148     }
00149     void append (String s)
00150     {  a=(new String(a)+s).toCharArray();
00151     }
00152     void chosen (boolean f)
00153     {  Chosen=f;
00154     }
00155     public boolean chosen () { return Chosen; }
00156     void block (int pos, int mode)
00157     {  switch (mode)
00158        {      case NONE : Block=NONE; break;
00159               case FULL : Block=FULL; break;
00160               case START :
00161                      Block|=START; Pos=pos; break;
00162               case END :
00163                      Block|=END; Posend=pos; break;
00164        }
00165     }
00166     String getblock ()
00167     {  if (Block==FULL)
00168        {      return new String(a,0,a.length); 
00169        }
00170        else if ((Block&START)!=0)
00171        {      if ((Block&END)!=0)
00172               {      return new String(a,Pos,Posend-Pos);
00173               }
00174               else return new String(a,Pos,a.length-Pos);
00175        }
00176        else if ((Block&END)!=0) return new String(a,0,Posend);
00177        else return "";
00178     }
00179 }