KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > FileSelectorStringComponent


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.swing;
35
36 import javax.swing.*;
37 import javax.swing.filechooser.FileFilter JavaDoc;
38 import java.awt.*;
39 import java.awt.event.ActionEvent JavaDoc;
40 import java.awt.event.ActionListener JavaDoc;
41 import java.awt.event.FocusListener JavaDoc;
42 import java.awt.event.FocusEvent JavaDoc;
43 import java.io.*;
44
45 /** Just like FileSelectorComponent, but it converts the file to a different string that gets displayed. */
46 public class FileSelectorStringComponent extends JPanel {
47   
48   /** The default number of columns for the text box. */
49   public static final int DEFAULT_NUM_COLS = 30;
50
51   /** The default font size for the text box. */
52   public static final float DEFAULT_FONT_SIZE = 10f;
53
54   /** The parent component of this component. */
55   protected final Component _parent;
56
57   /** Text field with the name of the selected file. */
58   protected final JTextField _textField;
59
60   /** "..." button to open the file chooser. */
61   protected final JButton _chooserButton;
62
63   /** File chooser to open when clicking the "..." button. */
64   protected final FileChooser _chooser;
65
66   /** The current file */
67   protected volatile File _file;
68   
69   /** Creates a new DirectorySelectorStringComponent with default dimensions.
70    * @param parent Parent of this component.
71    * @param chooser File chooser to display from the "..." button. Assumed non-null!
72    */

73   public FileSelectorStringComponent(Component parent, FileChooser chooser) {
74     this(parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE);
75   }
76
77   /** Creates a new DirectorySelectorStringComponent.
78    * @param parent Parent of this component.
79    * @param chooser File chooser to display from the "..." button. Assumed non-null!
80    * @param numCols Number of columns to display in the text field
81    * @param fontSize Font size for the text field
82    */

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 JavaDoc() {
96       public void actionPerformed(ActionEvent JavaDoc e) { _chooseFile(); }
97     });
98     _chooserButton.setMaximumSize(new Dimension(22, 22));
99     _chooserButton.setMargin(new Insets(0,5,0,5));
100     // Add components
101
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   /** Returns the file text field. */
113   public JTextField getTextField() { return _textField; }
114
115   /** Returns the file chooser. */
116   public FileChooser getFileChooser() { return _chooser; }
117
118   /** Converts a string representation from the text field into a File. */
119   public File convertStringToFile(String JavaDoc s) {
120     s = s.trim();
121     if (s.equals("")) return null;
122     return new File(s);
123   }
124
125   /** Converts a file to the string representation of the text field. */
126   public String JavaDoc convertFileToString(File f) {
127     if (f == null) return "";
128     return f.toString();
129   }
130   
131   /** Returns the last file that was selected. */
132   public File getFileFromField() {
133     // Get the file from the chooser
134
String JavaDoc 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   /** Returns the string in the text field. */
147   public String JavaDoc getText() { return _textField.getText(); }
148
149   /** Sets the string in the text field. */
150   public void setText(String JavaDoc s) { _textField.setText(s); }
151   
152   /** Sets the text of the file field to be the given file.
153    * @param file File to display in the file field.
154    */

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) { /* do nothing */ }
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 JavaDoc text) {
170     super.setToolTipText(text);
171     _textField.setToolTipText(text);
172     _chooserButton.setToolTipText(text);
173   }
174
175   /** Adds a filter to decide if a directory can be chosen. */
176   public void addChoosableFileFilter(FileFilter JavaDoc filter) {
177     _chooser.addChoosableFileFilter(filter);
178   }
179   
180   /** Removes the given filefilter from the chooser */
181   public void removeChoosableFileFilter(FileFilter JavaDoc filter) {
182     _chooser.removeChoosableFileFilter(filter);
183   }
184   
185   public void clearChoosableFileFilters() {
186     _chooser.resetChoosableFileFilters();
187   }
188
189   /** Opens the file chooser to select a file, putting the result in the file field. */
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