Revision 270 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.serv/org.gvsig.tools.swing.serv.field/src/main/java/org/gvsig/tools/swing/serv/field/component/date/JDateCalendarDynFieldComponent.java
JDateCalendarDynFieldComponent.java | ||
---|---|---|
35 | 35 |
import org.gvsig.tools.swing.serv.field.component.spinner.NullDateEditor; |
36 | 36 |
import org.gvsig.tools.swing.spi.AbstractJDynFieldComponent; |
37 | 37 |
|
38 |
public class JDateCalendarDynFieldComponent extends AbstractJDynFieldComponent implements JDynFieldComponent, ActionListener, FocusListener { |
|
38 |
public class JDateCalendarDynFieldComponent extends AbstractJDynFieldComponent |
|
39 |
implements JDynFieldComponent, ActionListener, FocusListener { |
|
39 | 40 |
|
40 |
private JButton jBtnChangeDate;
|
|
41 |
private DateSpinner dateField;
|
|
42 |
private DateCalendarPicker datePicker;
|
|
43 |
private JDialog dlg;
|
|
44 |
private JPanel pane;
|
|
41 |
private JButton jBtnChangeDate;
|
|
42 |
private DateSpinner dateField;
|
|
43 |
private DateCalendarPicker datePicker;
|
|
44 |
private JDialog dlg;
|
|
45 |
private JPanel pane;
|
|
45 | 46 |
|
46 |
|
|
47 |
public JDateCalendarDynFieldComponent(ValueField value, DynField dynField) |
|
48 |
throws ServiceException { |
|
49 |
super(dynField, value); |
|
50 |
// for (FocusListener listener : getTextField().getListeners(FocusListener.class)){ |
|
51 |
// getTextField().removeFocusListener(listener); |
|
52 |
// } |
|
53 |
getTextField().addFocusListener(this); |
|
54 |
// .agetTextField().getDocument().addDocumentListener( new DocumentListener(){ |
|
55 |
// |
|
56 |
// public void changedUpdate(DocumentEvent e) { |
|
57 |
// validate(); |
|
58 |
// } |
|
59 |
// |
|
60 |
// public void insertUpdate(DocumentEvent e) { |
|
61 |
// validate(); |
|
62 |
// } |
|
63 |
// |
|
64 |
// public void removeUpdate(DocumentEvent e) { |
|
65 |
// validate(); |
|
66 |
// }}); |
|
67 |
} |
|
68 |
|
|
69 |
/** |
|
70 |
* This method initializes jButton |
|
71 |
* |
|
72 |
* @return javax.swing.JButton |
|
73 |
*/ |
|
74 |
public JButton getJBtnChangeDate() { |
|
75 |
if (jBtnChangeDate == null) { |
|
76 |
jBtnChangeDate = new JButton(); |
|
77 |
jBtnChangeDate.setText("..."); //$NON-NLS-1$ |
|
78 |
jBtnChangeDate.setPreferredSize(new java.awt.Dimension(26,26)); |
|
79 |
} |
|
80 |
return jBtnChangeDate; |
|
81 |
} |
|
82 |
|
|
83 |
private DateSpinner getDateField() { |
|
84 |
if (this.dateField==null) |
|
85 |
this.dateField = new DateSpinner(DataTypes.DATE); |
|
86 |
return this.dateField; |
|
87 |
} |
|
47 |
public JDateCalendarDynFieldComponent(ValueField value, DynField dynField) |
|
48 |
throws ServiceException { |
|
49 |
super(dynField, value); |
|
50 |
// for (FocusListener listener : |
|
51 |
// getTextField().getListeners(FocusListener.class)){ |
|
52 |
// getTextField().removeFocusListener(listener); |
|
53 |
// } |
|
54 |
getTextField().addFocusListener(this); |
|
55 |
// .agetTextField().getDocument().addDocumentListener( new |
|
56 |
// DocumentListener(){ |
|
57 |
// |
|
58 |
// public void changedUpdate(DocumentEvent e) { |
|
59 |
// validate(); |
|
60 |
// } |
|
61 |
// |
|
62 |
// public void insertUpdate(DocumentEvent e) { |
|
63 |
// validate(); |
|
64 |
// } |
|
65 |
// |
|
66 |
// public void removeUpdate(DocumentEvent e) { |
|
67 |
// validate(); |
|
68 |
// }}); |
|
69 |
} |
|
88 | 70 |
|
89 |
|
|
71 |
/** |
|
72 |
* This method initializes jButton |
|
73 |
* |
|
74 |
* @return javax.swing.JButton |
|
75 |
*/ |
|
76 |
public JButton getJBtnChangeDate() { |
|
77 |
if (jBtnChangeDate == null) { |
|
78 |
jBtnChangeDate = new JButton(); |
|
79 |
jBtnChangeDate.setText("..."); //$NON-NLS-1$ |
|
80 |
jBtnChangeDate.setPreferredSize(new java.awt.Dimension(26, 26)); |
|
81 |
} |
|
82 |
return jBtnChangeDate; |
|
83 |
} |
|
90 | 84 |
|
91 |
private void initBtnChangeDate() { |
|
92 |
if (datePicker == null) { |
|
93 |
datePicker = new DateCalendarPicker(this.getDate(),pane.getLocale()); |
|
94 |
datePicker.setPreferredSize(new java.awt.Dimension(450,410)); |
|
95 |
// datePicker.setSize(datePicker.getPreferredSize()); |
|
96 |
datePicker.addButtonsActionListener(this); |
|
97 |
} |
|
98 |
getJBtnChangeDate().addActionListener(new ActionListener(){ |
|
99 |
public void actionPerformed(ActionEvent e) { |
|
100 |
datePicker.setSelectedDate(getDate()); |
|
101 |
dlg = new JDialog(); |
|
102 |
dlg.add(datePicker); |
|
103 |
dlg.setSize(datePicker.getPreferredSize()); |
|
104 |
dlg.setVisible(true); |
|
105 |
}}); |
|
106 |
} |
|
85 |
private DateSpinner getDateField() { |
|
86 |
if (this.dateField == null) |
|
87 |
this.dateField = new DateSpinner(DataTypes.DATE); |
|
88 |
return this.dateField; |
|
89 |
} |
|
107 | 90 |
|
108 |
|
|
109 |
private Date getDate() { |
|
110 |
return (Date) getValue(); |
|
111 |
} |
|
91 |
private void initBtnChangeDate() { |
|
92 |
if (datePicker == null) { |
|
93 |
datePicker = |
|
94 |
new DateCalendarPicker(this.getDate(), pane.getLocale()); |
|
95 |
datePicker.setPreferredSize(new java.awt.Dimension(450, 410)); |
|
96 |
// datePicker.setSize(datePicker.getPreferredSize()); |
|
97 |
datePicker.addButtonsActionListener(this); |
|
98 |
} |
|
99 |
getJBtnChangeDate().addActionListener(new ActionListener() { |
|
112 | 100 |
|
101 |
public void actionPerformed(ActionEvent e) { |
|
102 |
datePicker.setSelectedDate(getDate()); |
|
103 |
dlg = new JDialog(); |
|
104 |
dlg.add(datePicker); |
|
105 |
dlg.setSize(datePicker.getPreferredSize()); |
|
106 |
dlg.setVisible(true); |
|
107 |
} |
|
108 |
}); |
|
109 |
} |
|
113 | 110 |
|
114 |
public void createBoxLayout(Component labelComponent, Component fieldLabel, |
|
115 |
Integer marginLeft, Integer marginRight) { |
|
111 |
private Date getDate() { |
|
112 |
return (Date) getValue(); |
|
113 |
} |
|
116 | 114 |
|
117 |
// Create a panel that uses GridLayout. |
|
118 |
pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS)); |
|
119 |
pane.add(labelComponent); |
|
120 |
if (marginLeft != null) |
|
121 |
pane.add(Box.createHorizontalStrut(marginLeft)); |
|
122 |
// this.add(new JSeparator(SwingConstants.VERTICAL)); |
|
123 |
if (marginRight != null) |
|
124 |
pane.add(Box.createHorizontalStrut(marginRight)); |
|
125 |
pane.add(fieldLabel, BorderLayout.NORTH); |
|
126 |
// if (buttonComponent!=null) |
|
127 |
// this.add(buttonComponent); |
|
128 |
pane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); |
|
129 |
} |
|
115 |
public void createBoxLayout(Component labelComponent, Component fieldLabel, |
|
116 |
Integer marginLeft, Integer marginRight) { |
|
130 | 117 |
|
118 |
// Create a panel that uses GridLayout. |
|
119 |
pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS)); |
|
120 |
pane.add(labelComponent); |
|
121 |
if (marginLeft != null) |
|
122 |
pane.add(Box.createHorizontalStrut(marginLeft)); |
|
123 |
// this.add(new JSeparator(SwingConstants.VERTICAL)); |
|
124 |
if (marginRight != null) |
|
125 |
pane.add(Box.createHorizontalStrut(marginRight)); |
|
126 |
pane.add(fieldLabel, BorderLayout.NORTH); |
|
127 |
// if (buttonComponent!=null) |
|
128 |
// this.add(buttonComponent); |
|
129 |
pane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); |
|
130 |
} |
|
131 | 131 |
|
132 |
public void dateChanged(DateEvent dateEvent) {
|
|
133 |
Calendar cal = dateEvent.getSelectedDate();
|
|
134 |
if (cal!=null){
|
|
135 |
this.getDateField().setValue(null);
|
|
136 |
}else{
|
|
137 |
this.getDateField().setValue(cal.getTime());
|
|
138 |
}
|
|
139 |
dlg.setVisible(false);
|
|
132 |
public void dateChanged(DateEvent dateEvent) {
|
|
133 |
Calendar cal = dateEvent.getSelectedDate();
|
|
134 |
if (cal != null) {
|
|
135 |
this.getDateField().setValue(null);
|
|
136 |
} else {
|
|
137 |
this.getDateField().setValue(cal.getTime());
|
|
138 |
}
|
|
139 |
dlg.setVisible(false);
|
|
140 | 140 |
|
141 |
}
|
|
141 |
}
|
|
142 | 142 |
|
143 |
@Override
|
|
144 |
protected void setJDynFieldComponentListeners() {
|
|
145 |
}
|
|
143 |
@Override
|
|
144 |
protected void setJDynFieldComponentListeners() {
|
|
145 |
}
|
|
146 | 146 |
|
147 |
@Override
|
|
148 |
protected void setNonNullValue(Object value) {
|
|
149 |
this.dateField.setValue(value);
|
|
150 |
}
|
|
147 |
@Override
|
|
148 |
protected void setNonNullValue(Object value) {
|
|
149 |
this.dateField.setValue(value);
|
|
150 |
}
|
|
151 | 151 |
|
152 |
@Override
|
|
153 |
protected void setNullValue() {
|
|
154 |
this.dateField.setValue(null);
|
|
155 |
}
|
|
152 |
@Override
|
|
153 |
protected void setNullValue() {
|
|
154 |
this.dateField.setValue(null);
|
|
155 |
}
|
|
156 | 156 |
|
157 |
@Override |
|
158 |
protected void afterUI() { |
|
159 |
// TODO Auto-generated method stub |
|
160 |
|
|
161 |
} |
|
157 |
@Override |
|
158 |
protected void afterUI() { |
|
159 |
// TODO Auto-generated method stub |
|
162 | 160 |
|
163 |
@Override |
|
164 |
protected void initData() { |
|
165 |
// TODO Auto-generated method stub |
|
166 |
|
|
167 |
} |
|
161 |
} |
|
168 | 162 |
|
169 |
@Override |
|
170 |
protected void initUI() { |
|
171 |
pane = new JPanel(); |
|
172 |
pane.setLocale(Locale.getDefault()); |
|
173 |
createBoxLayout(getDateField(), getJBtnChangeDate(), 3, 1); |
|
174 |
initBtnChangeDate(); |
|
175 |
} |
|
163 |
@Override |
|
164 |
protected void initData() { |
|
165 |
// TODO Auto-generated method stub |
|
176 | 166 |
|
177 |
@Override |
|
178 |
protected void setReadOnly() { |
|
179 |
this.dateField.setEditable(false); |
|
180 |
this.getJBtnChangeDate().setVisible(false); |
|
181 |
} |
|
167 |
} |
|
182 | 168 |
|
183 |
public void requestFocus() { |
|
184 |
this.getDateField().requestFocus(); |
|
185 |
} |
|
169 |
@Override |
|
170 |
protected void initUI() { |
|
171 |
pane = new JPanel(); |
|
172 |
pane.setLocale(Locale.getDefault()); |
|
173 |
createBoxLayout(getDateField(), getJBtnChangeDate(), 3, 1); |
|
174 |
initBtnChangeDate(); |
|
175 |
} |
|
186 | 176 |
|
187 |
public void setEnabled(boolean isEnabled) { |
|
188 |
this.pane.setEnabled(isEnabled); |
|
189 |
} |
|
177 |
@Override |
|
178 |
protected void setReadOnly() { |
|
179 |
this.dateField.setEditable(false); |
|
180 |
this.getJBtnChangeDate().setVisible(false); |
|
181 |
} |
|
190 | 182 |
|
191 |
public JFormattedTextField getTextField(){ |
|
192 |
return ((NullDateEditor) this.getDateField().getEditor()).getTextField(); |
|
193 |
} |
|
194 |
|
|
195 |
public DateFormatter getFormat(){ |
|
196 |
return (DateFormatter) ((NullDateEditor) this.getDateField().getEditor()).getFormat(); |
|
197 |
} |
|
198 |
|
|
199 |
public Object getValue() { |
|
200 |
String txt = getTextField().getText(); |
|
201 |
if ((txt == null)||(txt.equals(""))) |
|
202 |
return null; |
|
203 |
return getFormat().parse(txt); |
|
204 |
} |
|
183 |
public void requestFocus() { |
|
184 |
this.getDateField().requestFocus(); |
|
185 |
} |
|
205 | 186 |
|
206 |
public JComponent asJComponent() {
|
|
207 |
return pane;
|
|
208 |
}
|
|
187 |
public void setEnabled(boolean isEnabled) {
|
|
188 |
this.pane.setEnabled(isEnabled);
|
|
189 |
}
|
|
209 | 190 |
|
210 |
public void actionPerformed(ActionEvent e) { |
|
211 |
JButton btn = (JButton) e.getSource(); |
|
212 |
if (btn.getActionCommand().equals("BTN_OK")){ |
|
213 |
this.getDateField().setValue(datePicker.getSelectedDate()); |
|
214 |
} |
|
215 |
dlg.setVisible(false); |
|
216 |
} |
|
191 |
public JFormattedTextField getTextField() { |
|
192 |
return ((NullDateEditor) this.getDateField().getEditor()) |
|
193 |
.getTextField(); |
|
194 |
} |
|
217 | 195 |
|
218 |
public void focusGained(FocusEvent e) { |
|
219 |
|
|
220 |
} |
|
196 |
public DateFormatter getFormat() { |
|
197 |
return (DateFormatter) ((NullDateEditor) this.getDateField() |
|
198 |
.getEditor()).getFormat(); |
|
199 |
} |
|
221 | 200 |
|
222 |
public void focusLost(FocusEvent e) { |
|
223 |
JFormattedTextField tf = (JFormattedTextField) e.getSource(); |
|
224 |
Date date = getFormat().parse(tf.getText()); |
|
225 |
if (date == null){ |
|
226 |
tf.setText(""); |
|
227 |
} |
|
228 |
} |
|
201 |
public Object getValue() { |
|
202 |
String txt = getTextField().getText(); |
|
203 |
if ((txt == null) || (txt.equals(""))) |
|
204 |
return null; |
|
205 |
return getFormat().parse(txt); |
|
206 |
} |
|
207 |
|
|
208 |
public JComponent asJComponent() { |
|
209 |
return pane; |
|
210 |
} |
|
211 |
|
|
212 |
public void actionPerformed(ActionEvent e) { |
|
213 |
JButton btn = (JButton) e.getSource(); |
|
214 |
if (btn.getActionCommand().equals("BTN_OK")) { |
|
215 |
this.getDateField().setValue(datePicker.getSelectedDate()); |
|
216 |
} |
|
217 |
dlg.setVisible(false); |
|
218 |
} |
|
219 |
|
|
220 |
public void focusGained(FocusEvent e) { |
|
221 |
|
|
222 |
} |
|
223 |
|
|
224 |
public void focusLost(FocusEvent e) { |
|
225 |
JFormattedTextField tf = (JFormattedTextField) e.getSource(); |
|
226 |
Date date = getFormat().parse(tf.getText()); |
|
227 |
if (date == null) { |
|
228 |
tf.setText(""); |
|
229 |
} |
|
230 |
} |
|
229 | 231 |
} |
Also available in: Unified diff