package awt;
import awt.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
A FileDialog clone *with* working filenamefilter ... |
public class FileDialog extends Dialog implements ActionListener,ItemListener {
public static final int LOAD = java.awt.FileDialog.LOAD;
public static final int SAVE = java.awt.FileDialog.SAVE;
private Frame parentFrame;
private int mode;
private String dir,file;
private FilenameFilter filter;
private TextField pathField,nameField;
private Button upButton,newButton,openButton;
// Pending: implement awt.List.addItemListener() !
private java.awt.List filelist;
public FileDialog(Frame parent) {
this(parent,"");
}
public FileDialog(Frame parent,String title) {
this(parent,title,LOAD);
}
public FileDialog(Frame parent,String title,int mode) {
super(parent,title,true);
setBackground(Color.lightGray);
parentFrame = parent;
this.mode = mode;
Component comp;
GridBagLayout gbag;
setLayout(gbag = new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
add(comp = new Label("Path:"));
gc.fill = GridBagConstraints.NONE;
gc.weightx = 0; gc.weighty = 0;
gbag.setConstraints(comp,gc);
add(pathField = new TextField(30));
pathField.addActionListener(this);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 1;
gbag.setConstraints(pathField,gc);
add(upButton = new Button("Up"));
gc.fill = GridBagConstraints.NONE;
gc.weightx = 0;
gbag.setConstraints(upButton,gc);
upButton.addActionListener(this);
add(newButton = new Button("New dir ..."));
gc.gridwidth = GridBagConstraints.REMAINDER;
gbag.setConstraints(newButton,gc);
newButton.addActionListener(this);
add(filelist = new java.awt.List());
gc.fill = GridBagConstraints.BOTH;
gc.weightx = gc.weighty = 1;
gbag.setConstraints(filelist,gc);
filelist.addActionListener(this);
filelist.addItemListener(this);
add(comp = new Label("File:"));
gc.fill = GridBagConstraints.NONE;
gc.weightx = gc.weighty = 0;
gc.gridwidth = 1;
gbag.setConstraints(comp,gc);
add(nameField = new TextField(""));
nameField.addActionListener(this);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 1;
gc.gridwidth = GridBagConstraints.REMAINDER;
gbag.setConstraints(nameField,gc);
Panel p = new Panel();
Button b;
p.add(openButton = new Button((mode == LOAD) ? "Open" : "Save"));
openButton.addActionListener(this);
p.add(b = new Button("Cancel"));
b.addActionListener(this);
add(p);
gc.fill = GridBagConstraints.HORIZONTAL;
gbag.setConstraints(p,gc);
pack();
{ // Window is really not high enough
Dimension siz = getSize();
setSize(siz.width,siz.height*2);
}
// it's the task of the caller to show()
updatelist();
}
public int getMode() { return mode; }
public void setMode(int m) {
switch (mode) {
case LOAD: case SAVE:
mode = m; break;
default:
throw new IllegalArgumentException("illegal file dialog mode "+mode);
}
}
public String getDirectory() { return dir; }
public void setDirectory(String d) {
if (d==null || d.length()==0)
dir = System.getProperty("user.dir");
else dir = d;
pathField.setText(d);
}
public String getFile() { return file; }
public void setFile(String d) { nameField.setText(file = d); }
public FilenameFilter getFilenameFilter() { return filter; }
public void setFilenameFilter(FilenameFilter f) {
filter = f;
updatelist();
}
protected String paramString() {
String str = super.paramString();
if (dir != null) str += ",dir="+dir;
return str + (mode==LOAD ? "LOAD" : "SAVE");
}
private void enterdir(String name) {
if (name.endsWith("/"))
name = name.substring(0,name.length()-1);
File f; // destination
{ File there = new File(name);
if (there.isAbsolute()) f = there;
else f = new File(dir,name);
}
// try to avoid having "./" and "../" in the dir name
String canon = null;
try { canon = f.getCanonicalPath(); } catch (IOException e) {}
if (canon != null) dir = canon;
else dir = f.getPath();
pathField.setText(dir);
updatelist();
}
// Return file name of i-th file in the list
private String filename(int i) {
String result = filelist.getItem(i);
return result.substring(result.indexOf(' ')+1);
}
public void itemStateChanged(ItemEvent e) {
int idx = filelist.getSelectedIndex();
if (idx < 0) return;
String name = filelist.getItem(idx);
if (!new File(dir,name).isDirectory())
nameField.setText(filename(idx));
}
Button presses or List double clicks |
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
Object src = e.getSource();
if (src instanceof List) {
// double click
String name = filename(filelist.getSelectedIndex()); //nameField.getText();
File f = new File(dir,name);
//System.out.println("doubleclick "+f+"; dir: "+f.isDirectory());
if (!f.isDirectory()) {
file = name;
dispose();
} else enterdir(name);
} else if (src == pathField) {
enterdir(pathField.getText());
} else if (src==nameField || cmd.equals("Open") || cmd.equals("Save")) {
file = nameField.getText();
if (new File(dir,file).isDirectory()) {
enterdir(file);
nameField.setText(file = "");
} else dispose();
} else if (cmd.equals("Up")) {
File f = new File(dir);
dir = f.getParent();
pathField.setText(dir);
updatelist();
} else if (cmd.equals("New dir ...")) {
new CreateDir(this,dir);
} else if (cmd.equals("Cancel")) {
dir = file = null;
dispose();
} else System.out.println(cmd);
}
protected void updatelist() {
File f;
if (dir==null || dir.length()==0)
pathField.setText(dir = System.getProperty("user.dir"));
f = new File(dir);
//System.out.println("updatelist "+f);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
String [] files = f.list(filter);
//System.out.println("updatelist "+f+" "+files.length+" files");
filelist.removeAll();
if (files == null) return;
// Add files in alphabetically sorted order
String lastAdded = "";
while (true) {
String best = null;
int besti = 0;
for (int i = 0; i < files.length; i++) {
if (files[i] == null) continue;
if (best == null) {
best = files[i];
besti = i;
continue;
}
if (best.compareTo(files[i]) > 0)
best = files[besti = i];
}
if (best == null) break;
files[besti] = null;
f = new File(dir,best);
if (f.isDirectory()) filelist.add(" "+best+"/");
else switch (mode) {
case LOAD: if (f.canRead()) filelist.add(f.length()+" "+best);
break;
case SAVE: if (f.canWrite()) filelist.add(f.length()+" "+best);
break;
}
}
setCursor(Cursor.getDefaultCursor());
}
}
class CreateDir extends Frame implements ActionListener {
private FileDialog dialog;
private String parentdir;
private TextField nameField;
public CreateDir(FileDialog d,String pardir) {
super("Create dir in "+pardir);
dialog = d;
parentdir = pardir;
add("North",new Label("Directory name:"));
add("Center",nameField = new TextField(""));
Panel p = new Panel();
Button b;
p.add(b = new Button("Create dir"));
b.addActionListener(this);
p.add(b = new Button("Cancel"));
b.addActionListener(this);
add("South",p);
pack();
show();
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Cancel")) {
dispose();
} else if (cmd.equals("Create dir")) {
new File(parentdir,nameField.getText()).mkdir();
dispose();
dialog.updatelist();
}
}
}