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

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

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

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

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

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

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

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

    
91
        JTextComponent text = this.getComponentsFactory().getJTextField(this.getDefinition(), null);
92
        this.jtext = text;
93
        this.fixPreferredWidth(text);
94
        text.addFocusListener(this);
95
        //this.jtext.setEditable(false);    
96
        this.jtext.addFocusListener(new FocusListener() {
97
            @Override
98
            public void focusGained(FocusEvent e) {
99
                fireFieldEnterEvent();
100
            }
101

    
102
            @Override
103
            public void focusLost(FocusEvent e) {
104
                fireFieldExitEvent();
105
            }
106
        });
107
        text.getDocument().addDocumentListener(new DocumentListener() {
108
            @Override
109
            public void insertUpdate(DocumentEvent e) {
110
                fireFieldChangedEvent();
111
            }
112

    
113
            @Override
114
            public void removeUpdate(DocumentEvent e) {
115
                fireFieldChangedEvent();
116
            }
117

    
118
            @Override
119
            public void changedUpdate(DocumentEvent e) {
120
                fireFieldChangedEvent();
121
            }
122
        });
123

    
124
        this.jbutton = this.getComponentsFactory().getJButton(this.getDefinition(), null);
125
        this.jbutton.setIcon(this.getIcon("picker-file"));
126
        if( StringUtils.equals("...", this.jbutton.getText()) ) {
127
            this.jbutton.setText("");        
128
        }
129
        this.jbutton.addActionListener((ActionEvent e) -> {
130
            fireFieldEnterEvent();
131
            onClickBrowse();
132
        });
133

    
134
        this.contents.add(jtext, BorderLayout.CENTER);
135
        this.contents.add(jbutton, BorderLayout.LINE_END);
136
        this.contents.setVisible(true);
137

    
138
        if (this.readonly) {
139
            this.jtext.setEditable(false);
140
            this.jbutton.setEnabled(false);
141
        } else {
142
            this.jtext.setEditable(true);
143
            this.jbutton.setEnabled(true);
144
        }
145
        this.setValue(this.assignedValue);
146
    }
147

    
148
    public void onClickBrowse() {
149
        fireFieldEnterEvent();
150
        File previous = this.currentValue;
151

    
152
        this.problemIndicator().restore();
153

    
154
        File initialPath = null;
155
        FileFilter filter = null;
156
        Tags tags = this.getDefinition().getTags();
157
        if (tags.has("path")) {
158
            initialPath = new File((String) tags.get("path"));
159
        }
160
        if (tags.has("filter")) {
161
            try {
162
                String extensions = (String) tags.get("filter");
163
                String filterDescription = extensions;
164
                if (tags.has("filterDescription")) {
165
                    filterDescription = (String) tags.get("filterDescription");
166
                }
167
                filter = new FileNameExtensionFilter(filterDescription, extensions.split("[|]"));
168
            } catch (Exception ex) {
169
                // Ignore 
170
            }
171
        }
172

    
173
        if (this.currentValue != null) {
174
            initialPath = this.currentValue.getParentFile();
175
        }
176
        File[] x = showOpenFileDialog(this.getLabel(), initialPath, filter);
177
        if (x == null) {
178
            return;
179
        }
180
        this.currentValue = x[0];
181
        this.jtext.setText(x[0].getPath());
182
        this.fireFieldChangedEventIfChanged(previous);
183
    }
184

    
185
    private void fireFieldChangedEventIfChanged(File previous) {
186
        if (previous == null) {
187
            if (this.currentValue == null) {
188
                return;
189
            }
190
            this.fireFieldChangedEvent();
191
            return;
192
        }
193
        if (this.currentValue == null) {
194
            this.fireFieldChangedEvent();
195
            return;
196
        }
197
        if (previous.getPath().equals(this.currentValue.getPath())) {
198
            this.fireFieldChangedEvent();
199
        }
200
    }
201

    
202
    @Override
203
    public void setValue(Object value) {
204
        File previous = this.currentValue;
205
        if (value == null) {
206
            this.jtext.setText("");
207
        } else {
208
            if (!(value instanceof File)) {
209
                LOGGER.info("setValue invoked with non File value (" + value.toString() + ").");
210
                return;
211
            }
212
            this.jtext.setText(((File) value).getPath());
213
            this.fixPreferredWidth(this.jtext);
214
        }
215
        this.assignedValue = (File) value;
216
        this.currentValue = this.assignedValue;
217
        this.fireFieldChangedEventIfChanged(previous);
218
    }
219

    
220
    public Object getValue() {
221
        File value = null;
222
        String s = "";
223
        s = this.jtext.getText();
224
        if (StringUtils.isBlank(s)){
225
            return null;
226
        }
227
//        if (s.trim().length() == 0) {
228
//            Object x = this.getDefinition().getDefaultValue();
229
//            if ((x != null && x instanceof File)) {
230
//                value = new File(x.toString());
231
//            } else {
232
//                value = null;
233
//            }
234
//        } else {
235
        value = new File(s);
236
//        }
237
        return value;
238
    }
239

    
240
    public boolean hasValidValue() {
241
        return true;
242
    }
243

    
244
    public File[] showOpenFileDialog(String title, File initialPath, FileFilter filter) {
245
        return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, filter, true);
246
    }
247

    
248
    public File[] showChooserDialog(
249
            final String title,
250
            final int type, // SAVE_DIALOG / OPEN_DIALOG
251
            final int selectionMode, //    JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES
252
            final boolean multiselection,
253
            final File initialPath,
254
            final FileFilter filter,
255
            final boolean fileHidingEnabled
256
    ) {
257
        File[] returnValue = null;
258

    
259
        JFileChooser fc = new JFileChooser();
260
        fc.setDialogTitle(title);
261
        fc.setDialogType(type);
262
        fc.setFileSelectionMode(selectionMode);
263
        fc.setMultiSelectionEnabled(multiselection);
264
        fc.setCurrentDirectory(initialPath);
265
        fc.setFileFilter(filter);
266
        fc.setFileHidingEnabled(fileHidingEnabled);
267
        int r = JFileChooser.CANCEL_OPTION;
268
        switch (type) {
269
            case JFileChooser.SAVE_DIALOG:
270
                r = fc.showSaveDialog(this.contents);
271
                break;
272
            case JFileChooser.OPEN_DIALOG:
273
            default:
274
                r = fc.showOpenDialog(this.contents);
275
                break;
276
        }
277
        if (r != JFileChooser.APPROVE_OPTION) {
278
            returnValue = null;
279
        } else {
280
            if (fc.isMultiSelectionEnabled()) {
281
                returnValue = fc.getSelectedFiles();
282
            } else {
283
                returnValue = new File[]{fc.getSelectedFile()};
284
            }
285
        }
286
        return returnValue;
287
    }
288

    
289
    public void focusGained(FocusEvent arg0) {
290
        fireFieldEnterEvent();
291
        this.problemIndicator().restore();
292
    }
293

    
294
    public void focusLost(FocusEvent arg0) {
295
        fireFieldExitEvent();
296
    }
297

    
298
    public void clear() {
299
        Object value = this.getDefinition().getDefaultValue();
300
        if (value != null) {
301
            value = value.toString();
302
        } else {
303
            value = "";
304
        }
305
        this.jtext.setText((String) value);
306
    }
307

    
308
    protected Object getAbsoluteFile(Object value) {
309
        if (!(value instanceof File)) {
310
            return value;
311
        }
312
        File f = (File) value;
313
        if (!f.isAbsolute()) {
314
            Tags tags = this.getDefinition().getTags();
315
            if (tags.has("path")) {
316
                File folder;
317
                if (tags.get("path") instanceof File) {
318
                    folder = (File) tags.get("path");
319
                } else {
320
                    folder = new File(Objects.toString(tags.get("path"), null));
321
                }
322
                File f2 = new File(folder, f.getPath());
323
                if (f2.exists()) {
324
                    f = f2;
325
                } else {
326
                    folder = ToolsLocator.getFoldersManager().get("Project");
327
                    if (folder != null) {
328
                        f = new File(folder, f.getPath());
329
                    }
330
                }
331
            }
332
        }
333
        return f;
334
    }
335

    
336
    @Override
337
    public boolean isModified() {
338
        String s = this.jtext.getText();
339

    
340
        File assigned = (File) getAssignedValue();
341
        if (StringUtils.isBlank(s)) {
342
            return assigned != null;
343
        }
344
        try {
345
            File value = new File(s);
346
            return !Objects.equals(value, assigned);
347
        } catch (Exception ex) {
348
            return false;
349
        }
350
    }
351
    
352
}