Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / dynobject / valuefield / DynObjectValueField.java @ 281

History | View | Annotate | Download (4.92 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
/*
23
 * AUTHORS (In addition to CIT):
24
 * 2010 Institute of New Imaging Technologies (INIT): 
25
 *   http://www.init.uji.es
26
 * Geographic Information research group: 
27
 *   http://www.geoinfo.uji.es
28
 * Universitat Jaume I, Spain
29
 */
30

    
31
/**
32
 * 
33
 */
34
package org.gvsig.tools.swing.impl.dynobject.valuefield;
35

    
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

    
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dataTypes.DataTypes;
41
import org.gvsig.tools.dynobject.DynField;
42
import org.gvsig.tools.dynobject.DynObject;
43
import org.gvsig.tools.dynobject.DynObjectManager;
44
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
45
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
46

    
47
/**
48
 * 
49
 * Default {@link ValueField} implementation for {@link DynObject}s and {@link DynField}s.
50
 * 
51
 * @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
52
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n&nbsp;</a>  - gvSIG Team
53
 * @version $Id$
54
 *
55
 */
56
public class DynObjectValueField implements ValueField {
57

    
58
    private static final Logger LOG = LoggerFactory.getLogger(DynObjectValueField.class);
59
    
60
    private final DynObject parent;
61
    private final String fieldName;
62
    private Boolean isDynObject;
63

    
64
    /**
65
     * 
66
     * Constructor.
67
     * 
68
     * @param dynObject
69
     *          the current DynObject value.
70
     * @param fieldName
71
     *          the field name of the DynField element.
72
     */
73
    public DynObjectValueField(DynObject dynObject, String fieldName) {
74
        parent = dynObject;
75
        this.fieldName = fieldName;
76
    }
77

    
78
    private boolean isDynObject() {
79
        if (this.isDynObject == null)
80
            this.isDynObject = (getDynField().getType() == DataTypes.DYNOBJECT);
81
        return this.isDynObject;
82
    }
83

    
84
    /*
85
     * (non-Javadoc)
86
     * 
87
     * @see
88
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
89
     */
90
    public Object getDefaultFieldValue() {
91
        return getDynField().getDefaultValue();
92
    }
93

    
94
    /*
95
     * (non-Javadoc)
96
     * 
97
     * @see org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getValue()
98
     */
99
    public Object getValue() {
100
        String name = getFieldName();
101
        Object value = parent.getDynValue(name);
102
        DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
103
        if ((value == null) && (isDynObject()))
104
            return dynManager.createDynObject(dynManager.get(this.getDynField()
105
                .getName()));
106
        return value;
107
    }
108

    
109
    /**
110
     * Returns the service name to be called to get the correspondent 
111
     *   JDynFieldComponent.
112
     * @return
113
     */
114
    private String getFieldName() {
115
        String name = this.fieldName;
116
        String suffix = "-List-item";
117
        // We have to remove this suffix if exists.
118
        if (name.endsWith(suffix))
119
            return name.replace(suffix, "");
120
        return name;
121
    }
122

    
123
    /*
124
     * (non-Javadoc)
125
     * 
126
     * @see
127
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
128
     * .lang.Object)
129
     */
130
    public void setFieldValue(Object value) {
131
        try {
132
            getDynField().validate(value);
133
            parent.setDynValue(fieldName, value);
134
        } catch (DynFieldValidateException e) {
135
            LOG.warn("[Validation Error]: Field '{}' not valid with value '{}'", getDynField().getName(),value);
136
        }
137
    }
138

    
139
    /*
140
     * (non-Javadoc)
141
     * 
142
     * @see ValueField#getDynField()
143
     */
144
    public DynField getDynField() {
145
        return parent.getDynClass().getDynField(fieldName);
146
    }
147
    
148
    /*
149
     * (non-Javadoc)
150
     * 
151
     * @see ValueField#getFieldValue()
152
     */
153
    public Object getFieldValue() {
154
        return parent.getDynValue(fieldName);
155
    }
156
    
157

    
158
    /*
159
     * (non-Javadoc)
160
     * 
161
     * @see ValueField#setValue(Object value)
162
     */
163
    public void setValue(Object value) {
164
        // do nothing
165
    }    
166
}