svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / openfile / FileTextField.java @ 40561
History | View | Annotate | Download (6.65 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.openfile; |
25 |
|
26 |
import java.awt.GridBagConstraints; |
27 |
import java.awt.GridBagLayout; |
28 |
import java.awt.Insets; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
import java.awt.event.FocusEvent; |
32 |
import java.awt.event.FocusListener; |
33 |
import java.io.File; |
34 |
import java.util.ArrayList; |
35 |
|
36 |
import javax.swing.JButton; |
37 |
import javax.swing.JPanel; |
38 |
import javax.swing.JTextField; |
39 |
|
40 |
import org.gvsig.gui.beans.Messages; |
41 |
import org.gvsig.gui.beans.swing.JFileChooser; |
42 |
|
43 |
public class FileTextField extends JPanel { |
44 |
private static final long serialVersionUID = 1L; |
45 |
private JTextField tf_fileName = null; |
46 |
private JButton bt_chooseFile = null; |
47 |
private File selectedFile = null; |
48 |
private ArrayList enabledListeners = new ArrayList(); |
49 |
private JFileChooser jfc; |
50 |
/**
|
51 |
* Used to create file choosers with 'memory'
|
52 |
*/
|
53 |
private String JFC_ID = null; |
54 |
|
55 |
public static final int ACTION_EVENT_FIELD_DISABLED = 0; |
56 |
public static final int ACTION_EVENT_FIELD_ENABLED = 1; |
57 |
public static final int ACTION_EVENT_VALUE_CHANGED = 1; |
58 |
|
59 |
public FileTextField() {
|
60 |
super();
|
61 |
JFC_ID = this.getClass().getName();
|
62 |
initializeUI(); |
63 |
} |
64 |
|
65 |
public FileTextField(String id) { |
66 |
super();
|
67 |
JFC_ID = id; |
68 |
initializeUI(); |
69 |
} |
70 |
|
71 |
private void initializeUI() { |
72 |
jfc = new JFileChooser(JFC_ID, (String) null); |
73 |
setLayout(new GridBagLayout()); |
74 |
|
75 |
GridBagConstraints constraints = new GridBagConstraints(); |
76 |
constraints.gridx = 0;
|
77 |
constraints.gridy = 0;
|
78 |
constraints.gridwidth = 1;
|
79 |
constraints.gridheight = 1;
|
80 |
constraints.fill = GridBagConstraints.BOTH;
|
81 |
constraints.anchor = GridBagConstraints.WEST;
|
82 |
constraints.weightx = 1.0;
|
83 |
constraints.weighty = 1.0;
|
84 |
constraints.insets = new Insets(0,0,0,4); |
85 |
this.add(getNameField(), constraints);
|
86 |
|
87 |
getChooseFileButton().addActionListener(new ActionListener() { |
88 |
|
89 |
public void actionPerformed(ActionEvent e) { |
90 |
if (e.getSource()==getChooseFileButton()) {
|
91 |
int action = jfc.showDialog(FileTextField.this, Messages.getText("Open")); |
92 |
if (action == JFileChooser.APPROVE_OPTION) { |
93 |
setSelectedFile(jfc.getSelectedFile()); |
94 |
setEnabled(true);
|
95 |
} |
96 |
} |
97 |
} |
98 |
}); |
99 |
constraints.gridx = 1;
|
100 |
constraints.gridy = 0;
|
101 |
constraints.gridwidth = 1;
|
102 |
constraints.gridheight = 1;
|
103 |
constraints.fill = GridBagConstraints.BOTH;
|
104 |
constraints.anchor = GridBagConstraints.WEST;
|
105 |
constraints.weightx = 0.0;
|
106 |
constraints.weighty = 1.0;
|
107 |
constraints.insets = new Insets(0,0,0,0); |
108 |
this.add(getChooseFileButton(), constraints);
|
109 |
} |
110 |
|
111 |
private JTextField getNameField() { |
112 |
if (tf_fileName==null) { |
113 |
tf_fileName = new JTextField(); |
114 |
tf_fileName.addFocusListener(new FocusListener() { |
115 |
public void focusGained(FocusEvent e) {} |
116 |
|
117 |
public void focusLost(FocusEvent e) { |
118 |
updateSelectedFile(); |
119 |
} |
120 |
}); |
121 |
} |
122 |
return tf_fileName;
|
123 |
} |
124 |
|
125 |
private JButton getChooseFileButton() { |
126 |
if (bt_chooseFile==null) { |
127 |
bt_chooseFile = new JButton("..."); |
128 |
} |
129 |
return bt_chooseFile;
|
130 |
} |
131 |
|
132 |
|
133 |
public void setSelectedFile(File file) { |
134 |
File oldFile = selectedFile;
|
135 |
selectedFile = normalizeExtension(file); |
136 |
getNameField().setText(selectedFile.toString()); |
137 |
if (file.isDirectory()) {
|
138 |
jfc.setLastPath(file); |
139 |
} |
140 |
else {
|
141 |
jfc.setSelectedFile(file); |
142 |
jfc.setLastPath(file.getParentFile()); |
143 |
} |
144 |
fireValueChanged(oldFile, file); |
145 |
} |
146 |
|
147 |
public File getSelectedFile() { |
148 |
return updateSelectedFile();
|
149 |
} |
150 |
|
151 |
private File normalizeExtension(File file) { |
152 |
javax.swing.filechooser.FileFilter filter = (javax.swing.filechooser.FileFilter) jfc.getFileFilter(); |
153 |
if (!filter.accept(file)) {
|
154 |
String path = file.getPath();
|
155 |
if (filter instanceof FileFilter) { |
156 |
FileFilter ourFilter = (FileFilter) filter; |
157 |
if (path.endsWith(".")) { |
158 |
path = path+ourFilter.getDefaultExtension(); |
159 |
} |
160 |
else {
|
161 |
path = path+"."+ourFilter.getDefaultExtension();
|
162 |
} |
163 |
file = new File(path); |
164 |
} |
165 |
} |
166 |
return file;
|
167 |
} |
168 |
|
169 |
private File updateSelectedFile() { |
170 |
File oldFile = selectedFile;
|
171 |
String text = getNameField().getText();
|
172 |
if ( (oldFile!=null && !oldFile.getPath().equals(text)) |
173 |
|| ((oldFile==null) && !text.equals(""))){ |
174 |
File newFile = normalizeExtension(new File(getNameField().getText())); |
175 |
selectedFile = newFile; |
176 |
fireValueChanged(oldFile, newFile); |
177 |
} |
178 |
return selectedFile;
|
179 |
} |
180 |
|
181 |
protected void fireValueChanged(File oldValue, File newValue) { |
182 |
firePropertyChange("selectedFileChanged", oldValue, newValue);
|
183 |
} |
184 |
|
185 |
protected void fireEnabledChanged(boolean oldValue, boolean newValue) { |
186 |
firePropertyChange("enabled", oldValue, newValue);
|
187 |
} |
188 |
|
189 |
public void setEnabled(boolean enabled) { |
190 |
boolean oldValue = isEnabled();
|
191 |
super.setEnabled(enabled);
|
192 |
getNameField().setEnabled(enabled); |
193 |
getChooseFileButton().setEnabled(enabled); |
194 |
fireEnabledChanged(oldValue, enabled); |
195 |
} |
196 |
|
197 |
public javax.swing.filechooser.FileFilter getFileFilter() {
|
198 |
return jfc.getFileFilter();
|
199 |
} |
200 |
|
201 |
public int getFileSelectionMode() { |
202 |
return jfc.getFileSelectionMode();
|
203 |
} |
204 |
|
205 |
public boolean removeChoosableFileFilter(FileFilter f) { |
206 |
return jfc.removeChoosableFileFilter(f);
|
207 |
} |
208 |
|
209 |
public void setFileFilter(FileFilter filter) { |
210 |
|
211 |
jfc.setFileFilter(filter); |
212 |
|
213 |
} |
214 |
|
215 |
public void addChoosableFileFilter(FileFilter filter) { |
216 |
jfc.addChoosableFileFilter(filter); |
217 |
} |
218 |
|
219 |
public FileFilter getAcceptAllFileFilter() { |
220 |
return (FileFilter) jfc.getAcceptAllFileFilter(); |
221 |
} |
222 |
|
223 |
public boolean isAcceptAllFileFilterUsed() { |
224 |
return jfc.isAcceptAllFileFilterUsed();
|
225 |
} |
226 |
|
227 |
public void resetChoosableFileFilters() { |
228 |
jfc.resetChoosableFileFilters(); |
229 |
} |
230 |
|
231 |
public void setAcceptAllFileFilterUsed(boolean b) { |
232 |
jfc.setAcceptAllFileFilterUsed(b); |
233 |
} |
234 |
|
235 |
} |