Back to index

wims  3.65+svn20090927
HistoryTextField.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.MenuItem;
00005 import java.awt.PopupMenu;
00006 import java.awt.event.KeyEvent;
00007 import java.awt.event.KeyListener;
00008 
00009 import rene.util.FileName;
00010 import rene.util.list.ListClass;
00011 import rene.util.list.ListElement;
00012 
00020 public class HistoryTextField extends TextFieldAction
00021     implements KeyListener,DoActionListener
00022 {      ListClass H;
00023        PopupMenu M=null;
00024        boolean Trigger=false;
00025        public int MaxLength=48;
00026        
00027        public HistoryTextField (DoActionListener l, String name)
00028        {      super(l,name);
00029            H=new ListClass();
00030               H.append(new ListElement(""));
00031               addKeyListener(this);
00032        }
00033        
00034        public HistoryTextField (DoActionListener l, String name, int s)
00035        {      super(l,name,s);
00036            H=new ListClass();
00037               H.append(new ListElement(""));
00038               addKeyListener(this);
00039        }
00040        
00041        public void keyPressed (KeyEvent ev)
00042        {      switch (ev.getKeyCode())
00043               {      case KeyEvent.VK_UP :
00044                      case KeyEvent.VK_DOWN : 
00045                             if (M==null)
00046                             {      M=new PopupMenu();
00047                                    ListElement e=H.last();
00048                                    int i=0,n=Global.getParameter("history.length",10);
00049                                    while (e!=null && i<n)
00050                                    {      String t=(String)e.content();
00051                                           if (!t.equals(""))
00052                                           {      MenuItem item=new MenuItemAction(this,
00053                                                         FileName.chop(t,MaxLength),t);
00054                                                  M.add(item);
00055                                           }
00056                                           e=e.previous();
00057                                           i++;
00058                                    }
00059                                    add(M);
00060                             }
00061                             M.show(this,10,10);
00062                             break;
00063                      default : return;
00064               }
00065        }
00066        
00067        public void keyReleased (KeyEvent e) {}
00068        
00069        public void keyTyped (KeyEvent e) {}
00070        
00071        String Last="";
00072        
00073        public void remember (String s)
00074        {      if (s.equals(Last)) return;
00075               deleteFromHistory(s);
00076               Last=s;
00077               H.last().content(s);
00078               H.append(new ListElement(""));
00079               M=null;
00080        }
00081        
00082        public void deleteFromHistory (String s)
00083        {      ListElement e=H.first();
00084               while (e!=null)
00085               {      String t=(String)e.content();
00086                      ListElement next=e.next();
00087                      if (t.equals(s))
00088                      {      H.remove(e);
00089                             if (H.first()==null) H.append(new ListElement(""));
00090                      }
00091                      e=next;
00092               }
00093        }
00094        
00095        public void remember ()
00096        {      remember(getText());
00097        }
00098        
00099        public void saveHistory (String name)
00100        {      int i,n=Global.getParameter("history.length",10);
00101               Global.removeAllParameters("history."+name);
00102               ListElement e=H.last();
00103               if (e==null) return;
00104               for (i=0; i<n && e!=null; e=e.previous())
00105               {      String s=(String)e.content();
00106                      if (!s.equals(""))
00107                      {      i++;
00108                             Global.setParameter("history."+name+"."+i,s);
00109                      }
00110               }
00111        }
00112        
00113        public void loadHistory (String name)
00114        {      int i=1;
00115            H=new ListClass();
00116               H.append(new ListElement(""));
00117               while (Global.haveParameter("history."+name+"."+i))
00118               {      String s=Global.getParameter("history."+name+"."+i,"");
00119                      if (!s.equals("") && filterHistory(s)) 
00120                             H.prepend(new ListElement(s));
00121                      i++;
00122               }
00123        }
00124        
00125        public boolean filterHistory (String name)
00126        {      return true;
00127        }
00128        
00129        public ListClass getHistory () { return H; }
00130        
00131        public void setTrigger (boolean f)
00132        {      Trigger=f;
00133        }
00134        
00135        public void doAction (String o)
00136        {      if (!o.equals(""))
00137               {      setText(o);
00138                      if (Trigger) triggerAction();
00139               }
00140        }
00141        public void itemAction (String o, boolean flag)
00142        {
00143        }
00144        
00145        public static void main (String args[])
00146        {      CloseFrame f=new CloseFrame("test");
00147               HistoryTextField t=new HistoryTextField(f,"Test",30);
00148               t.remember("AAAA");
00149               t.remember("BBBB");
00150               t.remember("CCCC");
00151               t.remember("DDDD");
00152               f.setLayout(new BorderLayout());
00153               f.add("Center",t);
00154               f.add("South",new HistoryTextFieldChoice(t));
00155               f.pack();
00156               f.setVisible(true);
00157        }
00158 }