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

History | View | Annotate | Download (4.2 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
import java.util.Objects;
30
import javax.swing.JFileChooser;
31
import org.apache.commons.lang3.StringUtils;
32
import org.gvsig.tools.dynform.DynFormFieldDefinition;
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 (StringUtils.isBlank(s)){
58
            return null;  
59
        }
60
//        if (s.trim().length() == 0) {
61
//            Object x = this.getDefinition().getDefaultValue();
62
//            if (x == null) {
63
//                value = null;
64
//            } else {
65
//                try {
66
//                    value = new URL(x.toString());
67
//                } catch (MalformedURLException e) {
68
//                    LOGGER.info("Error. URL Syntax: " + x.toString());
69
//                    value = null;
70
//                }
71
//            }
72
//        } else {
73
        try {
74
            value = new URL(s);
75
        } catch (MalformedURLException e) {
76
            LOGGER.info("Error. URL Syntax: " + s.toString());
77
            value = null;
78
        }
79
//        }
80
        return value;
81
    }
82

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

    
103
        this.currentValue = this.assignedValue;
104
    }
105

    
106
    public File[] showOpenFileDialog(String title, File initialPath) {
107
        return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_AND_DIRECTORIES, false, initialPath, null, false);
108
    }
109
    
110
    @Override
111
    public boolean isModified() {
112
        String s = this.jtext.getText();
113

    
114
        URL assigned = (URL) getAssignedValue();
115
        if (StringUtils.isBlank(s)) {
116
            return assigned != null;
117
        }
118
        try {
119
            URL value = new URL(s);
120
            return !Objects.equals(value, assigned);
121
        } catch (Exception ex) {
122
            return false;
123
        }
124
    }
125
    
126

    
127

    
128
}