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

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

    
56
    private Map modelsList;
57
    protected DynClass dynClass;
58

    
59
    /**
60
     * Constructor.
61
     * 
62
     * @param theClass
63
     *      the current DynClass.
64
     * @param initAll <br>
65
     *      &nbsp;&nbsp;&nbsp;&nbsp;<b>true</b>&nbsp;&nbsp;&nbsp;if we want to populate the Model with the default given values.
66
     *      <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b>&nbsp;&nbsp;if we want to get an empty DynObjectModel.  
67
     */
68
    public AbstractDynObjectModel(DynClass theClass, boolean initAll) {
69
        this.dynClass = theClass;
70
        this.modelsList = new LinkedHashMap();
71
        if (initAll)
72
            initDynFieldModelList();
73
    }
74

    
75
    /**
76
     * Creates a new DynFieldModel object with a default group set to "General" and a field name
77
     *   set to the <b>fieldName</b> value, and returns the created DynFieldModel.
78
     */
79
    public DynFieldModel add(String fieldName) {
80
        return add("General", fieldName);
81
    }
82

    
83
    /*
84
     * (non-Javadoc)
85
     * 
86
     * @see
87
     * org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#add(org.gvsig
88
     * .tools.dynobject.DynField)
89
     */
90
    public DynFieldModel add(String group, String fieldName) {
91
        addGroup(group);
92
        if (fieldName != null) {
93
            DynFieldModel elem =
94
                createDynObjectModelElement(fieldName, dynClass, group);
95
            List list = getGroupElements(group);
96
            if (isNewElement(list, elem))
97
                list.add(list.size(), elem);
98
            return elem;
99
        }
100
        return null;
101
    }
102

    
103
    public void addGroup(String group) {
104
        if (!hasGroup(group))
105
            this.modelsList.put(group, new ArrayList());
106
    }
107

    
108
    /**
109
     * Creates a new DynFieldModel based on the following parameters:
110
     * @param fieldName
111
     *      a given field name.
112
     * @param dynClass
113
     *      a given DynClass.
114
     * @param group
115
     *      a given group name.
116
     * @return
117
     */
118
    protected abstract DynFieldModel createDynObjectModelElement(
119
        String fieldName, DynClass dynClass, String group);
120

    
121

    
122

    
123
    /*
124
     * (non-Javadoc)
125
     * 
126
     * @see
127
     * org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#getFieldModel
128
     * 
129
     */
130
    public List getGroupElements(String group) {
131
        return (List) this.modelsList.get(group);
132
    }
133

    
134
    public String[] getGroups() {
135
        return (String[]) this.modelsList.keySet().toArray(new String[0]);
136
    }
137

    
138
    /**
139
     * Determines if a given group name exists or not in the list.
140
     * @param group
141
     *      the group name.
142
     * @return<br>
143
     *      &nbsp;&nbsp;&nbsp;&nbsp;<b>true</b>&nbsp;&nbsp;&nbsp;there are DynModelElements for this DynObjectModel.
144
     *      <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b>&nbsp;&nbsp;there are no DynModelElements for this DynObjectModel.  
145
     */
146
    private boolean hasGroup(String group) {
147
        return this.modelsList.containsKey(group);
148
    }
149

    
150
    /**
151
     * Inits the list of DynFieldModels.
152
     */
153
    public abstract void initDynFieldModelList();
154

    
155
    /**
156
     * 
157
     * Determines if the DynFieldModel is already in the List or not. If so, 
158
     *   it is not included again.
159
     * 
160
     * @param list
161
     *          the current list of DynFielModelElements
162
     * @param DynFieldModel 
163
     *          the element to be verified.
164
     * @return
165
     *          true if it already exists, false otherwise.
166
     */
167
    private boolean isNewElement(List list, DynFieldModel elem) {
168
        if ((list == null) || (elem == null))
169
            return false;
170
        DynFieldModel item;
171
        for (int i = 0; i < list.size(); i++) {
172
            item = (DynFieldModel) list.get(i);
173
            if (item.equals(elem)) {
174
                return false;
175
            }
176
        }
177
        return true;
178
    }
179
}