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 / Timestamp / JDynFormFieldTimestamp.java @ 2853
History | View | Annotate | Download (8.7 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.Timestamp; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Cursor; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.FocusEvent; |
30 |
import java.awt.event.FocusListener; |
31 |
import java.sql.Timestamp; |
32 |
import java.util.Date; |
33 |
import java.util.Objects; |
34 |
import javax.swing.JButton; |
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.JPanel; |
37 |
import javax.swing.JPopupMenu; |
38 |
import javax.swing.text.JTextComponent; |
39 |
import org.apache.commons.lang3.StringUtils; |
40 |
import org.freixas.jcalendar.DateEvent; |
41 |
import org.freixas.jcalendar.JCalendar; |
42 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
43 |
import org.gvsig.tools.dynform.JDynFormField; |
44 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
45 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
46 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
47 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
48 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
49 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
51 |
|
52 |
public class JDynFormFieldTimestamp extends AbstractJDynFormField implements JDynFormField, FocusListener { |
53 |
|
54 |
protected Object assignedValue = null; |
55 |
private JCalendar jcalendar = null; |
56 |
private JButton button = null; |
57 |
private boolean readonly = false; |
58 |
private JTextComponent txtTimestamp; |
59 |
|
60 |
public JDynFormFieldTimestamp(
|
61 |
DynFormSPIManager serviceManager, |
62 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
63 |
JDynFormFieldFactory factory, |
64 |
DynFormFieldDefinition definition, |
65 |
Object value
|
66 |
) { |
67 |
super(serviceManager, componentsFactory, factory, definition, value);
|
68 |
this.assignedValue = value;
|
69 |
} |
70 |
|
71 |
@Override
|
72 |
public void setReadOnly(boolean readonly) { |
73 |
JComponent theJlabel = this.getJLabel(); |
74 |
if( this.jlabel != null ) { |
75 |
this.jlabel.setEnabled(!readonly);
|
76 |
} else if( theJlabel !=null ) { |
77 |
theJlabel.setEnabled(!readonly); |
78 |
} |
79 |
this.readonly = readonly;
|
80 |
if (this.contents != null) { |
81 |
if (readonly) {
|
82 |
this.txtTimestamp.setEditable(false); |
83 |
this.button.setEnabled(false); |
84 |
} else {
|
85 |
this.txtTimestamp.setEditable(true); |
86 |
this.button.setEnabled(true); |
87 |
} |
88 |
} |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public Object getAssignedValue() { |
93 |
return this.assignedValue; |
94 |
} |
95 |
|
96 |
protected JButton getJButton() { |
97 |
return (JButton) this.button; |
98 |
} |
99 |
|
100 |
protected JCalendar getJCalendar() {
|
101 |
if (this.jcalendar == null) { |
102 |
this.jcalendar = new JCalendar(); |
103 |
this.jcalendar.addDateListener((DateEvent arg0) -> {
|
104 |
Timestamp currentDate = (Timestamp) getValue(); |
105 |
if(currentDate == null){ |
106 |
currentDate = new Timestamp(new Date().getTime()); |
107 |
} |
108 |
Date newDate = getJCalendar().getDate();
|
109 |
|
110 |
currentDate.setDate(newDate.getDate()); |
111 |
currentDate.setMonth(newDate.getMonth()); |
112 |
currentDate.setYear(newDate.getYear()); |
113 |
txtTimestamp.setText(StringUtils.rightPad(currentDate.toString(),29,'0')); |
114 |
try {
|
115 |
this.getDefinition().validate(currentDate);
|
116 |
this.problemIndicator().clear();
|
117 |
} catch (DynFieldValidateException e) {
|
118 |
this.problemIndicator().set(e.getLocalizedMessage());
|
119 |
} |
120 |
fireFieldChangedEvent(); |
121 |
}); |
122 |
} |
123 |
return (JCalendar) this.jcalendar; |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public void initComponent() { |
128 |
DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues(); |
129 |
if (availableValues == null) { |
130 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
131 |
this.contents = new JPanel(); |
132 |
this.contents.setLayout(new BorderLayout()); |
133 |
|
134 |
this.txtTimestamp = this.getComponentsFactory().getJTextField(this.getDefinition(), null); |
135 |
toolsSwingManager.addClearButton(this.txtTimestamp);
|
136 |
toolsSwingManager.setDefaultPopupMenu(this.txtTimestamp);
|
137 |
this.txtTimestamp.addFocusListener(new FocusListener() { |
138 |
@Override
|
139 |
public void focusGained(FocusEvent e) { |
140 |
fireFieldEnterEvent(); |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void focusLost(FocusEvent e) { |
145 |
fireFieldExitEvent(); |
146 |
} |
147 |
}); |
148 |
button = new JButton(); |
149 |
button.addActionListener((ActionEvent event) -> {
|
150 |
fireFieldEnterEvent(); |
151 |
JPopupMenu menu = new JPopupMenu(); |
152 |
menu.add(getJCalendar()); |
153 |
|
154 |
JComponent thisComp = (JComponent) event.getSource(); |
155 |
menu.show(thisComp, 0, thisComp.getY() + 22); |
156 |
}); |
157 |
button.setIcon(this.getIcon("picker-date")); |
158 |
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
159 |
|
160 |
this.contents.addFocusListener(this); |
161 |
if (this.getDefinition().isReadOnly()) { |
162 |
this.txtTimestamp.setEnabled(false); |
163 |
this.getJButton().setEnabled(false); |
164 |
} |
165 |
|
166 |
this.contents.add(txtTimestamp, BorderLayout.CENTER); |
167 |
this.contents.add(button, BorderLayout.LINE_END); |
168 |
} |
169 |
this.setValue(this.assignedValue); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public void setValue(Object value) { |
174 |
if (value == null) { |
175 |
this.txtTimestamp.setText(""); |
176 |
} else {
|
177 |
this.txtTimestamp.setText(StringUtils.rightPad(((Timestamp)value).toString(),29,'0')); |
178 |
} |
179 |
try {
|
180 |
this.getDefinition().validate(value);
|
181 |
this.problemIndicator().clear();
|
182 |
} catch (DynFieldValidateException e) {
|
183 |
this.problemIndicator().set(e.getLocalizedMessage());
|
184 |
} |
185 |
this.assignedValue = value;
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public Object getValue() { |
190 |
try {
|
191 |
String ss = this.txtTimestamp.getText(); |
192 |
if( StringUtils.isBlank(ss) ) {
|
193 |
return null; |
194 |
} |
195 |
Object value = Timestamp.valueOf(ss); |
196 |
this.getDefinition().validate(value);
|
197 |
this.problemIndicator().clear();
|
198 |
return value;
|
199 |
} catch (DynFieldValidateException e) {
|
200 |
throw new IllegalFieldValue(this, e.getLocalizedMessage()); |
201 |
} |
202 |
} |
203 |
|
204 |
@SuppressWarnings("unused") |
205 |
@Override
|
206 |
public boolean hasValidValue() { |
207 |
try {
|
208 |
Object value = this.getValue(); |
209 |
} catch (Exception e) { |
210 |
return false; |
211 |
} |
212 |
return true; |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void focusGained(FocusEvent arg0) { |
217 |
fireFieldEnterEvent(); |
218 |
this.problemIndicator().restore();
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public void focusLost(FocusEvent arg0) { |
223 |
if (this.hasValidValue()) { |
224 |
this.problemIndicator().clear();
|
225 |
} else {
|
226 |
try {
|
227 |
Object value = this.getValue(); |
228 |
} catch (Exception e) { |
229 |
this.problemIndicator().set(e.getLocalizedMessage());
|
230 |
} |
231 |
} |
232 |
fireFieldExitEvent(); |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public boolean isModified() { |
237 |
|
238 |
Timestamp assigned = (Timestamp) getAssignedValue(); |
239 |
String ss = this.txtTimestamp.getText(); |
240 |
if (StringUtils.isBlank(ss)) {
|
241 |
return assigned != null; |
242 |
} |
243 |
|
244 |
try {
|
245 |
Timestamp value = Timestamp.valueOf(ss); |
246 |
return !Objects.equals(value, assigned);
|
247 |
} catch (Exception ex) { |
248 |
return false; |
249 |
} |
250 |
} |
251 |
|
252 |
|
253 |
} |