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 @ 306

History | View | Annotate | Download (5.85 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 writable
72
         * @param dynField
73
         * @throws ServiceException
74
         */
75
        public JFileDynfieldComponent(ValueField parent)
76
        throws ServiceException {
77
                super(parent);
78
    }
79

    
80
    @Override
81
    protected void afterUI() {
82
        // TODO Auto-generated method stub
83

    
84
    }
85

    
86
    public void focusGained(FocusEvent e) {
87
        previousText = tfFile.getText();
88
    }
89

    
90
    public void focusLost(FocusEvent e) {
91
        // if (!this.isValid())
92
        // tfFile.setText(previousText);
93
        previousText = tfFile.getText();
94
        this.file = new File(previousText);
95
        this.fireValueChangedEvent();
96
    }
97

    
98
    public JComponent asJComponent() {
99
        return panel;
100
    }
101

    
102
    public Object getValue() {
103
        return file;
104
    }
105

    
106
    @Override
107
    protected void initData() {
108
        previousText = "";
109
    }
110

    
111
    @Override
112
    protected void initUI() {
113
        tfFile = new JTextField();
114
        tfFile.addKeyListener(this);
115
        panel = this.createBoxLayout(tfFile,getJBtnChangeFile() , 1, 1, null);
116
        initBtnChangeFile();
117
    }
118

    
119
    /**
120
     * This method initializes jButton
121
     * 
122
     * @return javax.swing.JButton
123
     */
124
    private JButton getJBtnChangeFile() {
125
        if (jBtnChangeFile == null) {
126
            jBtnChangeFile = new JButton();
127
            jBtnChangeFile.setText("..."); //$NON-NLS-1$
128
            jBtnChangeFile.setPreferredSize(new java.awt.Dimension(26, 26));
129
        }
130
        return jBtnChangeFile;
131
    }
132
    
133
    private void initBtnChangeFile() {
134
        getJBtnChangeFile().addActionListener(new ActionListener() {
135

    
136
            public void actionPerformed(ActionEvent e) {
137
                showResultProviderDialog();
138
            }
139

    
140
        });
141
    }
142

    
143
    @Override
144
    public boolean isMandatory() {
145
        return this.getDynField().isMandatory();
146
    }
147

    
148
    private void refresh() {
149
        if (file != null)
150
            tfFile.setText(file.getAbsolutePath());
151
        else
152
            tfFile.setText("");
153
    }
154

    
155
    public void requestFocus() {
156
        tfFile.requestFocus();
157
    }
158

    
159
    public void setEnabled(boolean isEnabled) {
160
        tfFile.setEnabled(isEnabled);
161
        this.getJBtnChangeFile().setEnabled(isEnabled);
162
    }
163

    
164
    @Override
165
    protected void setJDynFieldComponentListeners() {
166
        tfFile.addFocusListener(this);
167
    }
168

    
169
    @Override
170
    protected void setReadOnly() {
171
        tfFile.setEditable(false);
172
    }
173

    
174
    protected void setNullValue() {
175
        file = null;
176
        this.refresh();
177
    }
178

    
179
    protected void setNonNullValue(Object value) {
180
        file = (File) value;
181
        this.refresh();
182
    }
183

    
184
    public void showResultProviderDialog() {
185
        fc =  new JFileChooser(this);
186
        fc.alignToField(this);
187
        fc.showDialog();
188
    }
189

    
190
    public void keyPressed(KeyEvent e) {
191
        // TODO Auto-generated method stub
192

    
193
    }
194

    
195
    public void keyReleased(KeyEvent e) {
196
        // TODO Auto-generated method stub
197

    
198
    }
199

    
200
    public void keyTyped(KeyEvent e) {
201
       isValid();
202
       this.fireValueChangedEvent();
203
    }
204

    
205
    /**
206
     * @param path
207
     */
208
    private boolean checkPath(String path) {
209
        if ((path==null)||(path.equals(""))){
210
            if (this.getDynField().isMandatory())
211
                return false;
212
            return true;
213
        }
214
        java.io.File f = new File(path);
215
        this.file = f;        
216
        return true;
217
    }
218

    
219
    @Override
220
    public boolean isValid() {
221
        String path = this.tfFile.getText();
222
        return checkPath(path);
223
    }
224

    
225
}