Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / resource / file / FileResourceParameters.java @ 42602

History | View | Annotate | Download (3.94 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.fmap.dal.resource.file;
24

    
25
import java.io.File;
26

    
27
import org.gvsig.fmap.dal.resource.spi.AbstractResourceParameters;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dataTypes.DataTypes;
30
import org.gvsig.tools.dynobject.DelegatedDynObject;
31
import org.gvsig.tools.dynobject.DynClass;
32
import org.gvsig.tools.persistence.PersistenceManager;
33

    
34
public class FileResourceParameters extends AbstractResourceParameters {
35

    
36
    public static final String DYNCLASS_NAME = "FileResourceParameters";
37

    
38
    private static final String DYNFIELDNAME_FILE = "file";
39

    
40
    private DelegatedDynObject delegatedDynObject;
41

    
42
    public FileResourceParameters() {
43
        this.delegatedDynObject = (DelegatedDynObject) ToolsLocator
44
                .getDynObjectManager()
45
                .createDynObject(registerDynClass());
46
    }
47

    
48
    public FileResourceParameters(Object[] params) {
49
        this();
50
        // XXX puede que sobre
51
        setDynValue(DYNFIELDNAME_FILE, params[0]);
52
    }
53

    
54
    public FileResourceParameters(String name) {
55
        this();
56
        setDynValue(DYNFIELDNAME_FILE, name);
57
    }
58

    
59
    public String getFileName() {
60
        if (this.getFile() == null) {
61
            return null;
62
        }
63
        return this.getFile().getPath();
64
    }
65

    
66
    public FileResourceParameters setFileName(String fileName) {
67
        this.setDynValue(DYNFIELDNAME_FILE, fileName);
68
        return this;
69
    }
70

    
71
    public File getFile() {
72
        return (File) this.getDynValue(DYNFIELDNAME_FILE);
73
    }
74

    
75
    public FileResourceParameters setFile(File file) {
76
        this.setDynValue(DYNFIELDNAME_FILE, file);
77
        return this;
78
    }
79

    
80
    public FileResourceParameters setFile(String fileName) {
81
        this.setDynValue(DYNFIELDNAME_FILE, fileName);
82
        return this;
83
    }
84

    
85
    @Override
86
    public String getTypeName() {
87
        return FileResource.NAME;
88
    }
89

    
90
    @Override
91
    protected DelegatedDynObject getDelegatedDynObject() {
92
        return delegatedDynObject;
93
    }
94

    
95
    /**
96
     * Added "static synchronized" because sometimes there is a
97
     * "DuplicateDynClassException" when user selects more than one file (SHP,
98
     * for example) to be added to the view. "definition" is null but then there
99
     * is an exception because in the meantime another thread has registered it
100
     * (I'm not sure this is the cause, but I cannot imagine another reason)
101
     */
102
    private static synchronized DynClass registerDynClass() {
103
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
104
        DynClass definition = (DynClass) manager.getDefinition(DYNCLASS_NAME);
105
        if (definition == null) {
106
            definition = (DynClass) manager.addDefinition(
107
                    FileResourceParameters.class,
108
                    DYNCLASS_NAME,
109
                    DYNCLASS_NAME + " persistence definition",
110
                    null,
111
                    null
112
            );
113

    
114
            definition.addDynField(DYNFIELDNAME_FILE).setType(DataTypes.FILE)
115
                    .setDescription("The file");
116
        }
117
        return definition;
118
    }
119
}