Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.spi / src / main / java / org / gvsig / tools / swing / spi / AbstractDynObjectComponent.java @ 270

History | View | Annotate | Download (6.95 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.spi;
35

    
36
import java.awt.Component;
37
import java.util.HashMap;
38
import java.util.Iterator;
39
import java.util.Map;
40

    
41
import org.gvsig.tools.dynobject.DynField;
42
import org.gvsig.tools.dynobject.DynObject;
43
import org.gvsig.tools.service.Manager;
44
import org.gvsig.tools.service.ServiceException;
45
import org.gvsig.tools.swing.api.ToolsSwingLocator;
46
import org.gvsig.tools.swing.api.dynobject.DynObjectModel;
47
import org.gvsig.tools.swing.api.dynobject.DynObjectSwingManager;
48
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent;
49
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
50
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
51
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
52

    
53
/**
54
 * @author <a href="mailto:reinhold@uji.es">cmartin</a>
55
 * 
56
 */
57
public abstract class AbstractDynObjectComponent implements
58
    JDynObjectComponent, ValueField {
59

    
60
    private static final long serialVersionUID = 4088620749911859250L;
61

    
62
    private final DynObject dynObject;
63

    
64
    private DynObjectModel model;
65

    
66
    private final DynObjectSwingManager manager;
67

    
68
    private Map<Component, JDynFieldComponent> componentField;
69

    
70
    /**
71
     * Constructor.
72
     * 
73
     * @param dynObject
74
     *            the {@link DynObject} to render
75
     */
76
    public AbstractDynObjectComponent(DynObject dynObject, DynObjectModel model) {
77
        this.dynObject = dynObject;
78
        if (model == null)
79
            try {
80
                this.model =
81
                    ToolsSwingLocator.getDynObjectSwingManager()
82
                        .createDynObjectModel(this.dynObject.getDynClass());
83
            } catch (ServiceException e) {
84
                // TODO Auto-generated catch block
85
                e.printStackTrace();
86
            }
87
        else
88
            this.model = model;
89
        manager = ToolsSwingLocator.getDynObjectSwingManager();
90
    }
91

    
92
    protected void addComponentToList(Component component,
93
        JDynFieldComponent input) {
94
        this.getComponents().put(component, input);
95
    }
96

    
97
    private Map<Component, JDynFieldComponent> getComponents() {
98
        if (componentField == null) {
99
            componentField = new HashMap<Component, JDynFieldComponent>();
100
        }
101
        return componentField;
102

    
103
    }
104

    
105
    /*
106
     * (non-Javadoc)
107
     * 
108
     * @see
109
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
110
     */
111
    public Object getDefaultValue() {
112
        return this.getDynObject();
113
    }
114

    
115
    /*
116
     * (non-Javadoc)
117
     * 
118
     * @see
119
     * org.gvsig.tools.swing.impl.dynobject.JDynbjectComponent#getDynObject()
120
     */
121
    public DynObject getDynObject() {
122
        return dynObject;
123
    }
124

    
125
    protected JDynFieldComponent getJDynFieldComponent(DynField field)
126
        throws ServiceException {
127
        return this.getJDynFieldComponent(field, null);
128
    }
129

    
130
    /**
131
     * @return
132
     * @throws ServiceException
133
     */
134
    protected JDynFieldComponent getJDynFieldComponent(DynField field,
135
        ValueChangedListener listener) throws ServiceException {
136
        ValueField valueItem =
137
            new DynObjectValueField(this.getDynObject(), field.getName());
138
        JDynFieldComponent jfield =
139
            ((DynObjectSwingManager) this.getManager())
140
                .createJDynFieldComponent(valueItem, field);
141
        if (listener != null) {
142
            jfield.addValueChangedListener(this);
143
            jfield.fireValueChangedEvent();
144
        }
145
        return jfield;
146
    }
147

    
148
    /*
149
     * (non-Javadoc)
150
     * 
151
     * @see org.gvsig.tools.service.Service#getManager()
152
     */
153
    public Manager getManager() {
154
        return manager;
155
    }
156

    
157
    /*
158
     * (non-Javadoc)
159
     * 
160
     * @see org.gvsig.tools.swing.impl.dynobject.JDynbjectComponent#getModel()
161
     */
162
    public DynObjectModel getModel() {
163
        return model;
164
    }
165

    
166
    public Object getValue() {
167
        return this.getDynObject();
168
    }
169

    
170
    public boolean isValid() {
171
        Map<Component, JDynFieldComponent> items = this.getComponents();
172
        for (JDynFieldComponent item : items.values()) {
173
            if (!this.isValid(item))
174
                return false;
175
        }
176
        return true;
177
    }
178

    
179
    /**
180
     * 
181
     */
182
    protected void emptyAll() {
183
        Map<Component, JDynFieldComponent> items = this.getComponents();
184
        for (JDynFieldComponent item : items.values())
185
            item.setValue(null);
186
    }
187

    
188
    /**
189
     * @param value
190
     */
191
    protected void fillValues(DynObject dynObject) {
192
        Map<Component, JDynFieldComponent> items = this.getComponents();
193
        for (JDynFieldComponent item : items.values())
194
            item.setValue(dynObject.getDynValue(item.getDynField().getName()));
195
    }
196

    
197
    /**
198
     * @param component
199
     * @return
200
     */
201
    protected abstract boolean isValid(JDynFieldComponent component);
202

    
203
    public void saveStatus() {
204
        Iterator<JDynFieldComponent> componentFields =
205
            this.getComponents().values().iterator();
206
        JDynFieldComponent comp;
207
        while (componentFields.hasNext()) {
208
            comp = componentFields.next();
209
            comp.saveStatus();
210
        }
211
    }
212

    
213
    /*
214
     * (non-Javadoc)
215
     * 
216
     * @seeorg.gvsig.tools.swing.api.dynobject.JDynObjectComponent#
217
     * addValueChangedListener
218
     * (org.gvsig.tools.swing.api.dynobject.ValueChangedListener)
219
     */
220
    public void addValueChangedListener(ValueChangedListener listener) {
221
        Iterator<JDynFieldComponent> componentFields =
222
            this.getComponents().values().iterator();
223
        JDynFieldComponent comp;
224
        while (componentFields.hasNext()) {
225
            comp = componentFields.next();
226
            comp.addValueChangedListener(listener);
227
        }
228
    }
229

    
230
}