Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / dynobject / dynfield / JComboBoxDynFieldComponent.java @ 586

History | View | Annotate | Download (8.24 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.impl.dynobject.dynfield;
35

    
36
import java.awt.event.ItemEvent;
37
import java.awt.event.ItemListener;
38
import java.util.ArrayList;
39
import java.util.List;
40

    
41
import javax.swing.JComboBox;
42
import javax.swing.JComponent;
43

    
44
import org.gvsig.tools.dynobject.DynObjectValueItem;
45
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
46
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
47
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
48

    
49
/**
50
 * 
51
 * JDynFieldComponent that renders its available values in the form of a
52
 * {@link JComboBox}.
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 class JComboBoxDynFieldComponent extends AbstractNValueFieldComponent
61
    implements JDynFieldComponent, ItemListener {
62

    
63
    private JComboBox combo;
64

    
65
    private DynObjectValueItem[] items;
66
    private List<ValueChangedListener> listeners;
67

    
68
    /**
69
     * Constructor.
70
     * 
71
     * @param component
72
     *            the JDynFieldComponent object
73
     * 
74
     * @param valueField
75
     *            the ValueField object.
76
     */
77
    public JComboBoxDynFieldComponent(JDynFieldComponent component,
78
                        ValueField valueField) {
79
                super(component, valueField);
80
    }
81

    
82
    /*
83
     * (non-Javadoc)
84
     * 
85
     * @see
86
     * 
87
     * org.gvsig.tools.swing.spi.DelegatedJFieldComponent#addValueChangedListener
88
     * (org.gvsig.tools.swing.api.dynobject.ValueChangedListener)
89
     */
90
    @Override
91
    public void addValueChangedListener(ValueChangedListener listener) {
92
        this.listeners.add(listener);
93
    }
94

    
95
    /**
96
     * 
97
     */
98
    @Override
99
    protected void afterUI() {
100
        Object value = this.getInitialValue();
101
        Object item;
102
        if (value != null) {
103
            for (int i = 0; i < this.items.length; i++) {
104
                item = items[i].getValue();
105
                if (item == null) {
106
                    continue;
107
                }
108
                if (item.equals(value)) {
109
                    this.combo.setSelectedIndex(i + 1);
110
                    break;
111
                }
112
            }
113
        }
114
    }
115

    
116
    /*
117
     * (non-Javadoc)
118
     * 
119
     * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
120
     */
121
    public JComponent asJComponent() {
122
        return this.combo;
123
    }
124

    
125
    /*
126
     * (non-Javadoc)
127
     * 
128
     * @see
129
     * org.gvsig.tools.swing.spi.DelegatedJFieldComponent#fireValueChangedEvent
130
     * ()
131
     */
132
    @Override
133
    public void fireValueChangedEvent() {
134
        for (ValueChangedListener listener : this.listeners) {
135
            listener.handleValueChanged(this);
136
        }
137
    }
138

    
139
    /*
140
     * (non-Javadoc)
141
     * 
142
     * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#getValue()
143
     */
144
    public Object getValue() {
145
        int index = this.combo.getSelectedIndex();
146
        if (index < 1) {
147
            return null;
148
        }
149
       
150
        // null is at combo position 0, so the index is one unit ahead as of the
151
        // items index
152
        return this.items[index - 1].getValue();
153
    }
154

    
155
    /*
156
     * (non-Javadoc)
157
     * 
158
     * @see org.gvsig.tools.swing.spi.AbstractJDynField#initData()
159
     */
160
    @Override
161
    protected void initData() {
162

    
163
    }
164

    
165
    /**
166
     * 
167
     */
168
    @Override
169
    protected void initUI() {
170
        listeners = new ArrayList<ValueChangedListener>();
171
        items = getDynField().getAvailableValues();
172
        this.combo = new JComboBox();
173
        this.combo.addItem(null);
174
        for (DynObjectValueItem item : items) {
175
            this.combo.addItem(item.getLabel());
176
        }
177
        this.combo.setSelectedIndex(0);
178
        this.combo.addItemListener(this);
179
    }
180

    
181
    /*
182
     * (non-Javadoc)
183
     * 
184
     * @see
185
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isValid()
186
     */
187
    public boolean isValid() {
188
      boolean isNotValid = (isMandatory())&&(this.combo.getSelectedIndex()<0);
189
      return !isNotValid;
190
    }
191

    
192
    /*
193
     * (non-Javadoc)
194
     * 
195
     * @see
196
     * java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
197
     */
198
    public void itemStateChanged(ItemEvent evt) {
199
        this.fireValueChangedEvent();
200
    }
201

    
202
    /*
203
     * (non-Javadoc)
204
     * 
205
     * @see
206
     * 
207
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#requestFocus
208
     * ()
209
     */
210
    public void requestFocus() {
211
        this.combo.requestFocus();
212
    }
213

    
214
    /*
215
     * (non-Javadoc)
216
     * 
217
     * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#saveStatus()
218
     */
219
    public void saveStatus() {
220
        this.setFieldValue(this.getValue());
221
    }
222

    
223
    /*
224
     * (non-Javadoc)
225
     * 
226
     * @see
227
     * 
228
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setEnabled
229
     * (boolean)
230
     */
231
    public void setEnabled(boolean isEnabled) {
232
        // Do nothing, it's a combobox so it's just readonly
233
        this.combo.setEnabled(isEnabled);
234
    }
235

    
236
    /*
237
     * (non-Javadoc)
238
     * 
239
     * @see
240
     * 
241
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setFieldValue
242
     * (java.lang.Object)
243
     */
244
    @Override
245
    public void setFieldValue(Object value) {
246
        this.getComponent().setFieldValue(value);
247
    }
248

    
249
    @Override
250
    protected void setJDynFieldComponentListeners() {
251
        // TODO Auto-generated method stub
252

    
253
    }
254

    
255
    @Override
256
    protected void setNonNullValue(Object value) {
257
        Object item;
258
        String label;
259
        for (int i = 0; i < this.items.length; i++) {
260
            item = this.items[i].getValue();
261
            if (item == null) {
262
                continue;
263
            }
264

    
265
            if (item.equals(value)) {
266
                this.setSelectedIndex(i+1);
267
                break;
268
            }
269
            label = this.items[i].getLabel();
270
            if (label == null) {
271
                continue;
272
            }
273

    
274
            if (label.equals(value)) {
275
                this.setSelectedIndex(i+1);
276
                break;
277
            }
278
        }
279
    }
280

    
281
    @Override
282
    protected void setNullValue() {
283
        this.combo.setSelectedIndex(0);
284
    }
285

    
286
  
287
    @Override
288
    protected void setReadOnly() {
289
        this.setEnabled(false);
290
    }
291
    
292
    public void setPrintValue(Object value) {
293
        // DO nothing. Override if necessary by inhereted classes.
294
    }
295

    
296
    public String getPrintValue() {
297
        Object value = this.combo.getSelectedItem();
298
        if (value != null){
299
            return value.toString();
300
        }
301
        return "";
302
    }
303

    
304
    @Override
305
    public Object getFieldValue() {
306
        return this.combo.getSelectedIndex();
307
    }
308

    
309
        @Override
310
        protected void setSelectedIndex(int index) {
311
                this.combo.setSelectedIndex(index);
312
        }
313

    
314
        public void handleValueChanged(JDynFieldComponent jDynFieldComponent) {
315
                // TODO Auto-generated method stub
316
        }
317
   
318
}