001 /* $Id: FileField.java 69 2007-10-24 21:57:43Z ringler $
002 * This class is part of the de.mospace.swing library.
003 * Copyright (C) 2005-2006 Moritz Ringler
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
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU 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 02111-1307, USA.
018 */
019 package de.mospace.swing;
020
021 import java.awt.BorderLayout;
022 import java.util.logging.Logger;
023 import java.awt.Color;
024 import java.awt.event.ActionEvent;
025 import java.beans.PropertyChangeEvent;
026 import java.beans.PropertyChangeListener;
027 import java.io.File;
028 import javax.swing.AbstractAction;
029 import javax.swing.BorderFactory;
030 import javax.swing.JButton;
031 import javax.swing.JFileChooser;
032 import javax.swing.JLabel;
033 import javax.swing.JPanel;
034
035
036 /** A titled component that displays a file path that can be modified
037 * by means of an attached JFileChooser.
038 * This class has considerable overlap with PathInput and its use
039 * should be phased out in favour of PathInput.
040 *
041 * @version $Revision: 69 $ ($Date: 2007-10-24 23:57:43 +0200 (Mi, 24 Okt 2007) $)
042 * @author Moritz Ringler
043 * @deprecated use PathInput instead
044 **/
045 public class FileField extends JPanel {
046 private static final Logger logger = Logger.getLogger(FileField.class.getPackage().getName());
047 private final JLabel file;
048
049 /** Constructs a new FileField with the specified title and initial
050 * file path.
051 * @param label the title for this FileField
052 * @param f the inital file, can be <code>null</code>
053 **/
054 public FileField(String label, File f){
055 setLayout(new BorderLayout());
056 JPanel b = new JPanel(new BorderLayout());
057 JLabel text = new JLabel(label);
058 text.setBorder(BorderFactory.createEmptyBorder(2,0,5,0));
059 file = new JLabel((f==null)?"":f.toString()){
060 /* Strangely enough this modified setText
061 * leeds to the JLabel assuming its preferredSize
062 * That effect seems to depend on the registered
063 * propertyChangeListener below
064 */
065 public void setText(String s){
066 super.setText("");
067 super.setText(s);
068 }
069 };
070 file.setHorizontalAlignment(JLabel.LEFT);
071 file.setOpaque(true);
072 file.setBorder(BorderFactory.createMatteBorder(0, 4, 0, 4,Color.white));
073 file.setBackground(Color.white);
074 b.add(file, BorderLayout.CENTER);
075 b.add(new JButton(new AbstractAction(".."){
076 public void actionPerformed(ActionEvent e){
077 JFileChooser jfc = new JFileChooser();
078 try{
079 jfc.setCurrentDirectory(getFile());
080 } catch (Exception ex){
081 logger.warning(ex.toString());
082 }
083 if (jfc.showOpenDialog(FileField.this) == JFileChooser.APPROVE_OPTION){
084 file.setText(jfc.getSelectedFile().getAbsolutePath());
085 }
086 }
087 }), BorderLayout.EAST);
088 add(text, BorderLayout.NORTH);
089 add(b, BorderLayout.CENTER);
090 file.addPropertyChangeListener("text",new PropertyChangeListener(){
091 public void propertyChange(PropertyChangeEvent evt){
092 FileField.this.firePropertyChange(
093 evt.getPropertyName(),
094 evt.getOldValue(),
095 evt.getNewValue()
096 );
097
098 }
099 });
100 }
101
102 /** Sets the file path from a string. If possible {@link #setFile(File)}
103 * should be used instead.
104 * @param s a string value that will be copied to the file field. It is the
105 * caller's responsibility to assure that it represents a valid path.
106 * @see #getText()
107 **/
108 public void setText(String s){
109 file.setText(s);
110 }
111
112 /** Sets the file path of the FileField.
113 * @param f a file object whose path will be copied to the file field
114 * @see #getFile
115 **/
116 public void setFile(final File f){
117 if(f != null){
118 setText(f.toString());
119 }
120 }
121
122 /** Returns the current file path as a string. Consider using
123 * <code>String.valueOf(getFile())</code> instead.
124 *
125 * @return the current file path in string form. It is not guaranteed to
126 * be valid or absolute.
127 * @see #setText(String)
128 * @see #setFile(File)
129 * @see #getFile()
130 **/
131 public String getText(){
132 return file.getText();
133 }
134
135 /** Returns the current file path as a file object.
136 * @return the current file or <code>null</code> if the
137 * current path is invalid or designates a directory
138 * @see #setText(String)
139 * @see #setFile(File)
140 * @see #getText()
141 **/
142 public File getFile(){
143 try {
144 File f = (new File(getText())).getAbsoluteFile();
145 if (f.isDirectory()){
146 return null;
147 }
148 return f;
149 } catch (Exception ex){
150 return null;
151 }
152 }
153
154 }