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 / AbstractDynObjectModel.java @ 632

History | View | Annotate | Download (6.33 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;
35

    
36
import java.util.ArrayList;
37
import java.util.LinkedHashMap;
38
import java.util.List;
39
import java.util.Map;
40

    
41
import org.gvsig.tools.dynobject.DynStruct;
42
import org.gvsig.tools.swing.api.dynobject.DynFieldModel;
43
import org.gvsig.tools.swing.api.dynobject.DynObjectModel;
44

    
45
/**
46
 * 
47
 * Abstract class that implements the basics of the DynObjectModel
48
 * implementation.
49
 * 
50
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart�n&nbsp;</a> -
51
 *         gvSIG Team
52
 * @version $Id$
53
 * 
54
 */
55
public abstract class AbstractDynObjectModel implements DynObjectModel {
56

    
57
    private Map<String, List<DynFieldModel>> modelsList;
58
    protected DynStruct dynStruct;
59
        private List<String> compulsoryGroups;
60

    
61
    /**
62
     * Constructor.
63
     * 
64
     * @param dynStruct
65
     *            the current DynClass.
66
     * @param initAll
67
     * <br>
68
     *            &nbsp;&nbsp;&nbsp;&nbsp;<b>true</b>&nbsp;&nbsp;&nbsp;if we
69
     *            want to populate the Model with the default given values. <br>
70
     *            &nbsp;&nbsp;&nbsp;&nbsp;<b>false</b>&nbsp;&nbsp;if we want to
71
     *            get an empty DynObjectModel.
72
     */
73
    public AbstractDynObjectModel(DynStruct definition, boolean initAll) {
74
        this.dynStruct = definition;
75
                this.compulsoryGroups = new ArrayList<String>();
76
        this.modelsList = new LinkedHashMap<String, List<DynFieldModel>>();
77
                if (initAll) {
78
            initDynFieldModelList();
79
        }
80
    }
81

    
82
        public boolean isGroupCompulsory(String groupName) {
83
                return this.compulsoryGroups.contains(groupName);
84
        }
85

    
86
        /**
87
         * Creates a new DynFieldModel object with a default group set to "General"
88
         * and a field name set to the <b>fieldName</b> value, and returns the
89
         * created DynFieldModel.
90
         */
91
    public DynFieldModel add(String fieldName) {
92
        return add(this.getGenericLabel(), fieldName);
93
    }
94

    
95

    
96
    /*
97
     * (non-Javadoc)
98
     * 
99
     * @see
100
     * org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#add(org.gvsig
101
     * .tools.dynobject.DynField)
102
     */
103
    public DynFieldModel add(String group, String fieldName) {
104
        addGroup(group);
105
        if (fieldName != null) {
106
            DynFieldModel elem =
107
                createDynObjectModelElement(fieldName, dynStruct, group);
108
            List<DynFieldModel> list = getGroupElements(group);
109
            if (isNewElement(list, elem)) {
110
                list.add(list.size(), elem);
111
                                if (elem.getDynField().isMandatory()) {
112
                                        if (!this.compulsoryGroups.contains(group)) {
113
                                                compulsoryGroups.add(group);
114
                                        }
115
                                }
116
            }
117
            return elem;
118
        }
119
        return null;
120
    }
121

    
122
    public void addGroup(String group) {
123
        if (group == null){
124
            return;
125
        }
126
        
127
        if (!hasGroup(group)) {
128
            this.modelsList.put(group, new ArrayList<DynFieldModel>());
129
        }
130
    }
131

    
132
    /**
133
     * Creates a new DynFieldModel based on the following parameters:
134
     * 
135
     * @param fieldName
136
     *            a given field name.
137
     * @param definition
138
     *            a given DynClass.
139
     * @param group
140
     *            a given group name.
141
     * @return
142
     */
143
    protected abstract DynFieldModel createDynObjectModelElement(
144
        String fieldName, DynStruct definition, String group);
145

    
146
    /*
147
     * (non-Javadoc)
148
     * 
149
     * @see
150
     * org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#getFieldModel
151
     */
152
    public List<DynFieldModel> getGroupElements(String group) {
153
        return this.modelsList.get(group);
154
    }
155

    
156
    public String[] getGroups() {
157
        return this.modelsList.keySet().toArray(new String[0]);
158
    }
159

    
160
    /**
161
     * Determines if a given group name exists or not in the list.
162
     * 
163
     * @param group
164
     *            the group name.
165
     * @return<br>
166
     *             &nbsp;&nbsp;&nbsp;&nbsp;<b>true</b>&nbsp;&nbsp;&nbsp;there
167
     *             are DynModelElements for this DynObjectModel. <br>initDynFieldModelList
168
     *             &nbsp;&nbsp;&nbsp;&nbsp;<b>false</b>&nbsp;&nbsp;there are no
169
     *             DynModelElements for this DynObjectModel.
170
     */
171
    private boolean hasGroup(String group) {
172
        return this.modelsList.containsKey(group);
173
    }
174

    
175
    /**
176
     * Inits the list of DynFieldModels.
177
     */
178
    protected abstract void initDynFieldModelList();
179

    
180
    /**
181
     * 
182
     * Determines if the DynFieldModel is already in the List or not. If so,
183
     * it is not included again.
184
     * 
185
     * @param list
186
     *            the current list of DynFielModelElements
187
     * @param DynFieldModel
188
     *            the element to be verified.
189
     * @return
190
     *         true if it already exists, false otherwise.
191
     */
192
    private boolean isNewElement(List<DynFieldModel> list, DynFieldModel elem) {
193
        if ((list == null) || (elem == null)) {
194
            return false;
195
        }
196
        DynFieldModel item;
197
        for (int i = 0; i < list.size(); i++) {
198
            item = list.get(i);
199
            if (item.equals(elem)) {
200
                return false;
201
            }
202
        }
203
        return true;
204
    }
205
}