Statistics
| Revision:

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

History | View | Annotate | Download (7.36 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.Map;
28
import org.gvsig.tools.dispose.DisposableIterator;
29 1881 jjdelcerro
30 933 jjdelcerro
import org.gvsig.tools.dynform.DynFormDefinition;
31
import org.gvsig.tools.dynform.DynFormManager;
32
import org.gvsig.tools.dynform.JDynForm;
33 919 jjdelcerro
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
34
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
35
import org.gvsig.tools.dynobject.DynObject;
36
import org.gvsig.tools.dynobject.DynStruct;
37
import org.gvsig.tools.service.ServiceException;
38 1735 jjdelcerro
import org.gvsig.tools.dynform.JDynForm.DynFormContext;
39 1881 jjdelcerro
import org.gvsig.tools.dynform.JDynFormSet;
40
import org.gvsig.tools.dynform.services.dynformset.base.BaseJDynFormSetFactory;
41
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory;
42
import org.gvsig.tools.dynform.spi.dynformset.JDynFormSetFactory;
43
import org.gvsig.tools.dynobject.DynObjectSet;
44
import org.gvsig.tools.dynobject.Tags;
45 1894 jjdelcerro
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
46
import org.gvsig.tools.resourcesstorage.EmptyResourcesStorage;
47 1948 jjdelcerro
import org.gvsig.tools.script.ScriptManager;
48 919 jjdelcerro
49 1881 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
50 1076 jjdelcerro
public class DefaultDynFormManager implements DynFormManager {
51 919 jjdelcerro
52 1881 jjdelcerro
53
    private class EmptyDynFormContext implements DynFormContext {
54
55
        @Override
56
        public ResourcesStorage getResourcesStorage() {
57 1894 jjdelcerro
            return new EmptyResourcesStorage();
58 1881 jjdelcerro
        }
59 1948 jjdelcerro
60
        @Override
61
        public ScriptManager getScriptManager() {
62
            return null;
63
        }
64 1881 jjdelcerro
65 1948 jjdelcerro
66 1881 jjdelcerro
    }
67
68 1076 jjdelcerro
    DynFormSPIManager serviceManager = null;
69 1033 jbadia
70 1076 jjdelcerro
    private Map definitions = null;
71 1881 jjdelcerro
    private final JDynFormFactory defaultJDynFormFactory;
72
    private final JDynFormSetFactory defaultJDynFormSetFactory;
73 1076 jjdelcerro
74
    public DefaultDynFormManager() {
75 1881 jjdelcerro
        this.defaultJDynFormFactory = new DefaultJDynFormFactory();
76
        this.defaultJDynFormSetFactory = new BaseJDynFormSetFactory();
77 1076 jjdelcerro
    }
78 919 jjdelcerro
79 1076 jjdelcerro
    public DynFormSPIManager getServiceManager() {
80
        if ( serviceManager == null ) {
81
            serviceManager = DynFormSPILocator.getDynFormSPIManager();
82
        }
83
        return serviceManager;
84
    }
85 919 jjdelcerro
86 1076 jjdelcerro
    public Map getDefinitions() {
87
        if ( this.definitions == null ) {
88
            this.definitions = new HashMap();
89
        }
90
        return this.definitions;
91
    }
92 919 jjdelcerro
93 1361 jjdelcerro
    @Override
94 1076 jjdelcerro
    public DynFormDefinition getDefinition(String name) {
95 1361 jjdelcerro
        DefaultDynFormDefinition x;
96 1076 jjdelcerro
        x = (DefaultDynFormDefinition) this.getDefinitions().get(name);
97
        if ( x == null ) {
98
            x = new DefaultDynFormDefinition(name, this);
99
            this.getDefinitions().put(x.getName(), x);
100
        }
101
        return x;
102
    }
103 919 jjdelcerro
104 1361 jjdelcerro
    @Override
105 1076 jjdelcerro
    public DynFormDefinition getDefinition(DynStruct definition) {
106 1361 jjdelcerro
        String name = definition.getFullName();
107
        DefaultDynFormDefinition x ;
108 1076 jjdelcerro
        x = (DefaultDynFormDefinition) this.getDefinitions().get(name);
109
        if ( x == null ) {
110 1405 jjdelcerro
            x = new DefaultDynFormDefinition(definition, this);
111 1076 jjdelcerro
            this.getDefinitions().put(x.getName(), x);
112
        }
113
        return x;
114
    }
115 919 jjdelcerro
116 1361 jjdelcerro
    @Override
117 1076 jjdelcerro
    public DynFormDefinition getDefinition(DynObject obj) {
118
        return this.getDefinition(obj.getDynClass());
119
    }
120 919 jjdelcerro
121 1361 jjdelcerro
    @Override
122 1881 jjdelcerro
    public JDynFormSet createJDynFormSet(DynFormContext context, DynFormDefinition definition) {
123
        return this.createJDynFormSet(context, definition, null);
124
    }
125
126
    @Override
127
    public JDynFormSet createJDynFormSet(DynFormContext context, DynFormDefinition definition, Tags contextTags) {
128
        if( context == null ) {
129
            context = new EmptyDynFormContext();
130
        }
131
        JDynFormSet jdynformset;
132 1361 jjdelcerro
        try {
133 1881 jjdelcerro
            JDynFormSetFactory factory = getServiceManager().getJDynFormSetFactory(context,definition, contextTags);
134
            jdynformset = factory.create(getServiceManager(), context, definition, contextTags);
135
//            jdynformset.setContext(context);
136
        } catch (Exception ex) {
137
            jdynformset = this.defaultJDynFormSetFactory.create(getServiceManager(), context, definition, contextTags);
138
        }
139
        return jdynformset;
140
    }
141
142
    @Override
143
    public JDynFormSet createJDynFormSet(DynFormContext context, DynObjectSet data) {
144
        DynObject obj = null;
145
        DisposableIterator it = null;
146
        try {
147
            if ( data.getSize() < 1 ) {
148
                throw new IllegalArgumentException("The DynObjectSet not has elements.");
149 1405 jjdelcerro
            }
150 1881 jjdelcerro
            it = data.iterator();
151
            obj = (DynObject) it.next();
152
        } catch (Exception e) {
153
            throw new RuntimeException(e.getLocalizedMessage(), e);
154
        } finally {
155
            if ( it != null ) {
156
                it.dispose();
157 1423 jbadia
            }
158 1881 jjdelcerro
        }
159
        DynFormDefinition definition = this.getDefinition(obj);
160
        JDynFormSet formset = this.createJDynFormSet(null, definition);
161
        formset.setValues(data);
162
        return formset;
163
    }
164
165
    @Override
166
    public JDynForm createJDynForm(DynFormContext context, DynFormDefinition definition) {
167
        if( context == null ) {
168
            context = new EmptyDynFormContext();
169
        }
170
        JDynForm jdynform;
171
        try {
172
            JDynFormFactory factory = getServiceManager().getJDynFormFactory(context, definition);
173
            jdynform = factory.create(getServiceManager(), context, definition);
174 1735 jjdelcerro
            jdynform.setContext(context);
175 1881 jjdelcerro
        } catch (Exception ex) {
176
            jdynform = this.defaultJDynFormFactory.create(getServiceManager(), context, definition);
177 1361 jjdelcerro
        }
178
        return jdynform;
179 1076 jjdelcerro
    }
180 919 jjdelcerro
181 1361 jjdelcerro
    @Override
182 1881 jjdelcerro
    public JDynForm createJDynForm(DynFormContext context, DynStruct struct) {
183 1076 jjdelcerro
        DynFormDefinition definition = this.getDefinition(struct);
184 1735 jjdelcerro
        return this.createJDynForm(context, definition);
185 1076 jjdelcerro
    }
186 919 jjdelcerro
187 1361 jjdelcerro
    @Override
188 1881 jjdelcerro
    public JDynForm createJDynForm(DynFormContext context, DynObject obj) {
189 1076 jjdelcerro
        DynFormDefinition definition = this.getDefinition(obj);
190 1735 jjdelcerro
        JDynForm x = this.createJDynForm(context, definition);
191 1076 jjdelcerro
        x.setValues(obj);
192
        return x;
193
    }
194 919 jjdelcerro
195 1361 jjdelcerro
    @Override
196 1881 jjdelcerro
    public JDynForm createJDynForm(DynFormDefinition definition) {
197 1735 jjdelcerro
        return this.createJDynForm(null, definition);
198
    }
199
    @Override
200 1881 jjdelcerro
    public JDynForm createJDynForm(DynStruct struct) {
201 1735 jjdelcerro
        return this.createJDynForm(null, struct);
202
    }
203
204
    @Override
205 1881 jjdelcerro
    public JDynForm createJDynForm(DynObject obj) {
206 1735 jjdelcerro
        return this.createJDynForm(null, obj);
207
    }
208
209 1361 jjdelcerro
    @Override
210 1363 jjdelcerro
    public void removeDefinition(String name) {
211
        this.getDefinitions().remove(name);
212
    }
213 1881 jjdelcerro
214 919 jjdelcerro
}