Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. package rene.gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.MenuItem;
  5. import java.awt.PopupMenu;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8.  
  9. import rene.util.FileName;
  10. import rene.util.list.ListClass;
  11. import rene.util.list.ListElement;
  12.  
  13. /**
  14. A TextField, which display the old input, when cursor up is
  15. pressed. The old input is stored in a list. The class is derived
  16. from TextFieldAction.
  17. @see TextFieldAction
  18. */
  19.  
  20. public class HistoryTextField extends TextFieldAction
  21.     implements KeyListener,DoActionListener
  22. {       ListClass H;
  23.         PopupMenu M=null;
  24.         boolean Trigger=false;
  25.         public int MaxLength=48;
  26.        
  27.         public HistoryTextField (DoActionListener l, String name)
  28.         {       super(l,name);
  29.             H=new ListClass();
  30.                 H.append(new ListElement(""));
  31.                 addKeyListener(this);
  32.         }
  33.        
  34.         public HistoryTextField (DoActionListener l, String name, int s)
  35.         {       super(l,name,s);
  36.             H=new ListClass();
  37.                 H.append(new ListElement(""));
  38.                 addKeyListener(this);
  39.         }
  40.        
  41.         public void keyPressed (KeyEvent ev)
  42.         {       switch (ev.getKeyCode())
  43.                 {       case KeyEvent.VK_UP :
  44.                         case KeyEvent.VK_DOWN :
  45.                                 if (M==null)
  46.                                 {       M=new PopupMenu();
  47.                                         ListElement e=H.last();
  48.                                         int i=0,n=Global.getParameter("history.length",10);
  49.                                         while (e!=null && i<n)
  50.                                         {       String t=(String)e.content();
  51.                                                 if (!t.equals(""))
  52.                                                 {       MenuItem item=new MenuItemAction(this,
  53.                                                                 FileName.chop(t,MaxLength),t);
  54.                                                         M.add(item);
  55.                                                 }
  56.                                                 e=e.previous();
  57.                                                 i++;
  58.                                         }
  59.                                         add(M);
  60.                                 }
  61.                                 M.show(this,10,10);
  62.                                 break;
  63.                         default : return;
  64.                 }
  65.         }
  66.        
  67.         public void keyReleased (KeyEvent e) {}
  68.        
  69.         public void keyTyped (KeyEvent e) {}
  70.        
  71.         String Last="";
  72.        
  73.         public void remember (String s)
  74.         {       if (s.equals(Last)) return;
  75.                 deleteFromHistory(s);
  76.                 Last=s;
  77.                 H.last().content(s);
  78.                 H.append(new ListElement(""));
  79.                 M=null;
  80.         }
  81.        
  82.         public void deleteFromHistory (String s)
  83.         {       ListElement e=H.first();
  84.                 while (e!=null)
  85.                 {       String t=(String)e.content();
  86.                         ListElement next=e.next();
  87.                         if (t.equals(s))
  88.                         {       H.remove(e);
  89.                                 if (H.first()==null) H.append(new ListElement(""));
  90.                         }
  91.                         e=next;
  92.                 }
  93.         }
  94.        
  95.         public void remember ()
  96.         {       remember(getText());
  97.         }
  98.        
  99.         public void saveHistory (String name)
  100.         {       int i,n=Global.getParameter("history.length",10);
  101.                 Global.removeAllParameters("history."+name);
  102.                 ListElement e=H.last();
  103.                 if (e==null) return;
  104.                 for (i=0; i<n && e!=null; e=e.previous())
  105.                 {       String s=(String)e.content();
  106.                         if (!s.equals(""))
  107.                         {       i++;
  108.                                 Global.setParameter("history."+name+"."+i,s);
  109.                         }
  110.                 }
  111.         }
  112.        
  113.         public void loadHistory (String name)
  114.         {       int i=1;
  115.             H=new ListClass();
  116.                 H.append(new ListElement(""));
  117.                 while (Global.haveParameter("history."+name+"."+i))
  118.                 {       String s=Global.getParameter("history."+name+"."+i,"");
  119.                         if (!s.equals("") && filterHistory(s))
  120.                                 H.prepend(new ListElement(s));
  121.                         i++;
  122.                 }
  123.         }
  124.        
  125.         public boolean filterHistory (String name)
  126.         {       return true;
  127.         }
  128.        
  129.         public ListClass getHistory () { return H; }
  130.        
  131.         public void setTrigger (boolean f)
  132.         {       Trigger=f;
  133.         }
  134.        
  135.         public void doAction (String o)
  136.         {       if (!o.equals(""))
  137.                 {       setText(o);
  138.                         if (Trigger) triggerAction();
  139.                 }
  140.         }
  141.         public void itemAction (String o, boolean flag)
  142.         {
  143.         }
  144.        
  145.         public static void main (String args[])
  146.         {       CloseFrame f=new CloseFrame("test");
  147.                 HistoryTextField t=new HistoryTextField(f,"Test",30);
  148.                 t.remember("AAAA");
  149.                 t.remember("BBBB");
  150.                 t.remember("CCCC");
  151.                 t.remember("DDDD");
  152.                 f.setLayout(new BorderLayout());
  153.                 f.add("Center",t);
  154.                 f.add("South",new HistoryTextFieldChoice(t));
  155.                 f.pack();
  156.                 f.setVisible(true);
  157.         }
  158. }
  159.