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

History | View | Annotate | Download (8 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
     */
79
    public AbstractDynObjectComponent(DynObject dynObject, DynObjectModel model) {
80
        this.dynObject = dynObject;
81
        if (model == null) {
82
            try {
83
                this.model =
84
                    ToolsSwingLocator.getDynObjectSwingManager()
85
                        .createDynObjectModel(this.dynObject.getDynClass());
86
            } catch (ServiceException e) {
87
                // TODO Auto-generated catch block
88
                e.printStackTrace();
89
            }
90
        } else {
91
            this.model = model;
92
        }
93
        manager = ToolsSwingLocator.getDynObjectSwingManager();
94
    }
95

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

    
109
    /*
110
     * (non-Javadoc)
111
     * 
112
     * @seeorg.gvsig.tools.swing.api.dynobject.JDynObjectComponent#
113
     * addValueChangedListener
114
     * (org.gvsig.tools.swing.api.dynobject.ValueChangedListener)
115
     */
116
    public void addValueChangedListener(ValueChangedListener listener) {
117
        Iterator<JDynFieldComponent> componentFields =
118
            this.getComponents().values().iterator();
119
        JDynFieldComponent comp;
120
        while (componentFields.hasNext()) {
121
            comp = componentFields.next();
122
            comp.addValueChangedListener(listener);
123
        }
124
    }
125

    
126
    /**
127
     * 
128
     * Sets all FieldValues of this {@link JDynObjectComponent} to null.
129
     * 
130
     */
131
    protected void emptyAll() {
132
        Map<Component, JDynFieldComponent> items = this.getComponents();
133
        for (JDynFieldComponent item : items.values()) {
134
            item.setValue(null);
135
        }
136
    }
137

    
138
    /**
139
     * Sets all FieldValues of this {@link JDynObjectComponent} to its the
140
     * current values
141
     * of the same FieldValues at the {@link DynObject} given as input.
142
     * 
143
     * @param value
144
     */
145
    protected void fillValues(DynObject dynObject) {
146
        Map<Component, JDynFieldComponent> items = this.getComponents();
147
        for (JDynFieldComponent item : items.values()) {
148
            item.setValue(dynObject.getDynValue(item.getDynField().getName()));
149
        }
150
    }
151

    
152
    /**
153
     * Gets the map of all graphic JDynFieldComponents of this
154
     * JDynObjectComponent.
155
     * 
156
     * @return
157
     *         the map containing the graphic Components.
158
     */
159
    private Map<Component, JDynFieldComponent> getComponents() {
160
        if (componentField == null) {
161
            componentField = new HashMap<Component, JDynFieldComponent>();
162
        }
163
        return componentField;
164

    
165
    }
166

    
167
    /*
168
     * (non-Javadoc)
169
     * 
170
     * @see
171
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
172
     */
173
    public Object getDefaultValue() {
174
        return this.getDynObject();
175
    }
176

    
177
    /*
178
     * (non-Javadoc)
179
     * 
180
     * @see
181
     * org.gvsig.tools.swing.impl.dynobject.JDynbjectComponent#getDynObject()
182
     */
183
    public DynObject getDynObject() {
184
        return dynObject;
185
    }
186

    
187
    //
188
    // public Object getValue() {
189
    // return this.getDynObject();
190
    // }
191

    
192
    /**
193
     * 
194
     * Returns a {@link JDynFieldComponent} that best matches with the given
195
     * {@link ValueField} and adds, if any, the given listener to the
196
     * JDynFieldComponent listeneres.
197
     * 
198
     * @param valueItem
199
     *            the {@link ValueField} element.
200
     * @param listener
201
     *            the current {@link ValueChangedListener} to be added to the
202
     *            JDynFieldComponent listeners.
203
     * @return
204
     *         the {@link JDynFieldComponent}.
205
     * 
206
     * @throws ServiceException
207
     */
208
    protected JDynFieldComponent getJDynFieldComponent(ValueField valueItem,
209
        ValueChangedListener listener) throws ServiceException {
210
        JDynFieldComponent jfield =
211
            ((DynObjectSwingManager) this.getManager())
212
                .createJDynFieldComponent(valueItem);
213
        if (listener != null) {
214
            jfield.addValueChangedListener(listener);
215
            jfield.fireValueChangedEvent();
216
        }
217
        return jfield;
218
    }
219

    
220
    /*
221
     * (non-Javadoc)
222
     * 
223
     * @see org.gvsig.tools.service.Service#getManager()
224
     */
225
    public Manager getManager() {
226
        return manager;
227
    }
228

    
229
    /*
230
     * (non-Javadoc)
231
     * 
232
     * @see org.gvsig.tools.swing.impl.dynobject.JDynbjectComponent#getModel()
233
     */
234
    public DynObjectModel getModel() {
235
        return model;
236
    }
237

    
238
    public boolean isValid() {
239
        Map<Component, JDynFieldComponent> items = this.getComponents();
240
        for (JDynFieldComponent item : items.values()) {
241
            if (!item.isValid()) {
242
                return false;
243
            }
244
        }
245
        return true;
246
    }
247

    
248
    /**
249
     * Walks through all JDynFieldComponents to save its current values into
250
     * their DynObject.
251
     */
252
    public void saveStatus() {
253
        Iterator<JDynFieldComponent> componentFields =
254
            this.getComponents().values().iterator();
255
        JDynFieldComponent comp;
256
        while (componentFields.hasNext()) {
257
            comp = componentFields.next();
258
            comp.saveStatus();
259
        }
260
    }
261

    
262
}