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 @ 3056
History | View | Annotate | Download (11.4 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.GridBagConstraints; |
27 |
import java.awt.GridBagLayout; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.FocusEvent; |
30 |
import java.awt.event.FocusListener; |
31 |
import java.awt.event.MouseAdapter; |
32 |
import java.awt.event.MouseEvent; |
33 |
import java.sql.Timestamp; |
34 |
import java.util.Date; |
35 |
import java.util.Objects; |
36 |
import javax.swing.JButton; |
37 |
import javax.swing.JComponent; |
38 |
import javax.swing.JPanel; |
39 |
import javax.swing.JPopupMenu; |
40 |
import javax.swing.event.DocumentEvent; |
41 |
import javax.swing.event.DocumentListener; |
42 |
import javax.swing.text.JTextComponent; |
43 |
import org.apache.commons.lang3.StringUtils; |
44 |
import org.freixas.jcalendar.DateEvent; |
45 |
import org.freixas.jcalendar.JCalendar; |
46 |
import org.gvsig.tools.dataTypes.CoercionException; |
47 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
48 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
49 |
import org.gvsig.tools.dynform.JDynFormField; |
50 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
51 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
52 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
53 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
54 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
55 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
56 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
57 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
58 |
|
59 |
public class JDynFormFieldTimestamp extends AbstractJDynFormField implements JDynFormField { |
60 |
|
61 |
protected Object assignedValue = null; |
62 |
private JCalendar jcalendar = null; |
63 |
private JButton button = null; |
64 |
private boolean readonly = false; |
65 |
private JTextComponent txtTimestamp; |
66 |
private final DocumentListener documentListener; |
67 |
private final FocusListener focusListener; |
68 |
|
69 |
public JDynFormFieldTimestamp(
|
70 |
DynFormSPIManager serviceManager, |
71 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
72 |
JDynFormFieldFactory factory, |
73 |
DynFormFieldDefinition definition, |
74 |
Object value
|
75 |
) { |
76 |
super(serviceManager, componentsFactory, factory, definition, value);
|
77 |
this.assignedValue = value;
|
78 |
this.focusListener = new FocusListener() { |
79 |
@Override
|
80 |
public void focusGained(FocusEvent e) { |
81 |
fireFieldEnterEvent(); |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void focusLost(FocusEvent e) { |
86 |
if (hasValidValue()) {
|
87 |
problemIndicator().clear(); |
88 |
} else {
|
89 |
try {
|
90 |
Object value = getValue();
|
91 |
} catch (Exception ex) { |
92 |
problemIndicator().set(ex.getLocalizedMessage()); |
93 |
} |
94 |
} |
95 |
fireFieldExitEvent(); |
96 |
} |
97 |
}; |
98 |
this.documentListener = new DocumentListener() { |
99 |
@Override
|
100 |
public void insertUpdate(DocumentEvent e) { |
101 |
// updateProblemIndicator();
|
102 |
fireFieldChangedEvent(); |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public void removeUpdate(DocumentEvent e) { |
107 |
// updateProblemIndicator();
|
108 |
fireFieldChangedEvent(); |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void changedUpdate(DocumentEvent e) { |
113 |
// updateProblemIndicator();
|
114 |
fireFieldChangedEvent(); |
115 |
} |
116 |
}; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void setReadOnly(boolean readonly) { |
121 |
JComponent theJlabel = this.getJLabel(); |
122 |
if( this.jlabel != null ) { |
123 |
this.jlabel.setEnabled(!readonly);
|
124 |
} else if( theJlabel !=null ) { |
125 |
theJlabel.setEnabled(!readonly); |
126 |
} |
127 |
this.readonly = readonly;
|
128 |
if (this.contents != null) { |
129 |
if (readonly) {
|
130 |
this.txtTimestamp.setEditable(false); |
131 |
this.button.setEnabled(false); |
132 |
} else {
|
133 |
this.txtTimestamp.setEditable(true); |
134 |
this.button.setEnabled(true); |
135 |
} |
136 |
} |
137 |
this.setReadOnlyButtonsOfEvents(readonly);
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public Object getAssignedValue() { |
142 |
return this.assignedValue; |
143 |
} |
144 |
|
145 |
protected JButton getJButton() { |
146 |
return (JButton) this.button; |
147 |
} |
148 |
|
149 |
protected JCalendar getJCalendar() {
|
150 |
if (this.jcalendar == null) { |
151 |
this.jcalendar = new JCalendar(JCalendar.DISPLAY_DATE | JCalendar.DISPLAY_TIME, false); |
152 |
this.jcalendar.setNullAllowed(true); |
153 |
this.jcalendar.addDateListener((DateEvent arg0) -> {
|
154 |
doJCalendarChanged(); |
155 |
}); |
156 |
} |
157 |
return (JCalendar) this.jcalendar; |
158 |
} |
159 |
|
160 |
private void doJCalendarChanged() { |
161 |
Timestamp currentDate = null; |
162 |
try {
|
163 |
currentDate = (Timestamp) getValue();
|
164 |
} catch(Exception ex) { |
165 |
LOGGER.debug("Can't get current value",ex);
|
166 |
} |
167 |
if(currentDate == null){ |
168 |
currentDate = new Timestamp(new Date().getTime()); |
169 |
} |
170 |
Date newDate = getJCalendar().getDate();
|
171 |
if( newDate == null ) { |
172 |
return;
|
173 |
} |
174 |
|
175 |
currentDate.setDate(newDate.getDate()); |
176 |
currentDate.setMonth(newDate.getMonth()); |
177 |
currentDate.setYear(newDate.getYear()); |
178 |
txtTimestamp.setText(StringUtils.rightPad(currentDate.toString(),29,'0')); |
179 |
try {
|
180 |
this.getDefinition().validate(currentDate);
|
181 |
this.problemIndicator().clear();
|
182 |
} catch (DynFieldValidateException e) {
|
183 |
this.problemIndicator().set(e.getLocalizedMessage());
|
184 |
} |
185 |
fireFieldChangedEvent(); |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public void initComponent() { |
190 |
DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues(); |
191 |
if (availableValues == null) { |
192 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
193 |
this.contents = new JPanel(); |
194 |
|
195 |
this.txtTimestamp = this.getComponentsFactory().getJTextField(this.getDefinition(), null); |
196 |
this.txtTimestamp.removeFocusListener(focusListener);
|
197 |
this.txtTimestamp.getDocument().removeDocumentListener(documentListener);
|
198 |
|
199 |
button = this.getComponentsFactory().getJButton(this.getDefinition(), null); |
200 |
if( button == null ) { |
201 |
button = new JButton(); |
202 |
} |
203 |
|
204 |
toolsSwingManager.addClearButton(this.txtTimestamp);
|
205 |
toolsSwingManager.setDefaultPopupMenu(this.txtTimestamp);
|
206 |
this.txtTimestamp.addFocusListener(focusListener);
|
207 |
this.txtTimestamp.getDocument().addDocumentListener(documentListener);
|
208 |
|
209 |
ToolsSwingUtils.configurePickersButton( |
210 |
button, |
211 |
"_Show_calendar",
|
212 |
"picker-date",
|
213 |
(ActionEvent e) -> { doShowCalendar(); },
|
214 |
focusListener |
215 |
); |
216 |
|
217 |
this.contents.addFocusListener(this); |
218 |
if (this.getDefinition().isReadOnly()) { |
219 |
this.txtTimestamp.setEnabled(false); |
220 |
this.getJButton().setEnabled(false); |
221 |
} |
222 |
|
223 |
this.contents.setLayout(new GridBagLayout()); |
224 |
GridBagConstraints c = new GridBagConstraints(); |
225 |
c.fill = GridBagConstraints.HORIZONTAL;
|
226 |
c.ipadx = 4;
|
227 |
c.ipady = 1;
|
228 |
c.gridx = 1;
|
229 |
c.gridy = 0;
|
230 |
c.weightx = 1;
|
231 |
this.contents.add(this.txtTimestamp, c); |
232 |
c.fill = GridBagConstraints.NONE;
|
233 |
c.ipadx = 4;
|
234 |
c.ipady = 1;
|
235 |
c.gridx = 2;
|
236 |
c.gridy = 0;
|
237 |
c.weightx = 0;
|
238 |
this.contents.add(this.button, c); |
239 |
} |
240 |
this.setValue(this.assignedValue); |
241 |
} |
242 |
|
243 |
private void doShowCalendar() { |
244 |
fireFieldEnterEvent(); |
245 |
JPopupMenu menu = new JPopupMenu(); |
246 |
try {
|
247 |
Date v = (Date) this.getValue(); |
248 |
getJCalendar().setDate(v); |
249 |
} catch(Exception ex) { |
250 |
LOGGER.debug("Can't asign date to calendar");
|
251 |
getJCalendar().setDate(null);
|
252 |
} |
253 |
menu.add(getJCalendar()); |
254 |
|
255 |
menu.show(txtTimestamp, 0, txtTimestamp.getY() + 22); |
256 |
} |
257 |
|
258 |
@Override
|
259 |
public void setValue(Object value) { |
260 |
if (value == null) { |
261 |
this.txtTimestamp.setText(""); |
262 |
} else {
|
263 |
this.txtTimestamp.setText(StringUtils.rightPad(((Timestamp)value).toString(),29,'0')); |
264 |
} |
265 |
try {
|
266 |
this.getDefinition().validate(value);
|
267 |
this.problemIndicator().clear();
|
268 |
} catch (DynFieldValidateException e) {
|
269 |
this.problemIndicator().set(e.getLocalizedMessage());
|
270 |
} |
271 |
this.assignedValue = value;
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public Object getValue() { |
276 |
try {
|
277 |
String ss = this.txtTimestamp.getText(); |
278 |
if( StringUtils.isBlank(ss) ) {
|
279 |
return null; |
280 |
} |
281 |
Object value = null; |
282 |
try {
|
283 |
value = Timestamp.valueOf(ss);
|
284 |
} catch(Exception ex) { |
285 |
value = DataTypeUtils.toTimestamp(value); |
286 |
} |
287 |
this.getDefinition().validate(value);
|
288 |
this.problemIndicator().clear();
|
289 |
return value;
|
290 |
} catch (DynFieldValidateException e) {
|
291 |
throw new IllegalFieldValue(this, e.getLocalizedMessage()); |
292 |
} |
293 |
} |
294 |
|
295 |
@SuppressWarnings("unused") |
296 |
@Override
|
297 |
public boolean hasValidValue() { |
298 |
try {
|
299 |
Object value = this.getValue(); |
300 |
} catch (Exception e) { |
301 |
return false; |
302 |
} |
303 |
return true; |
304 |
} |
305 |
|
306 |
@Override
|
307 |
public boolean isModified() { |
308 |
|
309 |
Timestamp assigned = (Timestamp) getAssignedValue(); |
310 |
String ss = this.txtTimestamp.getText(); |
311 |
if (StringUtils.isBlank(ss)) {
|
312 |
return assigned != null; |
313 |
} |
314 |
|
315 |
try {
|
316 |
Timestamp value = Timestamp.valueOf(ss); |
317 |
return !Objects.equals(value, assigned);
|
318 |
} catch (Exception ex) { |
319 |
return false; |
320 |
} |
321 |
} |
322 |
|
323 |
private void updateProblemIndicator() { |
324 |
try {
|
325 |
Object value = this.getDefinition().coerce(getValue()); |
326 |
this.getDefinition().validate(value);
|
327 |
this.problemIndicator().clear();
|
328 |
} catch (DynFieldValidateException ex) {
|
329 |
this.problemIndicator().set(ex.getLocalizedMessageStack());
|
330 |
} catch (CoercionException ex) {
|
331 |
this.problemIndicator().set(ex.getLocalizedMessage());
|
332 |
} |
333 |
} |
334 |
} |