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

History | View | Annotate | Download (7.94 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
        manager = ToolsSwingLocator.getDynObjectSwingManager();
93
    }
94

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

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

    
121
    }
122

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

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

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

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

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

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

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

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

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

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

    
241
    /*
242
     * (non-Javadoc)
243
     * 
244
     * @seeorg.gvsig.tools.swing.api.dynobject.JDynObjectComponent#
245
     * addValueChangedListener
246
     * (org.gvsig.tools.swing.api.dynobject.ValueChangedListener)
247
     */
248
    public void addValueChangedListener(ValueChangedListener listener) {
249
        Iterator<JDynFieldComponent> componentFields =
250
            this.getComponents().values().iterator();
251
        JDynFieldComponent comp;
252
        while (componentFields.hasNext()) {
253
            comp = componentFields.next();
254
            comp.addValueChangedListener(listener);
255
        }
256
    }
257

    
258
}