001 /* JAPI - (Yet another (hopefully) useful) Java API 002 * 003 * Copyright (C) 2004-2006 Christian Hujer 004 * 005 * This program is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU General Public License as 007 * published by the Free Software Foundation; either version 2 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 * 02111-1307, USA. 019 */ 020 021 package net.sf.japi.swing; 022 023 import java.awt.Insets; 024 import java.io.File; 025 import java.io.IOException; 026 import javax.swing.JButton; 027 import javax.swing.JFileChooser; 028 import javax.swing.JTextField; 029 030 /** JButton for choosing a file from hd. 031 * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a> 032 */ 033 public class JFileChooserButton extends JButton { 034 035 /** Action Factory. */ 036 private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("net.sf.japi.swing"); 037 038 /** The JTextField to read/write the file path to. 039 * @serial include 040 */ 041 private JTextField textField; 042 043 /** The file selection mode. 044 * @serial include 045 */ 046 private int fileSelectionMode; 047 048 /** The base directory for choosing files. 049 * @serial include 050 */ 051 private File base; 052 053 /** The JFileChooser to use. 054 * @serial include 055 */ 056 private JFileChooser chooser; 057 058 /** Create a ChooseButton. 059 * A default chooser is used. 060 * The current working directory is used as base directory. 061 * @param textField JTextField to create button for 062 * @param fileSelectionMode see {@link JFileChooser} 063 */ 064 public JFileChooserButton(final JTextField textField, final int fileSelectionMode) { 065 this(new File(System.getProperty("user.dir")), null, textField, fileSelectionMode); 066 } 067 068 /** Create a ChooseButton. 069 * A default chooser is used. 070 * @param base base directory to use 071 * @param textField JTextField to create button for 072 * @param fileSelectionMode see {@link JFileChooser} 073 */ 074 public JFileChooserButton(final File base, final JTextField textField, final int fileSelectionMode) { 075 this(base, null, textField, fileSelectionMode); 076 } 077 078 /** Create a ChooseButton. 079 * A default chooser is used. 080 * The current working directory is used as base directory. 081 * @param chooser JFileChooser to associate with 082 * @param textField JTextField to create button for 083 * @param fileSelectionMode see {@link JFileChooser} 084 */ 085 public JFileChooserButton(final JFileChooser chooser, final JTextField textField, final int fileSelectionMode) { 086 this(new File(System.getProperty("user.dir")), chooser, textField, fileSelectionMode); 087 } 088 089 /** Create a ChooserButton. 090 * @param base base directory to use 091 * @param chooser JFileChooser to associate with 092 * @param textField JTextField to create button for 093 * @param fileSelectionMode see {@link JFileChooser} 094 */ 095 public JFileChooserButton(final File base, final JFileChooser chooser, final JTextField textField, final int fileSelectionMode) { 096 setAction(ACTION_FACTORY.createAction(false, "optionsChooseFile", this)); 097 this.textField = textField; 098 this.fileSelectionMode = fileSelectionMode; 099 setMargin(new Insets(0, 0, 0, 0)); 100 setChooser(chooser); 101 setBase(base); 102 } 103 104 /** Set the JFileChooser associated with this JFileChooserButton. 105 * @param chooser new JFileChooser to associate or <code>null</code> to instruct the button to create its own 106 */ 107 public void setChooser(final JFileChooser chooser) { 108 this.chooser = chooser != null ? chooser : new JFileChooser(); 109 } 110 111 /** Set the base directory to choose files from. 112 * This method always tries to convert the base file to canonical form. 113 * If that fails, the base file is converted to absolute form instead. 114 * @param base directory to choose files from 115 */ 116 public void setBase(final File base) { 117 try { 118 this.base = base.getCanonicalFile(); 119 } catch (final IOException e) { 120 this.base = base.getAbsoluteFile(); 121 } 122 } 123 124 /** Get the base directory to choose files from. 125 * @return base directrory to which chosen files are resolved 126 */ 127 public File getBase() { 128 return base; 129 } 130 131 /** Get the JFileChooser associated with this JFileChooserButton. 132 * @return associated JFileChooser 133 */ 134 public JFileChooser getChooser() { 135 return chooser; 136 } 137 138 /** Action method. 139 * @used 140 */ 141 public void optionsChooseFile() { 142 final String oldFilename = textField.getText(); 143 final File oldFile = new File(base, oldFilename); 144 chooser.setFileSelectionMode(fileSelectionMode); 145 chooser.setMultiSelectionEnabled(false); 146 if (oldFilename.length() > 0) { 147 chooser.setCurrentDirectory(oldFile.getParentFile()); 148 chooser.setSelectedFile(oldFile); 149 } else { 150 chooser.setCurrentDirectory(base); 151 chooser.setSelectedFile(null); 152 } 153 final int returnVal = chooser.showOpenDialog(this); 154 if (returnVal == JFileChooser.APPROVE_OPTION) { 155 textField.setText(chooser.getSelectedFile().getPath()); 156 } 157 } 158 159 } // class JFileChooserButton