Back to index

wims  3.65+svn20090927
ListerPanel.java
Go to the documentation of this file.
00001 /*
00002  * Created on 14.01.2006
00003  *
00004  */
00005 package rene.lister;
00006 
00007 import java.awt.*;
00008 import java.awt.event.*;
00009 import java.io.PrintWriter;
00010 import java.util.*;
00011 
00012 import rene.gui.*;
00013 import rene.util.*;
00014 
00015 public class ListerPanel
00016        extends MyPanel implements WheelListener
00017 {
00018               private MyVector V; // Vector of listed Elements
00019               int Top; // Top Element
00020        
00021               Image I; // Buffer Image
00022               int W,H; // width and height of current panel and image
00023               Graphics IG; // Graphics for the image
00024               Font F; // current font
00025               FontMetrics FM; // current font metrics
00026               int Leading,Height,Ascent,Descent; // font stuff
00027               int PageSize; // numbers of lines per page
00028               int HOffset; // horizontal offset of display
00029               boolean ShowLast; // Show last on next redraw
00030               
00031               Lister LD;
00032               String Name;
00033               
00034               public Color ListingBackground=null;
00035               
00036               public boolean MultipleSelection=true; // Allow multiple selection
00037               public boolean EasyMultipleSelection=false; // Multiple select without right mouse
00038               public boolean ReportSingleClick=false; // Report single clicks also
00039               public boolean RightMouseClick=false; // Report right mouse clicks also
00040               
00041               public ListerPanel (Lister ld, String name)
00042               {      LD=ld; Name=name;
00043                      V=new MyVector();
00044                      Top=0;
00045                      Wheel W=new Wheel(this);
00046                      addMouseWheelListener(W);
00047                      addMouseListener(new MouseAdapter()
00048                             {      public void mouseClicked (MouseEvent e)
00049                                    {      clicked(e);
00050                                    }
00051                             }
00052                      );
00053               }
00054               
00055               public ListerPanel (Lister ld)
00056               {      this(ld,"");
00057               }
00058               
00064               public synchronized void paint (Graphics g)
00065               {      Dimension d=getSize();
00066                      if (I==null || I.getWidth(this)!=d.width ||
00067                                    I.getHeight(this)!=d.height)
00068                      {      I=createImage(W=d.width,H=d.height);
00069                             if (I==null) return;
00070                             IG=I.getGraphics();
00071                             init();
00072                      }
00073                      dopaint(IG);
00074                      g.drawImage(I,0,0,W,H,this);
00075                      double vp,vs,hp,hs;
00076                      if (V.size()>1) vp=(double)Top/V.size();
00077                      else vp=0;
00078                      if (V.size()>2*PageSize) vs=(double)PageSize/V.size();
00079                      else vs=0.5;
00080                      if (HOffset<10*W) hp=(double)HOffset/(10*W);
00081                      else hp=0.9;
00082                      hs=0.1;
00083                      LD.setScrollbars(vp,vs,hp,hs);
00084               }
00085               
00086               public void update (Graphics g)
00087               {      paint(g);     
00088               }
00089               
00094               synchronized void init ()
00095               {      F=getFont();
00096                      FM=getFontMetrics(F);
00097                      Leading=FM.getLeading()+Global.getParameter("fixedfont.spacing",-1);
00098                      Height=FM.getHeight();
00099                      Ascent=FM.getAscent();
00100                      Descent=FM.getDescent();
00101                      if (Global.Background!=null) setBackground(Global.Background);
00102                      if (Height+Leading>0)
00103                             PageSize=H/(Height+Leading);
00104                      else
00105                             PageSize=10;
00106                      antialias(true);
00107                      Top=0;
00108               }
00109 
00115               public void antialias (boolean flag)
00116               {      if (Global.getParameter("font.smooth",true))
00117                      {      IG=(Graphics2D)IG;
00118                             ((Graphics2D)IG).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
00119                                    flag?RenderingHints.VALUE_TEXT_ANTIALIAS_ON:
00120                                           RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
00121                      }
00122               }
00123 
00128               public synchronized void dopaint (Graphics g)
00129               {      if (ShowLast)
00130                      {      Top=V.size()-PageSize+1;
00131                             if (Top<0) Top=0;
00132                             ShowLast=false;
00133                      }
00134                      if (ListingBackground!=null) g.setColor(ListingBackground); 
00135                      else g.setColor(getBackground());
00136                      g.fillRect(0,0,W,H);
00137                      g.setColor(Color.black);
00138                      int h=Leading+Ascent;
00139                      int totalh=getSize().height-Descent;
00140                      int line=Top;
00141                      if (line<0) return;
00142                      while (line-Top<PageSize && line<V.size())
00143                      {      Element el=(Element)V.elementAt(line);
00144                             if (isSelected(line))
00145                             {      g.setColor(getBackground().darker());
00146                                    g.fillRect(0,h-Ascent,W,Height);
00147                                    g.setColor(Color.black);
00148                             }
00149                             Color col=el.getElementColor();
00150                             if (col!=null) g.setColor(col);
00151                             else g.setColor(Color.black);
00152                             g.drawString(el.getElementString(State),2-HOffset,h);
00153                             h+=Leading+Height;
00154                             line++;
00155                      }
00156               }
00157               
00158               int State=0;
00159               public void setState (int s)
00160               {      State=s;
00161               }
00162 
00167               public synchronized void add (Element e)
00168               {      V.addElement(e);
00169               }
00170               
00171               // Used by the mouse wheel or external programs:
00172               
00173               public synchronized void up (int n) 
00174               {      Top+=n;
00175                      if (Top>=V.size()) Top=V.size()-1;
00176                      if (Top<0) Top=0;
00177                      repaint();
00178               }
00179 
00180               public synchronized void down (int n) 
00181               {      Top-=n;
00182                      if (Top<0) Top=0;
00183                      repaint();
00184               }
00185 
00186               public synchronized void pageUp() 
00187               {      up(PageSize-1);
00188                      repaint();
00189               }
00190 
00191               public synchronized void pageDown() 
00192               {      down(PageSize-1);
00193                      repaint();
00194               }      
00195 
00201               public synchronized void setVerticalPos (double x)
00202               {      Top=(int)(x*V.size());
00203                      if (Top>=V.size()) Top=V.size()-1;
00204                      repaint();
00205               }
00206               
00211               public synchronized void setHorizontalPos (double x)
00212               {      HOffset=(int)(x*10*W);
00213                      repaint();
00214               }
00215               
00219               public synchronized void clear ()
00220               {      Selected.removeAllElements();
00221                      V.removeAllElements();
00222                      Top=0;
00223               }
00224               
00228               public synchronized void showLast ()
00229               {      ShowLast=true;
00230               }
00231               
00232               // Mouse routines:
00233               
00234               Vector VAL=new Vector(); // Vector of action listener
00235               MyVector Selected=new MyVector(); // currently selected items
00236               
00242               public synchronized boolean isSelected (int sel)
00243               {      Enumeration e=Selected.elements();
00244                      while (e.hasMoreElements())
00245                      {      int n=((Integer)e.nextElement()).intValue();
00246                             if (n==sel) return true;
00247                      }
00248                      return false;
00249               }
00250               
00255               public synchronized void toggleSelect (int sel)
00256               {      Enumeration e=Selected.elements();
00257                      while (e.hasMoreElements())
00258                      {      Integer i=(Integer)e.nextElement();
00259                             if (i.intValue()==sel)
00260                             {      Selected.removeElement(i);
00261                                    return;
00262                             }
00263                      }
00264                      Selected.addElement(new Integer(sel));
00265               }
00266               
00272               public synchronized void expandSelect (int sel)
00273               {      // compute maximal selected index below sel.
00274                      int max=-1;
00275                      Enumeration e=Selected.elements();
00276                      while (e.hasMoreElements())
00277                      {      int i=((Integer)e.nextElement()).intValue();
00278                             if (i>max && i<sel) max=i;
00279                      }
00280                      if (max>=0)
00281                      {      for (int i=max+1; i<=sel; i++) select(i);
00282                             return;
00283                      }
00284                      int min=V.size();
00285                      e=Selected.elements();
00286                      while (e.hasMoreElements())
00287                      {      int i=((Integer)e.nextElement()).intValue();
00288                             if (i<min && i>sel) min=i;
00289                      }
00290                      if (min<V.size())
00291                      {      for (int i=sel; i<=min; i++) select(i);
00292                      }
00293               }
00294               
00299               public synchronized void select (int sel)
00300               {      if (!isSelected(sel))
00301                             Selected.addElement(new Integer(sel));
00302               }
00303 
00304               
00309               public void addActionListener (ActionListener al)
00310               {      VAL.addElement(al);
00311               }
00312               
00317               public void removeActionListener (ActionListener al)
00318               {      VAL.removeElement(al);
00319               }
00320               
00329               public void clicked (MouseEvent e)
00330               {      int n=e.getY()/(Leading+Height);
00331                      if (e.isMetaDown() && RightMouseClick)
00332                      {      Enumeration en=VAL.elements();
00333                             while (en.hasMoreElements())
00334                             {      ((ActionListener)(en.nextElement())).actionPerformed(
00335                                           new ListerMouseEvent(LD,Name,e));
00336                             }
00337                      }
00338                      else
00339                      {      if (Top+n>=V.size()) return;
00340                             int sel=n+Top;
00341                             if (e.getClickCount()>=2)
00342                             {      if (!MultipleSelection) Selected.removeAllElements();
00343                                    select(sel);
00344                             }
00345                             else if (MultipleSelection &&
00346                                           (e.isControlDown() || EasyMultipleSelection || e.isShiftDown()) 
00347                                           )
00348                             {      if (e.isControlDown() || EasyMultipleSelection)
00349                                           toggleSelect(sel);
00350                                    else if (e.isShiftDown())
00351                                           expandSelect(sel);
00352                             }
00353                             else
00354                             {      Selected.removeAllElements();
00355                                    Selected.addElement(new Integer(sel));
00356                             }
00357                             Graphics g=getGraphics();
00358                             paint(g);
00359                             g.dispose();
00360                             if (e.getClickCount()>=2 || ReportSingleClick)
00361                             {      Enumeration en=VAL.elements();
00362                                    while (en.hasMoreElements())
00363                                    {      ((ActionListener)(en.nextElement())).actionPerformed(
00364                                                  new ListerMouseEvent(LD,Name,e));
00365                                    }
00366                             }
00367                      }
00368               }
00369 
00370               public Dimension getPreferredSize ()
00371               {      return new Dimension(200,300);
00372               }
00373 
00374               public synchronized Element getElementAt (int n)
00375               {      return (Element)V.elementAt(n);
00376               }
00377 
00378               public synchronized void save (PrintWriter o)
00379               {      Enumeration e=V.elements();
00380                      while (e.hasMoreElements())
00381                      {      Element el=(Element)e.nextElement();
00382                             o.println(el.getElementString());
00383                      }
00384               }
00385 
00386               public void setListingBackground (Color c)
00387               {      ListingBackground=c;
00388               }
00389 }
00390