Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformfield / File / JDynFormFieldFile.java @ 1782

History | View | Annotate | Download (8.51 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.tools.dynform.services.dynformfield.File;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.awt.event.FocusEvent;
29
import java.awt.event.FocusListener;
30
import java.io.File;
31
import javax.swing.JButton;
32
import javax.swing.JFileChooser;
33
import javax.swing.JPanel;
34
import javax.swing.JTextField;
35
import javax.swing.filechooser.FileFilter;
36
import javax.swing.filechooser.FileNameExtensionFilter;
37
import org.gvsig.tools.dynform.JDynFormField;
38
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
39
import org.gvsig.tools.dynform.spi.dynformfield.JCustomTextField;
40
import org.gvsig.tools.dynobject.DynObject;
41
import org.gvsig.tools.dynobject.Tags;
42
import org.gvsig.tools.service.spi.ServiceManager;
43

    
44
public class JDynFormFieldFile extends AbstractJDynFormField implements JDynFormField, FocusListener {
45

    
46
    protected File assignedValue = null;
47
    protected File currentValue = null;
48
    protected JTextField jtext = null;
49
    protected JButton jbutton = null;
50
    protected boolean readonly = false;
51

    
52
    public JDynFormFieldFile(DynObject parameters,
53
            ServiceManager serviceManager) {
54
        super(parameters, serviceManager);
55
        this.assignedValue = (File) this.getParameterValue();
56
    }
57

    
58
    public void setReadOnly(boolean readonly) {
59
        super.setReadOnly(readonly);
60
        if (this.contents != null) {
61
            if (readonly) {
62
                this.jtext.setEditable(false);
63
                this.jbutton.setEnabled(false);
64
            } else {
65
                this.jtext.setEditable(true);
66
                this.jbutton.setEnabled(true);
67
            }
68
        }
69
    }
70

    
71
    public Object getAssignedValue() {
72
        return this.assignedValue;
73
    }
74

    
75
    public void initComponent() {
76
        this.contents = new JPanel();
77
        this.contents.setLayout(new BorderLayout());
78

    
79
        this.jtext = new JCustomTextField(this.getLabel());
80
        this.jtext.addFocusListener(this);
81
        //this.jtext.setEditable(false);
82
        this.jbutton = new JButton("...");
83
        this.jbutton.addActionListener(new ActionListener() {
84
            public void actionPerformed(ActionEvent e) {
85
                onClickBrowse();
86
            }
87
        });
88

    
89
        this.contents.add(jtext, BorderLayout.CENTER);
90
        this.contents.add(jbutton, BorderLayout.LINE_END);
91
        this.contents.setVisible(true);
92

    
93
        if (this.readonly) {
94
            this.jtext.setEditable(false);
95
            this.jbutton.setEnabled(false);
96
        } else {
97
            this.jtext.setEditable(true);
98
            this.jbutton.setEnabled(true);
99
        }
100
        this.setValue(this.assignedValue);
101
    }
102

    
103
    public void onClickBrowse() {
104
        fireFieldEnterEvent();
105
        File previous = this.currentValue;
106
        
107
        this.problemIndicator().restore();
108

    
109
        File initialPath = null;
110
        FileFilter filter = null;
111
        Tags tags = this.getDefinition().getTags();
112
        if (tags.has("path")) {
113
            initialPath = new File((String) tags.get("path"));
114
        }
115
        if (tags.has("filter")) {
116
            try {
117
                String extensions = (String) tags.get("filter");
118
                String filterDescription = extensions;
119
                if (tags.has("filterDescription")) {
120
                    filterDescription = (String) tags.get("filterDescription");
121
                }
122
                filter = new FileNameExtensionFilter(filterDescription, extensions.split("[|]"));
123
            } catch (Exception ex) {
124
                // Ignore 
125
            }
126
        }
127

    
128
        if (this.currentValue != null) {
129
            initialPath = this.currentValue.getParentFile();
130
        }
131
        File[] x = showOpenFileDialog(this.getLabel(), initialPath, filter);
132
        if (x == null) {
133
            return;
134
        }
135
        this.currentValue = x[0];
136
        this.jtext.setText(x[0].getPath());
137
        this.fireFieldChangedEventIfChanged(previous);
138
    }
139
    
140
    private void fireFieldChangedEventIfChanged(File previous) {
141
        if( previous==null ) {
142
            if( this.currentValue==null ) {
143
                return;
144
            }
145
            this.fireFieldChangedEvent();
146
            return;
147
        } 
148
        if( this.currentValue==null ) {
149
            this.fireFieldChangedEvent();
150
            return;
151
        }
152
        if( previous.getPath().equals(this.currentValue.getPath()) ) {
153
            this.fireFieldChangedEvent();
154
        }
155
    }
156

    
157

    
158
    public void setValue(Object value) {
159
        File previous = this.currentValue;
160
        if (value == null) {
161
            this.jtext.setText("");
162
        } else {
163
            if (!(value instanceof File)) {
164
                logger.info("setValue invoked with non File value (" + value.toString() + ").");
165
                return;
166
            }
167
            this.jtext.setText(((File) value).getPath());
168
        }
169
        this.assignedValue = (File) value;
170
        this.currentValue = this.assignedValue;
171
        this.fireFieldChangedEventIfChanged(previous);
172
    }
173

    
174
    public Object getValue() {
175
        File value = null;
176
        String s = "";
177
        s = this.jtext.getText();
178
        if (s.trim().length() == 0) {
179
            Object x = this.getDefinition().getDefaultValue();
180
            if ((x != null && x instanceof File)) {
181
                value = new File(x.toString());
182
            } else {
183
                value = null;
184
            }
185
        } else {
186
            value = new File(s);
187
        }
188
        return value;
189
    }
190

    
191
    public boolean hasValidValue() {
192
        return true;
193
    }
194

    
195
    public File[] showOpenFileDialog(String title, File initialPath, FileFilter filter) {
196
        return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, filter, true);
197
    }
198

    
199
    public File[] showChooserDialog(
200
            final String title,
201
            final int type, // SAVE_DIALOG / OPEN_DIALOG
202
            final int selectionMode, //    JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES
203
            final boolean multiselection,
204
            final File initialPath,
205
            final FileFilter filter,
206
            final boolean fileHidingEnabled
207
    ) {
208
        File[] returnValue = null;
209

    
210
        JFileChooser fc = new JFileChooser();
211
        fc.setDialogTitle(title);
212
        fc.setDialogType(type);
213
        fc.setFileSelectionMode(selectionMode);
214
        fc.setMultiSelectionEnabled(multiselection);
215
        fc.setCurrentDirectory(initialPath);
216
        fc.setFileFilter(filter);
217
        fc.setFileHidingEnabled(fileHidingEnabled);
218
        int r = JFileChooser.CANCEL_OPTION;
219
        switch (type) {
220
            case JFileChooser.SAVE_DIALOG:
221
                r = fc.showSaveDialog(this.contents);
222
                break;
223
            case JFileChooser.OPEN_DIALOG:
224
            default:
225
                r = fc.showOpenDialog(this.contents);
226
                break;
227
        }
228
        if (r != JFileChooser.APPROVE_OPTION) {
229
            returnValue = null;
230
        } else {
231
            if (fc.isMultiSelectionEnabled()) {
232
                returnValue = fc.getSelectedFiles();
233
            } else {
234
                returnValue = new File[]{fc.getSelectedFile()};
235
            }
236
        }
237
        return returnValue;
238
    }
239

    
240
    public void focusGained(FocusEvent arg0) {
241
        fireFieldEnterEvent();
242
        this.problemIndicator().restore();
243
    }
244

    
245
    public void focusLost(FocusEvent arg0) {
246
        fireFieldExitEvent();
247
    }
248

    
249
    public void clear() {
250
        Object value = this.getDefinition().getDefaultValue();
251
        if (value != null) {
252
            value = value.toString();
253
        } else {
254
            value = "";
255
        }
256
        this.jtext.setText((String) value);
257
    }
258

    
259
}