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 / Date / JDynFormFieldDate.java @ 2853
History | View | Annotate | Download (11.3 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.Date; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.awt.event.FocusEvent; |
30 |
import java.awt.event.FocusListener; |
31 |
import java.net.URL; |
32 |
import java.text.DateFormat; |
33 |
import java.text.SimpleDateFormat; |
34 |
import java.util.Date; |
35 |
import java.util.Locale; |
36 |
import java.util.Objects; |
37 |
import javax.swing.ImageIcon; |
38 |
import javax.swing.JButton; |
39 |
import javax.swing.JComponent; |
40 |
import javax.swing.JPanel; |
41 |
import javax.swing.JPopupMenu; |
42 |
import javax.swing.JSpinner; |
43 |
import javax.swing.event.ChangeEvent; |
44 |
import org.freixas.jcalendar.DateEvent; |
45 |
import org.freixas.jcalendar.DateListener; |
46 |
import org.freixas.jcalendar.JCalendar; |
47 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
48 |
import org.gvsig.tools.dynform.JDynFormField; |
49 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
50 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
51 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
52 |
import org.gvsig.tools.dynform.spi.dynformfield.CustomSpinnerDateModel; |
53 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
54 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
55 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
56 |
|
57 |
/**
|
58 |
* @author gvSIG team
|
59 |
*
|
60 |
*/
|
61 |
public class JDynFormFieldDate extends AbstractJDynFormField implements JDynFormField, FocusListener { |
62 |
|
63 |
protected Object assignedValue = null; |
64 |
private JCalendar jcalendar = null; |
65 |
private JSpinner timeSpinner = null; |
66 |
private JButton button = null; |
67 |
private static final String DATE_PANEL_NAME = "Date panel"; |
68 |
|
69 |
public JDynFormFieldDate(
|
70 |
DynFormSPIManager serviceManager, |
71 |
ComponentsFactory componentsFactory, |
72 |
JDynFormFieldFactory factory, |
73 |
DynFormFieldDefinition definition, |
74 |
Object value
|
75 |
) { |
76 |
super(serviceManager, componentsFactory, factory, definition, value);
|
77 |
this.assignedValue = value;
|
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void setReadOnly(boolean readonly) { |
82 |
JComponent theJlabel = this.getJLabel(); |
83 |
if( this.jlabel != null ) { |
84 |
this.jlabel.setEnabled(!readonly);
|
85 |
} else if( theJlabel !=null ) { |
86 |
theJlabel.setEnabled(!readonly); |
87 |
} |
88 |
this.readOnly = readonly;
|
89 |
if (this.contents != null) { |
90 |
if (this.readOnly ) { |
91 |
this.timeSpinner.setEnabled(false); |
92 |
this.button.setEnabled(false); |
93 |
} else {
|
94 |
this.timeSpinner.setEnabled(true); |
95 |
this.button.setEnabled(true); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
public Object getAssignedValue() { |
101 |
return this.assignedValue; |
102 |
} |
103 |
|
104 |
protected JSpinner getJSpinner() { |
105 |
return (JSpinner) this.timeSpinner; |
106 |
} |
107 |
|
108 |
protected JButton getJButton() { |
109 |
return (JButton) this.button; |
110 |
} |
111 |
|
112 |
protected JCalendar getJCalendar() {
|
113 |
if (this.jcalendar == null) { |
114 |
this.jcalendar = new JCalendar(); |
115 |
this.jcalendar.addDateListener(new DateListener() { |
116 |
public void dateChanged(DateEvent arg0) { |
117 |
getJSpinner().setValue(getJCalendar().getDate()); |
118 |
} |
119 |
}); |
120 |
} |
121 |
return (JCalendar) this.jcalendar; |
122 |
} |
123 |
|
124 |
public void initComponent() { |
125 |
DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues(); |
126 |
if (availableValues == null) { |
127 |
this.contents = new JPanel(); |
128 |
this.contents.setName(DATE_PANEL_NAME);
|
129 |
this.contents.setLayout(new BorderLayout()); |
130 |
timeSpinner = this.getComponentsFactory().getJSpinner(this.getDefinition(), null); |
131 |
CustomSpinnerDateModel model = new CustomSpinnerDateModel(true); |
132 |
timeSpinner.setModel(model); |
133 |
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); |
134 |
String pattern = "dd-MM-yyyy"; |
135 |
try {
|
136 |
SimpleDateFormat sdf = (SimpleDateFormat) dateFormat; |
137 |
pattern = sdf.toPattern(); |
138 |
} catch (ClassCastException e) { |
139 |
LOGGER.warn("Can't determinate date format pattern, using default pattern:" + pattern);
|
140 |
} |
141 |
|
142 |
NullDateEditor timeEditor = new NullDateEditor(timeSpinner, pattern);
|
143 |
this.timeSpinner.setEditor(timeEditor);
|
144 |
this.timeSpinner.addFocusListener(new FocusListener() { |
145 |
@Override
|
146 |
public void focusGained(FocusEvent e) { |
147 |
fireFieldEnterEvent(); |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public void focusLost(FocusEvent e) { |
152 |
fireFieldExitEvent(); |
153 |
} |
154 |
}); |
155 |
// timeEditor.dismiss(timeSpinner);
|
156 |
button = this.getComponentsFactory().getJButton(this.getDefinition(), null); |
157 |
if( button.getIcon()==null ) { |
158 |
URL imageResource = this.getClass().getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-date.png"); |
159 |
if (imageResource != null) { |
160 |
button.setIcon(new ImageIcon(imageResource)); |
161 |
} |
162 |
} |
163 |
if( button.getText().trim().equals("...") ) { |
164 |
button.setText("");
|
165 |
} |
166 |
// button.setText("\u2026");
|
167 |
button.addActionListener(new ActionListener() { |
168 |
public void actionPerformed(ActionEvent event) { |
169 |
fireFieldEnterEvent(); |
170 |
JPopupMenu menu = new JPopupMenu(); |
171 |
menu.add(getJCalendar()); |
172 |
Date value = (Date) getJSpinner().getValue(); |
173 |
if (value != null) { |
174 |
getJCalendar().setDate(value); |
175 |
} |
176 |
JComponent thisComp = (JComponent) event.getSource(); |
177 |
menu.show(thisComp, 0, thisComp.getY() + 22); |
178 |
setValue(getJCalendar().getDate()); |
179 |
} |
180 |
}); |
181 |
|
182 |
if (this.getDefinition().isReadOnly()) { |
183 |
this.getJSpinner().setEnabled(false); |
184 |
this.getJButton().setEnabled(false); |
185 |
} |
186 |
this.getJSpinner().addFocusListener(this); |
187 |
|
188 |
if( !this.getComponentsFactory().containsJSpinner(this.getDefinition(), null) ) { |
189 |
this.contents.add(timeSpinner, BorderLayout.CENTER); |
190 |
this.contents.add(button, BorderLayout.LINE_END); |
191 |
} |
192 |
} |
193 |
this.setValue(this.assignedValue); |
194 |
} |
195 |
|
196 |
public void setValue(Object value) { |
197 |
if (value == null) { |
198 |
value = this.getDefinition().getDefaultValue();
|
199 |
if (value == null) { |
200 |
value = null;
|
201 |
} |
202 |
} else {
|
203 |
try {
|
204 |
this.getDefinition().validate(value);
|
205 |
this.problemIndicator().clear();
|
206 |
} catch (DynFieldValidateException e) {
|
207 |
this.problemIndicator().set(e.getLocalizedMessage());
|
208 |
} |
209 |
} |
210 |
if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) { |
211 |
try {
|
212 |
this.getJSpinner().setValue(value);
|
213 |
} catch (Exception ex) { |
214 |
|
215 |
} |
216 |
} |
217 |
this.assignedValue = value;
|
218 |
} |
219 |
|
220 |
/*
|
221 |
* Métodos específicos de cada tipo de datos
|
222 |
*/
|
223 |
public Object getValue() { |
224 |
Object value = null; |
225 |
if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) { |
226 |
value = getJSpinner().getValue(); |
227 |
} |
228 |
try {
|
229 |
this.getDefinition().validate(value);
|
230 |
this.problemIndicator().clear();
|
231 |
} catch (DynFieldValidateException e) {
|
232 |
throw new IllegalFieldValue(this, e.getLocalizedMessage()); |
233 |
} |
234 |
return value;
|
235 |
} |
236 |
|
237 |
@SuppressWarnings("unused") |
238 |
public boolean hasValidValue() { |
239 |
try {
|
240 |
Object value = this.getValue(); |
241 |
} catch (Exception e) { |
242 |
return false; |
243 |
} |
244 |
return true; |
245 |
} |
246 |
|
247 |
public void focusGained(FocusEvent arg0) { |
248 |
fireFieldEnterEvent(); |
249 |
this.problemIndicator().restore();
|
250 |
} |
251 |
|
252 |
public void focusLost(FocusEvent arg0) { |
253 |
if (this.hasValidValue()) { |
254 |
this.problemIndicator().clear();
|
255 |
} else {
|
256 |
try {
|
257 |
Object value = this.getValue(); |
258 |
} catch (Exception e) { |
259 |
this.problemIndicator().set(e.getLocalizedMessage());
|
260 |
} |
261 |
} |
262 |
fireFieldExitEvent(); |
263 |
} |
264 |
|
265 |
public void clear() { |
266 |
setValue(null);
|
267 |
// Object value = this.getDefinition().getDefaultValue();
|
268 |
// if( value != null ) {
|
269 |
// if (!(value instanceof Date)){
|
270 |
// try {
|
271 |
// value = DateFormat.getInstance().parse(value.toString());
|
272 |
// } catch (ParseException e) {
|
273 |
// this.problemIndicator().set(e.getLocalizedMessage());
|
274 |
// }
|
275 |
// }
|
276 |
// } else {
|
277 |
// value = "";
|
278 |
// }
|
279 |
// try {
|
280 |
// this.timeSpinner.setValue(value);
|
281 |
// } catch (RuntimeException e) {
|
282 |
// this.timeSpinner.setValue(getJCalendar().getDate());
|
283 |
// }
|
284 |
} |
285 |
|
286 |
private class NullDateEditor extends JSpinner.DateEditor { |
287 |
|
288 |
/**
|
289 |
*
|
290 |
*/
|
291 |
private static final long serialVersionUID = -522934985228645970L; |
292 |
|
293 |
/**
|
294 |
* @param jspinner
|
295 |
* @param pattern
|
296 |
*
|
297 |
*/
|
298 |
public NullDateEditor(JSpinner jspinner, String pattern) { |
299 |
super(jspinner, pattern);
|
300 |
} |
301 |
|
302 |
public NullDateEditor(JSpinner jspinner) { |
303 |
super(jspinner);
|
304 |
} |
305 |
|
306 |
public void stateChanged(ChangeEvent e) { |
307 |
Object value = getSpinner().getValue();
|
308 |
|
309 |
String text = ""; |
310 |
if (value != null) { |
311 |
text = getFormat().format((Date) value);
|
312 |
} |
313 |
getTextField().setText(text); |
314 |
} |
315 |
} |
316 |
|
317 |
@Override
|
318 |
public boolean isModified() { |
319 |
|
320 |
Date assigned = (Date) getAssignedValue(); |
321 |
if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) { |
322 |
Date value = (Date) getJSpinner().getValue(); |
323 |
return !Objects.equals(value, assigned);
|
324 |
} |
325 |
return false; |
326 |
} |
327 |
|
328 |
} |