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

History | View | Annotate | Download (7.68 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.DynObject;
42
import org.gvsig.tools.service.Manager;
43
import org.gvsig.tools.service.ServiceException;
44
import org.gvsig.tools.swing.api.ToolsSwingLocator;
45
import org.gvsig.tools.swing.api.dynobject.DynObjectModel;
46
import org.gvsig.tools.swing.api.dynobject.DynObjectSwingManager;
47
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent;
48
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
49
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
50
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
51

    
52
/**
53
 * 
54
 * @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
55
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n&nbsp;</a> -
56
 *         gvSIG Team
57
 * @version $Id$
58
 * 
59
 */
60
public abstract class AbstractDynObjectComponent implements
61
    JDynObjectComponent, ValueField {
62

    
63
    private static final long serialVersionUID = 4088620749911859250L;
64

    
65
    private final DynObject dynObject;
66

    
67
    private DynObjectModel model;
68

    
69
    private final DynObjectSwingManager manager;
70

    
71
    private Map<Component, JDynFieldComponent> componentField;
72

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

    
97
    /**
98
     * Adds a new JDynFieldComponent to the list.
99
     * 
100
     * @param component
101
     *            the java awt Component
102
     * @param input
103
     *            the correspondent JDynFieldComponent object
104
     */
105
    protected void addComponentToList(Component component,
106
        JDynFieldComponent input) {
107
        this.getComponents().put(component, input);
108
    }
109

    
110
    /**
111
     * Gets the map of all graphic JDynFieldComponents of this
112
     * JDynObjectComponent.
113
     * 
114
     * @return
115
     *         the map containing the graphic Components.
116
     */
117
    private Map<Component, JDynFieldComponent> getComponents() {
118
        if (componentField == null) {
119
            componentField = new HashMap<Component, JDynFieldComponent>();
120
        }
121
        return componentField;
122

    
123
    }
124

    
125
    /*
126
     * (non-Javadoc)
127
     * 
128
     * @see
129
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
130
     */
131
    public Object getDefaultValue() {
132
        return this.getDynObject();
133
    }
134

    
135
    /*
136
     * (non-Javadoc)
137
     * 
138
     * @see
139
     * org.gvsig.tools.swing.impl.dynobject.JDynbjectComponent#getDynObject()
140
     */
141
    public DynObject getDynObject() {
142
        return dynObject;
143
    }
144

    
145
    /**
146
     * 
147
     * Returns a {@link JDynFieldComponent} that best matches with the given
148
     * {@link ValueField} and adds, if any, the given listener to the
149
     * JDynFieldComponent listeneres.
150
     * 
151
     * @param valueItem
152
     *            the {@link ValueField} element.
153
     * @param listener
154
     *            the current {@link ValueChangedListener} to be added to the
155
     *            JDynFieldComponent listeners.
156
     * @return
157
     *         the {@link JDynFieldComponent}.
158
     * 
159
     * @throws ServiceException
160
     */
161
    protected JDynFieldComponent getJDynFieldComponent(ValueField valueItem,
162
        ValueChangedListener listener) throws ServiceException {
163
        JDynFieldComponent jfield =
164
            ((DynObjectSwingManager) this.getManager())
165
                .createJDynFieldComponent(valueItem);
166
        if (listener != null) {
167
            jfield.addValueChangedListener(listener);
168
            jfield.fireValueChangedEvent();
169
        }
170
        return jfield;
171
    }
172

    
173
    /*
174
     * (non-Javadoc)
175
     * 
176
     * @see org.gvsig.tools.service.Service#getManager()
177
     */
178
    public Manager getManager() {
179
        return manager;
180
    }
181

    
182
    /*
183
     * (non-Javadoc)
184
     * 
185
     * @see org.gvsig.tools.swing.impl.dynobject.JDynbjectComponent#getModel()
186
     */
187
    public DynObjectModel getModel() {
188
        return model;
189
    }
190

    
191
    public boolean isValid() {
192
        Map<Component, JDynFieldComponent> items = this.getComponents();
193
        for (JDynFieldComponent item : items.values()) {
194
            if (!item.isValid()) {
195
                return false;
196
            }
197
        }
198
        return true;
199
    }
200

    
201
    /**
202
     * 
203
     * Sets all FieldValues of this {@link JDynObjectComponent} to null.
204
     * 
205
     */
206
    protected void emptyAll() {
207
        Map<Component, JDynFieldComponent> items = this.getComponents();
208
        for (JDynFieldComponent item : items.values())
209
            item.setValue(null);
210
    }
211

    
212
    /**
213
     * Sets all FieldValues of this {@link JDynObjectComponent} to its the
214
     * current values
215
     * of the same FieldValues at the {@link DynObject} given as input.
216
     * 
217
     * @param value
218
     */
219
    protected void fillValues(DynObject dynObject) {
220
        Map<Component, JDynFieldComponent> items = this.getComponents();
221
        for (JDynFieldComponent item : items.values())
222
            item.setValue(dynObject.getDynValue(item.getDynField().getName()));
223
    }
224

    
225
    /**
226
     * Walks through all JDynFieldComponents to save its current values into
227
     * their DynObject.
228
     */
229
    public void saveStatus() {
230
        Iterator<JDynFieldComponent> componentFields =
231
            this.getComponents().values().iterator();
232
        JDynFieldComponent comp;
233
        while (componentFields.hasNext()) {
234
            comp = componentFields.next();
235
            comp.saveStatus();
236
        }
237
    }
238

    
239
    public void addValueChangedListener(ValueChangedListener listener) {
240
        Iterator<JDynFieldComponent> componentFields =
241
            this.getComponents().values().iterator();
242
        JDynFieldComponent comp;
243
        while (componentFields.hasNext()) {
244
            comp = componentFields.next();
245
            comp.addValueChangedListener(listener);
246
        }
247
    }
248

    
249
}