Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00036 package gui;
00037
00038 import java.lang.*;
00039 import java.awt.*;
00040 import java.io.*;
00041 import java.awt.event.*;
00042
00043 public class NewFileDialog extends Dialog
00044 {
00045
00046
00047 public static final int LOAD = 0;
00048 public static final int SAVE = 1;
00049
00050
00051
00052 protected TextField directoryField;
00053 protected TextField fileField;
00054 protected TextField filterField;
00055 protected List fileList;
00056
00057 protected String directory = null;
00058 protected String file = null;
00059 protected int mode = -1;
00060
00061
00062
00063 public NewFileDialog(Frame dw, String title)
00064 {
00065 super(dw, title, true);
00066
00067 GridBagLayout gridbag = new GridBagLayout();
00068 GridBagConstraints constr = new GridBagConstraints();
00069 setLayout(gridbag);
00070
00071 directoryField = new TextField(40);
00072 directoryField.addTextListener(new TextListener()
00073 { public void textValueChanged(TextEvent e)
00074 {directoryChanged();}});
00075
00076 Panel p1 = new Panel();
00077 p1.add(new Label("Directory"));
00078 p1.add(directoryField);
00079 constr.fill = GridBagConstraints.NONE;
00080 constr.gridwidth = GridBagConstraints.REMAINDER;
00081 gridbag.setConstraints(p1,constr);
00082 add(p1);
00083
00084 fileList = new List(4);
00085 fileList.addItemListener(new ItemListener()
00086 { public void itemStateChanged(ItemEvent e) {
00087 itemSelected();}});
00088 constr.fill = GridBagConstraints.BOTH;
00089 constr.gridwidth = GridBagConstraints.REMAINDER;
00090 constr.weighty = 1.0;
00091 gridbag.setConstraints(fileList,constr);
00092 add(fileList);
00093
00094 Panel psum = new Panel();
00095
00096 filterField = new TextField(8);
00097 Panel p4 = new Panel();
00098 p4.add(new Label("Filter"));
00099 p4.add(filterField);
00100 psum.add(p4);
00101
00102 fileField = new TextField(20);
00103 Panel p3 = new Panel();
00104 p3.add(new Label("File"));
00105 p3.add(fileField);
00106 psum.add(p3);
00107 constr.fill = GridBagConstraints.NONE;
00108 constr.gridwidth = GridBagConstraints.REMAINDER;
00109 constr.weighty = 0.0;
00110 gridbag.setConstraints(psum,constr);
00111 add(psum);
00112
00113
00114
00115 Panel p5 = new Panel();
00116 Button okButton = new Button("Ok");
00117 okButton.addActionListener(new ActionListener()
00118 { public void actionPerformed(ActionEvent e)
00119 { okPressed();}});
00120 Button cancelButton = new Button("Cancel");
00121 cancelButton.addActionListener(new ActionListener()
00122 { public void
00123 actionPerformed(ActionEvent e)
00124 { cancelPressed();}});
00125 p5.add(okButton);
00126 p5.add(cancelButton);
00127 constr.fill = GridBagConstraints.NONE;
00128 constr.gridwidth = GridBagConstraints.REMAINDER;
00129 gridbag.setConstraints(p5,constr);
00130 add(p5);
00131
00132
00133
00134 setSize(580,200);
00135 pack();
00136 }
00137
00138
00139
00140 public void okPressed()
00141 {
00142 directory = directoryField.getText();
00143 file = fileField.getText();
00144 closeDialog();
00145 }
00146
00147 public void cancelPressed()
00148 {
00149 directory = null;
00150 file = null;
00151 closeDialog();
00152 }
00153
00154 public void closeDialog()
00155 {
00156 setVisible(false);
00157 dispose();
00158 }
00159
00160 public void directoryChanged() {
00161 File dir = new File(directoryField.getText());
00162 if (dir.isDirectory()) {
00163 loadDirectory(dir);
00164 }
00165 }
00166
00167 public void loadDirectory(File dir) {
00168 String[] dirlist = dir.list();
00169 int i;
00170 if (fileList.getItemCount()>0) {
00171 fileList.removeAll();
00172 }
00173 fileList.add("..");
00174 fileList.addItemListener(new ItemListener()
00175 { public void itemStateChanged(ItemEvent e) {
00176 itemSelected();}});
00177 for(i=0;i<dirlist.length;i++) {
00178 fileList.add(dirlist[i]);
00179 }
00180 }
00181
00182 public void itemSelected() {
00183 if (fileList.getSelectedIndex() >= 0) {
00184 String item = fileList.getSelectedItem();
00185 fileList.deselect(fileList.getSelectedIndex());
00186 File fileOrDir;
00187 File newDir;
00188 if (item.equals("..")) {
00189 fileOrDir = new File(directoryField.getText());
00190 String dirStr = fileOrDir.getParent();
00191 if (dirStr != null) {
00192 newDir = new File(dirStr);
00193 directoryField.setText(newDir.getAbsolutePath());
00194 directoryChanged();
00195 }
00196 } else {
00197 fileOrDir = new File(directoryField.getText(),item);
00198 if (fileOrDir.isDirectory()) {
00199 directoryField.setText(fileOrDir.getAbsolutePath());
00200 directoryChanged();
00201 } else {
00202 fileField.setText(item);
00203 }
00204 }
00205 }
00206 }
00207
00208
00209
00210 public synchronized void setDirectory(String dir) {
00211 File fdir = new File(dir);
00212 if (!fdir.isDirectory()) {
00213 fdir = new File("");
00214 }
00215 directoryField.setText(fdir.getAbsolutePath());
00216 loadDirectory(fdir);
00217 }
00218
00219 public String getDirectory() {
00220 return directory;
00221 }
00222
00223 public synchronized void setFile(String file) {
00224 fileField.setText(file);
00225 }
00226
00227 public String getFile() {
00228 return file;
00229 }
00230
00231 public synchronized void setFilenameFilter(FilenameFilter filter) {
00232 }
00233
00234 public FilenameFilter getFilenameFilter() {
00235 return null;
00236 }
00237
00238 public synchronized void setMode(int mode) {
00239 this.mode = mode;
00240 }
00241
00242 public int getMode(int mode) {
00243 return mode;
00244 }
00245
00246 }
00247