Revision 298 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/DateCalendarPicker.java
DateCalendarPicker.java | ||
---|---|---|
15 | 15 |
|
16 | 16 |
import javax.swing.BorderFactory; |
17 | 17 |
import javax.swing.JButton; |
18 |
import javax.swing.JFormattedTextField; |
|
19 | 18 |
import javax.swing.JLabel; |
20 | 19 |
import javax.swing.JPanel; |
21 | 20 |
import javax.swing.JSeparator; |
21 |
import javax.swing.SwingConstants; |
|
22 | 22 |
import javax.swing.border.Border; |
23 | 23 |
|
24 | 24 |
import org.freixas.jcalendar.DateEvent; |
25 | 25 |
import org.freixas.jcalendar.DateListener; |
26 | 26 |
import org.freixas.jcalendar.JCalendar; |
27 | 27 |
|
28 |
public class DateCalendarPicker extends JPanel implements DateListener, ActionListener { |
|
28 |
public class DateCalendarPicker extends JPanel implements DateListener, |
|
29 |
ActionListener { |
|
29 | 30 |
|
31 |
/** |
|
32 |
* |
|
33 |
*/ |
|
34 |
private static final long serialVersionUID = -4084206317377526029L; |
|
30 | 35 |
private JCalendar jCal = null; |
31 | 36 |
boolean okPressed = false; |
32 | 37 |
private Calendar calendar; |
... | ... | |
42 | 47 |
|
43 | 48 |
public DateCalendarPicker(Date date, Locale loc) { |
44 | 49 |
super(); |
45 |
if (date == null) |
|
50 |
if (date == null) {
|
|
46 | 51 |
date = new Date(); |
52 |
} |
|
47 | 53 |
this.iniDate = date; |
48 | 54 |
this.setLocale(loc); |
49 | 55 |
beforeUI(date); |
... | ... | |
51 | 57 |
addButtonsActionListener(this); |
52 | 58 |
} |
53 | 59 |
|
54 |
public JLabel getLbl() { |
|
55 |
if (this.dateLabel == null) { |
|
56 |
this.dateLabel = new JLabel(getDateString()); |
|
57 |
// Label settings |
|
58 |
this.dateLabel.setBorder(BorderFactory |
|
59 |
.createEmptyBorder(8, 8, 5, 8)); |
|
60 |
this.dateLabel.setHorizontalAlignment(JFormattedTextField.CENTER); |
|
61 |
this.dateLabel.setBackground(Color.BLUE.brighter()); |
|
62 |
this.dateLabel.setForeground(Color.BLUE.brighter()); |
|
63 |
this.dateLabel.setFont(new Font("SansSerif", Font.BOLD, 18)); |
|
60 |
public void actionPerformed(ActionEvent e) { |
|
61 |
JButton button = (JButton) e.getSource(); |
|
62 |
if (button.getActionCommand().equals("BUTTON_OK")) { |
|
63 |
this.okPressed = true; |
|
64 |
} else { |
|
65 |
this.okPressed = false; |
|
64 | 66 |
} |
65 |
return this.dateLabel; |
|
67 |
|
|
66 | 68 |
} |
67 | 69 |
|
68 |
private void initUI() {
|
|
69 |
// Setting border
|
|
70 |
Border compoundBorder = createCompoundBorder();
|
|
71 |
this.setBorder(compoundBorder);
|
|
70 |
public void addButtonsActionListener(ActionListener listener) {
|
|
71 |
this.getButtonCancel().addActionListener(listener);
|
|
72 |
this.getButtonOK().addActionListener(listener);
|
|
73 |
}
|
|
72 | 74 |
|
73 |
JPanel datePanel = new JPanel(new GridBagLayout()); |
|
74 |
GridBagConstraints c = new GridBagConstraints(); |
|
75 |
c.insets = new Insets(2, 2, 4, 2); |
|
76 |
c.fill = GridBagConstraints.NONE; |
|
77 |
c.gridx = 0; |
|
78 |
datePanel.add(this.getUICalendar(), c); |
|
75 |
public void addDateListener(DateListener listener) { |
|
76 |
this.getUICalendar().addDateListener(this); |
|
77 |
} |
|
79 | 78 |
|
80 |
JPanel buttonsPanel = new JPanel(new GridBagLayout()); |
|
81 |
c = new GridBagConstraints(); |
|
82 |
c.insets = new Insets(2, 2, 4, 2); |
|
83 |
c.fill = GridBagConstraints.NONE; |
|
84 |
buttonsPanel.add(getButtonOK(), c); |
|
85 |
c.insets = new Insets(2, 4, 2, 2); |
|
86 |
c.fill = GridBagConstraints.NONE; |
|
87 |
buttonsPanel.add(getButtonCancel(), c); |
|
79 |
private void beforeUI(Date date) { |
|
80 |
// DateFormat |
|
81 |
formatter = |
|
82 |
DateFormat.getDateInstance(DateFormat.FULL, this.getLocale()); |
|
83 |
// Calendar settings |
|
84 |
JCalendar cal = getUICalendar(); |
|
85 |
cal.setDate(date); |
|
88 | 86 |
|
89 |
JPanel headerPanel = new JPanel(new GridBagLayout()); |
|
90 |
headerPanel.add(getLbl()); |
|
91 |
headerPanel.add(new JSeparator()); |
|
92 |
headerPanel.setBackground(new Color(0, 0, 255, 20)); |
|
87 |
// Setting fonts |
|
88 |
// Set fonts rather than using defaults |
|
89 |
cal.setTitleFont(new Font("SansSerif", Font.BOLD, 22)); |
|
90 |
cal.setDayOfWeekFont(new Font("SansSerif", Font.ITALIC, 12)); |
|
91 |
cal.setDayFont(new Font("SansSerif", Font.BOLD, 14)); |
|
92 |
cal.setTimeFont(new Font("DialogInput", Font.PLAIN, 10)); |
|
93 |
cal.setTodayFont(new Font("Dialog", Font.PLAIN, 14)); |
|
94 |
} |
|
93 | 95 |
|
94 |
// this.add(getLbl(), BorderLayout.PAGE_START);
|
|
95 |
this.add(headerPanel, BorderLayout.PAGE_START);
|
|
96 |
this.add(datePanel, BorderLayout.CENTER);
|
|
97 |
this.add(buttonsPanel, BorderLayout.PAGE_END);
|
|
98 |
|
|
96 |
private Border createCompoundBorder() {
|
|
97 |
// Create a border for all calendars
|
|
98 |
Border etchedBorder = BorderFactory.createEtchedBorder();
|
|
99 |
Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
|
|
100 |
return BorderFactory.createCompoundBorder(etchedBorder, emptyBorder); |
|
99 | 101 |
} |
100 | 102 |
|
101 |
private JButton getButtonOK() { |
|
102 |
if (buttonOK == null) { |
|
103 |
buttonOK = new JButton("Ok"); |
|
104 |
buttonOK.setActionCommand("BTN_OK"); |
|
105 |
buttonOK.setSize(90, 23); |
|
106 |
} |
|
107 |
return buttonOK; |
|
103 |
public void dateChanged(DateEvent dateEvent) { |
|
104 |
this.calendar = dateEvent.getSelectedDate(); |
|
105 |
refreshLbl(); |
|
108 | 106 |
} |
109 | 107 |
|
110 | 108 |
private JButton getButtonCancel() { |
... | ... | |
116 | 114 |
return buttonCancel; |
117 | 115 |
} |
118 | 116 |
|
117 |
private JButton getButtonOK() { |
|
118 |
if (buttonOK == null) { |
|
119 |
buttonOK = new JButton("Ok"); |
|
120 |
buttonOK.setActionCommand("BTN_OK"); |
|
121 |
buttonOK.setSize(90, 23); |
|
122 |
} |
|
123 |
return buttonOK; |
|
124 |
} |
|
125 |
|
|
119 | 126 |
private Calendar getDateCalendar() { |
120 | 127 |
if (this.calendar == null) { |
121 | 128 |
this.calendar = Calendar.getInstance(this.getLocale()); |
... | ... | |
124 | 131 |
return this.calendar; |
125 | 132 |
} |
126 | 133 |
|
127 |
private void beforeUI(Date date) { |
|
128 |
// DateFormat |
|
129 |
formatter = |
|
130 |
DateFormat.getDateInstance(DateFormat.FULL, this.getLocale()); |
|
131 |
// Calendar settings |
|
132 |
JCalendar cal = getUICalendar(); |
|
133 |
cal.setDate(date); |
|
134 |
public String getDateString() { |
|
135 |
return getLblText(getSelectedDate()); |
|
136 |
} |
|
134 | 137 |
|
135 |
// Setting fonts |
|
136 |
// Set fonts rather than using defaults |
|
137 |
cal.setTitleFont(new Font("SansSerif", Font.BOLD, 22)); |
|
138 |
cal.setDayOfWeekFont(new Font("SansSerif", Font.ITALIC, 12)); |
|
139 |
cal.setDayFont(new Font("SansSerif", Font.BOLD, 14)); |
|
140 |
cal.setTimeFont(new Font("DialogInput", Font.PLAIN, 10)); |
|
141 |
cal.setTodayFont(new Font("Dialog", Font.PLAIN, 14)); |
|
138 |
public JLabel getLbl() { |
|
139 |
if (this.dateLabel == null) { |
|
140 |
this.dateLabel = new JLabel(getDateString()); |
|
141 |
// Label settings |
|
142 |
this.dateLabel.setBorder(BorderFactory |
|
143 |
.createEmptyBorder(8, 8, 5, 8)); |
|
144 |
this.dateLabel.setHorizontalAlignment(SwingConstants.CENTER); |
|
145 |
this.dateLabel.setBackground(Color.BLUE.brighter()); |
|
146 |
this.dateLabel.setForeground(Color.BLUE.brighter()); |
|
147 |
this.dateLabel.setFont(new Font("SansSerif", Font.BOLD, 18)); |
|
148 |
} |
|
149 |
return this.dateLabel; |
|
142 | 150 |
} |
143 | 151 |
|
144 |
private Border createCompoundBorder() { |
|
145 |
// Create a border for all calendars |
|
146 |
Border etchedBorder = BorderFactory.createEtchedBorder(); |
|
147 |
Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10); |
|
148 |
return BorderFactory.createCompoundBorder(etchedBorder, emptyBorder); |
|
152 |
public String getLblText(Date date) { |
|
153 |
return formatter.format(this.getSelectedDate()); |
|
149 | 154 |
} |
150 | 155 |
|
156 |
public Date getSelectedDate() { |
|
157 |
return getDateCalendar().getTime(); |
|
158 |
} |
|
159 |
|
|
151 | 160 |
private JCalendar getUICalendar() { |
152 | 161 |
if (jCal == null) { |
153 | 162 |
jCal = new JCalendar(); |
... | ... | |
159 | 168 |
return jCal; |
160 | 169 |
} |
161 | 170 |
|
162 |
public void addDateListener(DateListener listener) { |
|
163 |
this.getUICalendar().addDateListener(this); |
|
164 |
} |
|
171 |
private void initUI() { |
|
172 |
// Setting border |
|
173 |
Border compoundBorder = createCompoundBorder(); |
|
174 |
this.setBorder(compoundBorder); |
|
165 | 175 |
|
166 |
public void dateChanged(DateEvent dateEvent) { |
|
167 |
this.calendar = dateEvent.getSelectedDate(); |
|
168 |
refreshLbl(); |
|
169 |
} |
|
176 |
JPanel datePanel = new JPanel(new GridBagLayout()); |
|
177 |
GridBagConstraints c = new GridBagConstraints(); |
|
178 |
c.insets = new Insets(2, 2, 4, 2); |
|
179 |
c.fill = GridBagConstraints.NONE; |
|
180 |
c.gridx = 0; |
|
181 |
datePanel.add(this.getUICalendar(), c); |
|
170 | 182 |
|
171 |
private void refreshLbl() { |
|
172 |
this.getLbl().setText(getDateString()); |
|
173 |
} |
|
183 |
JPanel buttonsPanel = new JPanel(new GridBagLayout()); |
|
184 |
c = new GridBagConstraints(); |
|
185 |
c.insets = new Insets(2, 2, 4, 2); |
|
186 |
c.fill = GridBagConstraints.NONE; |
|
187 |
buttonsPanel.add(getButtonOK(), c); |
|
188 |
c.insets = new Insets(2, 4, 2, 2); |
|
189 |
c.fill = GridBagConstraints.NONE; |
|
190 |
buttonsPanel.add(getButtonCancel(), c); |
|
174 | 191 |
|
175 |
public String getLblText(Date date) { |
|
176 |
return formatter.format(this.getSelectedDate()); |
|
177 |
} |
|
192 |
JPanel headerPanel = new JPanel(new GridBagLayout()); |
|
193 |
headerPanel.add(getLbl()); |
|
194 |
headerPanel.add(new JSeparator()); |
|
195 |
headerPanel.setBackground(new Color(0, 0, 255, 20)); |
|
178 | 196 |
|
179 |
public Date getSelectedDate() { |
|
180 |
return getDateCalendar().getTime(); |
|
197 |
// this.add(getLbl(), BorderLayout.PAGE_START); |
|
198 |
this.add(headerPanel, BorderLayout.PAGE_START); |
|
199 |
this.add(datePanel, BorderLayout.CENTER); |
|
200 |
this.add(buttonsPanel, BorderLayout.PAGE_END); |
|
201 |
|
|
181 | 202 |
} |
182 | 203 |
|
183 |
public String getDateString() {
|
|
184 |
return getLblText(getSelectedDate());
|
|
204 |
public boolean isOkPressed() {
|
|
205 |
return this.okPressed;
|
|
185 | 206 |
} |
186 | 207 |
|
187 |
public void addButtonsActionListener(ActionListener listener) { |
|
188 |
this.getButtonCancel().addActionListener(listener); |
|
189 |
this.getButtonOK().addActionListener(listener); |
|
208 |
private void refreshLbl() { |
|
209 |
this.getLbl().setText(getDateString()); |
|
190 | 210 |
} |
191 | 211 |
|
192 | 212 |
public void setSelectedDate(Date date) { |
... | ... | |
197 | 217 |
this.getUICalendar().setDate(date); |
198 | 218 |
refreshLbl(); |
199 | 219 |
} |
200 |
|
|
201 |
public boolean isOkPressed(){ |
|
202 |
return this.okPressed; |
|
203 |
} |
|
204 |
|
|
205 |
public void actionPerformed(ActionEvent e) { |
|
206 |
JButton button = (JButton) e.getSource(); |
|
207 |
if (button.getActionCommand().equals("BUTTON_OK")){ |
|
208 |
this.okPressed = true; |
|
209 |
}else{ |
|
210 |
this.okPressed = false; |
|
211 |
} |
|
212 |
|
|
213 |
} |
|
214 | 220 |
} |
Also available in: Unified diff