Back to index

wims  3.65+svn20090927
SearchFileDialog.java
Go to the documentation of this file.
00001 package rene.dialogs;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Button;
00005 import java.awt.Checkbox;
00006 import java.awt.FlowLayout;
00007 import java.awt.Frame;
00008 import java.awt.GridLayout;
00009 import java.awt.Panel;
00010 import java.awt.event.ActionEvent;
00011 import java.awt.event.FocusEvent;
00012 import java.io.File;
00013 import java.util.Enumeration;
00014 
00015 import rene.gui.ButtonAction;
00016 import rene.gui.CheckboxAction;
00017 import rene.gui.CloseDialog;
00018 import rene.gui.Global;
00019 import rene.gui.HistoryTextField;
00020 import rene.gui.MyLabel;
00021 import rene.gui.MyList;
00022 import rene.gui.MyPanel;
00023 import rene.gui.Panel3D;
00024 import rene.util.FileList;
00025 
00026 class FileListFinder extends FileList
00027 {      String Res;   
00028        public FileListFinder (String dir, String pattern, boolean recurse)
00029        {      super(dir,pattern,recurse);
00030        }
00031        public boolean file (File file)
00032        {      try
00033               {      Res=file.getCanonicalPath();
00034               }
00035               catch (Exception e) {}
00036               return false;
00037        }
00038        public String getResult ()
00039        {      return Res; 
00040        }
00041 }
00042 
00043 class SearchFileThread extends Thread
00044 {      SearchFileDialog D;  
00045        MyList L;     
00046        String Dir,Pattern;
00047        boolean Recurse;
00048        public SearchFileThread (SearchFileDialog dialog,
00049               MyList l, String d, String p, boolean r)
00050        {      D=dialog; L=l; Dir=d; Pattern=p; Recurse=r;
00051        }
00052        public void run ()
00053        {      D.enableButtons(false);     
00054               L.removeAll();
00055               L.setEnabled(false);
00056               FileList f=new FileList(Dir,Pattern,Recurse);
00057               D.F=f;
00058               f.search();
00059               f.sort();
00060               Enumeration e=f.files();
00061               while (e.hasMoreElements())
00062               {      try    
00063                      {      L.add(((File)e.nextElement()).getCanonicalPath());
00064                      }
00065                      catch (Exception ex) {}
00066               }
00067               L.setEnabled(true);
00068               D.enableButtons(true);
00069        }
00070 }
00071 
00089 public class SearchFileDialog extends CloseDialog
00090        implements Runnable, Enumeration
00091 {      HistoryTextField Dir,Pattern;
00092        MyList L;
00093        static public int ListNumber=Global.getParameter("searchfile.number",10);
00094        String Result=null;
00095        Button ActionButton,CloseButton,SearchButton,SearchrekButton;
00096        public FileList F=null;
00097        public boolean Abort=true;
00098        Checkbox Mod;
00099        public SearchFileDialog (Frame f, String action,
00100               String modify, boolean modifystate)
00101        {      super(f,Global.name("searchfile.title"),true);
00102               setLayout(new BorderLayout());
00103               Panel north=new MyPanel();
00104               north.setLayout(new BorderLayout());
00105               Panel northa=new MyPanel();
00106               northa.setLayout(new BorderLayout());
00107               Panel north1=new MyPanel();
00108               north1.setLayout(new GridLayout(0,2));
00109               north1.add(new MyLabel(Global.name("searchfile.directory")));
00110               north1.add(Dir=new HistoryTextField(this,"Dir",20));
00111               Dir.setText(".");
00112               north1.add(new MyLabel(Global.name("searchfile.pattern")));
00113               north1.add(Pattern=new HistoryTextField(this,"TextAction",20));
00114               northa.add("Center",north1);
00115               Panel north2=new MyPanel();
00116               north2.add(SearchButton=
00117                      new ButtonAction(this,Global.name("searchfile.search"),"Search"));
00118               north2.add(SearchrekButton=
00119                      new ButtonAction(this,Global.name("searchfile.searchrek"),"SearchRek"));
00120               northa.add("South",north2);
00121               north.add("North",northa);
00122               add("North",new Panel3D(north));
00123               add("Center",new Panel3D(L=new MyList(ListNumber)));
00124               L.addActionListener(this);
00125               L.setMultipleMode(true);
00126               Panel south=new MyPanel();
00127               south.setLayout(new FlowLayout(FlowLayout.RIGHT));
00128               if (!modify.equals(""))
00129               {      south.add(Mod=
00130                             new CheckboxAction(this,modify,""));
00131                      Mod.setState(modifystate);
00132               }
00133               south.add(ActionButton=
00134                      new ButtonAction(this,action,"Action"));
00135               south.add(CloseButton=
00136                      new ButtonAction(this,Global.name("abort"),"Close"));
00137               add("South",new Panel3D(south));
00138               pack();
00139               Dir.loadHistory("searchfile.dir");
00140               Pattern.loadHistory("searchfile.pattern");
00141               
00142               // size
00143               setSize("searchfiledialog");
00144               addKeyListener(this);
00145               Dir.addKeyListener(this);
00146               Pattern.addKeyListener(this);
00147        }
00148        public SearchFileDialog (Frame f, String action)
00149        {      this(f,action,"",false);
00150        }
00151        public void actionPerformed (ActionEvent e)
00152        {      if (e.getSource()==L)
00153               {      action();
00154               }
00155               else super.actionPerformed(e);
00156        }
00157        public void doAction (String o)
00158        {      Result=null;
00159               if (o.equals("SearchRek")) search(true);
00160               else if (o.equals("Search")) search(false);
00161               else if (o.equals("TextAction")) { L.removeAll(); action(); }
00162               else if (o.equals("Action")) action();
00163               else if (o.equals("Help")) help ();
00164               else if (o.equals("Close")) { Abort=true; doclose(); }
00165        }
00166        public void help () {}
00167        Thread Run;
00168        public void search (boolean recurse)
00169        {      saveHistory();
00170               if (Run!=null && Run.isAlive()) return;
00171               Run=new SearchFileThread(this,L,
00172                      Dir.getText(),Pattern.getText(),recurse);
00173               Run.start();
00174        }
00175        public void action ()
00176        {      saveHistory();
00177               if (Run!=null && Run.isAlive()) return;   
00178               Run=new Thread(this);
00179               Run.start();
00180        }
00181        public void enableButtons (boolean f)
00182        {      Pattern.setEnabled(f);
00183               SearchButton.setEnabled(f);
00184               SearchrekButton.setEnabled(f);
00185               ActionButton.setEnabled(f);
00186        }
00187        public void run ()
00188        {      Result=null;
00189               enableButtons(false);
00190               if (L.getItemCount()>0)
00191               {      int i=L.getSelectedIndex(); 
00192                      if (i>0) Result=L.getItem(i);
00193                      else Result=L.getItem(0);
00194               }
00195               else
00196               {      FileListFinder f=new FileListFinder(Dir.getText(),
00197                             Pattern.getText(),true);
00198                      F=f;
00199                      f.search();
00200                      Result=f.getResult();
00201               }
00202               enableButtons(true);
00203               Abort=false;
00204               doclose();
00205        }
00206        public String getResult() { return Result; }
00207        public void focusGained (FocusEvent e)
00208        {      Pattern.requestFocus();
00209        }
00210        public void setVisible (boolean flag)
00211        {      if (flag) enableButtons(true);
00212               super.setVisible(flag);
00213        }
00214        public boolean close ()
00215        {      Abort=true;
00216               return true;
00217        }
00218        public void doclose ()
00219        {      if (F!=null) F.stopIt();
00220               Dir.saveHistory("searchfile.dir");
00221               Pattern.saveHistory("searchfile.pattern");
00222               noteSize("searchfiledialog");
00223               super.doclose();
00224        }
00225        public void saveHistory ()
00226        {      Dir.remember();
00227               Pattern.remember();
00228        }
00229        public void setPattern (String s)
00230        {      Pattern.setText(s);
00231        }
00232        String S[];
00233        int Sn;
00238        public Enumeration getFiles ()
00239        {      S=L.getSelectedItems();
00240               Sn=0;
00241               return this;
00242        }
00243        public boolean hasMoreElements ()
00244        {      return Sn<S.length;
00245        }
00246        public Object nextElement ()
00247        {      if (Sn>=S.length) return null;
00248               String s=S[Sn];
00249               Sn++;
00250               return s;
00251        }
00252        public boolean isAborted ()
00253        {      return Abort;
00254        }
00255        public void deselectAll ()
00256        {      for (int i=L.getItemCount()-1; i>=0; i--)
00257               {      L.deselect(i);
00258               }
00259        }
00260        public boolean isModified ()
00261        {      return Mod.getState();
00262        }
00263 }