Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libUIComponent / src / org / gvsig / gui / beans / openfile / FileTextField.java @ 33964

History | View | Annotate | Download (5.64 KB)

1 28992 cmartinez
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 33684 cordinyana
                    jfc.setSelectedFile(file);
119
            jfc.setLastPath(file.getParentFile());
120 28992 cmartinez
                }
121
                fireValueChanged(oldFile, file);
122
        }
123
124
        public File getSelectedFile() {
125
                return updateSelectedFile();
126
        }
127
128
        private File normalizeExtension(File file) {
129
                javax.swing.filechooser.FileFilter filter = (javax.swing.filechooser.FileFilter) jfc.getFileFilter();
130
                if (!filter.accept(file)) {
131
                        String path = file.getPath();
132
                        if (filter instanceof FileFilter)  {
133
                                FileFilter ourFilter = (FileFilter) filter;
134
                        if (path.endsWith(".")) {
135
                                path = path+ourFilter.getDefaultExtension();
136
                        }
137
                        else {
138
                                path = path+"."+ourFilter.getDefaultExtension();
139
                        }
140
                        file = new File(path);
141
                        }
142
                }
143
                return file;
144
        }
145
146
        private File updateSelectedFile() {
147
                File oldFile = selectedFile;
148
                String text = getNameField().getText();
149
                if ( (oldFile!=null && !oldFile.getPath().equals(text))
150
                                || ((oldFile==null) && !text.equals(""))){
151
                        File newFile = normalizeExtension(new File(getNameField().getText()));
152
                        selectedFile = newFile;
153
                        fireValueChanged(oldFile, newFile);
154
                }
155
                return selectedFile;
156
        }
157
158
        protected void fireValueChanged(File oldValue, File newValue) {
159
                firePropertyChange("selectedFileChanged", oldValue, newValue);
160
        }
161
162
        protected void fireEnabledChanged(boolean oldValue, boolean newValue) {
163
                firePropertyChange("enabled", oldValue, newValue);
164
        }
165
166
        public void setEnabled(boolean enabled) {
167
                boolean oldValue = isEnabled();
168
                super.setEnabled(enabled);
169
                getNameField().setEnabled(enabled);
170
                fireEnabledChanged(oldValue, enabled);
171
        }
172
173
        public javax.swing.filechooser.FileFilter getFileFilter() {
174
                return jfc.getFileFilter();
175
        }
176
177
        public int getFileSelectionMode() {
178
                return jfc.getFileSelectionMode();
179
        }
180
181
        public boolean removeChoosableFileFilter(FileFilter f) {
182
                return jfc.removeChoosableFileFilter(f);
183
        }
184
185
        public void setFileFilter(FileFilter filter) {
186
187
                jfc.setFileFilter(filter);
188
189
        }
190
191
        public void addChoosableFileFilter(FileFilter filter) {
192
                jfc.addChoosableFileFilter(filter);
193
        }
194
195
        public FileFilter getAcceptAllFileFilter() {
196
                return (FileFilter) jfc.getAcceptAllFileFilter();
197
        }
198
199
        public boolean isAcceptAllFileFilterUsed() {
200
                return jfc.isAcceptAllFileFilterUsed();
201
        }
202
203
        public void resetChoosableFileFilters() {
204
                jfc.resetChoosableFileFilters();
205
        }
206
207
        public void setAcceptAllFileFilterUsed(boolean b) {
208
                jfc.setAcceptAllFileFilterUsed(b);
209
        }
210
211
}