1 33 34 package edu.rice.cs.util.swing; 35 36 import javax.swing.*; 37 import javax.swing.filechooser.FileFilter ; 38 import java.awt.*; 39 import java.awt.event.ActionEvent ; 40 import java.awt.event.ActionListener ; 41 import java.awt.event.FocusListener ; 42 import java.awt.event.FocusEvent ; 43 import java.io.*; 44 45 51 public class FileSelectorComponent extends JPanel { 52 53 54 public static final int DEFAULT_NUM_COLS = 30; 55 56 57 public static final float DEFAULT_FONT_SIZE = 10f; 58 59 60 protected final Frame _parent; 61 62 63 protected final JTextField _fileField; 64 65 66 protected final JButton _chooserButton; 67 68 69 protected final JFileChooser _chooser; 70 71 72 protected FileFilter _fileFilter; 73 74 75 protected File _file; 76 77 78 protected boolean _mustExist; 79 80 85 public FileSelectorComponent(Frame parent, JFileChooser chooser) { 86 this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE, true); 87 } 88 89 96 public FileSelectorComponent(Frame parent, JFileChooser chooser, int numCols, float fontSize) { 97 this(parent, chooser, numCols, fontSize, true); 98 } 99 100 108 public FileSelectorComponent(Frame parent, JFileChooser chooser, int numCols, float fontSize, boolean mustExist) { 109 110 _parent = parent; 111 _chooser = chooser; 112 _fileFilter = null; 113 _mustExist = mustExist; 114 115 _fileField = new JTextField(numCols) { 116 public Dimension getMaximumSize() { 117 return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); 118 } 119 }; 120 121 _fileField.setFont(_fileField.getFont().deriveFont(fontSize)); 122 _fileField.setPreferredSize(new Dimension(22, 22)); 123 _fileField.addActionListener(new ActionListener () { 124 public void actionPerformed(ActionEvent e) { validateTextField(); } 125 }); 126 127 _fileField.addFocusListener(new FocusListener () { 128 public void focusGained(FocusEvent e) { } 129 public void focusLost(FocusEvent e) { validateTextField(); } 130 }); 131 132 _chooserButton = new JButton("..."); 133 _chooserButton.addActionListener(new ActionListener () { 134 public void actionPerformed(ActionEvent e) { _chooseFile(); } 135 }); 136 137 _chooserButton.setMaximumSize(new Dimension(22, 22)); 138 _chooserButton.setMargin(new Insets(0, 5 ,0, 5)); 139 140 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 142 this.add(_fileField); 143 this.add(_chooserButton); 144 } 145 146 public void setEnabled(boolean isEnabled) { 147 _fileField.setEnabled(isEnabled); 148 _chooserButton.setEnabled(isEnabled); 149 super.setEnabled(isEnabled); 150 } 151 152 153 public JTextField getFileField() { return _fileField; } 154 155 156 public JFileChooser getFileChooser() { return _chooser; } 157 158 159 public File getFileFromField() { 160 String txt = _fileField.getText().trim(); 161 if (txt.equals("")) _file = null; 162 else _file = new File(txt); 163 164 return _file; 165 } 166 167 170 public void setFileField(File file) { 171 _file = file; 172 if (file != null && !file.getPath().equals("")) { 173 try {_file = file.getCanonicalFile(); } 174 catch(IOException e) { } 175 } 176 resetFileField(); 177 } 178 179 public void resetFileField() { 180 if (_file == null) _fileField.setText(""); 181 else { 182 _fileField.setText(_file.getPath()); 183 _fileField.setCaretPosition(_fileField.getText().length()); 184 } 185 } 186 187 188 public void setFileFilter(FileFilter filter) { _fileFilter = filter; } 189 190 public void setToolTipText(String text) { 191 super.setToolTipText(text); 192 _fileField.setToolTipText(text); 193 _chooser.setToolTipText(text); 194 } 195 196 197 private void _chooseFile() { 198 if (_file != null && _file.exists()) { 200 _chooser.setCurrentDirectory(_file); 201 _chooser.setSelectedFile(_file); 202 } 203 204 if (_fileFilter != null) _chooser.setFileFilter(_fileFilter); 206 207 int returnValue = _chooser.showDialog(_parent, null); 209 if (returnValue == JFileChooser.APPROVE_OPTION) { 210 File chosen = _chooser.getSelectedFile(); 211 if (chosen != null) setFileField(chosen); 212 } 213 } 214 215 220 223 public boolean validateTextField() { 224 227 String newValue = _fileField.getText().trim(); 228 229 File newFile = null; 230 if (!newValue.equals("")) 231 newFile = new File(newValue); 232 233 if (newFile != null && _mustExist && !newFile.exists()) { 234 JOptionPane.showMessageDialog(_parent, "The file '"+ newValue + "'\nis invalid because it does not exist.", 235 "Invalid File Name", JOptionPane.ERROR_MESSAGE); 236 if (!_file.exists()) _file = null; 237 resetFileField(); 239 return false; 241 } 242 else { 243 setFileField(newFile); 244 return true; 246 } 247 } 248 } | Popular Tags |