View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *  Copyright (C) 2005  Felix Berger
6    *
7    *  This library is free software; you can redistribute it and/or
8    *  modify it under the terms of the GNU Lesser General Public
9    *  License as published by the Free Software Foundation; either
10   *  version 2.1 of the License, or (at your option) any later version.
11   *
12   *  This library is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   *  Lesser General Public License for more details.
16   *
17   *  You should have received a copy of the GNU Lesser General Public
18   *  License along with this library; if not, write to the Free Software
19   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package org.xnap.commons.gui;
22  
23  import java.awt.Component;
24  import java.awt.Dimension;
25  import java.awt.event.ActionEvent;
26  import java.io.File;
27  import javax.swing.Action;
28  import javax.swing.Box;
29  import javax.swing.BoxLayout;
30  import javax.swing.JFileChooser;
31  import javax.swing.JPanel;
32  import javax.swing.JTextField;
33  import org.xnap.commons.gui.action.AbstractXNapAction;
34  import org.xnap.commons.gui.completion.Completion;
35  import org.xnap.commons.gui.completion.FileCompletionModel;
36  import org.xnap.commons.gui.dnd.DefaultTextFieldFileTransferHandler;
37  import org.xnap.commons.i18n.I18n;
38  import org.xnap.commons.i18n.I18nFactory;
39  
40  /***
41   * Provides a panel with a <code>JTextField</code> and a button for file
42   * selection.
43   * 
44   * @author Steffen Pingel
45   * @author Felix Berger
46   */
47  public class FileChooserPanel extends JPanel 
48  {
49      private static I18n i18n = I18nFactory.getI18n(FileChooserPanel.class);
50  	
51      private JTextField filenameTextField;
52  	private FileChooserAction fileChooserAction;
53  	private Component dialogParent = this;
54  	private Completion completion;
55  	private JFileChooser chooser;
56  	
57  	/***
58  	 * Constructs a file chooser panel with an initial text.
59  	 * 
60  	 * @param file the filename to display  
61  	 * can be <code>null</code>
62  	 * @param columns the number of columns for calculating the preferred 
63  	 * width of the text field, can be 0
64  	 * @see #setFile(File)
65  	 */
66  	public FileChooserPanel(File file, int columns) {
67  		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
68  
69  		filenameTextField = new JTextField(columns);
70  		filenameTextField.setMaximumSize
71  			(new Dimension(Integer.MAX_VALUE,
72  						   filenameTextField.getPreferredSize().height));
73  		DefaultTextFieldFileTransferHandler.install(filenameTextField);
74  		add(filenameTextField);
75  
76  		completion = Builder.addCompletion(filenameTextField, 
77  										   new FileCompletionModel());
78  		
79  		add(Box.createHorizontalStrut(4));
80  
81  		fileChooserAction = new FileChooserAction();
82  		add(Builder.createIconButton(fileChooserAction));
83  		setFile(file);
84      }
85  
86  	/***
87  	 * Constructs a file chooser panel.
88  	 * @param columns the number of columns for calculating the preferred 
89  	 * width of the text field, can be 0
90  	 */
91  	public FileChooserPanel(int columns) {
92  		this(null, columns);
93  	}
94  
95      /***
96       * Sub classes can overwrite this.
97       */
98      protected void fileSelected(File file)
99      {
100     }
101 
102     /***
103      * Returns a file having the path name of the currently set text in the
104      * text field.
105      * @return the returned file does not necessarily have to exist; null, 
106      * if no file was entered
107      */
108     public File getFile()
109     {
110 		return (filenameTextField.getText().length() != 0)
111 				? new File(filenameTextField.getText())
112 				: null;
113     }
114 
115     /***
116      * Returns the file chooser that is used to select the file.
117      * @return the file chooser; never returns null
118      */
119 	public JFileChooser getFileChooser()
120 	{
121 		if (chooser == null) {
122 			chooser = new JFileChooser();
123 			// TODO has no effect since we use showSaveDialog
124 			chooser.setApproveButtonText(i18n.tr("OK"));
125 			chooser.setDialogTitle(i18n.tr("Choose File"));
126 		}
127 		return chooser;
128 	}
129     
130     /***
131      * Sets the text in the text field to the absolute path of <code>file</code>.
132      * 
133      * @param file can be <code>null</code> the text field is cleared then.
134      */
135     public void setFile(File file)
136     {
137 		filenameTextField.setText(file != null ? file.getAbsolutePath() : "");
138     }
139 
140     /***
141      * Sets the file chooser to use that is used by the file chooser action. 
142      * @param chooser the file chooser; if null, a default file chooser will be 
143      * created when the action is performed
144      */
145     public void setFileChooser(JFileChooser chooser)
146     {
147     	this.chooser = chooser;
148     }
149     
150     /***
151      * Returns the text that is currently set in the text field of the chooser
152      * panel.
153      * 
154      * <p>Use {@link #getFile()}.
155      *      
156      * @deprecated
157      */
158     public String getFilename()
159     {
160 		return filenameTextField.getText();
161     }
162 
163     /***
164      * Sets the text in the text field.
165      * 
166      * <p>Use {@link #setFile(File)}.
167      * 
168      * @param filename the text
169      * @deprecated
170      */
171     public void setFilename(String filename)
172     {
173 		filenameTextField.setText(filename);
174     }
175 
176     /***
177      * Returns the text field used internally.
178      */
179     public JTextField getTextField()
180     {
181 		return filenameTextField;
182     }
183     
184     /***
185      * Returns the completion object associated with the file text field.
186      * <p>
187      * Thus, you can {@link Completion#setEnabled(boolean) disable} 
188      * completion or 
189      * {@link org.xnap.commons.settings.CompletionModeSetting save}
190      *  the used completion mode in a setting. 
191      */
192     public Completion getCompletion()
193     {
194     	return completion;
195     }
196 	
197 	public Action getFileChooserAction() 
198 	{
199 		return fileChooserAction;
200 	}
201 
202 	/***
203 	 * Returns the parent of the file chooser dialog.
204 	 * 
205 	 * @return the parent of the file chooser dialog
206 	 */
207 	public Component getDialogParent() 
208 	{
209 		return dialogParent;
210 	}
211 	
212 	/***
213 	 * Enables/disables this component and all of its subcomponents.
214 	 */
215 	public void setEnabled(boolean enabled) 
216 	{
217 		super.setEnabled(enabled);
218 		getTextField().setEnabled(enabled);
219 		getFileChooserAction().setEnabled(enabled);
220 	}
221 
222 	/***
223 	 * Sets the parent of the file chooser dialog that is used internally.
224 	 * <p>
225 	 * See {@link JFileChooser#showDialog(java.awt.Component, java.lang.String)}
226 	 * for details on dialog parents.
227 	 */
228 	public void setDialogParent(Component dialogParent)
229 	{
230 		this.dialogParent = dialogParent;
231 	}
232 	
233 	/***
234 	 * The default implementation shows a {@link JFileChooser}.
235 	 * <p>
236 	 * This method can be overriden in a subclass, so other file choosers
237 	 * can be provided
238 	 * @return <code>true</code> if a file was successfully selected and
239 	 * set using {@link #setFile(File)}, <code>false</code> otherwise
240 	 */
241 	protected boolean showChooser()
242 	{
243 		getFileChooser().setSelectedFile(getFile());
244 		if (getFileChooser().showSaveDialog(getDialogParent()) 
245 				== JFileChooser.APPROVE_OPTION) {
246 			File file = chooser.getSelectedFile();
247 			setFile(file);
248 			return true;
249 		}
250 		return false;
251 	}
252 
253     private class FileChooserAction extends AbstractXNapAction 
254     {
255 	
256         public FileChooserAction() 
257 		{
258 			putValue(ICON_FILENAME, "fileopen.png");
259             putValue(SHORT_DESCRIPTION, i18n.tr("Choose a file") );
260 		}
261 
262         public void actionPerformed(ActionEvent event) 
263 		{
264 			if (showChooser()) {
265 				fileSelected(getFile());
266 			}
267 		}
268     }
269 
270 }