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
/**
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
 * 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
 * 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
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

    
30
import org.gvsig.tools.dynform.DynFormDefinition;
31
import org.gvsig.tools.dynform.DynFormManager;
32
import org.gvsig.tools.dynform.JDynForm;
33
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
import org.gvsig.tools.dynform.JDynForm.DynFormContext;
39
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
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
46
import org.gvsig.tools.resourcesstorage.EmptyResourcesStorage;
47
import org.gvsig.tools.script.ScriptManager;
48

    
49
@SuppressWarnings("UseSpecificCatch")
50
public class DefaultDynFormManager implements DynFormManager {
51

    
52
    
53
    private class EmptyDynFormContext implements DynFormContext {
54

    
55
        @Override
56
        public ResourcesStorage getResourcesStorage() {
57
            return new EmptyResourcesStorage();
58
        }
59

    
60
        @Override
61
        public ScriptManager getScriptManager() {
62
            return null;
63
        }
64
        
65
        
66
    }
67
    
68
    DynFormSPIManager serviceManager = null;
69

    
70
    private Map definitions = null;
71
    private final JDynFormFactory defaultJDynFormFactory;
72
    private final JDynFormSetFactory defaultJDynFormSetFactory;
73
            
74
    public DefaultDynFormManager() {
75
        this.defaultJDynFormFactory = new DefaultJDynFormFactory();
76
        this.defaultJDynFormSetFactory = new BaseJDynFormSetFactory();
77
    }
78

    
79
    public DynFormSPIManager getServiceManager() {
80
        if ( serviceManager == null ) {
81
            serviceManager = DynFormSPILocator.getDynFormSPIManager();
82
        }
83
        return serviceManager;
84
    }
85

    
86
    public Map getDefinitions() {
87
        if ( this.definitions == null ) {
88
            this.definitions = new HashMap();
89
        }
90
        return this.definitions;
91
    }
92

    
93
    @Override
94
    public DynFormDefinition getDefinition(String name) {
95
        DefaultDynFormDefinition x;
96
        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

    
104
    @Override
105
    public DynFormDefinition getDefinition(DynStruct definition) {
106
        String name = definition.getFullName();
107
        DefaultDynFormDefinition x ;
108
        x = (DefaultDynFormDefinition) this.getDefinitions().get(name);
109
        if ( x == null ) {
110
            x = new DefaultDynFormDefinition(definition, this);
111
            this.getDefinitions().put(x.getName(), x);
112
        }
113
        return x;
114
    }
115

    
116
    @Override
117
    public DynFormDefinition getDefinition(DynObject obj) {
118
        return this.getDefinition(obj.getDynClass());
119
    }
120

    
121
    @Override
122
    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
        try {
133
            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
            }
150
            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
            }
158
        }
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
            jdynform.setContext(context);
175
        } catch (Exception ex) {
176
            jdynform = this.defaultJDynFormFactory.create(getServiceManager(), context, definition);
177
        }
178
        return jdynform;
179
    }
180

    
181
    @Override
182
    public JDynForm createJDynForm(DynFormContext context, DynStruct struct) {
183
        DynFormDefinition definition = this.getDefinition(struct);
184
        return this.createJDynForm(context, definition);
185
    }
186

    
187
    @Override
188
    public JDynForm createJDynForm(DynFormContext context, DynObject obj) {
189
        DynFormDefinition definition = this.getDefinition(obj);
190
        JDynForm x = this.createJDynForm(context, definition);
191
        x.setValues(obj);
192
        return x;
193
    }
194

    
195
    @Override
196
    public JDynForm createJDynForm(DynFormDefinition definition) {
197
        return this.createJDynForm(null, definition);
198
    }
199
    @Override
200
    public JDynForm createJDynForm(DynStruct struct) {
201
        return this.createJDynForm(null, struct);
202
    }
203

    
204
    @Override
205
    public JDynForm createJDynForm(DynObject obj) {
206
        return this.createJDynForm(null, obj);
207
    }
208
        
209
    @Override
210
    public void removeDefinition(String name) {
211
        this.getDefinitions().remove(name);
212
    }
213
    
214
}