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 / URL / JDynFormFieldURL.java @ 1881

History | View | Annotate | Download (3.59 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dynform.services.dynformfield.URL;
25

    
26
import java.io.File;
27
import java.net.MalformedURLException;
28
import java.net.URL;
29

    
30
import javax.swing.JFileChooser;
31
import org.gvsig.tools.dynform.DynFormFieldDefinition;
32

    
33
import org.gvsig.tools.dynform.services.dynformfield.File.JDynFormFieldFile;
34
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
35
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
36

    
37
public class JDynFormFieldURL extends JDynFormFieldFile {
38

    
39
    protected URL assignedValue = null;
40
    protected URL currentValue = null;
41

    
42
    public JDynFormFieldURL(
43
            DynFormSPIManager serviceManager,
44
            DynFormSPIManager.ComponentsFactory componentsFactory,
45
            JDynFormFieldFactory factory,
46
            DynFormFieldDefinition definition,
47
            Object value
48
    ) {
49
        super(serviceManager, componentsFactory, factory, definition, value);
50
        this.assignedValue = (URL) value;
51
    }
52

    
53
    public Object getValue() {
54
        URL value = null;
55
        String s = "";
56
        s = this.jtext.getText();
57
        if (s.trim().length() == 0) {
58
            Object x = this.getDefinition().getDefaultValue();
59
            if (x == null) {
60
                value = null;
61
            } else {
62
                try {
63
                    value = new URL(x.toString());
64
                } catch (MalformedURLException e) {
65
                    LOGGER.info("Error. URL Syntax: " + x.toString());
66
                    value = null;
67
                }
68
            }
69
        } else {
70
            try {
71
                value = new URL(s);
72
            } catch (MalformedURLException e) {
73
                LOGGER.info("Error. URL Syntax: " + s.toString());
74
                value = null;
75
            }
76
        }
77
        return value;
78
    }
79

    
80
    public void setValue(Object value) {
81
        if (value == null) {
82
            this.jtext.setText("");
83
            this.assignedValue = null;
84
        } else {
85
            if (!(value instanceof URL)) {
86
                LOGGER.info("setValue invoked with non URL value (" + value.toString() + ").");
87
                return;
88
            }
89
            this.jtext.setText(((URL) value).toString());
90
            try {
91
                this.assignedValue = new URL(((URL) value).toString());
92
            } catch (MalformedURLException e) {
93
                LOGGER.info("Error in URL creation.");
94
                this.assignedValue = null;
95
            }
96
        }
97

    
98
        this.currentValue = this.assignedValue;
99
    }
100

    
101
    public File[] showOpenFileDialog(String title, File initialPath) {
102
        return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_AND_DIRECTORIES, false, initialPath, null, false);
103
    }
104

    
105
}