Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.serv / org.gvsig.tools.swing.serv.field / src / main / java / org / gvsig / tools / swing / serv / field / component / JFileDynfieldComponent.java @ 298

History | View | Annotate | Download (8.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
/*
23
 * AUTHORS (In addition to CIT):
24
 * 2010 Institute of New Imaging Technologies (INIT): 
25
 *   http://www.init.uji.es
26
 * Geographic Information research group: 
27
 *   http://www.geoinfo.uji.es
28
 * Universitat Jaume I, Spain
29
 */
30

    
31
/**
32
 * 
33
 */
34
package org.gvsig.tools.swing.serv.field.component;
35

    
36
import java.awt.event.ActionEvent;
37
import java.awt.event.ActionListener;
38
import java.awt.event.FocusEvent;
39
import java.awt.event.FocusListener;
40
import java.awt.event.KeyEvent;
41
import java.awt.event.KeyListener;
42
import java.io.File;
43

    
44
import javax.swing.JButton;
45
import javax.swing.JComponent;
46
import javax.swing.JPanel;
47
import javax.swing.JTextField;
48

    
49
import org.gvsig.tools.service.ServiceException;
50
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
51
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
52
import org.gvsig.tools.swing.components.file.JFileChooser;
53
import org.gvsig.tools.swing.spi.AbstractJDynFieldComponent;
54

    
55
/**
56
 * @author <a href="mailto:reinhold@uji.es">cmartin</a>
57
 * 
58
 */
59
public class JFileDynfieldComponent extends AbstractJDynFieldComponent
60
    implements JDynFieldComponent, FocusListener, KeyListener {
61

    
62
    private JTextField tfFile;
63
    private JPanel panel;
64
    private File file;
65
    private String previousText;
66
    private JFileChooser fc;
67
    private JButton jBtnChangeFile;
68

    
69
    /**
70
     * @param parent
71
     * @param dynField
72
     * @throws ServiceException
73
     */
74
    public JFileDynfieldComponent(ValueField parent) throws ServiceException {
75
        super(parent);
76
    }
77

    
78
    /*
79
     * (non-Javadoc)
80
     * 
81
     * @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#afterUI()
82
     */
83
    @Override
84
    protected void afterUI() {
85
        // TODO Auto-generated method stub
86

    
87
    }
88

    
89
    /*
90
     * (non-Javadoc)
91
     * 
92
     * @see
93
     * org.gvsig.tools.swing.api.dynobject.dynfield.JComponent#getComponent()
94
     */
95
    public JComponent asJComponent() {
96
        return panel;
97
    }
98

    
99
    /**
100
     * @param path
101
     */
102
    private boolean checkPath(String path) {
103
        if ((path == null) || (path.equals(""))) {
104
            if (this.getDynField().isMandatory()) {
105
                return false;
106
            }
107
            return true;
108
        }
109
        java.io.File f = new File(path);
110
        if (f == null) {
111
            return false;
112
        }
113
        this.file = f;
114
        return true;
115
    }
116

    
117
    /*
118
     * (non-Javadoc)
119
     * 
120
     * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
121
     */
122
    public void focusGained(FocusEvent e) {
123
        previousText = tfFile.getText();
124
    }
125

    
126
    /*
127
     * (non-Javadoc)
128
     * 
129
     * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
130
     */
131
    public void focusLost(FocusEvent e) {
132
        // if (!this.isValid())
133
        // tfFile.setText(previousText);
134
        previousText = tfFile.getText();
135
        this.file = new File(previousText);
136
        this.fireValueChangedEvent();
137
    }
138

    
139
    /**
140
     * This method initializes jButton
141
     * 
142
     * @return javax.swing.JButton
143
     */
144
    private JButton getJBtnChangeFile() {
145
        if (jBtnChangeFile == null) {
146
            jBtnChangeFile = new JButton();
147
            jBtnChangeFile.setText("..."); //$NON-NLS-1$
148
            jBtnChangeFile.setPreferredSize(new java.awt.Dimension(26, 26));
149
        }
150
        return jBtnChangeFile;
151
    }
152

    
153
    /*
154
     * (non-Javadoc)
155
     * 
156
     * @see org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getValue()
157
     */
158
    @Override
159
    public Object getValue() {
160
        return file;
161
    }
162

    
163
    private void initBtnChangeFile() {
164
        getJBtnChangeFile().addActionListener(new ActionListener() {
165

    
166
            public void actionPerformed(ActionEvent e) {
167
                showResultProviderDialog();
168
            }
169

    
170
        });
171
    }
172

    
173
    /*
174
     * (non-Javadoc)
175
     * 
176
     * @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#initData()
177
     */
178
    @Override
179
    protected void initData() {
180
        previousText = "";
181
    }
182

    
183
    @Override
184
    protected void initUI() {
185
        tfFile = new JTextField();
186
        tfFile.addKeyListener(this);
187
        panel = this.createBoxLayout(tfFile, getJBtnChangeFile(), 1, 1, null);
188
        initBtnChangeFile();
189
    }
190

    
191
    /*
192
     * (non-Javadoc)
193
     * 
194
     * @see
195
     * 
196
     * 
197
     * 
198
     * 
199
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isMandatory
200
     * ()
201
     */
202
    @Override
203
    public boolean isMandatory() {
204
        return this.getDynField().isMandatory();
205
    }
206

    
207
    /*
208
     * (non-Javadoc)
209
     * 
210
     * @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#isValid()
211
     */
212
    @Override
213
    public boolean isValid() {
214
        String path = this.tfFile.getText();
215
        return checkPath(path);
216
    }
217

    
218
    /*
219
     * (non-Javadoc)
220
     * 
221
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
222
     */
223
    public void keyPressed(KeyEvent e) {
224

    
225
    }
226

    
227
    /*
228
     * (non-Javadoc)
229
     * 
230
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
231
     */
232
    public void keyReleased(KeyEvent e) {
233
        // TODO Auto-generated method stub
234

    
235
    }
236

    
237
    /*
238
     * (non-Javadoc)
239
     * 
240
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
241
     */
242
    public void keyTyped(KeyEvent e) {
243
        if (!isValid()) {
244
            file = null;
245
            this.fireValueChangedEvent();
246
        }
247
    }
248

    
249
    /**
250
     * 
251
     */
252
    private void refresh() {
253
        if (file != null) {
254
            tfFile.setText(file.getAbsolutePath());
255
        } else {
256
            tfFile.setText("");
257
        }
258
    }
259

    
260
    /*
261
     * (non-Javadoc)
262
     * 
263
     * @see
264
     * 
265
     * 
266
     * 
267
     * 
268
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#requestFocus
269
     * ()
270
     */
271
    public void requestFocus() {
272
        tfFile.requestFocus();
273
    }
274

    
275
    /*
276
     * (non-Javadoc)
277
     * 
278
     * @see
279
     * 
280
     * 
281
     * 
282
     * 
283
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setEnabled
284
     * (boolean)
285
     */
286
    public void setEnabled(boolean isEnabled) {
287
        tfFile.setEnabled(isEnabled);
288
        this.getJBtnChangeFile().setEnabled(isEnabled);
289
    }
290

    
291
    /*
292
     * (non-Javadoc)
293
     * 
294
     * @seeorg.gvsig.tools.swing.spi.AbstractJDynFieldComponent#
295
     * setJDynFieldComponentListeners()
296
     */
297
    @Override
298
    protected void setJDynFieldComponentListeners() {
299
        tfFile.addFocusListener(this);
300
    }
301

    
302
    @Override
303
    protected void setNonNullValue(Object value) {
304
        file = (File) value;
305
        this.refresh();
306
    }
307

    
308
    /*
309
     * (non-Javadoc)
310
     * 
311
     * @see
312
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
313
     * .lang.Object)
314
     */
315
    @Override
316
    protected void setNullValue() {
317
        file = null;
318
        this.refresh();
319
    }
320

    
321
    /*
322
     * (non-Javadoc)
323
     * 
324
     * @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#setReadOnly()
325
     */
326
    @Override
327
    protected void setReadOnly() {
328
        tfFile.setEditable(false);
329
    }
330

    
331
    /*
332
     * (non-Javadoc)
333
     * 
334
     * @seeorg.gvsig.tools.swing.serv.field.component.ResultListener#
335
     * showResultProviderDialog()
336
     */
337
    public void showResultProviderDialog() {
338
        fc = new JFileChooser(this);
339
        fc.alignToField(this);
340
        fc.showDialog();
341
    }
342

    
343
}