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

History | View | Annotate | Download (10.6 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 java.util.Objects;
32
import javax.swing.JButton;
33
import javax.swing.JFileChooser;
34
import javax.swing.JPanel;
35
import javax.swing.event.DocumentEvent;
36
import javax.swing.event.DocumentListener;
37
import javax.swing.filechooser.FileFilter;
38
import javax.swing.filechooser.FileNameExtensionFilter;
39
import javax.swing.text.JTextComponent;
40
import org.apache.commons.lang3.StringUtils;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.dynform.DynFormFieldDefinition;
43
import org.gvsig.tools.dynform.JDynFormField;
44
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
45
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
46
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
47
import org.gvsig.tools.dynobject.Tags;
48

    
49
public class JDynFormFieldFile extends AbstractJDynFormField implements JDynFormField, FocusListener {
50

    
51
    protected File assignedValue = null;
52
    protected File currentValue = null;
53
    protected JTextComponent jtext = null;
54
    protected JButton jbutton = null;
55
    protected boolean readonly = false;
56

    
57
    public JDynFormFieldFile(
58
            DynFormSPIManager serviceManager,
59
            DynFormSPIManager.ComponentsFactory componentsFactory,
60
            JDynFormFieldFactory factory,
61
            DynFormFieldDefinition definition,
62
            Object value
63
    ) {
64
        super(serviceManager, componentsFactory, factory, definition, value);
65
        this.assignedValue = (File) value;
66
    }
67

    
68
    @Override
69
    public void setReadOnly(boolean readonly) {
70
        super.setReadOnly(readonly);
71
        if (this.contents != null) {
72
            if (readonly) {
73
                this.jtext.setEditable(false);
74
                this.jbutton.setEnabled(false);
75
            } else {
76
                this.jtext.setEditable(true);
77
                this.jbutton.setEnabled(true);
78
            }
79
        }
80
    }
81

    
82
    @Override
83
    public Object getAssignedValue() {
84
        return this.assignedValue;
85
    }
86

    
87
    @Override
88
    public void initComponent() {
89
        this.contents = new JPanel();
90
        this.contents.setLayout(new BorderLayout());
91

    
92
        JTextComponent text = this.getComponentsFactory().getJTextField(this.getDefinition(), null);
93
        this.jtext = text;
94
        text.addFocusListener(this);
95
        //this.jtext.setEditable(false);    
96
        text.getDocument().addDocumentListener(new DocumentListener() {
97
            @Override
98
            public void insertUpdate(DocumentEvent e) {
99
                fireFieldChangedEvent();
100
            }
101

    
102
            @Override
103
            public void removeUpdate(DocumentEvent e) {
104
                fireFieldChangedEvent();
105
            }
106

    
107
            @Override
108
            public void changedUpdate(DocumentEvent e) {
109
                fireFieldChangedEvent();
110
            }
111
        });
112

    
113
        this.jbutton = this.getComponentsFactory().getJButton(this.getDefinition(), null);
114
        this.jbutton.addActionListener(new ActionListener() {
115
            @Override
116
            public void actionPerformed(ActionEvent e) {
117
                onClickBrowse();
118
            }
119
        });
120

    
121
        this.contents.add(jtext, BorderLayout.CENTER);
122
        this.contents.add(jbutton, BorderLayout.LINE_END);
123
        this.contents.setVisible(true);
124

    
125
        if (this.readonly) {
126
            this.jtext.setEditable(false);
127
            this.jbutton.setEnabled(false);
128
        } else {
129
            this.jtext.setEditable(true);
130
            this.jbutton.setEnabled(true);
131
        }
132
        this.setValue(this.assignedValue);
133
    }
134

    
135
    public void onClickBrowse() {
136
        fireFieldEnterEvent();
137
        File previous = this.currentValue;
138

    
139
        this.problemIndicator().restore();
140

    
141
        File initialPath = null;
142
        FileFilter filter = null;
143
        Tags tags = this.getDefinition().getTags();
144
        if (tags.has("path")) {
145
            initialPath = new File((String) tags.get("path"));
146
        }
147
        if (tags.has("filter")) {
148
            try {
149
                String extensions = (String) tags.get("filter");
150
                String filterDescription = extensions;
151
                if (tags.has("filterDescription")) {
152
                    filterDescription = (String) tags.get("filterDescription");
153
                }
154
                filter = new FileNameExtensionFilter(filterDescription, extensions.split("[|]"));
155
            } catch (Exception ex) {
156
                // Ignore 
157
            }
158
        }
159

    
160
        if (this.currentValue != null) {
161
            initialPath = this.currentValue.getParentFile();
162
        }
163
        File[] x = showOpenFileDialog(this.getLabel(), initialPath, filter);
164
        if (x == null) {
165
            return;
166
        }
167
        this.currentValue = x[0];
168
        this.jtext.setText(x[0].getPath());
169
        this.fireFieldChangedEventIfChanged(previous);
170
    }
171

    
172
    private void fireFieldChangedEventIfChanged(File previous) {
173
        if (previous == null) {
174
            if (this.currentValue == null) {
175
                return;
176
            }
177
            this.fireFieldChangedEvent();
178
            return;
179
        }
180
        if (this.currentValue == null) {
181
            this.fireFieldChangedEvent();
182
            return;
183
        }
184
        if (previous.getPath().equals(this.currentValue.getPath())) {
185
            this.fireFieldChangedEvent();
186
        }
187
    }
188

    
189
    public void setValue(Object value) {
190
        File previous = this.currentValue;
191
        if (value == null) {
192
            this.jtext.setText("");
193
        } else {
194
            if (!(value instanceof File)) {
195
                LOGGER.info("setValue invoked with non File value (" + value.toString() + ").");
196
                return;
197
            }
198
            this.jtext.setText(((File) value).getPath());
199
        }
200
        this.assignedValue = (File) value;
201
        this.currentValue = this.assignedValue;
202
        this.fireFieldChangedEventIfChanged(previous);
203
    }
204

    
205
    public Object getValue() {
206
        File value = null;
207
        String s = "";
208
        s = this.jtext.getText();
209
        if (StringUtils.isBlank(s)){
210
            return null;
211
        }
212
//        if (s.trim().length() == 0) {
213
//            Object x = this.getDefinition().getDefaultValue();
214
//            if ((x != null && x instanceof File)) {
215
//                value = new File(x.toString());
216
//            } else {
217
//                value = null;
218
//            }
219
//        } else {
220
        value = new File(s);
221
//        }
222
        return value;
223
    }
224

    
225
    public boolean hasValidValue() {
226
        return true;
227
    }
228

    
229
    public File[] showOpenFileDialog(String title, File initialPath, FileFilter filter) {
230
        return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, filter, true);
231
    }
232

    
233
    public File[] showChooserDialog(
234
            final String title,
235
            final int type, // SAVE_DIALOG / OPEN_DIALOG
236
            final int selectionMode, //    JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES
237
            final boolean multiselection,
238
            final File initialPath,
239
            final FileFilter filter,
240
            final boolean fileHidingEnabled
241
    ) {
242
        File[] returnValue = null;
243

    
244
        JFileChooser fc = new JFileChooser();
245
        fc.setDialogTitle(title);
246
        fc.setDialogType(type);
247
        fc.setFileSelectionMode(selectionMode);
248
        fc.setMultiSelectionEnabled(multiselection);
249
        fc.setCurrentDirectory(initialPath);
250
        fc.setFileFilter(filter);
251
        fc.setFileHidingEnabled(fileHidingEnabled);
252
        int r = JFileChooser.CANCEL_OPTION;
253
        switch (type) {
254
            case JFileChooser.SAVE_DIALOG:
255
                r = fc.showSaveDialog(this.contents);
256
                break;
257
            case JFileChooser.OPEN_DIALOG:
258
            default:
259
                r = fc.showOpenDialog(this.contents);
260
                break;
261
        }
262
        if (r != JFileChooser.APPROVE_OPTION) {
263
            returnValue = null;
264
        } else {
265
            if (fc.isMultiSelectionEnabled()) {
266
                returnValue = fc.getSelectedFiles();
267
            } else {
268
                returnValue = new File[]{fc.getSelectedFile()};
269
            }
270
        }
271
        return returnValue;
272
    }
273

    
274
    public void focusGained(FocusEvent arg0) {
275
        fireFieldEnterEvent();
276
        this.problemIndicator().restore();
277
    }
278

    
279
    public void focusLost(FocusEvent arg0) {
280
        fireFieldExitEvent();
281
    }
282

    
283
    public void clear() {
284
        Object value = this.getDefinition().getDefaultValue();
285
        if (value != null) {
286
            value = value.toString();
287
        } else {
288
            value = "";
289
        }
290
        this.jtext.setText((String) value);
291
    }
292

    
293
    protected Object getAbsoluteFile(Object value) {
294
        if (!(value instanceof File)) {
295
            return value;
296
        }
297
        File f = (File) value;
298
        if (!f.isAbsolute()) {
299
            Tags tags = this.getDefinition().getTags();
300
            if (tags.has("path")) {
301
                File folder;
302
                if (tags.get("path") instanceof File) {
303
                    folder = (File) tags.get("path");
304
                } else {
305
                    folder = new File(Objects.toString(tags.get("path"), null));
306
                }
307
                File f2 = new File(folder, f.getPath());
308
                if (f2.exists()) {
309
                    f = f2;
310
                } else {
311
                    folder = ToolsLocator.getFoldersManager().get("Project");
312
                    if (folder != null) {
313
                        f = new File(folder, f.getPath());
314
                    }
315
                }
316
            }
317
        }
318
        return f;
319
    }
320

    
321
}