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 49 public class DirectorySelectorComponent extends JPanel { 50 51 52 public static final int DEFAULT_NUM_COLS = 30; 53 54 55 public static final float DEFAULT_FONT_SIZE = 10f; 56 57 58 protected final Component _parent; 59 60 61 protected final JTextField _fileField; 62 63 64 protected final JButton _chooserButton; 65 66 67 protected final DirectoryChooser _chooser; 68 69 70 protected File _file; 71 72 73 protected boolean _mustExist; 74 75 79 public DirectorySelectorComponent(Component parent, DirectoryChooser chooser) { 80 this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE); 81 } 82 83 89 public DirectorySelectorComponent(Component parent, DirectoryChooser chooser, int numCols, float fontSize) { 90 this(parent, chooser, numCols, fontSize, true); 91 } 92 93 100 public DirectorySelectorComponent(Component parent, DirectoryChooser chooser, int numCols, float fontSize, 101 boolean mustExist) { 102 103 _parent = parent; 104 _chooser = chooser; 105 _file = null; 106 _mustExist = mustExist; 107 108 _fileField = new JTextField(numCols) { 109 public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); } 110 }; 111 112 _fileField.setFont(_fileField.getFont().deriveFont(fontSize)); 113 _fileField.setPreferredSize(new Dimension(22,22)); 114 _fileField.addActionListener(new ActionListener () { 115 public void actionPerformed(ActionEvent e) { validateTextField(); } 116 }); 117 118 _fileField.addFocusListener(new FocusListener () { 119 public void focusGained(FocusEvent e) { } 120 public void focusLost(FocusEvent e) { validateTextField(); } 121 }); 122 123 _chooserButton = new JButton("..."); 124 _chooserButton.addActionListener(new ActionListener () { 125 public void actionPerformed(ActionEvent e) { _chooseFile(); } 126 }); 127 128 _chooserButton.setMaximumSize(new Dimension(22, 22)); 129 _chooserButton.setMargin(new Insets(0,5,0,5)); 130 131 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 133 this.add(_fileField); 134 this.add(_chooserButton); 135 } 136 137 public void setEnabled(boolean isEnabled) { 138 _fileField.setEnabled(isEnabled); 139 _chooserButton.setEnabled(isEnabled); 140 super.setEnabled(isEnabled); 141 } 142 143 144 public JTextField getFileField() { return _fileField; } 145 146 147 public DirectoryChooser getFileChooser() { return _chooser; } 148 149 150 public File getFileFromField() { 151 String txt = _fileField.getText().trim(); 152 if (txt.equals("")) _file = null; 153 else _file = new File(txt); 154 155 return _file; 156 } 157 158 161 public void setFileField(File file) { 162 _file = file; 163 if (file != null && ! file.getPath().equals("")) { 164 try { _file = file.getCanonicalFile(); } 165 catch(IOException e) { } 166 } 167 resetFileField(); 168 } 169 170 public void resetFileField() { 171 if (_file == null) _fileField.setText(""); 172 else { 173 _fileField.setText(_file.toString()); 174 _fileField.setCaretPosition(_fileField.getText().length()); 175 } 176 } 177 178 public void setToolTipText(String text) { 179 super.setToolTipText(text); 180 _fileField.setToolTipText(text); 181 _chooserButton.setToolTipText(text); 182 } 183 184 185 public void addChoosableFileFilter(FileFilter filter) { _chooser.addChoosableFileFilter(filter); } 186 187 188 public void removeChoosableFileFilter(FileFilter filter) { _chooser.removeChoosableFileFilter(filter); } 189 190 public void clearChoosableFileFilters() { _chooser.resetChoosableFileFilters(); } 191 192 195 private boolean _validationInProgress = false; 196 197 200 public boolean validateTextField() { 201 if (_validationInProgress) return true; 202 _validationInProgress = true; 203 204 String newValue = _fileField.getText().trim(); 205 206 File newFile = null; 207 if (! newValue.equals("")) { 208 newFile = new File(newValue); 209 if (! newFile.isDirectory() && _chooser.isFileSelectionEnabled()) newFile = newFile.getParentFile(); 210 } 211 212 if (newFile != null && _mustExist && ! newFile.exists()) { 213 JOptionPane.showMessageDialog(_parent, "The file '"+ newValue + "'\nis invalid because it does not exist.", 214 "Invalid File Name", JOptionPane.ERROR_MESSAGE); 215 resetFileField(); _validationInProgress = false; 217 return false; 218 } 219 else { 220 setFileField(newFile); 221 _validationInProgress = false; 222 return true; 223 } 224 } 225 226 227 private void _chooseFile() { 228 229 int returnValue = _chooser.showDialog(_file); 231 if (returnValue == DirectoryChooser.APPROVE_OPTION) { 232 File chosen = _chooser.getSelectedDirectory(); 233 if (chosen != null) setFileField(chosen); 234 } 235 } 236 237 } | Popular Tags |