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

History | View | Annotate | Download (5.87 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.DynClass;
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="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
51
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n&nbsp;</a> -
52
 *         gvSIG Team
53
 * @version $Id$
54
 * 
55
 */
56
public abstract class AbstractDynObjectModel implements DynObjectModel {
57

    
58
    private Map modelsList;
59
    protected DynClass dynClass;
60

    
61
    /**
62
     * Constructor.
63
     * 
64
     * @param theClass
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(DynClass theClass, boolean initAll) {
74
        this.dynClass = theClass;
75
        this.modelsList = new LinkedHashMap();
76
        if (initAll) {
77
            initDynFieldModelList();
78
        }
79
    }
80

    
81
    /**
82
     * Creates a new DynFieldModel object with a default group set to "General"
83
     * and a field name
84
     * set to the <b>fieldName</b> value, and returns the created DynFieldModel.
85
     */
86
    public DynFieldModel add(String fieldName) {
87
        return add("General", fieldName);
88
    }
89

    
90
    /*
91
     * (non-Javadoc)
92
     * 
93
     * @see
94
     * org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#add(org.gvsig
95
     * .tools.dynobject.DynField)
96
     */
97
    public DynFieldModel add(String group, String fieldName) {
98
        addGroup(group);
99
        if (fieldName != null) {
100
            DynFieldModel elem =
101
                createDynObjectModelElement(fieldName, dynClass, group);
102
            List list = getGroupElements(group);
103
            if (isNewElement(list, elem)) {
104
                list.add(list.size(), elem);
105
            }
106
            return elem;
107
        }
108
        return null;
109
    }
110

    
111
    public void addGroup(String group) {
112
        if (!hasGroup(group)) {
113
            this.modelsList.put(group, new ArrayList());
114
        }
115
    }
116

    
117
    /**
118
     * Creates a new DynFieldModel based on the following parameters:
119
     * 
120
     * @param fieldName
121
     *            a given field name.
122
     * @param dynClass
123
     *            a given DynClass.
124
     * @param group
125
     *            a given group name.
126
     * @return
127
     */
128
    protected abstract DynFieldModel createDynObjectModelElement(
129
        String fieldName, DynClass dynClass, String group);
130

    
131
    /*
132
     * (non-Javadoc)
133
     * 
134
     * @see
135
     * org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#getFieldModel
136
     */
137
    public List getGroupElements(String group) {
138
        return (List) this.modelsList.get(group);
139
    }
140

    
141
    public String[] getGroups() {
142
        return (String[]) this.modelsList.keySet().toArray(new String[0]);
143
    }
144

    
145
    /**
146
     * Determines if a given group name exists or not in the list.
147
     * 
148
     * @param group
149
     *            the group name.
150
     * @return<br>
151
     *             &nbsp;&nbsp;&nbsp;&nbsp;<b>true</b>&nbsp;&nbsp;&nbsp;there
152
     *             are DynModelElements for this DynObjectModel. <br>
153
     *             &nbsp;&nbsp;&nbsp;&nbsp;<b>false</b>&nbsp;&nbsp;there are no
154
     *             DynModelElements for this DynObjectModel.
155
     */
156
    private boolean hasGroup(String group) {
157
        return this.modelsList.containsKey(group);
158
    }
159

    
160
    /**
161
     * Inits the list of DynFieldModels.
162
     */
163
    public abstract void initDynFieldModelList();
164

    
165
    /**
166
     * 
167
     * Determines if the DynFieldModel is already in the List or not. If so,
168
     * it is not included again.
169
     * 
170
     * @param list
171
     *            the current list of DynFielModelElements
172
     * @param DynFieldModel
173
     *            the element to be verified.
174
     * @return
175
     *         true if it already exists, false otherwise.
176
     */
177
    private boolean isNewElement(List list, DynFieldModel elem) {
178
        if ((list == null) || (elem == null)) {
179
            return false;
180
        }
181
        DynFieldModel item;
182
        for (int i = 0; i < list.size(); i++) {
183
            item = (DynFieldModel) list.get(i);
184
            if (item.equals(elem)) {
185
                return false;
186
            }
187
        }
188
        return true;
189
    }
190
}