Revision 1388 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
JDynFormFieldDate.java | ||
---|---|---|
29 | 29 |
import java.awt.event.FocusEvent; |
30 | 30 |
import java.awt.event.FocusListener; |
31 | 31 |
import java.text.DateFormat; |
32 |
import java.text.ParseException;
|
|
32 |
import java.text.SimpleDateFormat;
|
|
33 | 33 |
import java.util.Date; |
34 |
import java.util.Locale; |
|
34 | 35 |
|
35 | 36 |
import javax.swing.JButton; |
36 |
import javax.swing.JCheckBox; |
|
37 | 37 |
import javax.swing.JComponent; |
38 | 38 |
import javax.swing.JPanel; |
39 | 39 |
import javax.swing.JPopupMenu; |
40 | 40 |
import javax.swing.JSpinner; |
41 |
import javax.swing.event.ChangeEvent; |
|
41 | 42 |
|
42 | 43 |
import org.freixas.jcalendar.DateEvent; |
43 | 44 |
import org.freixas.jcalendar.DateListener; |
44 | 45 |
import org.freixas.jcalendar.JCalendar; |
46 |
|
|
45 | 47 |
import org.gvsig.tools.dynform.JDynFormField; |
46 | 48 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
47 | 49 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomSpinner; |
... | ... | |
50 | 52 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
51 | 53 |
import org.gvsig.tools.service.spi.ServiceManager; |
52 | 54 |
|
55 |
/** |
|
56 |
* @author gvSIG team |
|
57 |
* |
|
58 |
*/ |
|
53 | 59 |
public class JDynFormFieldDate extends AbstractJDynFormField implements JDynFormField, FocusListener { |
54 | 60 |
|
55 | 61 |
protected Object assignedValue = null; |
... | ... | |
59 | 65 |
private boolean readonly; |
60 | 66 |
private static final String DATE_PANEL_NAME = "Date panel"; |
61 | 67 |
|
68 |
/** |
|
69 |
* @param parameters |
|
70 |
* @param serviceManager |
|
71 |
*/ |
|
62 | 72 |
public JDynFormFieldDate(DynObject parameters, |
63 | 73 |
ServiceManager serviceManager) { |
64 | 74 |
super(parameters, serviceManager); |
... | ... | |
110 | 120 |
this.contents.setName(DATE_PANEL_NAME); |
111 | 121 |
this.contents.setLayout(new BorderLayout()); |
112 | 122 |
|
113 |
//timeSpinner = new JSpinner( new SpinnerDateModel() ); |
|
114 |
timeSpinner = new JCustomSpinner(this.getLabel()); |
|
115 |
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "dd-MM-yyyy"); |
|
123 |
timeSpinner = new JCustomSpinner(this.getLabel(), true); |
|
124 |
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); |
|
125 |
String pattern = "dd-MM-yyyy"; |
|
126 |
try{ |
|
127 |
SimpleDateFormat sdf = (SimpleDateFormat)dateFormat; |
|
128 |
pattern =sdf.toPattern(); |
|
129 |
} catch (ClassCastException e) { |
|
130 |
logger.warn("Can't determinate date format pattern, using default pattern:"+pattern); |
|
131 |
} |
|
132 |
|
|
133 |
NullDateEditor timeEditor = new NullDateEditor(timeSpinner, pattern); |
|
116 | 134 |
timeSpinner.setEditor(timeEditor); |
117 |
//timeEditor.dismiss(timeSpinner);
|
|
118 |
button = new JButton("...");
|
|
135 |
// timeEditor.dismiss(timeSpinner);
|
|
136 |
button = new JButton("\u2026");
|
|
119 | 137 |
button.addActionListener(new ActionListener() { |
120 | 138 |
public void actionPerformed(ActionEvent event) { |
121 | 139 |
JPopupMenu menu = new JPopupMenu(); |
122 | 140 |
menu.add(getJCalendar()); |
123 |
|
|
141 |
Date value = (Date)getJSpinner().getValue(); |
|
142 |
if(value!=null){ |
|
143 |
getJCalendar().setDate(value); |
|
144 |
} |
|
124 | 145 |
JComponent thisComp = (JComponent)event.getSource(); |
125 | 146 |
menu.show(thisComp, 0, thisComp.getY()+22); |
147 |
setValue(getJCalendar().getDate()); |
|
126 | 148 |
} |
127 | 149 |
}); |
128 | 150 |
|
129 |
|
|
130 |
|
|
131 | 151 |
this.contents.addFocusListener(this); |
132 | 152 |
if(this.getDefinition().isReadOnly()) { |
133 | 153 |
this.getJSpinner().setEnabled(false); |
... | ... | |
154 | 174 |
this.problemIndicator().set(e.getLocalizedMessage()); |
155 | 175 |
} |
156 | 176 |
} |
157 |
if( this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME) ) {
|
|
158 |
this.getJSpinner().setValue(value);
|
|
159 |
}
|
|
177 |
if (this.contents instanceof JPanel && this.contents.getName().equalsIgnoreCase(DATE_PANEL_NAME)) {
|
|
178 |
this.getJSpinner().setValue(value);
|
|
179 |
}
|
|
160 | 180 |
this.assignedValue = value; |
161 | 181 |
} |
162 | 182 |
|
... | ... | |
208 | 228 |
} |
209 | 229 |
|
210 | 230 |
public void clear() { |
211 |
Object value = this.getDefinition().getDefaultValue(); |
|
212 |
if( value != null ) { |
|
213 |
if (!(value instanceof Date)){ |
|
214 |
try { |
|
215 |
value = DateFormat.getInstance().parse(value.toString()); |
|
216 |
} catch (ParseException e) { |
|
217 |
this.problemIndicator().set(e.getLocalizedMessage()); |
|
218 |
} |
|
219 |
} |
|
231 |
setValue(null); |
|
232 |
// Object value = this.getDefinition().getDefaultValue(); |
|
233 |
// if( value != null ) { |
|
234 |
// if (!(value instanceof Date)){ |
|
235 |
// try { |
|
236 |
// value = DateFormat.getInstance().parse(value.toString()); |
|
237 |
// } catch (ParseException e) { |
|
238 |
// this.problemIndicator().set(e.getLocalizedMessage()); |
|
239 |
// } |
|
240 |
// } |
|
241 |
// } else { |
|
242 |
// value = ""; |
|
243 |
// } |
|
244 |
// try { |
|
245 |
// this.timeSpinner.setValue(value); |
|
246 |
// } catch (RuntimeException e) { |
|
247 |
// this.timeSpinner.setValue(getJCalendar().getDate()); |
|
248 |
// } |
|
249 |
} |
|
250 |
|
|
251 |
|
|
252 |
private class NullDateEditor extends JSpinner.DateEditor { |
|
253 |
|
|
254 |
/** |
|
255 |
* |
|
256 |
*/ |
|
257 |
private static final long serialVersionUID = -522934985228645970L; |
|
258 |
|
|
259 |
/** |
|
260 |
* @param jspinner |
|
261 |
* @param pattern |
|
262 |
* |
|
263 |
*/ |
|
264 |
public NullDateEditor(JSpinner jspinner, String pattern) { |
|
265 |
super(jspinner, pattern); |
|
266 |
} |
|
267 |
|
|
268 |
public NullDateEditor(JSpinner jspinner) { |
|
269 |
super(jspinner); |
|
270 |
} |
|
271 |
|
|
272 |
public void stateChanged(ChangeEvent e) { |
|
273 |
Object value = getSpinner().getValue(); |
|
274 |
|
|
275 |
String text = ""; |
|
276 |
if (value != null) { |
|
277 |
text = getFormat().format((Date) value); |
|
220 | 278 |
} |
221 |
this.timeSpinner.setValue(value);
|
|
279 |
getTextField().setText(text);
|
|
222 | 280 |
} |
281 |
} |
|
223 | 282 |
} |
Also available in: Unified diff