1
2
3
4
5
6
7 package org.astrogrid.ui;
8
9 import java.awt.BorderLayout;
10 import java.awt.Insets;
11 import java.awt.Dimension;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.io.File;
15 import javax.swing.BorderFactory;
16 import javax.swing.Icon;
17 import javax.swing.JButton;
18 import javax.swing.JFileChooser;
19 import javax.swing.JTextField;
20 import javax.swing.JComboBox;
21 import javax.swing.filechooser.FileFilter;
22
23 /***
24 * A ready made button that when pressed brings up a file selection dialog
25 * box to pick a file for a text field.
26 *
27 * @version %I%
28 * @author M Hill
29 */
30
31 public class JSetFileButton extends JButton implements ActionListener
32 {
33 private JComboBox comboBox = null;
34 private JTextField textField = null;
35 private JFileChooser fileChooser = new JFileChooser();
36
37 public JSetFileButton()
38 {
39 super(IconFactory.getIcon("Open"));
40
41 addActionListener(this);
42
43 if (getIcon() == null)
44 {
45 setText("Set");
46 }
47 else
48 {
49
50
51
52
53
54 setBorder(null);
55
56 }
57 setToolTipText("Choose file from browser");
58 }
59
60 public JSetFileButton(JTextField textField)
61 {
62 this();
63 setTextField(textField);
64 }
65
66
67 /***
68 * Used if special file choosers are required
69 */
70 public void setChooser(JFileChooser specialChooser)
71 {
72 fileChooser = specialChooser;
73 }
74
75 /***
76 * Used to access chooser properties (eg default dir)
77 */
78 public JFileChooser getChooser()
79 {
80 return fileChooser;
81 }
82
83 /***
84 * Used if special file filter is required
85 */
86 public void addChoosableFilter(FileFilter specialFilter)
87 {
88 fileChooser.addChoosableFileFilter(specialFilter);
89 }
90
91 /***
92 * Used if special file filter is required
93 */
94 public void setFilter(FileFilter specialFilter)
95 {
96 fileChooser.setFileFilter(specialFilter);
97 }
98
99
100 /***
101 * Set the input text field that will show the path to the chosen file
102 */
103 public void setTextField(JTextField givenField)
104 {
105 textField = givenField;
106
107 if (getIcon() != null)
108 {
109 setPreferredSize(new Dimension(
110
111 textField.getPreferredSize().height,
112 textField.getPreferredSize().height
113 ));
114 }
115 }
116
117 /***
118 * Set an editable combo box that will show the path to the chosen file
119 */
120 public void setComboBox(JComboBox givenCombo)
121 {
122 comboBox = givenCombo;
123
124 if (getIcon() != null)
125 {
126 setPreferredSize(new Dimension(
127
128 comboBox.getPreferredSize().height,
129 comboBox.getPreferredSize().height
130 ));
131 }
132
133 };
134
135 /***
136 * Invoked when an action occurs - ie wehn the button is pressed. Displays
137 * the file chooser and puts the chosen file into the text field
138 */
139 public void actionPerformed(ActionEvent e)
140 {
141 int returnVal = fileChooser.showDialog(this, "Set");
142
143 if (returnVal == JFileChooser.APPROVE_OPTION)
144 {
145 fileChosen(getChosenFile());
146 }
147 }
148
149
150 /*** Sets the associated UI compronentso to the
151 * given File
152 */
153 protected void fileChosen(File givenFile)
154 {
155 if (textField != null)
156 {
157 textField.setText(givenFile.getAbsolutePath());
158 }
159
160 if (comboBox != null)
161 {
162 comboBox.setSelectedItem(givenFile.getAbsolutePath());
163 }
164 }
165
166 /***
167 * Returns the file chosen
168 */
169 public File getChosenFile()
170 {
171 return fileChooser.getSelectedFile();
172 }
173
174
175 /***
176 * "Test harness"
177 */
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 }
195
196 /***
197 $Log: JSetFileButton.java,v $
198 Revision 1.6 2004/04/15 16:34:53 mch
199 Tidied up, introduced stuff from datacenter ui
200
201 Revision 1.1 2004/03/03 17:40:58 mch
202 Moved ui package
203
204 Revision 1.1 2004/02/17 16:04:06 mch
205 New Desktop GUI
206
207 Revision 1.1.1.1 2003/08/25 18:36:35 mch
208 Reimported to fit It02 source structure
209
210 Revision 1.6 2003/07/03 18:17:08 mch
211 Fixed double-line-spacing
212
213 */
214