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 @ 2846
History | View | Annotate | Download (8.47 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 |
this.readonly = readonly;
|
74 |
if (this.contents != null) { |
75 |
if (readonly) {
|
76 |
this.txtTimestamp.setEditable(false); |
77 |
this.button.setEnabled(false); |
78 |
} else {
|
79 |
this.txtTimestamp.setEditable(true); |
80 |
this.button.setEnabled(true); |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public Object getAssignedValue() { |
87 |
return this.assignedValue; |
88 |
} |
89 |
|
90 |
protected JButton getJButton() { |
91 |
return (JButton) this.button; |
92 |
} |
93 |
|
94 |
protected JCalendar getJCalendar() {
|
95 |
if (this.jcalendar == null) { |
96 |
this.jcalendar = new JCalendar(); |
97 |
this.jcalendar.addDateListener((DateEvent arg0) -> {
|
98 |
Timestamp currentDate = (Timestamp) getValue(); |
99 |
if(currentDate == null){ |
100 |
currentDate = new Timestamp(new Date().getTime()); |
101 |
} |
102 |
Date newDate = getJCalendar().getDate();
|
103 |
|
104 |
currentDate.setDate(newDate.getDate()); |
105 |
currentDate.setMonth(newDate.getMonth()); |
106 |
currentDate.setYear(newDate.getYear()); |
107 |
txtTimestamp.setText(StringUtils.rightPad(currentDate.toString(),29,'0')); |
108 |
try {
|
109 |
this.getDefinition().validate(currentDate);
|
110 |
this.problemIndicator().clear();
|
111 |
} catch (DynFieldValidateException e) {
|
112 |
this.problemIndicator().set(e.getLocalizedMessage());
|
113 |
} |
114 |
fireFieldChangedEvent(); |
115 |
}); |
116 |
} |
117 |
return (JCalendar) this.jcalendar; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void initComponent() { |
122 |
DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues(); |
123 |
if (availableValues == null) { |
124 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
125 |
this.contents = new JPanel(); |
126 |
this.contents.setLayout(new BorderLayout()); |
127 |
|
128 |
this.txtTimestamp = this.getComponentsFactory().getJTextField(this.getDefinition(), null); |
129 |
toolsSwingManager.addClearButton(this.txtTimestamp);
|
130 |
toolsSwingManager.setDefaultPopupMenu(this.txtTimestamp);
|
131 |
this.txtTimestamp.addFocusListener(new FocusListener() { |
132 |
@Override
|
133 |
public void focusGained(FocusEvent e) { |
134 |
fireFieldEnterEvent(); |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public void focusLost(FocusEvent e) { |
139 |
fireFieldExitEvent(); |
140 |
} |
141 |
}); |
142 |
button = new JButton(); |
143 |
button.addActionListener((ActionEvent event) -> {
|
144 |
fireFieldEnterEvent(); |
145 |
JPopupMenu menu = new JPopupMenu(); |
146 |
menu.add(getJCalendar()); |
147 |
|
148 |
JComponent thisComp = (JComponent) event.getSource(); |
149 |
menu.show(thisComp, 0, thisComp.getY() + 22); |
150 |
}); |
151 |
button.setIcon(this.getIcon("picker-date")); |
152 |
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
153 |
|
154 |
this.contents.addFocusListener(this); |
155 |
if (this.getDefinition().isReadOnly()) { |
156 |
this.txtTimestamp.setEnabled(false); |
157 |
this.getJButton().setEnabled(false); |
158 |
} |
159 |
|
160 |
this.contents.add(txtTimestamp, BorderLayout.CENTER); |
161 |
this.contents.add(button, BorderLayout.LINE_END); |
162 |
} |
163 |
this.setValue(this.assignedValue); |
164 |
} |
165 |
|
166 |
@Override
|
167 |
public void setValue(Object value) { |
168 |
if (value == null) { |
169 |
this.txtTimestamp.setText(""); |
170 |
} else {
|
171 |
this.txtTimestamp.setText(StringUtils.rightPad(((Timestamp)value).toString(),29,'0')); |
172 |
} |
173 |
try {
|
174 |
this.getDefinition().validate(value);
|
175 |
this.problemIndicator().clear();
|
176 |
} catch (DynFieldValidateException e) {
|
177 |
this.problemIndicator().set(e.getLocalizedMessage());
|
178 |
} |
179 |
this.assignedValue = value;
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public Object getValue() { |
184 |
try {
|
185 |
String ss = this.txtTimestamp.getText(); |
186 |
if( StringUtils.isBlank(ss) ) {
|
187 |
return null; |
188 |
} |
189 |
Object value = Timestamp.valueOf(ss); |
190 |
this.getDefinition().validate(value);
|
191 |
this.problemIndicator().clear();
|
192 |
return value;
|
193 |
} catch (DynFieldValidateException e) {
|
194 |
throw new IllegalFieldValue(this, e.getLocalizedMessage()); |
195 |
} |
196 |
} |
197 |
|
198 |
@SuppressWarnings("unused") |
199 |
@Override
|
200 |
public boolean hasValidValue() { |
201 |
try {
|
202 |
Object value = this.getValue(); |
203 |
} catch (Exception e) { |
204 |
return false; |
205 |
} |
206 |
return true; |
207 |
} |
208 |
|
209 |
@Override
|
210 |
public void focusGained(FocusEvent arg0) { |
211 |
fireFieldEnterEvent(); |
212 |
this.problemIndicator().restore();
|
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void focusLost(FocusEvent arg0) { |
217 |
if (this.hasValidValue()) { |
218 |
this.problemIndicator().clear();
|
219 |
} else {
|
220 |
try {
|
221 |
Object value = this.getValue(); |
222 |
} catch (Exception e) { |
223 |
this.problemIndicator().set(e.getLocalizedMessage());
|
224 |
} |
225 |
} |
226 |
fireFieldExitEvent(); |
227 |
} |
228 |
|
229 |
@Override
|
230 |
public boolean isModified() { |
231 |
|
232 |
Timestamp assigned = (Timestamp) getAssignedValue(); |
233 |
String ss = this.txtTimestamp.getText(); |
234 |
if (StringUtils.isBlank(ss)) {
|
235 |
return assigned != null; |
236 |
} |
237 |
|
238 |
try {
|
239 |
Timestamp value = Timestamp.valueOf(ss); |
240 |
return !Objects.equals(value, assigned);
|
241 |
} catch (Exception ex) { |
242 |
return false; |
243 |
} |
244 |
} |
245 |
|
246 |
|
247 |
} |