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 @ 168
History | View | Annotate | Download (3.84 KB)
1 | 168 | cmartin | /* 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.DynFieldComponentModel; |
||
43 | import org.gvsig.tools.swing.api.dynobject.DynObjectModel; |
||
44 | |||
45 | /**
|
||
46 | * @author <a href="mailto:reinhold@uji.es">cmartin</a>
|
||
47 | *
|
||
48 | */
|
||
49 | public abstract class AbstractDynObjectModel implements DynObjectModel{ |
||
50 | |||
51 | private Map modelsList; |
||
52 | protected DynClass dynClass;
|
||
53 | |||
54 | |||
55 | public AbstractDynObjectModel(DynClass theClass, boolean initAll){ |
||
56 | this.dynClass = theClass;
|
||
57 | this.modelsList = new LinkedHashMap(); |
||
58 | if (initAll)
|
||
59 | initDynFieldComponentModelList(); |
||
60 | } |
||
61 | |||
62 | public DynFieldComponentModel add(String fieldName) { |
||
63 | return add("General",fieldName); |
||
64 | } |
||
65 | |||
66 | /* (non-Javadoc)
|
||
67 | * @see org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#add(org.gvsig.tools.dynobject.DynField)
|
||
68 | */
|
||
69 | public DynFieldComponentModel add(String group, String fieldName) { |
||
70 | addGroup(group); |
||
71 | if (fieldName!=null){ |
||
72 | DynFieldComponentModel elem = createDynObjectModelElement(fieldName,dynClass, group); |
||
73 | List list = getGroup(group);
|
||
74 | if (isNewElement(list, elem))
|
||
75 | list.add(list.size(),elem); |
||
76 | return elem;
|
||
77 | } |
||
78 | return null; |
||
79 | } |
||
80 | |||
81 | public void addGroup(String group){ |
||
82 | if (!hasGroup(group))
|
||
83 | this.modelsList.put(group, new ArrayList()); |
||
84 | } |
||
85 | |||
86 | |||
87 | /**
|
||
88 | * @param field
|
||
89 | * @param dynClass
|
||
90 | * @return
|
||
91 | */
|
||
92 | protected abstract DynFieldComponentModel createDynObjectModelElement( |
||
93 | String fieldName, DynClass dynClass, String group); |
||
94 | |||
95 | |||
96 | |||
97 | protected List getGroup(String group){ |
||
98 | return (List) this.modelsList.get(group); |
||
99 | } |
||
100 | |||
101 | |||
102 | /* (non-Javadoc)
|
||
103 | * @see org.gvsig.tools.swing.api.dynobject.DynObjectComponentModel#getFieldModel()
|
||
104 | */
|
||
105 | public List getGroupElements(String group) { |
||
106 | return (List) this.modelsList.get(group); |
||
107 | } |
||
108 | |||
109 | public String[] getGroups(){ |
||
110 | return (String[]) this.modelsList.keySet().toArray(new String[0]); |
||
111 | } |
||
112 | |||
113 | protected boolean hasGroup(String group){ |
||
114 | return this.modelsList.containsKey(group); |
||
115 | } |
||
116 | /**
|
||
117 | *
|
||
118 | */
|
||
119 | public abstract void initDynFieldComponentModelList(); |
||
120 | |||
121 | /**
|
||
122 | * @param list
|
||
123 | * @param elem
|
||
124 | * @return
|
||
125 | */
|
||
126 | private boolean isNewElement(List list, DynFieldComponentModel elem) { |
||
127 | if ((list==null)||(elem==null)) |
||
128 | return false; |
||
129 | DynFieldComponentModel item; |
||
130 | for (int i=0;i<list.size();i++){ |
||
131 | item = (DynFieldComponentModel) list.get(i); |
||
132 | if (item.equals(elem)){
|
||
133 | return false; |
||
134 | } |
||
135 | } |
||
136 | return true; |
||
137 | } |
||
138 | } |