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

History | View | Annotate | Download (5.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.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.service.ServiceException;
46
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
47
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
48
import org.gvsig.tools.swing.spi.AbstractJDynFieldComponent;
49

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

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

    
64
        /**
65
         * @param writable
66
         * @param dynField
67
         * @param value
68
         * @throws ServiceException
69
         */
70
        public JBooleanDynFieldComponent(ValueField parent)
71
        throws ServiceException {
72
                super(parent);
73
        if (parent.getDynField().isContainer()) {
74
            this.btUnknown.setEnabled(false);
75
        }
76
    }
77

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

    
89
    }
90

    
91
    @Override
92
    protected void afterUI() {
93

    
94
    }
95

    
96
    public JComponent asJComponent() {
97
        return panel;
98
    }
99

    
100
    public Object getValue() {
101
        return value;
102
    }
103

    
104
    @Override
105
    protected void initData() {
106

    
107
    }
108

    
109
    @Override
110
    protected void initUI() {
111
        // In initialization code:
112
        // Create the radio buttons.
113
        String txtTrue = this.translate("True");
114
        String txtFalse = this.translate("False");
115
        String txtUnkown = this.translate("Unknown");
116

    
117
        btTrue = new JRadioButton(txtTrue);
118
        btTrue.setMnemonic(KeyEvent.VK_T);
119
        btTrue.setActionCommand(txtTrue);
120

    
121
        btFalse = new JRadioButton(txtFalse);
122
        btFalse.setMnemonic(KeyEvent.VK_F);
123
        btFalse.setActionCommand(txtFalse);
124

    
125
        btUnknown = new JRadioButton(txtUnkown);
126
        btUnknown.setMnemonic(KeyEvent.VK_U);
127
        btUnknown.setActionCommand(txtUnkown);
128

    
129
        // Group the radio buttons.
130
        group = new ButtonGroup();
131
        group.add(btTrue);
132
        group.add(btFalse);
133
        group.add(btUnknown);
134

    
135
        // Register a listener for the radio buttons.
136
        btUnknown.addActionListener(this);
137
        btTrue.addActionListener(this);
138
        btFalse.addActionListener(this);
139

    
140
        // Initializing panel
141
        // panel = new JPanel(new BorderLayout());
142

    
143
        panel = this.createBoxLayout(btTrue, btFalse, 1, 1, null);
144
        panel.add(btUnknown);
145
    }
146

    
147
    @Override
148
    public boolean isMandatory() {
149
        return this.getDynField().isMandatory();
150
    }
151

    
152
    public void requestFocus() {
153
        btFalse.requestFocus();
154
    }
155

    
156
    public void setEnabled(boolean isEnabled) {
157
        // We are trying to enable something that is ReadOnly.
158
        // This is not possible
159
        if ((this.isReadOnly()) && (isEnabled == true)) {
160
            return;
161
        }
162

    
163
        btFalse.setEnabled(isEnabled);
164
        btTrue.setEnabled(isEnabled);
165
        btUnknown.setEnabled(isEnabled);
166
    }
167

    
168
    @Override
169
    protected void setJDynFieldComponentListeners() {
170
        // TODO Auto-generated method stub
171

    
172
    }
173

    
174
    @Override
175
    protected void setReadOnly() {
176
        this.setEnabled(false);
177
    }
178

    
179
    protected void setNullValue() {
180
        btTrue.setSelected(false);
181
        btFalse.setSelected(false);
182
        btUnknown.setSelected(true);
183
        this.value = null;
184
    }
185

    
186
    protected void setNonNullValue(Object value) {
187
        if (value instanceof String) {
188
            setNullValue();
189
            return;
190
        }
191
        this.value = (Boolean) value;
192

    
193
        btUnknown.setSelected(false);
194
        if (this.value == true) {
195
            btTrue.setSelected(true);
196
            btFalse.setSelected(false);
197
        } else
198
            if (this.value == false) {
199
                btFalse.setSelected(true);
200
                btTrue.setSelected(false);
201
            } else {
202
                setNullValue();
203
            }
204
    }
205
}