1 /* 2 Copyright (C) 2000 - 2007 Grid Systems, S.A. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2, as 6 published by the Free Software Foundation. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 18 package com.gridsystems.config.tools.swing; 19 20 import java.io.File; 21 import java.io.IOException; 22 import javax.swing.filechooser.FileFilter; 23 import javax.swing.JFileChooser; 24 25 /** 26 * Manages a file value asking the user through a file system browser. 27 * 28 * @author SJM 29 */ 30 public class FileChooserField extends PopupTextField { 31 /** 32 * Allows selecting both files and directories. 33 */ 34 public static final int FILES_AND_DIRECTORIES = JFileChooser.FILES_AND_DIRECTORIES; 35 36 /** 37 * Allows selecting only files directories. 38 */ 39 public static final int DIRECTORIES_ONLY = JFileChooser.DIRECTORIES_ONLY; 40 41 /** 42 * Allows selecting only files. 43 */ 44 public static final int FILES_ONLY = JFileChooser.FILES_ONLY; 45 46 /** 47 * Opens the popup as an "Open" file menu (chose an existing file. 48 */ 49 public static final int TYPE_OPEN = JFileChooser.OPEN_DIALOG; 50 51 /** 52 * Opens the popup as a "Save" file menu (chose an existing file. 53 */ 54 public static final int TYPE_SAVE = JFileChooser.SAVE_DIALOG; 55 56 /** 57 * The internal file chooser instance. 58 */ 59 private JFileChooser jFileChooser = new JFileChooser(); 60 61 /** 62 * Constructor. 63 * 64 * Is equivalent to 65 * FileChooserField(null, FileChooserField.FILES_AND_DIRECTORIES) 66 */ 67 public FileChooserField() { 68 this(null, FileChooserField.FILES_AND_DIRECTORIES); 69 } 70 71 /** 72 * Constructor. 73 * 74 * Is equivalent to FileChooserField(<code>initialSelection</code>, 75 * FileChooserField.FILES_AND_DIRECTORIES) 76 * 77 * @param initialSelection a String with the path initially selected. 78 */ 79 public FileChooserField(String initialSelection) { 80 this(initialSelection, FileChooserField.FILES_AND_DIRECTORIES); 81 } 82 83 /** 84 * Constructor. 85 * 86 * Is equivalent to FileChooserField(null,<code>mode</code>) 87 * 88 * @param mode an int with the mode of selection of files. 89 */ 90 public FileChooserField(int mode) { 91 this(null, mode); 92 } 93 94 /** 95 * Constructor. 96 * 97 * @param initialSelection a String with the path initially selected. If it 98 * is not a valid path or the associated file does not exist, it will be 99 * ignored. 100 * @param mode an int with the mode of selection of files. It can be 101 * FILES_AND_DIRECTORIES, FILES_ONLY or DIRECTORIES_ONLY. 102 */ 103 public FileChooserField(String initialSelection, int mode) { 104 super(); 105 106 this.setValue(initialSelection); 107 this.setMode(mode); 108 } 109 110 /** 111 * Opens the file chooser dialog. 112 */ 113 protected void openPopup() { 114 File selectedFile = null; 115 File parent = null; 116 try { 117 String currentPath = (String)this.getValue(); 118 if (currentPath != null) { 119 selectedFile = new File(currentPath); 120 if (!selectedFile.exists()) { 121 selectedFile = null; 122 } else { 123 selectedFile = selectedFile.getCanonicalFile(); 124 parent = selectedFile.getParentFile(); 125 } 126 } 127 } catch (IOException ioe) { 128 selectedFile = null; 129 } 130 this.jFileChooser.setCurrentDirectory(parent); 131 this.jFileChooser.setSelectedFile(selectedFile); 132 133 int action = this.jFileChooser.showOpenDialog(FileChooserField.this); 134 if (action == JFileChooser.APPROVE_OPTION) { 135 FileChooserField.this.setValue( 136 jFileChooser.getSelectedFile().getAbsolutePath()); 137 } 138 } 139 140 /** 141 * Sets the managed value of the object. 142 * 143 * Sets the managed value of the object. This implementation just tries to 144 * use the methods from File to get the canonical path and use it instead of 145 * the value passed as a parameter, if this can not be done the value of the 146 * parameter is used. The internal storage/management of the object does not 147 * change. 148 * 149 * @param value the Object with the value to be managed by the component. If 150 * it is a instance of File, its getCanonicalPath() method is used. If it is 151 * not, the value of toString() is used to try to get the canonical path. If 152 * it fails, then the toString() value is stored as the internal value. 153 */ 154 public void setValue(Object value) { 155 if (value == null) { 156 super.setValue(null); 157 return; 158 } 159 160 if (value instanceof File) { 161 File file = (File) value; 162 try { 163 super.setValue(file.getCanonicalPath()); 164 } catch (IOException ioe) { 165 super.setValue(file.toString()); 166 } 167 return; 168 } 169 170 String path = value.toString(); 171 try { 172 File file = new File(path); 173 super.setValue(file.getCanonicalPath()); 174 return; 175 } catch (IOException ioe) { 176 } 177 super.setValue(path); 178 } 179 180 /** 181 * Gets the selected file. 182 * 183 * @return The selected file 184 */ 185 public File getFile() { 186 String path = super.getValue().toString(); 187 return new File(path); 188 } 189 190 /** 191 * Sets the mode of selection of files. 192 * 193 * @param mode an int with the mode of selection of files. It can be 194 * FILES_AND_DIRECTORIES, FILES_ONLY or DIRECTORIES_ONLY. 195 */ 196 public void setMode(int mode) { 197 this.jFileChooser.setFileSelectionMode(mode); 198 } 199 200 /** 201 * Gets the mode of selection of files. 202 * 203 * @return an int with the mode of selection of files. It can be 204 * FILES_AND_DIRECTORIES, FILES_ONLY or DIRECTORIES_ONLY. 205 */ 206 public int getMode() { 207 return this.jFileChooser.getFileSelectionMode(); 208 } 209 210 /** 211 * Sets the type of menu to show. 212 * 213 * @param type an int with the type of menu to show. It can be TYPE_OPEN or 214 * TYPE_SAVE. 215 */ 216 public void setType(int type) { 217 this.jFileChooser.setDialogType(type); 218 } 219 220 /** 221 * Gets the type of menu to show. 222 * 223 * @return an int with the type of menu to show. It can be TYPE_OPEN or 224 * TYPE_SAVE. 225 */ 226 public int getType() { 227 return this.jFileChooser.getDialogType(); 228 } 229 230 /** 231 * Sets a filter for the files to be displayed at the file system browser. 232 * 233 * @param fileFilter a javax.swing.filechooser.FileFilter that selects the 234 * files to display. 235 * 236 * @see JFileChooser#setFileFilter(FileFilter) 237 */ 238 public void setFilter(FileFilter fileFilter) { 239 this.jFileChooser.setFileFilter(fileFilter); 240 } 241 242 /** 243 * Gets the filter for the files to be displayed at the file system browser. 244 * 245 * @return the javax.swing.filechooser.FileFilter that selects the files to 246 * display. It can be null. 247 * 248 * @see JFileChooser#getFileFilter() 249 */ 250 public FileFilter getFilter() { 251 return this.jFileChooser.getFileFilter(); 252 } 253 } 254