Revision 24558

View differences:

trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/openfile/FileFilter.java
1
package org.gvsig.gui.beans.openfile;
2

  
3
import java.io.File;
4

  
5
public abstract class FileFilter extends javax.swing.filechooser.FileFilter {
6

  
7
	/**
8
	 * <p>Gets the default file extension, which will be
9
	 * added to the selected file if its extension is not
10
	 * valid.</p>
11
	 * 
12
	 * @return
13
	 */
14
	public abstract String getDefaultExtension();
15
}
0 16

  
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/openfile/FileTextField.java
1
package org.gvsig.gui.beans.openfile;
2

  
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.Insets;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.awt.event.FocusEvent;
9
import java.awt.event.FocusListener;
10
import java.io.File;
11
import java.util.ArrayList;
12

  
13
import javax.swing.JButton;
14
import javax.swing.JPanel;
15
import javax.swing.JTextField;
16

  
17
import org.gvsig.gui.beans.Messages;
18
import org.gvsig.gui.beans.swing.JFileChooser;
19

  
20
public class FileTextField extends JPanel {
21
	private static final long serialVersionUID = 1L;
22
	private JTextField tf_fileName = null;
23
	private JButton bt_chooseFile = null;
24
	private File selectedFile = null;
25
	private ArrayList enabledListeners = new ArrayList();
26
	private JFileChooser jfc;
27
	/**
28
	 * Used to create file choosers with 'memory'
29
	 */
30
	private String JFC_ID = null;
31

  
32
	public static final int ACTION_EVENT_FIELD_DISABLED = 0;
33
	public static final int ACTION_EVENT_FIELD_ENABLED = 1;
34
	public static final int ACTION_EVENT_VALUE_CHANGED = 1;
35

  
36
	public FileTextField() {
37
		super();
38
		JFC_ID = this.getClass().getName();
39
		initializeUI();
40
	}
41

  
42
	public FileTextField(String id) {
43
		super();
44
		JFC_ID = id;
45
		initializeUI();
46
	}
47

  
48
	private void initializeUI() {
49
		 jfc = new JFileChooser(JFC_ID, (String) null);
50
		setLayout(new GridBagLayout());
51

  
52
		GridBagConstraints constraints = new GridBagConstraints();
53
		constraints.gridx = 0;
54
		constraints.gridy = 0;
55
		constraints.gridwidth = 1;
56
		constraints.gridheight = 1;
57
		constraints.fill = GridBagConstraints.BOTH;
58
		constraints.anchor = GridBagConstraints.WEST;
59
		constraints.weightx = 1.0;
60
		constraints.weighty = 1.0;
61
		constraints.insets = new Insets(0,0,0,4);
62
		this.add(getNameField(), constraints);
63

  
64
		getChooseFileButton().addActionListener(new ActionListener() {
65

  
66
			public void actionPerformed(ActionEvent e) {
67
				if (e.getSource()==getChooseFileButton()) {
68
					int action = jfc.showDialog(FileTextField.this, Messages.getText("Open"));
69
					if (action == JFileChooser.APPROVE_OPTION) {
70
						setSelectedFile(jfc.getSelectedFile());
71
						setEnabled(true);	
72
					}
73
				}
74
			}
75
		});
76
		constraints.gridx = 1;
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 = 0.0;
83
		constraints.weighty = 1.0;
84
		constraints.insets = new Insets(0,0,0,0);
85
		this.add(getChooseFileButton(), constraints);
86
	}
87

  
88
	private JTextField getNameField() {
89
		if (tf_fileName==null) {
90
			tf_fileName = new JTextField();
91
			tf_fileName.addFocusListener(new FocusListener() {
92
				public void focusGained(FocusEvent e) {}
93

  
94
				public void focusLost(FocusEvent e) {
95
					updateSelectedFile();
96
				}
97
			});
98
		}
99
		return tf_fileName;
100
	}
101
	
102
	private JButton getChooseFileButton() {
103
		if (bt_chooseFile==null) {
104
			bt_chooseFile = new JButton("...");
105
		}
106
		return bt_chooseFile;
107
	}
108

  
109

  
110
	public void setSelectedFile(File file) {
111
		File oldFile = selectedFile;
112
		selectedFile = normalizeExtension(file);
113
		getNameField().setText(selectedFile.toString());
114
		if (file.isDirectory()) {
115
			jfc.setLastPath(file);
116
		}
117
		else {
118
			jfc.setLastPath(file.getParentFile());
119
		}
120
		fireValueChanged(oldFile, file);
121
	}
122

  
123
	public File getSelectedFile() {
124
		return updateSelectedFile();
125
	}
126
	
127
	private File normalizeExtension(File file) {
128
		FileFilter filter = (FileFilter) jfc.getFileFilter();
129
		if (!filter.accept(file)) {
130
			String path = file.getPath();
131
			if (path.endsWith(".")) {
132
				path = path+filter.getDefaultExtension();
133
			}
134
			else {
135
				path = path+"."+filter.getDefaultExtension();
136
			}
137
			file = new File(path);
138
		}
139
		return file;	
140
	}
141
	
142
	private File updateSelectedFile() {
143
		File oldFile = selectedFile;
144
		String text = getNameField().getText();
145
		if ( (oldFile!=null && !oldFile.getPath().equals(text))
146
				|| ((oldFile==null) && !text.equals(""))){
147
			File newFile = normalizeExtension(new File(getNameField().getText()));
148
			selectedFile = newFile;
149
			fireValueChanged(oldFile, newFile);
150
		}
151
		return selectedFile;
152
	}
153

  
154
	protected void fireValueChanged(File oldValue, File newValue) {
155
		firePropertyChange("selectedFileChanged", oldValue, newValue);
156
	}
157

  
158
	protected void fireEnabledChanged(boolean oldValue, boolean newValue) {
159
		firePropertyChange("enabled", oldValue, newValue);
160
	}
161

  
162
	public void setEnabled(boolean enabled) {
163
		boolean oldValue = isEnabled();
164
		super.setEnabled(enabled);
165
		getNameField().setEnabled(enabled);
166
		fireEnabledChanged(oldValue, enabled);
167
	}
168

  
169
	public FileFilter getFileFilter() {
170
		return (FileFilter) jfc.getFileFilter();
171
	}
172

  
173
	public int getFileSelectionMode() {
174
		return jfc.getFileSelectionMode();
175
	}
176

  
177
	public boolean removeChoosableFileFilter(FileFilter f) {
178
		return jfc.removeChoosableFileFilter(f);
179
	}
180

  
181
	public void setFileFilter(FileFilter filter) {
182

  
183
		jfc.setFileFilter(filter);
184
		
185
	}
186

  
187
	public void addChoosableFileFilter(FileFilter filter) {
188
		jfc.addChoosableFileFilter(filter);
189
	}
190

  
191
	public FileFilter getAcceptAllFileFilter() {
192
		return (FileFilter) jfc.getAcceptAllFileFilter();
193
	}
194

  
195
	public boolean isAcceptAllFileFilterUsed() {
196
		return jfc.isAcceptAllFileFilterUsed();
197
	}
198

  
199
	public void resetChoosableFileFilters() {
200
		jfc.resetChoosableFileFilters();
201
	}
202

  
203
	public void setAcceptAllFileFilterUsed(boolean b) {
204
		jfc.setAcceptAllFileFilterUsed(b);
205
	}
206
	
207
}
0 208

  

Also available in: Unified diff