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 @ 1947
History | View | Annotate | Download (10.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.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 javax.swing.ImageIcon; |
37 |
|
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 |
|
45 |
import org.freixas.jcalendar.DateEvent; |
46 |
import org.freixas.jcalendar.DateListener; |
47 |
import org.freixas.jcalendar.JCalendar; |
48 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
49 |
|
50 |
import org.gvsig.tools.dynform.JDynFormField; |
51 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
52 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
53 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
54 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
55 |
import org.gvsig.tools.dynform.spi.dynformfield.CustomSpinnerDateModel; |
56 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
57 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
58 |
|
59 |
/**
|
60 |
* @author gvSIG team
|
61 |
*
|
62 |
*/
|
63 |
public class JDynFormFieldDate extends AbstractJDynFormField implements JDynFormField, FocusListener { |
64 |
|
65 |
protected Object assignedValue = null; |
66 |
private JCalendar jcalendar = null; |
67 |
private JSpinner timeSpinner = null; |
68 |
private JButton button = null; |
69 |
private static final String DATE_PANEL_NAME = "Date panel"; |
70 |
|
71 |
public JDynFormFieldDate(
|
72 |
DynFormSPIManager serviceManager, |
73 |
ComponentsFactory componentsFactory, |
74 |
JDynFormFieldFactory factory, |
75 |
DynFormFieldDefinition definition, |
76 |
Object value
|
77 |
) { |
78 |
super(serviceManager, componentsFactory, factory, definition, value);
|
79 |
this.assignedValue = value;
|
80 |
} |
81 |
|
82 |
@Override
|
83 |
public void setReadOnly(boolean readonly) { |
84 |
JComponent theJlabel = this.getJLabel(); |
85 |
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 |
timeSpinner.setEditor(timeEditor); |
144 |
// timeEditor.dismiss(timeSpinner);
|
145 |
button = this.getComponentsFactory().getJButton(this.getDefinition(), null); |
146 |
if( button.getIcon()==null ) { |
147 |
URL imageResource = this.getClass().getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-date.png"); |
148 |
if (imageResource != null) { |
149 |
button.setIcon(new ImageIcon(imageResource)); |
150 |
} |
151 |
} |
152 |
if( button.getText().trim().equals("...") ) { |
153 |
button.setText("");
|
154 |
} |
155 |
// button.setText("\u2026");
|
156 |
button.addActionListener(new ActionListener() { |
157 |
public void actionPerformed(ActionEvent event) { |
158 |
JPopupMenu menu = new JPopupMenu(); |
159 |
menu.add(getJCalendar()); |
160 |
Date value = (Date) getJSpinner().getValue(); |
161 |
if (value != null) { |
162 |
getJCalendar().setDate(value); |
163 |
} |
164 |
JComponent thisComp = (JComponent) event.getSource(); |
165 |
menu.show(thisComp, 0, thisComp.getY() + 22); |
166 |
setValue(getJCalendar().getDate()); |
167 |
} |
168 |
}); |
169 |
|
170 |
if (this.getDefinition().isReadOnly()) { |
171 |
this.getJSpinner().setEnabled(false); |
172 |
this.getJButton().setEnabled(false); |
173 |
} |
174 |
this.getJSpinner().addFocusListener(this); |
175 |
|
176 |
if( !this.getComponentsFactory().containsJSpinner(this.getDefinition(), null) ) { |
177 |
this.contents.add(timeSpinner, BorderLayout.CENTER); |
178 |
this.contents.add(button, BorderLayout.LINE_END); |
179 |
} |
180 |
} |
181 |
this.setValue(this.assignedValue); |
182 |
} |
183 |
|
184 |
public void setValue(Object value) { |
185 |
if (value == null) { |
186 |
value = this.getDefinition().getDefaultValue();
|
187 |
if (value == null) { |
188 |
value = null;
|
189 |
} |
190 |
} else {
|
191 |
try {
|
192 |
this.getDefinition().validate(value);
|
193 |
this.problemIndicator().clear();
|
194 |
} catch (DynFieldValidateException e) {
|
195 |
this.problemIndicator().set(e.getLocalizedMessage());
|
196 |
} |
197 |
} |
198 |
if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) { |
199 |
try {
|
200 |
this.getJSpinner().setValue(value);
|
201 |
} catch (Exception ex) { |
202 |
|
203 |
} |
204 |
} |
205 |
this.assignedValue = value;
|
206 |
} |
207 |
|
208 |
/*
|
209 |
* Métodos específicos de cada tipo de datos
|
210 |
*/
|
211 |
public Object getValue() { |
212 |
Object value = null; |
213 |
if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) { |
214 |
value = getJSpinner().getValue(); |
215 |
} |
216 |
try {
|
217 |
this.getDefinition().validate(value);
|
218 |
this.problemIndicator().clear();
|
219 |
} catch (DynFieldValidateException e) {
|
220 |
throw new IllegalFieldValue(this, e.getLocalizedMessage()); |
221 |
} |
222 |
return value;
|
223 |
} |
224 |
|
225 |
@SuppressWarnings("unused") |
226 |
public boolean hasValidValue() { |
227 |
try {
|
228 |
Object value = this.getValue(); |
229 |
} catch (Exception e) { |
230 |
return false; |
231 |
} |
232 |
return true; |
233 |
} |
234 |
|
235 |
public void focusGained(FocusEvent arg0) { |
236 |
fireFieldEnterEvent(); |
237 |
this.problemIndicator().restore();
|
238 |
} |
239 |
|
240 |
public void focusLost(FocusEvent arg0) { |
241 |
if (this.hasValidValue()) { |
242 |
this.problemIndicator().clear();
|
243 |
} else {
|
244 |
try {
|
245 |
Object value = this.getValue(); |
246 |
} catch (Exception e) { |
247 |
this.problemIndicator().set(e.getLocalizedMessage());
|
248 |
} |
249 |
} |
250 |
fireFieldExitEvent(); |
251 |
} |
252 |
|
253 |
public void clear() { |
254 |
setValue(null);
|
255 |
// Object value = this.getDefinition().getDefaultValue();
|
256 |
// if( value != null ) {
|
257 |
// if (!(value instanceof Date)){
|
258 |
// try {
|
259 |
// value = DateFormat.getInstance().parse(value.toString());
|
260 |
// } catch (ParseException e) {
|
261 |
// this.problemIndicator().set(e.getLocalizedMessage());
|
262 |
// }
|
263 |
// }
|
264 |
// } else {
|
265 |
// value = "";
|
266 |
// }
|
267 |
// try {
|
268 |
// this.timeSpinner.setValue(value);
|
269 |
// } catch (RuntimeException e) {
|
270 |
// this.timeSpinner.setValue(getJCalendar().getDate());
|
271 |
// }
|
272 |
} |
273 |
|
274 |
private class NullDateEditor extends JSpinner.DateEditor { |
275 |
|
276 |
/**
|
277 |
*
|
278 |
*/
|
279 |
private static final long serialVersionUID = -522934985228645970L; |
280 |
|
281 |
/**
|
282 |
* @param jspinner
|
283 |
* @param pattern
|
284 |
*
|
285 |
*/
|
286 |
public NullDateEditor(JSpinner jspinner, String pattern) { |
287 |
super(jspinner, pattern);
|
288 |
} |
289 |
|
290 |
public NullDateEditor(JSpinner jspinner) { |
291 |
super(jspinner);
|
292 |
} |
293 |
|
294 |
public void stateChanged(ChangeEvent e) { |
295 |
Object value = getSpinner().getValue();
|
296 |
|
297 |
String text = ""; |
298 |
if (value != null) { |
299 |
text = getFormat().format((Date) value);
|
300 |
} |
301 |
getTextField().setText(text); |
302 |
} |
303 |
} |
304 |
} |