root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / algorithm / FileSelectionComboPanel.java @ 338
History | View | Annotate | Download (6.77 KB)
1 |
package es.unex.sextante.gui.algorithm; |
---|---|
2 |
|
3 |
import info.clearthought.layout.TableLayout; |
4 |
import info.clearthought.layout.TableLayoutConstants; |
5 |
|
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.io.File; |
9 |
|
10 |
import javax.swing.JButton; |
11 |
import javax.swing.JFileChooser; |
12 |
import javax.swing.JPanel; |
13 |
import javax.swing.JComboBox; |
14 |
|
15 |
/**
|
16 |
* A panel with a combo box and a button, which pops-up a file selection dialog and puts the selected file in the combo box's active item.
|
17 |
*
|
18 |
* @author volaya
|
19 |
*
|
20 |
*/
|
21 |
public class FileSelectionComboPanel |
22 |
extends
|
23 |
JPanel {
|
24 |
|
25 |
private JComboBox comboBox; |
26 |
private JButton button; |
27 |
private final String[] m_sExtensions; |
28 |
private boolean m_bFolder = false; |
29 |
private boolean m_bOpen = false; |
30 |
private final String m_sDescription; |
31 |
private String m_sSelected = null; |
32 |
|
33 |
|
34 |
/**
|
35 |
* Creates a new file selection panel.
|
36 |
*
|
37 |
* @param bFolder
|
38 |
* true if the selection must be a folder instead of a file
|
39 |
* @param bOpen
|
40 |
* true to show a open file dialog. False to show a save file one
|
41 |
* @param sExts
|
42 |
* a list of permitted file extensions
|
43 |
* @param sDescription
|
44 |
* a description of the panel to show in the file dialog
|
45 |
*/
|
46 |
public FileSelectionComboPanel(final boolean bFolder, |
47 |
final boolean bOpen, |
48 |
final String[] sExts, |
49 |
final String sDescription) { |
50 |
|
51 |
super();
|
52 |
|
53 |
m_bFolder = bFolder; |
54 |
m_bOpen = bOpen; |
55 |
m_sExtensions = sExts; |
56 |
m_sDescription = sDescription; |
57 |
|
58 |
initGUI(); |
59 |
|
60 |
} |
61 |
|
62 |
|
63 |
/**
|
64 |
* Creates a new file selection panel. Allows just one extension.
|
65 |
*
|
66 |
* @param bFolder
|
67 |
* true if the selection must be a folder instead of a file
|
68 |
* @param bOpen
|
69 |
* true to show a open file dialog. False to show a save file one
|
70 |
* @param sExt
|
71 |
* a file extension
|
72 |
* @param sDescription
|
73 |
* a description of the panel to show in the file dialog
|
74 |
*/
|
75 |
public FileSelectionComboPanel(final boolean bFolder, |
76 |
final boolean bOpen, |
77 |
final String sExt, |
78 |
final String sDescription) { |
79 |
|
80 |
this(bFolder, bOpen, new String[] { sExt }, sDescription); |
81 |
|
82 |
} |
83 |
|
84 |
|
85 |
/**
|
86 |
* Creates a new file selection panel
|
87 |
*
|
88 |
* @param bFolder
|
89 |
* true if the selection must be a folder instead of a file
|
90 |
* @param bOpen
|
91 |
* true to show a open file dialog. False to show a save file one
|
92 |
* @param sExt
|
93 |
* the permitted file extension
|
94 |
* @param sDescription
|
95 |
* a description of the panel to show in the file dialog
|
96 |
* @param sSelection
|
97 |
* the name of the default selected file or folder
|
98 |
*/
|
99 |
public FileSelectionComboPanel(final boolean bFolder, |
100 |
final boolean bOpen, |
101 |
final String sExt, |
102 |
final String sDescription, |
103 |
final String sSelection) { |
104 |
|
105 |
this(bFolder, bOpen, new String[] { sExt }, sDescription); |
106 |
m_sSelected = sSelection; |
107 |
|
108 |
} |
109 |
|
110 |
|
111 |
/**
|
112 |
* Creates a new file selection panel
|
113 |
*
|
114 |
* @param bFolder
|
115 |
* true if the selection must be a folder instead of a file
|
116 |
* @param bOpen
|
117 |
* true to show a open file dialog. False to show a save file one
|
118 |
* @param sExt
|
119 |
* a file extensions
|
120 |
* @param sDescription
|
121 |
* a description of the panel to show in the file dialog
|
122 |
* @param sSelection
|
123 |
* the name of the default selected file or folder
|
124 |
*/
|
125 |
public FileSelectionComboPanel(final boolean bFolder, |
126 |
final boolean bOpen, |
127 |
final String sExt[], |
128 |
final String sDescription, |
129 |
final String sSelection) { |
130 |
|
131 |
this(bFolder, bOpen, sExt, sDescription);
|
132 |
m_sSelected = sSelection; |
133 |
|
134 |
} |
135 |
|
136 |
|
137 |
private void initGUI() { |
138 |
|
139 |
button = new JButton("..."); |
140 |
|
141 |
comboBox = new JComboBox(); |
142 |
comboBox.setMaximumSize(new java.awt.Dimension(340, 18)); |
143 |
button.addActionListener(new ActionListener() { |
144 |
public void actionPerformed(final ActionEvent evt) { |
145 |
btnActionPerformed(); |
146 |
} |
147 |
}); |
148 |
|
149 |
final TableLayout thisLayout = new TableLayout(new double[][] { { TableLayoutConstants.FILL, 25.0 }, |
150 |
{ TableLayoutConstants.FILL } }); |
151 |
this.setLayout(thisLayout);
|
152 |
this.add(comboBox, "0, 0"); |
153 |
this.add(button, "1, 0"); |
154 |
|
155 |
} |
156 |
|
157 |
|
158 |
/**
|
159 |
* Returns the filepath currently shown in the text field
|
160 |
*
|
161 |
* @return the filepath currently shown in the text field
|
162 |
*/
|
163 |
public String getFilepath() { |
164 |
|
165 |
return (String) comboBox.getSelectedItem(); |
166 |
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Returns this panel's JTextField widget.
|
171 |
*
|
172 |
* @return the text field widget used to input/store the selected file's path and name
|
173 |
*/
|
174 |
public JComboBox getComboBox() { |
175 |
|
176 |
return (this.comboBox); |
177 |
|
178 |
} |
179 |
|
180 |
|
181 |
/**
|
182 |
* Returns this panel's JButton widget.
|
183 |
*
|
184 |
* @return the button widget used to pop up the file selector widget
|
185 |
*/
|
186 |
public JButton getButton() { |
187 |
|
188 |
return (this.button); |
189 |
|
190 |
} |
191 |
|
192 |
|
193 |
private void btnActionPerformed() { |
194 |
|
195 |
int returnVal;
|
196 |
JFileChooser fc = new JFileChooser(); |
197 |
|
198 |
fc.setDialogTitle(m_sDescription); |
199 |
|
200 |
if (m_bFolder) {
|
201 |
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
202 |
} |
203 |
|
204 |
if (m_sSelected != null) { |
205 |
fc.setSelectedFile(new File(m_sSelected)); |
206 |
} |
207 |
|
208 |
if ((m_sExtensions != null) && !m_bFolder) { |
209 |
fc.setFileFilter(new GenericFileFilter(m_sExtensions, m_sDescription));
|
210 |
} |
211 |
|
212 |
if (m_bOpen) {
|
213 |
returnVal = fc.showOpenDialog(this.getParent().getParent());
|
214 |
} |
215 |
else {
|
216 |
returnVal = fc.showSaveDialog(this.getParent().getParent());
|
217 |
} |
218 |
|
219 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
220 |
comboBox.setSelectedItem(fc.getSelectedFile().getAbsolutePath()); |
221 |
} |
222 |
|
223 |
} |
224 |
|
225 |
|
226 |
/**
|
227 |
* Sets the current filepath to be shown in the text field
|
228 |
*
|
229 |
* @param sFilepath
|
230 |
* the new filepath to set
|
231 |
*/
|
232 |
public void setFilepath(final String sFilepath) { |
233 |
|
234 |
comboBox.setSelectedItem(sFilepath); |
235 |
|
236 |
} |
237 |
|
238 |
|
239 |
@Override
|
240 |
public void setToolTipText(final String sText) { |
241 |
|
242 |
comboBox.setToolTipText(sText); |
243 |
|
244 |
} |
245 |
} |