Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.serv / org.gvsig.tools.swing.serv.field / src / main / java / org / gvsig / tools / swing / serv / field / component / JBooleanDynFieldComponent.java @ 232

History | View | Annotate | Download (6.49 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.serv.field.component;
35

    
36
import java.awt.event.ActionEvent;
37
import java.awt.event.ActionListener;
38
import java.awt.event.KeyEvent;
39

    
40
import javax.swing.ButtonGroup;
41
import javax.swing.JComponent;
42
import javax.swing.JPanel;
43
import javax.swing.JRadioButton;
44

    
45
import org.gvsig.tools.dynobject.DynField;
46
import org.gvsig.tools.service.ServiceException;
47
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
48
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
49
import org.gvsig.tools.swing.spi.AbstractJDynFieldComponent;
50

    
51
/**
52
 * @author <a href="mailto:reinhold@uji.es">cmartin</a>
53
 * 
54
 */
55
public class JBooleanDynFieldComponent extends AbstractJDynFieldComponent
56
        implements JDynFieldComponent, ActionListener {
57

    
58
    private JRadioButton btTrue;
59
    private JRadioButton btFalse;
60
    private JRadioButton btUnknown;
61
    private Boolean value;
62
    private JPanel panel;
63
    private ButtonGroup group;
64

    
65
    /**
66
     * @param dynField
67
     * @param value
68
     * @throws ServiceException
69
     */
70
    public JBooleanDynFieldComponent(DynField dynField, ValueField value)
71
            throws ServiceException {
72
        super(dynField, value);
73
    }
74

    
75
    public void actionPerformed(ActionEvent e) {
76
        Object source = e.getSource();
77
        if (source == btTrue) {
78
            this.setValue(true);
79
        } else if (source == btFalse) {
80
            this.setValue(false);
81
        } else {
82
            this.setValue(null);
83
        }
84

    
85
    }
86

    
87
    /*
88
     * (non-Javadoc)
89
     * 
90
     * @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#afterUI()
91
     */
92
    @Override
93
    protected void afterUI() {
94

    
95
    }
96

    
97
    /*
98
     * (non-Javadoc)
99
     * 
100
     * @see
101
     * org.gvsig.tools.swing.api.dynobject.dynfield.JComponent#getComponent()
102
     */
103
        public JComponent asJComponent() {
104
        return panel;
105
    }
106

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

    
116
    /*
117
     * (non-Javadoc)
118
     * 
119
     * @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#initData()
120
     */
121
    @Override
122
    protected void initData() {
123

    
124
    }
125

    
126
    @Override
127
    protected void initUI() {
128
        // In initialization code:
129
        // Create the radio buttons.
130
        String txtTrue = this.translate("True");
131
        String txtFalse = this.translate("False");
132
        String txtUnkown = this.translate("Unknown");
133

    
134
        btTrue = new JRadioButton(txtTrue);
135
        btTrue.setMnemonic(KeyEvent.VK_T);
136
        btTrue.setActionCommand(txtTrue);
137

    
138
        btFalse = new JRadioButton(txtFalse);
139
        btFalse.setMnemonic(KeyEvent.VK_F);
140
        btFalse.setActionCommand(txtFalse);
141

    
142
        btUnknown = new JRadioButton(txtUnkown);
143
        btUnknown.setMnemonic(KeyEvent.VK_U);
144
        btUnknown.setActionCommand(txtUnkown);
145

    
146
        // Group the radio buttons.
147
        group = new ButtonGroup();
148
        group.add(btTrue);
149
        group.add(btFalse);
150
        group.add(btUnknown);
151

    
152
        // Register a listener for the radio buttons.
153
        btUnknown.addActionListener(this);
154
        btTrue.addActionListener(this);
155
        btFalse.addActionListener(this);
156

    
157
        // Initializing panel
158
        // panel = new JPanel(new BorderLayout());
159

    
160
        panel = this.createBoxLayout(btTrue, btFalse, 1, 1, null);
161
        panel.add(btUnknown);
162
    }
163

    
164
    /*
165
     * (non-Javadoc)
166
     * 
167
     * @see
168
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isMandatory
169
     * ()
170
     */
171
    @Override
172
    public boolean isMandatory() {
173
        return this.getDynField().isMandatory();
174
    }
175

    
176
    /*
177
     * (non-Javadoc)
178
     * 
179
     * @see
180
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setFocus
181
     * ()
182
     */
183
    public void requestFocus() {
184
        btFalse.requestFocus();
185
    }
186

    
187
    /*
188
     * (non-Javadoc)
189
     * 
190
     * @see
191
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setEnabled
192
     * (boolean)
193
     */
194
    public void setEnabled(boolean isEnabled) {
195
        // We are trying to enable something that is ReadOnly.
196
        // This is not possible
197
        if ((this.isReadOnly()) && (isEnabled == true))
198
            return;
199

    
200
        btFalse.setEnabled(isEnabled);
201
        btTrue.setEnabled(isEnabled);
202
        btUnknown.setEnabled(isEnabled);
203
    }
204

    
205
    /*
206
     * (non-Javadoc)
207
     * 
208
     * @seeorg.gvsig.tools.swing.spi.AbstractJDynFieldComponent#
209
     * setJDynFieldComponentListeners()
210
     */
211
    @Override
212
    protected void setJDynFieldComponentListeners() {
213
        // TODO Auto-generated method stub
214

    
215
    }
216

    
217
    /*
218
     * (non-Javadoc)
219
     * 
220
     * @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#setReadOnly()
221
     */
222
    @Override
223
    protected void setReadOnly() {
224
        this.setEnabled(false);
225
    }
226

    
227
    /*
228
     * (non-Javadoc)
229
     * 
230
     * @see
231
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
232
     * .lang.Object)
233
     */
234
    protected void setNullValue() {
235
        btTrue.setSelected(false);
236
        btFalse.setSelected(false);
237
        btUnknown.setSelected(true);
238
        this.value = null;
239
    }
240

    
241
    protected void setNonNullValue(Object value) {
242
        if (value instanceof String){
243
            setNullValue();
244
            return;
245
        }
246
        this.value = (Boolean) value;
247

    
248
        btUnknown.setSelected(false);
249
        if (this.value == true){
250
            btTrue.setSelected(true);
251
            btFalse.setSelected(false);
252
        }else if (this.value == false){
253
            btFalse.setSelected(true);
254
            btTrue.setSelected(false);
255
        }else{
256
            setNullValue();
257
        }
258
    }
259
}