Back to index

wims  3.65+svn20090927
MyFileDialog.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.Component;
00006 import java.awt.FileDialog;
00007 import java.awt.FlowLayout;
00008 import java.awt.Frame;
00009 import java.awt.GridLayout;
00010 import java.awt.Panel;
00011 import java.awt.TextField;
00012 import java.awt.event.ActionEvent;
00013 import java.awt.event.FocusEvent;
00014 import java.awt.event.ItemEvent;
00015 import java.awt.event.ItemListener;
00016 import java.awt.event.MouseEvent;
00017 import java.awt.event.MouseListener;
00018 import java.io.File;
00019 import java.io.FilenameFilter;
00020 import java.util.Enumeration;
00021 
00022 import rene.gui.ButtonAction;
00023 import rene.gui.CloseDialog;
00024 import rene.gui.CloseFrame;
00025 import rene.gui.DoActionListener;
00026 import rene.gui.Global;
00027 import rene.gui.HistoryTextField;
00028 import rene.gui.HistoryTextFieldChoice;
00029 import rene.gui.MyLabel;
00030 import rene.gui.MyPanel;
00031 import rene.gui.Panel3D;
00032 import rene.gui.TextFieldAction;
00033 import rene.util.FileList;
00034 import rene.util.FileName;
00035 import rene.util.MyVector;
00036 import rene.lister.*;
00037 
00038 class DirFieldListener 
00039        implements DoActionListener
00040 {      MyFileDialog T;
00041        
00042        public DirFieldListener (MyFileDialog t)
00043        {      T=t;
00044        }
00045 
00046        public void doAction (String o) 
00047        {      T.setFile(o);
00048        }
00049 
00050        public void itemAction(String o, boolean flag) {
00051        }
00052 }
00053 
00068 public class MyFileDialog extends CloseDialog
00069        implements ItemListener,FilenameFilter,MouseListener
00070 {      //java.awt.List Dirs,Files;
00071        Lister Dirs,Files;
00072        HistoryTextField DirField,FileField,PatternField;
00073        HistoryTextFieldChoice DirHistory,FileHistory;
00074        TextField Chosen;
00075        String CurrentDir=".";
00076        boolean Aborted=true;
00077        String DirAppend="",PatternAppend="",FileAppend="";
00078        Button Home;
00079        Frame F;
00080 
00086        public MyFileDialog (Frame f, 
00087               String title, String action, boolean saving, boolean help)
00088        {      super(f,title,true);
00089               F=f;
00090               setLayout(new BorderLayout());
00091               
00092               // title prompt
00093               add("North",new Panel3D(Chosen=new TextFieldAction(this,"")));
00094               Chosen.setEditable(false);
00095               
00096               // center panels
00097               Panel center=new MyPanel();
00098               center.setLayout(new GridLayout(1,2,5,0));
00099               Dirs=new Lister();
00100               if (Global.NormalFont!=null) Dirs.setFont(Global.NormalFont);
00101               Dirs.addActionListener(this);
00102               Dirs.setMode(false,false,false,false);
00103               center.add(Dirs);
00104               Files=new Lister();
00105               if (Global.NormalFont!=null) Files.setFont(Global.NormalFont);
00106               Files.addActionListener(this);
00107               Files.setMode(false,false,true,false);
00108               center.add(Files);
00109               add("Center",new Panel3D(center));        
00110               
00111               // south panel
00112               Panel south=new MyPanel();
00113               south.setLayout(new BorderLayout());
00114               
00115               Panel px=new MyPanel();
00116               px.setLayout(new BorderLayout());
00117               
00118               Panel p0=new MyPanel();
00119               p0.setLayout(new GridLayout(0,1));
00120               
00121               Panel p1=new MyPanel();
00122               p1.setLayout(new BorderLayout());
00123               p1.add("North",linePanel(
00124                             new MyLabel(Global.name("myfiledialog.dir")),
00125                             DirField=
00126                                    new HistoryTextField(this,"Dir",32)
00127                                           {      public boolean filterHistory (String name)
00128                                                  // avoid a Windows bug with C: instead of c:
00129                                                  {      if (name.length()<2) return true;
00130                                                         if (name.charAt(1)==':' && 
00131                                                                Character.isUpperCase(name.charAt(0)))
00132                                                                       return false;
00133                                                         return true;
00134                                                  }
00135                                           }
00136                             ));
00137               DirField.setText(".");
00138               p1.add("South",linePanel(
00139                             new MyLabel(Global.name("myfiledialog.olddirs","")),
00140                             DirHistory=new HistoryTextFieldChoice(DirField)
00141                             ));
00142               p0.add(new Panel3D(p1));
00143               
00144               Panel p2=new MyPanel();
00145               p2.setLayout(new BorderLayout());
00146               p2.add("North",linePanel(
00147                             new MyLabel(Global.name("myfiledialog.file")),
00148                             FileField=new HistoryTextField(this,"File")
00149                             ));
00150               p2.add("South",linePanel(
00151                             new MyLabel(Global.name("myfiledialog.oldfiles","")),
00152                             FileHistory=new HistoryTextFieldChoice(FileField)
00153                             ));
00154               p0.add(new Panel3D(p2));
00155               
00156               px.add("Center",p0);
00157               
00158               px.add("South",new Panel3D(linePanel(
00159                             new MyLabel(Global.name("myfiledialog.pattern")),
00160                             PatternField=new HistoryTextField(this,"Pattern")
00161                             )));
00162               PatternField.setText("*");
00163               
00164               south.add("Center",px);
00165               
00166               Panel buttons=new MyPanel();
00167               buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
00168               buttons.add(Home=new ButtonAction(this,Global.name("myfiledialog.home","Home"),"Home"));
00169               buttons.add(new ButtonAction(this,Global.name("myfiledialog.mkdir","Create Directory"),"Create"));
00170               buttons.add(new ButtonAction(this,Global.name("myfiledialog.back","Back"),"Back"));
00171               buttons.add(new MyLabel(""));
00172               buttons.add(new ButtonAction(this,action,"Action"));
00173               buttons.add(new ButtonAction(this,Global.name("abort"),"Close"));
00174               if (help)
00175               {      addHelp(buttons,"filedialog");
00176               }
00177               
00178               south.add("South",buttons);
00179               
00180               add("South",new Panel3D(south));
00181               
00182               // set sizes
00183               pack();
00184               setSize("myfiledialog");
00185               addKeyListener(this);
00186               DirField.addKeyListener(this);
00187               DirField.setTrigger(true);
00188               FileHistory.setDoActionListener(new DirFieldListener(this));
00189               PatternField.addKeyListener(this);
00190               PatternField.setTrigger(true);
00191               FileField.addKeyListener(this);
00192               FileField.setTrigger(true);
00193               Home.addMouseListener(this);
00194        }
00195        
00196        Panel linePanel (Component x, Component y)
00197        {      Panel p=new MyPanel();
00198               p.setLayout(new GridLayout(1,0));
00199               p.add(x); p.add(y);
00200               return p;
00201        }
00202        
00203        public MyFileDialog (Frame f, 
00204               String title, String action, boolean saving)
00205        {      this(f,title,action,saving,false);
00206        }
00207 
00208        FileDialog FD;
00209 
00210        public MyFileDialog (Frame f,
00211               String title, boolean saving)
00212        {      super(f,"",true);
00213               FD=new FileDialog(f,title,
00214                      saving?FileDialog.SAVE:FileDialog.LOAD);
00215        }
00216 
00217        boolean HomeShiftControl=false;
00218 
00219        public void mousePressed (MouseEvent e)
00220        {      HomeShiftControl = e.isShiftDown() && e.isControlDown();
00221        }
00222        public void mouseReleased (MouseEvent e) {}
00223        public void mouseClicked (MouseEvent e) {}
00224        public void mouseEntered (MouseEvent e) {}
00225        public void mouseExited (MouseEvent e) {}
00226        
00227        public void doAction (String o)
00228        {      if (o.equals("Dir") || o.equals("Pattern"))
00229               {      if (updateDir()) updateFiles();
00230                      PatternField.remember(PatternField.getText());
00231               }
00232               else if (o.equals("File") || o.equals("Action"))
00233               {      if (FileField.getText().equals("")) return;
00234                      leave();
00235               }
00236               else if (o.equals("Home"))
00237               {      if (HomeShiftControl)
00238                      {      String s=Global.getParameter("myfiledialog.homdir","");
00239                             if (s.equals(""))
00240                                    Global.setParameter("myfiledialog.homdir",DirField.getText());
00241                             else
00242                                    Global.setParameter("myfiledialog.homdir","");                        
00243                      }
00244                      try
00245                      {      String s=Global.getParameter("myfiledialog.homdir","");
00246                             if (s.equals(""))
00247                             {      String s1=System.getProperty("user.home");
00248                                    String s2=Global.name("myfiledialog.windowshome","");
00249                                    String s3=Global.name("myfiledialog.homedir","");
00250                                    String sep=System.getProperty("file.separator");
00251                                    if (new File(s1+sep+s2+sep+s3).exists())
00252                                           DirField.setText(s1+sep+s2+sep+s3);
00253                                    else if (new File(s1+sep+s2).exists())
00254                                           DirField.setText(s1+sep+s2);
00255                                    else
00256                                           DirField.setText(s1);
00257                             }
00258                             else DirField.setText(s);
00259                             updateDir();
00260                             updateFiles();
00261                      }
00262                      catch (Exception e) {}
00263               }
00264               else if (o.equals("Create"))
00265               {      try
00266                      {      File f=new File(DirField.getText());
00267                             if (!f.exists())
00268                             {      f.mkdir();
00269                             }
00270                             updateDir();
00271                             updateFiles();
00272                      }
00273                      catch (Exception e){}
00274               }
00275               else if (o.equals("Back"))
00276               {      String dir=getUndo();
00277                      if (!dir.equals(""))
00278                      {      DirField.setText(dir);
00279                             updateDir();
00280                             updateFiles();
00281                      }
00282               }
00283               else super.doAction(o);
00284        }
00285        
00286        public void actionPerformed (ActionEvent e)
00287        {      if (e.getSource()==Dirs)
00288               {      String s=Dirs.getSelectedItem();
00289                      if (s==null) return;
00290                      if (s.equals("..")) dirup();
00291                      else dirdown(s);
00292               }
00293               if (e.getSource()==Files)
00294               {      if (e instanceof ListerMouseEvent)
00295                      {      ListerMouseEvent em=(ListerMouseEvent)e;
00296                             if (em.clickCount()>=2) leave();
00297                             else
00298                             {      String s=Files.getSelectedItem();
00299                                    if (s!=null) FileField.setText(s);
00300                             }
00301                      }
00302               }
00303               else super.actionPerformed(e);
00304        }
00305        
00306        public void setFile (String s)
00307        {      DirField.setText(FileName.path(s));
00308               FileField.setText(FileName.filename(s));
00309               // System.out.println(s);
00310               update(false);
00311        }
00312        
00313        public void dirup ()
00314        {      DirField.setText(FileName.path(CurrentDir));
00315               if (DirField.getText().equals(""))
00316                      DirField.setText(""+File.separatorChar);
00317               if (updateDir()) updateFiles();           
00318        }
00319        
00320        public void dirdown (String subdir)
00321        {      DirField.setText(CurrentDir+File.separatorChar+subdir);
00322               if (updateDir()) updateFiles();
00323        }
00324        
00328        void leave ()
00329        {      if (FD!=null) return;
00330               if (!FileField.getText().equals("")) Aborted=false;
00331               if (!Aborted)
00332               {      noteSize("myfiledialog");
00333                      DirField.remember(DirField.getText());
00334                      DirField.saveHistory("myfiledialog.dir.history"+DirAppend);
00335                      PatternField.saveHistory("myfiledialog.pattern.history"+PatternAppend);
00336                      FileField.remember(getFilePath());
00337                      FileField.saveHistory("myfiledialog.file.history"+FileAppend);
00338               }
00339               doclose();
00340        }
00341 
00346        public boolean updateDir ()
00347        {      if (FD!=null) return true;
00348               File dir=new File(DirField.getText()+File.separatorChar);
00349               if (!dir.isDirectory()) return false;
00350               try
00351               {      String s=FileName.canonical(dir.getCanonicalPath());
00352                      addUndo(s);
00353                      DirField.setText(s);
00354                      Chosen.setText(
00355                             FileName.chop(16,
00356                             DirField.getText()+File.separatorChar+
00357                                    PatternField.getText(),48));
00358               }
00359               catch (Exception e) { return false; }
00360               return true;
00361        }
00362        
00363        MyVector Undo=new MyVector();
00364        
00368        public void addUndo (String dir)
00369        {      if (Undo.size()>0 && 
00370                      ((String)Undo.elementAt(Undo.size()-1)).equals(dir)) return;
00371               Undo.addElement(dir);
00372        }
00373        
00377        public String getUndo ()
00378        {      if (Undo.size()<2) return "";
00379               String s=(String)Undo.elementAt(Undo.size()-2);
00380               Undo.truncate(Undo.size()-1);
00381               return s;
00382        }
00383        
00387        public void updateFiles ()
00388        {      if (FD!=null) return;
00389               File dir=new File(DirField.getText());
00390               if (!dir.isDirectory()) return;
00391               CurrentDir=DirField.getText();
00392               if (PatternField.getText().equals("")) PatternField.setText("*");
00393               try
00394               {      Files.clear();
00395                      Dirs.clear();
00396                      FileList l=new FileList(DirField.getText(),
00397                             PatternField.getText(),false);
00398                      l.setCase(Global.getParameter("filedialog.usecaps",false));
00399                      l.search();
00400                      l.sort();
00401                      Enumeration e=l.files();
00402                      while (e.hasMoreElements())
00403                      {      File f=(File)e.nextElement();
00404                             Files.addElement(FileName.filename(f.getCanonicalPath()));
00405                      }
00406                      Dirs.addElement("..");
00407                      e=l.dirs();
00408                      while (e.hasMoreElements())
00409                      {      File f=(File)e.nextElement();
00410                             Dirs.addElement(FileName.filename(f.getCanonicalPath()));
00411                      }
00412               }
00413               catch (Exception e) {}
00414               Dirs.updateDisplay();
00415               Files.updateDisplay();
00416               Files.requestFocus();
00417        }
00418        
00419        public void setDirectory (String dir)
00420        {      if (FD!=null) FD.setDirectory(dir);
00421               else DirField.setText(dir);
00422        }
00423        
00424        public void setPattern (String pattern)
00425        {      if (FD!=null)
00426               {      FD.setFilenameFilter(this); // does not work
00427                      String s=pattern.replace(' ',';');
00428                      FD.setFile(s);
00429               }
00430               else PatternField.setText(pattern);
00431        }
00432        
00433        public void setFilePath (String file)
00434        {      if (FD!=null)
00435               {      FD.setFile(file);
00436                      return;
00437               }
00438               String dir=FileName.path(file);
00439               if (!dir.equals(""))
00440               {      DirField.setText(dir);
00441                      FileField.setText(FileName.filename(file));
00442               }
00443               else FileField.setText(file);
00444        }
00445        
00449        public boolean isAborted ()
00450        {      if (FD!=null) return FD.getFile()==null || FD.getFile().equals("");
00451               else return Aborted;
00452        }
00453        
00457        public String getFilePath ()
00458        {      if (FD!=null) 
00459               {      if (FD.getFile()!=null) return FD.getDirectory()+FD.getFile();
00460                      else return "";
00461               }
00462               String file=FileField.getText();
00463               if (!FileName.path(file).equals("")) return file;
00464               else return CurrentDir+File.separatorChar+FileField.getText();
00465        }
00466 
00470        public void update (boolean recent)
00471        {      if (FD!=null) return;
00472               loadHistories(recent);
00473               setFilePath(FileField.getText());
00474               if (updateDir()) updateFiles();
00475               Aborted=true;
00476        }
00477        public void update ()
00478        {      update(true); 
00479        }
00480        
00481        public void setVisible (boolean flag)
00482        {      if (FD!=null) FD.setVisible(flag);
00483               else super.setVisible(flag);
00484        }
00485        
00486        public void center (Frame f)
00487        {      if (FD!=null) CloseDialog.center(f,FD);
00488               else super.center(f);
00489        }
00490 
00491        public static void main (String args[])
00492        {      Frame f=new CloseFrame()
00493               {      public void doclose ()
00494                      {      System.exit(0);
00495                      }
00496               };
00497               f.setSize(500,500);
00498               f.setLocation(400,400);
00499               f.setVisible(true);
00500               MyFileDialog d=new MyFileDialog(f,"Title","Save",false);
00501               d.center(f);
00502               d.update();
00503               d.setVisible(true);
00504        }
00505 
00506        public void focusGained (FocusEvent e)
00507        {      FileField.requestFocus();
00508        }
00509        
00513        public boolean accept (File dir, String file)
00514        {      return true;
00515        }
00516        
00517        public void loadHistories (boolean recent)
00518        {      if (FD!=null) return;
00519               DirField.loadHistory("myfiledialog.dir.history"+DirAppend);
00520               DirHistory.update();
00521               if (recent) setDirectory(DirHistory.getRecent());
00522               if (updateDir()) updateFiles();           
00523               PatternField.loadHistory("myfiledialog.pattern.history"+PatternAppend);
00524               FileField.loadHistory("myfiledialog.file.history"+FileAppend);
00525               FileHistory.update();
00526        }
00527        public void loadHistories ()
00528        {      loadHistories(true);
00529        }
00530 
00540        public void loadHistories (String dir, String pattern, String file)
00541        {      setAppend(dir,pattern,file);
00542               loadHistories();
00543        }
00544        
00555        public void setAppend (String dir, String pattern, String file)
00556        {      if (FD!=null) return;
00557               if (!dir.equals("")) DirAppend="."+dir;
00558               else DirAppend=".default";
00559               if (!pattern.equals("")) PatternAppend="."+pattern;
00560               else PatternAppend=".default";
00561               if (!file.equals("")) FileAppend="."+file;
00562               else FileAppend=".default";
00563        }
00564 
00565        public void itemStateChanged(ItemEvent e) {
00566        }
00567 }