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 @ 2548
History | View | Annotate | Download (5.15 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.FocusListener; |
27 |
import java.awt.event.ItemEvent; |
28 |
import java.awt.event.ItemListener; |
29 |
import java.util.Objects; |
30 |
|
31 |
import javax.swing.JCheckBox; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.gvsig.tools.dataTypes.CoercionException; |
34 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
35 |
|
36 |
import org.gvsig.tools.dynform.JDynFormField; |
37 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
38 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
39 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
40 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField.IllegalFieldValue; |
41 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormFieldWithValueList; |
42 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
43 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
44 |
|
45 |
public class JDynFormFieldBoolean extends AbstractJDynFormFieldWithValueList implements JDynFormField, FocusListener { |
46 |
|
47 |
public JDynFormFieldBoolean(
|
48 |
DynFormSPIManager serviceManager, |
49 |
ComponentsFactory componentsFactory, |
50 |
JDynFormFieldFactory factory, |
51 |
DynFormFieldDefinition definition, |
52 |
Object value
|
53 |
) { |
54 |
super(serviceManager, componentsFactory, factory, definition, value);
|
55 |
} |
56 |
|
57 |
protected JCheckBox getJCheckBox() { |
58 |
return (JCheckBox) this.contents; |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public void initComponent() { |
63 |
DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues(); |
64 |
if (availableValues == null) { |
65 |
JCheckBox checkBox = this.getComponentsFactory().getJCheckBox(this.getDefinition(), null); |
66 |
this.contents = checkBox;
|
67 |
checkBox.addItemListener((ItemEvent e) -> {
|
68 |
fireFieldChangedEvent(); |
69 |
}); |
70 |
this.contents.addFocusListener(this); |
71 |
if (this.getDefinition().isReadOnly()) { |
72 |
this.getJCheckBox().setEnabled(false); |
73 |
} |
74 |
this.setValue(this.getAssignedValue()); |
75 |
} else {
|
76 |
super.initComponent();
|
77 |
} |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void setValue(Object value) { |
82 |
boolean s = false; |
83 |
if (value == null) { |
84 |
value = this.getDefinition().getDefaultValue();
|
85 |
if (value != null) { |
86 |
s = (Boolean) value;
|
87 |
} |
88 |
} else {
|
89 |
s = (Boolean) value;
|
90 |
try {
|
91 |
this.getDefinition().validate(value);
|
92 |
this.problemIndicator().clear();
|
93 |
} catch (DynFieldValidateException e) {
|
94 |
this.problemIndicator().set(e.getLocalizedMessage());
|
95 |
} |
96 |
} |
97 |
if (this.contents instanceof JCheckBox) { |
98 |
this.getJCheckBox().setSelected(s);
|
99 |
this.assignedValue = value;
|
100 |
} else {
|
101 |
super.setValue(s);
|
102 |
} |
103 |
} |
104 |
|
105 |
/*
|
106 |
* M?todos espec?ficos de cada tipo de datos
|
107 |
*/
|
108 |
@Override
|
109 |
public Object getValue() { |
110 |
Object value = null; |
111 |
if (this.contents instanceof JCheckBox) { |
112 |
boolean s = getJCheckBox().isSelected();
|
113 |
value = s; |
114 |
try {
|
115 |
this.getDefinition().validate(value);
|
116 |
this.problemIndicator().clear();
|
117 |
} catch (DynFieldValidateException e) {
|
118 |
throw new IllegalFieldValue(this, e.getLocalizedMessage()); |
119 |
} |
120 |
return value;
|
121 |
} else {
|
122 |
return super.getValue(); |
123 |
} |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public boolean isModified() { |
128 |
Boolean assigned = (Boolean) getAssignedValue(); |
129 |
if (this.contents instanceof JCheckBox) { |
130 |
boolean value = getJCheckBox().isSelected();
|
131 |
if(!value && assigned == null){ |
132 |
return false; |
133 |
} |
134 |
return !Objects.equals(value, assigned);
|
135 |
} |
136 |
|
137 |
String s = this.getValueFromJComponent(); |
138 |
if (StringUtils.isBlank(s)) {
|
139 |
return assigned != null; |
140 |
} |
141 |
|
142 |
try {
|
143 |
Object value = this.getDefinition().coerce(s); |
144 |
return !Objects.equals(value, assigned);
|
145 |
} catch (CoercionException e) {
|
146 |
return true; |
147 |
} |
148 |
|
149 |
} |
150 |
|
151 |
} |