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 46 public class FileSelectorStringComponent extends JPanel { 47 48 49 public static final int DEFAULT_NUM_COLS = 30; 50 51 52 public static final float DEFAULT_FONT_SIZE = 10f; 53 54 55 protected final Component _parent; 56 57 58 protected final JTextField _textField; 59 60 61 protected final JButton _chooserButton; 62 63 64 protected final FileChooser _chooser; 65 66 67 protected volatile File _file; 68 69 73 public FileSelectorStringComponent(Component parent, FileChooser chooser) { 74 this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE); 75 } 76 77 83 public FileSelectorStringComponent(Component parent, FileChooser chooser, int numCols, float fontSize) { 84 _parent = parent; 85 _chooser = chooser; 86 _file = null; 87 88 _textField = new JTextField(numCols) { 89 public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); } 90 }; 91 _textField.setFont(_textField.getFont().deriveFont(fontSize)); 92 _textField.setPreferredSize(new Dimension(22,22)); 93 94 _chooserButton = new JButton("..."); 95 _chooserButton.addActionListener(new ActionListener () { 96 public void actionPerformed(ActionEvent e) { _chooseFile(); } 97 }); 98 _chooserButton.setMaximumSize(new Dimension(22, 22)); 99 _chooserButton.setMargin(new Insets(0,5,0,5)); 100 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 102 this.add(_textField); 103 this.add(_chooserButton); 104 } 105 106 public void setEnabled(boolean isEnabled) { 107 _textField.setEnabled(isEnabled); 108 _chooserButton.setEnabled(isEnabled); 109 super.setEnabled(isEnabled); 110 } 111 112 113 public JTextField getTextField() { return _textField; } 114 115 116 public FileChooser getFileChooser() { return _chooser; } 117 118 119 public File convertStringToFile(String s) { 120 s = s.trim(); 121 if (s.equals("")) return null; 122 return new File(s); 123 } 124 125 126 public String convertFileToString(File f) { 127 if (f == null) return ""; 128 return f.toString(); 129 } 130 131 132 public File getFileFromField() { 133 String newValue = _textField.getText(); 135 File newFile = null; 136 if (! newValue.equals("")) { 137 newFile = convertStringToFile(newValue); 138 if (! newFile.isDirectory() && ! _chooser.isFileSelectionEnabled()) newFile = newFile.getParentFile(); 139 } 140 141 if (newFile != null && ! newFile.exists()) newFile = _file; 142 143 return newFile; 144 } 145 146 147 public String getText() { return _textField.getText(); } 148 149 150 public void setText(String s) { _textField.setText(s); } 151 152 155 public void setFileField(File file) { 156 _file = file; 157 if (file != null && ! file.getPath().equals("")) { 158 try { _file = file.getCanonicalFile(); } 159 catch(IOException e) { } 160 } 161 resetFileField(); 162 } 163 164 public void resetFileField() { 165 _textField.setText(convertFileToString(_file)); 166 _textField.setCaretPosition(_textField.getText().length()); 167 } 168 169 public void setToolTipText(String text) { 170 super.setToolTipText(text); 171 _textField.setToolTipText(text); 172 _chooserButton.setToolTipText(text); 173 } 174 175 176 public void addChoosableFileFilter(FileFilter filter) { 177 _chooser.addChoosableFileFilter(filter); 178 } 179 180 181 public void removeChoosableFileFilter(FileFilter filter) { 182 _chooser.removeChoosableFileFilter(filter); 183 } 184 185 public void clearChoosableFileFilters() { 186 _chooser.resetChoosableFileFilters(); 187 } 188 189 190 private void _chooseFile() { 191 File f = getFileFromField(); 192 if (f != null && f.exists()) { 193 _chooser.setCurrentDirectory(f); 194 _chooser.setSelectedFile(f); 195 } 196 int returnValue = _chooser.showDialog(_parent, null); 197 if (returnValue == FileChooser.APPROVE_OPTION) { 198 File chosen = _chooser.getSelectedFile(); 199 if (chosen != null) { setFileField(chosen); } 200 } 201 } 202 203 } | Popular Tags |