VisClient/org/hfbk/util/DirectoryChooser.java

Go to the documentation of this file.
00001 package org.hfbk.util;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Button;
00005 import java.awt.Dialog;
00006 import java.awt.Frame;
00007 import java.awt.List;
00008 import java.awt.Panel;
00009 import java.awt.TextField;
00010 import java.awt.event.ActionEvent;
00011 import java.awt.event.ActionListener;
00012 import java.awt.event.ItemEvent;
00013 import java.awt.event.ItemListener;
00014 import java.io.File;
00015 import java.io.IOException;
00016 import java.util.Collections;
00017 import java.util.LinkedList;
00018 
00019 public class DirectoryChooser extends Dialog{
00020 
00021         TextField dirField=new TextField(50);
00022         List dirList= new List(20);
00023         String oldDir;
00024         
00025         public DirectoryChooser(String title, File startDir) {
00026                 super(new Frame(), title);
00027                 
00028                 oldDir=startDir.getAbsolutePath();
00029                 dirField.setText(oldDir);
00030                 add(dirField,BorderLayout.NORTH);
00031                 
00032                 
00033                 updateList();
00034                 add(dirList, BorderLayout.CENTER);
00035                 dirList.addItemListener(new ItemListener(){
00036                         public void itemStateChanged(ItemEvent ie) {
00037                                 if (ie.getStateChange()==ItemEvent.SELECTED){
00038                                         String dirName=dirList.getItem((Integer)ie.getItem());
00039                                         try {
00040                                                 dirField.setText(new File(dirField.getText(), dirName ).getCanonicalPath());
00041                                         } catch (IOException e) {
00042                                                 e.printStackTrace();
00043                                         }
00044                                         updateList();
00045                                 }
00046                         }
00047                 });
00048         
00049                 Panel p=new Panel();
00050                 p.add(new SimpleButton("CANCEL"){
00051                         void action() {
00052                                 finish(false);
00053                         }
00054                 });
00055                 p.add(new SimpleButton("OK"){
00056                         void action() {
00057                                 finish(true);
00058                         }
00059                 });
00060                 add(p, BorderLayout.SOUTH);
00061                 
00062                 pack();
00063                 UIUtils.blackify(this);
00064                 setModal(true);
00065         }
00066         
00067         void finish(boolean success){
00068                 if (!success) dirField.setText(oldDir);
00069                 dispose();
00070         }
00071 
00072         public String chooseDir(){
00073                 setVisible(true); //dialog is modal, so this blocks until closed.
00074                 return dirField.getText();
00075         };
00076         
00077         public class CaseInsensitiveComparator 
00078          implements java.util.Comparator<String> {
00079           public int compare(String o1, String o2) {
00080             String s1 = o1.toUpperCase();
00081             String s2 = o2.toUpperCase();
00082             return s1.compareTo(s2);
00083           }
00084         }
00085 
00086         void updateList(){
00087                 LinkedList<String> entries=new LinkedList<String>();   //and faces as vertex indicies
00088                 
00089                 entries.add("..");
00090 
00091                 for (File d: new File(dirField.getText()).listFiles())
00092                         if (d.isDirectory() && !d.isHidden())
00093                                 entries.add(d.getName());
00094                         
00095                 Collections.sort(entries, new CaseInsensitiveComparator());
00096 
00097                 dirList.removeAll();
00098                 for (String e: entries) dirList.add(e);
00099         }
00100         
00101         
00102         class SimpleButton extends Button {
00103                 public SimpleButton(String text) {
00104                         super(text);
00105                         addActionListener(new ActionListener() {
00106                                 public void actionPerformed(ActionEvent e) {
00107                                         action();
00108                                 }
00109                         });
00110                 }
00111 
00112                 void action() {
00113                 };
00114         }
00115 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1