gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.impl / src / main / java / org / gvsig / tools / dynform / impl / DefaultDynFormManager.java @ 1361
History | View | Annotate | Download (7.73 KB)
1 | 954 | jbadia | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
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 3
|
||
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 | 1076 | jjdelcerro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 954 | jbadia | * 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 | 1076 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 954 | jbadia | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 919 | jjdelcerro | package org.gvsig.tools.dynform.impl; |
25 | |||
26 | import java.util.HashMap; |
||
27 | import java.util.List; |
||
28 | import java.util.Map; |
||
29 | |||
30 | import org.gvsig.tools.dispose.DisposableIterator; |
||
31 | 933 | jjdelcerro | import org.gvsig.tools.dynform.DynFormDefinition; |
32 | import org.gvsig.tools.dynform.DynFormManager; |
||
33 | import org.gvsig.tools.dynform.JDynForm; |
||
34 | import org.gvsig.tools.dynform.JDynFormSet; |
||
35 | 919 | jjdelcerro | import org.gvsig.tools.dynform.spi.DynFormSPILocator; |
36 | import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
||
37 | import org.gvsig.tools.dynobject.DynField; |
||
38 | import org.gvsig.tools.dynobject.DynObject; |
||
39 | import org.gvsig.tools.dynobject.DynObjectSet; |
||
40 | import org.gvsig.tools.dynobject.DynStruct; |
||
41 | 1067 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct_v2; |
42 | 919 | jjdelcerro | import org.gvsig.tools.service.Service; |
43 | import org.gvsig.tools.service.ServiceException; |
||
44 | |||
45 | 1076 | jjdelcerro | @SuppressWarnings({"unchecked", "rawtypes"}) |
46 | public class DefaultDynFormManager implements DynFormManager { |
||
47 | 919 | jjdelcerro | |
48 | 1076 | jjdelcerro | DynFormSPIManager serviceManager = null;
|
49 | 1033 | jbadia | |
50 | 1076 | jjdelcerro | private Map definitions = null; |
51 | private String defaultJDynFormSetname = "Subform"; |
||
52 | |||
53 | public DefaultDynFormManager() {
|
||
54 | } |
||
55 | 919 | jjdelcerro | |
56 | 1076 | jjdelcerro | public DynFormSPIManager getServiceManager() {
|
57 | if ( serviceManager == null ) { |
||
58 | serviceManager = DynFormSPILocator.getDynFormSPIManager(); |
||
59 | } |
||
60 | return serviceManager;
|
||
61 | } |
||
62 | 919 | jjdelcerro | |
63 | 1076 | jjdelcerro | public Map getDefinitions() { |
64 | if ( this.definitions == null ) { |
||
65 | this.definitions = new HashMap(); |
||
66 | } |
||
67 | return this.definitions; |
||
68 | } |
||
69 | 919 | jjdelcerro | |
70 | 1361 | jjdelcerro | @Override
|
71 | 1076 | jjdelcerro | public Service getService(DynObject parameters) throws ServiceException { |
72 | Service service = this.serviceManager.createService(parameters);
|
||
73 | return service;
|
||
74 | } |
||
75 | 1033 | jbadia | |
76 | 1361 | jjdelcerro | @Override
|
77 | 1076 | jjdelcerro | public DynFormDefinition getDefinition(String name) { |
78 | 1361 | jjdelcerro | DefaultDynFormDefinition x; |
79 | 1076 | jjdelcerro | x = (DefaultDynFormDefinition) this.getDefinitions().get(name);
|
80 | if ( x == null ) { |
||
81 | x = new DefaultDynFormDefinition(name, this); |
||
82 | this.getDefinitions().put(x.getName(), x);
|
||
83 | } |
||
84 | return x;
|
||
85 | } |
||
86 | 919 | jjdelcerro | |
87 | 1361 | jjdelcerro | @Override
|
88 | 1076 | jjdelcerro | public DynFormDefinition getDefinition(DynStruct definition) {
|
89 | 1361 | jjdelcerro | String name = definition.getFullName();
|
90 | DefaultDynFormDefinition x ; |
||
91 | 1076 | jjdelcerro | x = (DefaultDynFormDefinition) this.getDefinitions().get(name);
|
92 | if ( x == null ) { |
||
93 | x = new DefaultDynFormDefinition(name, this); |
||
94 | this.getDefinitions().put(x.getName(), x);
|
||
95 | DynField[] fields = definition.getDynFields();
|
||
96 | 1361 | jjdelcerro | for (DynField field : fields) {
|
97 | x.add(field); |
||
98 | 1076 | jjdelcerro | } |
99 | } |
||
100 | if ( definition instanceof DynStruct_v2 ) { |
||
101 | x.getTags().add(((DynStruct_v2) definition).getTags()); |
||
102 | } |
||
103 | 1078 | jbadia | x.setElementsType(definition); |
104 | 1076 | jjdelcerro | return x;
|
105 | } |
||
106 | 919 | jjdelcerro | |
107 | 1361 | jjdelcerro | @Override
|
108 | 1076 | jjdelcerro | public DynFormDefinition getDefinition(DynObject obj) {
|
109 | return this.getDefinition(obj.getDynClass()); |
||
110 | } |
||
111 | 919 | jjdelcerro | |
112 | 1361 | jjdelcerro | @Override
|
113 | 1076 | jjdelcerro | public JDynForm createJDynForm(DynFormDefinition definition) throws ServiceException { |
114 | 1361 | jjdelcerro | DynFormSPIManager serviceManager = this.getServiceManager();
|
115 | JDynForm jdynform; |
||
116 | try {
|
||
117 | DynObject params = serviceManager.createServiceParameters( |
||
118 | serviceManager.makeServiceName(definition.getName()) |
||
119 | ); |
||
120 | jdynform = (JDynForm) serviceManager.getService(params); |
||
121 | } catch (ServiceException ex) {
|
||
122 | jdynform = new DefaultJDynForm(this, definition); |
||
123 | } |
||
124 | return jdynform;
|
||
125 | 1076 | jjdelcerro | } |
126 | 919 | jjdelcerro | |
127 | 1361 | jjdelcerro | @Override
|
128 | 1076 | jjdelcerro | public JDynForm createJDynForm(DynStruct struct) throws ServiceException { |
129 | DynFormDefinition definition = this.getDefinition(struct);
|
||
130 | 1361 | jjdelcerro | return this.createJDynForm(definition); |
131 | 1076 | jjdelcerro | } |
132 | 919 | jjdelcerro | |
133 | 1361 | jjdelcerro | @Override
|
134 | 1076 | jjdelcerro | public JDynForm createJDynForm(DynObject obj) throws ServiceException { |
135 | DynFormDefinition definition = this.getDefinition(obj);
|
||
136 | 1361 | jjdelcerro | JDynForm x = this.createJDynForm(definition);
|
137 | 1076 | jjdelcerro | x.setValues(obj); |
138 | return x;
|
||
139 | } |
||
140 | 919 | jjdelcerro | |
141 | 1361 | jjdelcerro | @Override
|
142 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(DynFormDefinition definition)
|
143 | throws ServiceException {
|
||
144 | return this.createJDynFormSet(this.defaultJDynFormSetname, definition); |
||
145 | } |
||
146 | 919 | jjdelcerro | |
147 | 1361 | jjdelcerro | @Override
|
148 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(DynStruct struct) throws ServiceException { |
149 | return this.createJDynFormSet(this.defaultJDynFormSetname, struct); |
||
150 | } |
||
151 | 1068 | jjdelcerro | |
152 | 1361 | jjdelcerro | @Override
|
153 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(DynObjectSet data)
|
154 | throws ServiceException {
|
||
155 | return this.createJDynFormSet(this.defaultJDynFormSetname, data); |
||
156 | 1068 | jjdelcerro | } |
157 | |||
158 | 1361 | jjdelcerro | @Override
|
159 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(List data) |
160 | throws ServiceException {
|
||
161 | return this.createJDynFormSet(this.defaultJDynFormSetname, data); |
||
162 | 1068 | jjdelcerro | } |
163 | |||
164 | 1361 | jjdelcerro | @Override
|
165 | 1076 | jjdelcerro | public DynObject createServiceParameters(String name) |
166 | throws ServiceException {
|
||
167 | DynObject params = this.getServiceManager().createServiceParameters(name);
|
||
168 | return params;
|
||
169 | 1068 | jjdelcerro | } |
170 | |||
171 | 1361 | jjdelcerro | @Override
|
172 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(String name, DynFormDefinition definition) throws ServiceException { |
173 | DynObject params = this.createServiceParameters(name);
|
||
174 | params.setDynValue("definition", definition);
|
||
175 | JDynFormSet formset = (JDynFormSet) this.getServiceManager().getService(params);
|
||
176 | return formset;
|
||
177 | 1068 | jjdelcerro | } |
178 | 1076 | jjdelcerro | |
179 | 1361 | jjdelcerro | @Override
|
180 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(String name, DynStruct struct) throws ServiceException { |
181 | DynFormDefinition definition = this.getDefinition(struct);
|
||
182 | return this.createJDynFormSet(name, definition); |
||
183 | } |
||
184 | |||
185 | 1361 | jjdelcerro | @Override
|
186 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(String name, List data) throws ServiceException { |
187 | if ( data.size() < 1 ) { |
||
188 | throw new IllegalArgumentException("The DynObjectSet not has elements."); |
||
189 | } |
||
190 | DynObject obj = (DynObject) data.get(0);
|
||
191 | JDynFormSet formset = this.createJDynFormSet(name, obj.getDynClass());
|
||
192 | formset.setValues(data); |
||
193 | return formset;
|
||
194 | } |
||
195 | |||
196 | 1361 | jjdelcerro | @Override
|
197 | 1076 | jjdelcerro | public JDynFormSet createJDynFormSet(String name, DynObjectSet data) throws ServiceException { |
198 | DynObject obj = null;
|
||
199 | DisposableIterator it = null;
|
||
200 | try {
|
||
201 | if ( data.getSize() < 1 ) { |
||
202 | throw new IllegalArgumentException("The DynObjectSet not has elements."); |
||
203 | } |
||
204 | it = data.iterator(); |
||
205 | obj = (DynObject) it.next(); |
||
206 | } catch (Exception e) { |
||
207 | throw new RuntimeException(e.getLocalizedMessage(), e); |
||
208 | } finally {
|
||
209 | if ( it != null ) { |
||
210 | it.dispose(); |
||
211 | } |
||
212 | } |
||
213 | JDynFormSet formset = this.createJDynFormSet(name, obj.getDynClass());
|
||
214 | formset.setValues(data); |
||
215 | return formset;
|
||
216 | } |
||
217 | |||
218 | 1361 | jjdelcerro | @Override
|
219 | 1076 | jjdelcerro | public String getDefaultJDynFormSetName() { |
220 | return this.defaultJDynFormSetname; |
||
221 | } |
||
222 | |||
223 | 1361 | jjdelcerro | @Override
|
224 | 1076 | jjdelcerro | public void setDefaultJDynFormSetName(String name) { |
225 | this.defaultJDynFormSetname = name;
|
||
226 | } |
||
227 | |||
228 | |||
229 | 919 | jjdelcerro | } |