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 / ListItemValueField.java @ 281

History | View | Annotate | Download (4.31 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
package org.gvsig.tools.swing.impl.dynobject.valuefield;
32

    
33
import java.util.ArrayList;
34
import java.util.List;
35

    
36
import org.gvsig.tools.dynobject.DynField;
37
import org.gvsig.tools.dynobject.DynObject;
38
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
39

    
40
/**
41
 * 
42
 * Implementation that emulates the ValueField behavior for List objects.
43
 * 
44
 * @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
45
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n&nbsp;</a>  - gvSIG Team
46
 * @version $Id$
47
 *
48
 */
49

    
50
public class ListItemValueField implements ValueField {
51

    
52
    private List<Object> items;
53
    private DynField dynField;
54
    private DynObject dynObject;
55
    private String fieldName;
56
    private int index;
57

    
58
    /**
59
     * 
60
     * Constructor.
61
     * 
62
     * @param dynObject
63
     *      the current DynObject value.
64
     * @param fieldName
65
     *      the current field name.
66
     * @param index
67
     *      the current index value to be pointing at the list.
68
     */
69
    public ListItemValueField(DynObject dynObject, String fieldName, int index) {
70

    
71
        this.dynObject = dynObject;
72
        this.fieldName = fieldName;
73
        this.index = index;
74
        this.dynField = dynObject.getDynClass().getDynField(fieldName);
75
        
76

    
77
        Object value = dynObject.getDynValue(fieldName);
78
        if (value == null) {
79
            value = getDefaultValue();
80
        }
81
        this.setValue(value);
82
    }
83

    
84
    /**
85
     * Returns the List of values.
86
     * 
87
     * @return  the List of values.
88
     */
89
    public Object getValue() {
90
        return this.items;
91
    }
92

    
93
    /**
94
     * Reutrns the item of the list at a given position.
95
     * 
96
     * @param index
97
     *     the index position
98
     * @return
99
     *     the resulting item.
100
     */
101
    public Object getValueItem(int index) {
102
        return this.items.get(index);
103
    }
104

    
105
    /*
106
     * (non-Javadoc)
107
     * 
108
     * @see
109
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
110
     * .lang.Object)
111
     */
112
    public void setValue(Object value) {
113
        if (value==null)
114
            this.items = new ArrayList<Object>();
115
        else
116
            this.items = (List<Object>) value;
117
    }
118

    
119
 
120
    /**
121
     * Sets an item into the list at the index specified.
122
     * 
123
     * @param item
124
     *      the item to be inserted.
125
     * @param index
126
     *      the index to insert the item.
127
     */
128
    public void setValueItem(Object item, int index) {
129
        this.items.set(index, item);
130
    }
131

    
132
    public DynField getDynField() {
133
        return dynField.getElementsType();
134
    }
135

    
136
    public Object getDefaultFieldValue() {
137
        return getDynField().getDefaultValue();
138
    }
139
    
140
    /*
141
     * (non-Javadoc)
142
     * 
143
     * @see
144
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
145
     */
146
    public Object getDefaultValue() {
147
        return this.dynField.getDefaultValue();
148
    }
149

    
150
    public Object getFieldValue() {
151
        return this.items;
152
    }
153

    
154
    public void setFieldValue(Object value) {
155
        this.dynObject.setDynValue(this.fieldName, value);
156
    }
157
}