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 | ||
---|---|---|
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 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.dynform/org.gvsig.tools.dynform.spi/src/main/java/org/gvsig/tools/dynform/spi/dynformfield/JCustomSpinner.java | ||
---|---|---|
24 | 24 |
package org.gvsig.tools.dynform.spi.dynformfield; |
25 | 25 |
|
26 | 26 |
import javax.swing.Action; |
27 |
import javax.swing.JComponent; |
|
27 | 28 |
import javax.swing.JPopupMenu; |
28 | 29 |
import javax.swing.JSpinner; |
29 | 30 |
import javax.swing.text.JTextComponent; |
30 | 31 |
|
32 |
import org.slf4j.Logger; |
|
33 |
import org.slf4j.LoggerFactory; |
|
34 |
|
|
31 | 35 |
import org.gvsig.tools.dynform.spi.DynFormSPILocator; |
32 | 36 |
|
37 |
/** |
|
38 |
* @author gvSIG team |
|
39 |
* |
|
40 |
*/ |
|
33 | 41 |
public class JCustomSpinner extends JSpinner{ |
34 | 42 |
|
35 | 43 |
/** |
... | ... | |
40 | 48 |
private JPopupMenu popupMenu = null; |
41 | 49 |
private JTextComponent jtext = null; |
42 | 50 |
private String title = null; |
43 |
|
|
51 |
|
|
52 |
protected static final Logger logger = LoggerFactory |
|
53 |
.getLogger(JCustomSpinner.class); |
|
54 |
|
|
55 |
/** |
|
56 |
* @param title |
|
57 |
*/ |
|
44 | 58 |
public JCustomSpinner(String title){ |
45 |
super(new CustomSpinnerDateModel()); |
|
46 |
this.title = title; |
|
47 |
this.allowNull = false; |
|
48 |
initComponents(); |
|
59 |
this(title, false); |
|
49 | 60 |
} |
50 |
|
|
61 |
|
|
62 |
/** |
|
63 |
* @param title |
|
64 |
* @param allowNull |
|
65 |
*/ |
|
51 | 66 |
public JCustomSpinner(String title, boolean allowNull){ |
52 |
super(); |
|
67 |
super(new CustomSpinnerDateModel(allowNull)); |
|
68 |
this.title = title; |
|
53 | 69 |
this.allowNull = allowNull; |
70 |
initComponents(); |
|
54 | 71 |
} |
55 |
|
|
72 |
|
|
73 |
/** |
|
74 |
* @param allow |
|
75 |
*/ |
|
56 | 76 |
public void setAllowNull(boolean allow){ |
57 | 77 |
this.allowNull = allow; |
58 | 78 |
} |
59 |
|
|
79 |
|
|
80 |
/** |
|
81 |
* @return true if allow null value |
|
82 |
*/ |
|
60 | 83 |
public boolean allowNull(){ |
61 | 84 |
return this.allowNull; |
62 | 85 |
} |
86 |
|
|
63 | 87 |
private void setActionEnabled(String name, boolean enabled) { |
64 | 88 |
Action action = this.getActionMap().get(name); |
65 | 89 |
if( action != null ) { |
66 | 90 |
action.setEnabled(enabled); |
67 | 91 |
} |
68 | 92 |
} |
69 |
|
|
93 |
|
|
70 | 94 |
public void setEnabled(boolean enabled){ |
71 | 95 |
setActionEnabled("Cut", enabled); |
72 | 96 |
setActionEnabled("Paste", enabled); |
73 | 97 |
super.setEnabled(enabled); |
74 | 98 |
} |
75 |
|
|
99 |
|
|
100 |
/** |
|
101 |
* @param enabled |
|
102 |
*/ |
|
76 | 103 |
public void setEditable(boolean enabled){ |
77 | 104 |
setActionEnabled("Cut", enabled); |
78 | 105 |
setActionEnabled("Paste", enabled); |
... | ... | |
82 | 109 |
} |
83 | 110 |
super.setEnabled(enabled); |
84 | 111 |
} |
85 |
|
|
112 |
|
|
86 | 113 |
private void initComponents() { |
87 | 114 |
try { |
88 | 115 |
DefaultEditor x = (DefaultEditor) this.getEditor(); |
... | ... | |
95 | 122 |
this.setComponentPopupMenu(getJPopupMenu()); |
96 | 123 |
} |
97 | 124 |
} |
98 |
|
|
125 |
|
|
126 |
/* (non-Javadoc) |
|
127 |
* @see javax.swing.JSpinner#setEditor(javax.swing.JComponent) |
|
128 |
*/ |
|
129 |
@Override |
|
130 |
public void setEditor(JComponent arg0) { |
|
131 |
super.setEditor(arg0); |
|
132 |
this.popupMenu = null; |
|
133 |
initComponents(); |
|
134 |
} |
|
135 |
|
|
136 |
/** |
|
137 |
* @return |
|
138 |
*/ |
|
99 | 139 |
public JPopupMenu getJPopupMenu(){ |
100 | 140 |
if(this.popupMenu == null){ |
101 | 141 |
this.popupMenu = DynFormSPILocator.getDynFormSPIManager().createTextFieldPopupMenu(title, this.jtext, false); |
102 | 142 |
} |
103 | 143 |
return this.popupMenu; |
104 | 144 |
} |
105 |
|
|
145 |
|
|
146 |
/** |
|
147 |
* @param name |
|
148 |
* @param action |
|
149 |
*/ |
|
106 | 150 |
public void addActionToPopupMenu(String name, Action action){ |
107 | 151 |
if(name != null && !name.isEmpty()){ |
108 | 152 |
action.putValue(Action.NAME, name); |
109 | 153 |
} |
110 | 154 |
getJPopupMenu().add(action); |
111 | 155 |
} |
112 |
|
|
156 |
|
|
157 |
/** |
|
158 |
* |
|
159 |
*/ |
|
113 | 160 |
public void addSeparatorToPopupMenu(){ |
114 | 161 |
getJPopupMenu().addSeparator(); |
115 |
} |
|
116 |
// |
|
117 |
// private boolean valid = true; |
|
118 |
// |
|
119 |
// @Override |
|
120 |
// public void setValue(Object val){ |
|
121 |
// if (val != null){ |
|
122 |
// try{ |
|
123 |
// valid = true; |
|
124 |
// super.setValue(val); |
|
125 |
// return; |
|
126 |
// } |
|
127 |
// catch (Exception e){} |
|
128 |
// } |
|
129 |
// valid = false; |
|
130 |
// } |
|
131 |
// |
|
132 |
// @Override |
|
133 |
// public Object getValue(){ |
|
134 |
// if (valid){ |
|
135 |
// return super.getValue(); |
|
136 |
// }else{ |
|
137 |
// this.setValue(null); |
|
138 |
// return null; |
|
139 |
// } |
|
140 |
// } |
|
141 |
// |
|
142 |
// @Override |
|
143 |
// public Object getPreviousValue(){ |
|
144 |
// if(getValue()==null){ |
|
145 |
// return null; |
|
146 |
// }else{ |
|
147 |
// return super.getPreviousValue(); |
|
148 |
// } |
|
149 |
// } |
|
150 |
// |
|
151 |
// @Override |
|
152 |
// public Object getNextValue(){ |
|
153 |
// if(getValue()==null){ |
|
154 |
// return null; |
|
155 |
// }else{ |
|
156 |
// return super.getNextValue(); |
|
157 |
// } |
|
158 |
// } |
|
162 |
} |
|
159 | 163 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.dynform/org.gvsig.tools.dynform.spi/src/main/java/org/gvsig/tools/dynform/spi/dynformfield/CustomSpinnerDateModel.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dynform.spi.dynformfield; |
25 | 25 |
|
26 |
import java.util.Date; |
|
27 |
|
|
26 | 28 |
import javax.swing.SpinnerDateModel; |
27 | 29 |
|
28 |
public class CustomSpinnerDateModel extends SpinnerDateModel{ |
|
30 |
import org.slf4j.Logger; |
|
31 |
import org.slf4j.LoggerFactory; |
|
32 |
/** |
|
33 |
* SpinerDateModel nullable |
|
34 |
* |
|
35 |
* @author fdiaz |
|
36 |
* |
|
37 |
*/ |
|
38 |
public class CustomSpinnerDateModel extends SpinnerDateModel { |
|
29 | 39 |
|
30 |
/**
|
|
31 |
*
|
|
40 |
/**
|
|
41 |
* |
|
32 | 42 |
*/ |
33 |
private static final long serialVersionUID = 4389535440347616567L; |
|
34 |
|
|
35 |
} |
|
43 |
private static final long serialVersionUID = 4389535440347616567L; |
|
44 |
|
|
45 |
private static Logger logger = LoggerFactory |
|
46 |
.getLogger(CustomSpinnerDateModel.class); |
|
47 |
|
|
48 |
private boolean valid; |
|
49 |
private boolean allowNull; |
|
50 |
|
|
51 |
/** |
|
52 |
* |
|
53 |
*/ |
|
54 |
public CustomSpinnerDateModel() { |
|
55 |
this(false); |
|
56 |
} |
|
57 |
/** |
|
58 |
* @param allowNull |
|
59 |
* |
|
60 |
*/ |
|
61 |
public CustomSpinnerDateModel(boolean allowNull) { |
|
62 |
super(); |
|
63 |
valid = false; |
|
64 |
this.allowNull = allowNull; |
|
65 |
} |
|
66 |
|
|
67 |
public void setValue(Object val) { |
|
68 |
if(!allowNull){ |
|
69 |
super.setValue(val); |
|
70 |
return; |
|
71 |
} |
|
72 |
if (val != null) { |
|
73 |
valid = true; |
|
74 |
super.setValue(val); |
|
75 |
return; |
|
76 |
} |
|
77 |
valid = false; |
|
78 |
} |
|
79 |
|
|
80 |
public Object getValue() { |
|
81 |
if(!allowNull){ |
|
82 |
return super.getValue(); |
|
83 |
} |
|
84 |
if (valid) { |
|
85 |
return super.getValue(); |
|
86 |
} else { |
|
87 |
return null; |
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
public Object getNextValue() { |
|
92 |
if (!allowNull){ |
|
93 |
return super.getNextValue(); |
|
94 |
} |
|
95 |
if (!valid) { |
|
96 |
Date p = new Date(); |
|
97 |
return p; |
|
98 |
} |
|
99 |
|
|
100 |
return super.getNextValue(); |
|
101 |
} |
|
102 |
|
|
103 |
public Object getPreviousValue() { |
|
104 |
if (!allowNull){ |
|
105 |
return super.getPreviousValue(); |
|
106 |
} |
|
107 |
if (!valid) { |
|
108 |
Date p = new Date(); |
|
109 |
return p; |
|
110 |
} |
|
111 |
|
|
112 |
return super.getPreviousValue(); |
|
113 |
} |
|
114 |
|
|
115 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/AbstractCoerceToDate.java | ||
---|---|---|
36 | 36 |
* Abstract implementation for Date coercion classes. If the value is not a |
37 | 37 |
* {@link Date}, it will use the {@link Object#toString()} method to convert the |
38 | 38 |
* resulting {@link String} to a Date object. |
39 |
*
|
|
39 |
* |
|
40 | 40 |
* @author gvSIG Team |
41 | 41 |
* @version $Id$ |
42 | 42 |
*/ |
... | ... | |
47 | 47 |
} |
48 | 48 |
|
49 | 49 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
50 |
return coerce(value, this.createFormatter(locale)); |
|
51 |
} |
|
52 |
|
|
53 |
protected Object coerce(Object value, DateFormat dateFormatter) throws CoercionException { |
|
50 | 54 |
if( value == null ) { |
51 | 55 |
return null; |
52 | 56 |
} |
53 | 57 |
if (!(value instanceof Date)) { |
54 |
DateFormat dateFormatter = createFormatter(locale); |
|
55 | 58 |
String valueStr = value.toString(); |
56 | 59 |
if( valueStr == null ) { |
57 | 60 |
return null; |
... | ... | |
60 | 63 |
if( valueStr.length()==0 ) { |
61 | 64 |
return null; |
62 | 65 |
} |
63 |
|
|
66 |
|
|
64 | 67 |
try { |
65 | 68 |
Date d = dateFormatter.parse(valueStr); |
66 | 69 |
if (d == null) { |
... | ... | |
87 | 90 |
/** |
88 | 91 |
* Returns the {@link DateFormat} to apply when the value to coerce is not |
89 | 92 |
* of Date type and it will be parsed as String. |
90 |
*
|
|
93 |
* |
|
91 | 94 |
* @return the {@link DateFormat} to apply to parse the value to coerce as |
92 | 95 |
* {@link String} |
93 | 96 |
*/ |
... | ... | |
97 | 100 |
* Returns the name of the Date type being coerced. Ex: Date, DateTime, |
98 | 101 |
* Time. Only used for description when an error is produced when coercing |
99 | 102 |
* the value. |
100 |
*
|
|
103 |
* |
|
101 | 104 |
* @return the name of the Date type being coerced |
102 | 105 |
*/ |
103 | 106 |
protected abstract String getDateType(); |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDate.java | ||
---|---|---|
24 | 24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
25 | 25 |
|
26 | 26 |
import java.text.DateFormat; |
27 |
import java.text.ParseException; |
|
28 |
import java.text.SimpleDateFormat; |
|
27 | 29 |
import java.util.Date; |
28 | 30 |
import java.util.Locale; |
29 | 31 |
|
32 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
33 |
|
|
30 | 34 |
/** |
31 | 35 |
* Coerces a value to a {@link Date}. If the value is not a {@link Date}, it |
32 | 36 |
* will use the {@link Object#toString()} method to convert the |
33 | 37 |
* resulting {@link String} to a Date object using the current locale default |
34 | 38 |
* formatter for Date through the {@link DateFormat#getDateInstance(int)} method |
35 | 39 |
* and the {@link DateFormat#SHORT} style. |
36 |
*
|
|
40 |
* |
|
37 | 41 |
* @author gvSIG Team |
38 | 42 |
* @version $Id$ |
39 | 43 |
*/ |
... | ... | |
43 | 47 |
return DateFormat.getDateInstance(DateFormat.SHORT, locale); |
44 | 48 |
} |
45 | 49 |
|
50 |
/* (non-Javadoc) |
|
51 |
* @see org.gvsig.tools.dataTypes.impl.coercion.AbstractCoerceToDate#coerce(java.lang.Object) |
|
52 |
*/ |
|
53 |
@Override |
|
54 |
public Object coerce(Object value) throws CoercionException { |
|
55 |
try { |
|
56 |
return super.coerce(value); |
|
57 |
} catch (Throwable e) { |
|
58 |
//Do nothing |
|
59 |
} |
|
60 |
//PostgreSQL usa este formato de fecha para los valores por defecto |
|
61 |
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); |
|
62 |
return super.coerce(value, dateFormatter); |
|
63 |
|
|
64 |
} |
|
65 |
|
|
46 | 66 |
protected String getDateType() { |
47 | 67 |
return "Date"; |
48 | 68 |
} |
Also available in: Unified diff