gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformfield / Boolean / JDynFormFieldBoolean.java @ 1782
History | View | Annotate | Download (4.96 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dynform.services.dynformfield.Boolean; |
25 |
|
26 |
import java.awt.event.FocusEvent; |
27 |
import java.awt.event.FocusListener; |
28 |
import java.awt.event.ItemEvent; |
29 |
import java.awt.event.ItemListener; |
30 |
|
31 |
import javax.swing.JCheckBox; |
32 |
import javax.swing.event.ChangeEvent; |
33 |
import javax.swing.event.ChangeListener; |
34 |
|
35 |
import org.gvsig.tools.dynform.JDynFormField; |
36 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField.IllegalFieldValue; |
37 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormFieldWithValueList; |
38 |
import org.gvsig.tools.dynobject.DynObject; |
39 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
40 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
41 |
import org.gvsig.tools.service.spi.ServiceManager; |
42 |
|
43 |
public class JDynFormFieldBoolean extends AbstractJDynFormFieldWithValueList implements JDynFormField, FocusListener { |
44 |
|
45 |
protected Object assignedValue = null; |
46 |
|
47 |
public JDynFormFieldBoolean(DynObject parameters,
|
48 |
ServiceManager serviceManager) { |
49 |
super(parameters, serviceManager);
|
50 |
this.assignedValue = this.getParameterValue(); |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public Object getAssignedValue() { |
55 |
return this.assignedValue; |
56 |
} |
57 |
|
58 |
protected JCheckBox getJCheckBox() { |
59 |
return (JCheckBox) this.contents; |
60 |
} |
61 |
|
62 |
@Override
|
63 |
public void initComponent() { |
64 |
DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues(); |
65 |
if( availableValues==null ) { |
66 |
this.contents = new JCheckBox(); |
67 |
((JCheckBox)(this.contents)).addItemListener(new ItemListener() { |
68 |
@Override
|
69 |
public void itemStateChanged(ItemEvent e) { |
70 |
fireFieldChangedEvent(); |
71 |
} |
72 |
}); |
73 |
this.contents.addFocusListener(this); |
74 |
if( this.getDefinition().isReadOnly() ) { |
75 |
this.getJCheckBox().setEnabled(false); |
76 |
} |
77 |
this.setValue(this.assignedValue); |
78 |
} else {
|
79 |
super.initComponent();
|
80 |
} |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public void setValue(Object value) { |
85 |
boolean s = false; |
86 |
if( value == null ) { |
87 |
value = this.getDefinition().getDefaultValue();
|
88 |
if( value != null ) { |
89 |
s = (Boolean) value;
|
90 |
} |
91 |
} else {
|
92 |
s = (Boolean) value;
|
93 |
try {
|
94 |
this.getDefinition().validate(value);
|
95 |
this.problemIndicator().clear();
|
96 |
} catch (DynFieldValidateException e) {
|
97 |
this.problemIndicator().set(e.getLocalizedMessage());
|
98 |
} |
99 |
} |
100 |
if( this.contents instanceof JCheckBox) { |
101 |
this.getJCheckBox().setSelected(s);
|
102 |
this.assignedValue = value;
|
103 |
} else {
|
104 |
super.setValue(s);
|
105 |
} |
106 |
} |
107 |
|
108 |
/*
|
109 |
* M?todos espec?ficos de cada tipo de datos
|
110 |
*/
|
111 |
|
112 |
@Override
|
113 |
public Object getValue() { |
114 |
Object value = null; |
115 |
if( this.contents instanceof JCheckBox ) { |
116 |
boolean s = getJCheckBox().isSelected();
|
117 |
value = s; |
118 |
try {
|
119 |
this.getDefinition().validate(value);
|
120 |
this.problemIndicator().clear();
|
121 |
} catch (DynFieldValidateException e) {
|
122 |
throw new IllegalFieldValue(this, e.getLocalizedMessage()); |
123 |
} |
124 |
return value;
|
125 |
} else {
|
126 |
return super.getValue(); |
127 |
} |
128 |
} |
129 |
|
130 |
// /**
|
131 |
// *
|
132 |
// * @return
|
133 |
// */
|
134 |
// @SuppressWarnings("unused")
|
135 |
// @Override
|
136 |
// public boolean hasValidValue() {
|
137 |
// try {
|
138 |
// Object value = this.getValue();
|
139 |
// } catch(Exception e) {
|
140 |
// return false;
|
141 |
// }
|
142 |
// return true;
|
143 |
// }
|
144 |
//
|
145 |
// @Override
|
146 |
// public void focusGained(FocusEvent arg0) {
|
147 |
// fireFieldEnterEvent();
|
148 |
// this.problemIndicator().restore();
|
149 |
// }
|
150 |
//
|
151 |
// @Override
|
152 |
// public void focusLost(FocusEvent arg0) {
|
153 |
// if( this.hasValidValue() ) {
|
154 |
// this.problemIndicator().clear();
|
155 |
// } else {
|
156 |
// try {
|
157 |
// Object value = this.getValue();
|
158 |
// } catch(Exception e) {
|
159 |
// this.problemIndicator().set(e.getLocalizedMessage());
|
160 |
// }
|
161 |
// }
|
162 |
// fireFieldExitEvent();
|
163 |
// }
|
164 |
|
165 |
} |