|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DatePicker.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Created on 2004/05/12
|
|
| 3 |
*/
|
|
| 4 |
package org.asyrinx.brownie.swing;
|
|
| 5 |
|
|
| 6 |
import java.awt.Color;
|
|
| 7 |
import java.awt.Component;
|
|
| 8 |
import java.awt.Dimension;
|
|
| 9 |
import java.awt.Font;
|
|
| 10 |
import java.awt.Insets;
|
|
| 11 |
import java.awt.event.ActionEvent;
|
|
| 12 |
import java.awt.event.ActionListener;
|
|
| 13 |
import java.awt.event.MouseAdapter;
|
|
| 14 |
import java.awt.event.MouseEvent;
|
|
| 15 |
import java.text.DateFormat;
|
|
| 16 |
import java.text.SimpleDateFormat;
|
|
| 17 |
import java.util.Calendar;
|
|
| 18 |
import java.util.Date;
|
|
| 19 |
import java.util.GregorianCalendar;
|
|
| 20 |
|
|
| 21 |
import javax.swing.JButton;
|
|
| 22 |
import javax.swing.JLabel;
|
|
| 23 |
import javax.swing.JPanel;
|
|
| 24 |
import javax.swing.JTextField;
|
|
| 25 |
import javax.swing.plaf.BorderUIResource;
|
|
| 26 |
|
|
| 27 |
import org.asyrinx.brownie.core.util.DateUtils;
|
|
| 28 |
|
|
| 29 |
public final class DatePicker extends JPanel { |
|
| 30 |
/** Small font. */
|
|
| 31 |
private static final Font smallFont = new Font("Dialog", Font.PLAIN, 10); |
|
| 32 |
|
|
| 33 |
/** Large font. */
|
|
| 34 |
private static final Font largeFont = new Font("Dialog", Font.PLAIN, 12); |
|
| 35 |
|
|
| 36 |
/** Highlighted color. */
|
|
| 37 |
private static final Color highlight = new Color(255, 255, 204); |
|
| 38 |
|
|
| 39 |
/** Enabled color. */
|
|
| 40 |
private static final Color white = new Color(255, 255, 255); |
|
| 41 |
|
|
| 42 |
/** Disabled color. */
|
|
| 43 |
private static final Color gray = new Color(204, 204, 204); |
|
| 44 |
|
|
| 45 |
/** Most recently selected day component. */
|
|
| 46 |
private Component selectedDay = null; |
|
| 47 |
|
|
| 48 |
/** Currently selected date. */
|
|
| 49 |
private GregorianCalendar selectedDate = null; |
|
| 50 |
|
|
| 51 |
/** Tracks the original date set when the component was created. */
|
|
| 52 |
private GregorianCalendar originalDate = null; |
|
| 53 |
|
|
| 54 |
/**
|
|
| 55 |
* When true, the panel will be hidden as soon as a day is selected by
|
|
| 56 |
* clicking the day or clicking the Today button.
|
|
| 57 |
*/
|
|
| 58 |
private boolean hideOnSelect = true; |
|
| 59 |
|
|
| 60 |
/**
|
|
| 61 |
* When clicked, displays the previous year.
|
|
| 62 |
*/
|
|
| 63 |
private final JButton yearBackButton = new JButton(); |
|
| 64 |
|
|
| 65 |
/**
|
|
| 66 |
* When clicked, displays the previous month.
|
|
| 67 |
*/
|
|
| 68 |
private final JButton monthBackButton = new JButton(); |
|
| 69 |
|
|
| 70 |
/**
|
|
| 71 |
* Displays the currently selected month and year.
|
|
| 72 |
*/
|
|
| 73 |
private final JLabel monthAndYear = new JLabel(); |
|
| 74 |
|
|
| 75 |
/**
|
|
| 76 |
* When clicked, displays the next month.
|
|
| 77 |
*/
|
|
| 78 |
private final JButton monthForwardButton = new JButton(); |
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* When clicked, displays the next year.
|
|
| 82 |
*/
|
|
| 83 |
private final JButton yearForwardButton = new JButton(); |
|
| 84 |
|
|
| 85 |
/**
|
|
| 86 |
* currently selected time field.
|
|
| 87 |
*/
|
|
| 88 |
private final JTextField selectedTimeField = new JTextField(); |
|
| 89 |
|
|
| 90 |
/**
|
|
| 91 |
* Column headings for the days of the week.
|
|
| 92 |
*/
|
|
| 93 |
private final JTextField[] dayHeadings = new JTextField[] { |
|
| 94 |
new JTextField("S"), new JTextField("M"), new JTextField("T"), |
|
| 95 |
new JTextField("W"), new JTextField("T"), new JTextField("F"), |
|
| 96 |
new JTextField("S") }; |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* 2-dimensional array for 6 weeks of 7 days each.
|
|
| 100 |
*/
|
|
| 101 |
private final JTextField[][] daysInMonth = initDayTextFields();
|
|
| 102 |
|
|
| 103 | 0 |
private JTextField[][] initDayTextFields() {
|
| 104 | 0 |
final JTextField[][] result = new JTextField[6][7];
|
| 105 | 0 |
for (int w = 0; w < result.length; w++) { |
| 106 | 0 |
for (int d = 0; d < result[w].length; d++) { |
| 107 | 0 |
result[w][d] = new JTextField();
|
| 108 |
} |
|
| 109 |
} |
|
| 110 | 0 |
return result;
|
| 111 |
} |
|
| 112 |
|
|
| 113 |
/**
|
|
| 114 |
* When clicked, sets the selected day to the current date.
|
|
| 115 |
*/
|
|
| 116 |
private final JButton todayButton = new JButton(); |
|
| 117 |
|
|
| 118 |
/**
|
|
| 119 |
* When clicked, hides the calendar and sets the selected date to "empty".
|
|
| 120 |
*/
|
|
| 121 |
private final JButton cancelButton = new JButton(); |
|
| 122 |
|
|
| 123 |
private DateFormat titleDateFormat = new SimpleDateFormat("yyyy/MM"); |
|
| 124 |
|
|
| 125 |
private DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); |
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* Default constructor that sets the currently selected date to the current
|
|
| 129 |
* date.
|
|
| 130 |
*/
|
|
| 131 | 0 |
public DatePicker() {
|
| 132 | 0 |
super();
|
| 133 | 0 |
selectedDate = getToday(); |
| 134 | 0 |
initialize(); |
| 135 |
} |
|
| 136 |
|
|
| 137 |
/**
|
|
| 138 |
* Alternate constructor that sets the currently selected date to the
|
|
| 139 |
* specified date if non-null.
|
|
| 140 |
*
|
|
| 141 |
* @param initialDate
|
|
| 142 |
*/
|
|
| 143 | 0 |
public DatePicker(final Date initialDate) {
|
| 144 | 0 |
super();
|
| 145 | 0 |
if (null == initialDate) |
| 146 | 0 |
selectedDate = getToday(); |
| 147 |
else
|
|
| 148 | 0 |
(selectedDate = new GregorianCalendar()).setTime(initialDate);
|
| 149 | 0 |
originalDate = new GregorianCalendar(selectedDate.get(Calendar.YEAR),
|
| 150 |
selectedDate.get(Calendar.MONTH), selectedDate |
|
| 151 |
.get(Calendar.DATE)); |
|
| 152 | 0 |
initialize(); |
| 153 |
} |
|
| 154 |
|
|
| 155 |
/**
|
|
| 156 |
* This method initializes this
|
|
| 157 |
*
|
|
| 158 |
* @return void
|
|
| 159 |
*/
|
|
| 160 | 0 |
private void initialize() { |
| 161 | 0 |
final Insets insets = new Insets(2, 2, 2, 2);
|
| 162 |
//
|
|
| 163 | 0 |
setLayout(new AbsoluteLayout());
|
| 164 | 0 |
this.setMinimumSize(new Dimension(210, 240)); |
| 165 | 0 |
this.setMaximumSize(getMinimumSize());
|
| 166 | 0 |
this.setPreferredSize(getMinimumSize());
|
| 167 | 0 |
this.setBorder(new BorderUIResource.EtchedBorderUIResource()); |
| 168 |
//
|
|
| 169 | 0 |
yearBackButton.setFont(smallFont); |
| 170 | 0 |
yearBackButton.setText("<<");
|
| 171 | 0 |
yearBackButton.setMargin(insets); |
| 172 | 0 |
yearBackButton.setDefaultCapable(false);
|
| 173 | 0 |
yearBackButton.addActionListener(new ActionListener() {
|
| 174 | 0 |
public void actionPerformed(final ActionEvent evt) { |
| 175 | 0 |
onYearBackClicked(evt); |
| 176 |
} |
|
| 177 |
}); |
|
| 178 | 0 |
add(yearBackButton, new AbsoluteConstraints(10, 10, 30, 20));
|
| 179 |
//
|
|
| 180 | 0 |
monthBackButton.setFont(smallFont); |
| 181 | 0 |
monthBackButton.setText("<");
|
| 182 | 0 |
monthBackButton.setMargin(insets); |
| 183 | 0 |
monthBackButton.setDefaultCapable(false);
|
| 184 | 0 |
monthBackButton.addActionListener(new ActionListener() {
|
| 185 | 0 |
public void actionPerformed(final ActionEvent evt) { |
| 186 | 0 |
onMonthBackClicked(evt); |
| 187 |
} |
|
| 188 |
}); |
|
| 189 | 0 |
add(monthBackButton, new AbsoluteConstraints(40, 10, 20, 20));
|
| 190 |
//
|
|
| 191 | 0 |
monthAndYear.setFont(largeFont); |
| 192 | 0 |
monthAndYear.setHorizontalAlignment(JTextField.CENTER); |
| 193 | 0 |
monthAndYear.setText(titleDateFormat.format(selectedDate.getTime())); |
| 194 | 0 |
add(monthAndYear, new AbsoluteConstraints(50, 10, 100, 20));
|
| 195 |
//
|
|
| 196 | 0 |
monthForwardButton.setFont(smallFont); |
| 197 | 0 |
monthForwardButton.setText(">");
|
| 198 | 0 |
monthForwardButton.setMargin(insets); |
| 199 | 0 |
monthForwardButton.setDefaultCapable(false);
|
| 200 | 0 |
monthForwardButton.addActionListener(new ActionListener() {
|
| 201 | 0 |
public void actionPerformed(final ActionEvent evt) { |
| 202 | 0 |
onMonthForwardClicked(evt); |
| 203 |
} |
|
| 204 |
}); |
|
| 205 | 0 |
add(monthForwardButton, new AbsoluteConstraints(150, 10, 20, 20));
|
| 206 |
//
|
|
| 207 | 0 |
yearForwardButton.setFont(smallFont); |
| 208 | 0 |
yearForwardButton.setText(">>");
|
| 209 | 0 |
yearForwardButton.setMargin(insets); |
| 210 | 0 |
yearForwardButton.setDefaultCapable(false);
|
| 211 | 0 |
yearForwardButton.addActionListener(new ActionListener() {
|
| 212 | 0 |
public void actionPerformed(final ActionEvent evt) { |
| 213 | 0 |
onYearForwardClicked(evt); |
| 214 |
} |
|
| 215 |
}); |
|
| 216 | 0 |
add(yearForwardButton, new AbsoluteConstraints(170, 10, 30, 20));
|
| 217 |
// layout the column headings for the days of the week
|
|
| 218 | 0 |
final int startX = 35;
|
| 219 | 0 |
final int startY = 60;
|
| 220 | 0 |
int x = startX;
|
| 221 | 0 |
for (int ii = 0; ii < dayHeadings.length; ii++) { |
| 222 | 0 |
dayHeadings[ii].setBackground(gray); |
| 223 | 0 |
dayHeadings[ii].setEditable(false);
|
| 224 | 0 |
dayHeadings[ii].setFont(smallFont); |
| 225 | 0 |
dayHeadings[ii].setHorizontalAlignment(JTextField.CENTER); |
| 226 | 0 |
dayHeadings[ii].setFocusable(false);
|
| 227 | 0 |
add(dayHeadings[ii], new AbsoluteConstraints(x, 40, 21, 21));
|
| 228 | 0 |
x += 20; |
| 229 |
} |
|
| 230 |
// layout the days of the month
|
|
| 231 | 0 |
x = startX; |
| 232 | 0 |
int y = startY;
|
| 233 | 0 |
for (int ii = 0; ii < daysInMonth.length; ii++) { |
| 234 | 0 |
for (int jj = 0; jj < daysInMonth[ii].length; jj++) { |
| 235 | 0 |
daysInMonth[ii][jj].setBackground(gray); |
| 236 | 0 |
daysInMonth[ii][jj].setEditable(false);
|
| 237 | 0 |
daysInMonth[ii][jj].setFont(smallFont); |
| 238 | 0 |
daysInMonth[ii][jj].setHorizontalAlignment(JTextField.RIGHT); |
| 239 | 0 |
daysInMonth[ii][jj].setText("");
|
| 240 | 0 |
daysInMonth[ii][jj].setFocusable(false);
|
| 241 | 0 |
daysInMonth[ii][jj].addMouseListener(new MouseAdapter() {
|
| 242 | 0 |
public void mouseClicked(final MouseEvent e) { |
| 243 | 0 |
onDayClicked(e); |
| 244 |
} |
|
| 245 |
}); |
|
| 246 | 0 |
add(daysInMonth[ii][jj], new AbsoluteConstraints(x, y, 21, 21));
|
| 247 | 0 |
x += 20; |
| 248 |
} |
|
| 249 | 0 |
x = startX; |
| 250 | 0 |
y += 20; |
| 251 |
} |
|
| 252 |
//
|
|
| 253 |
//monthAndYear.setFont(largeFont);
|
|
| 254 | 0 |
selectedTimeField.setHorizontalAlignment(JTextField.CENTER); |
| 255 | 0 |
selectedTimeField.setText(timeFormat.format(selectedDate.getTime())); |
| 256 | 0 |
add(selectedTimeField, new AbsoluteConstraints(60, 186, 100, 20));
|
| 257 |
|
|
| 258 | 0 |
initButtons(true);
|
| 259 | 0 |
calculateCalendar(); |
| 260 | 0 |
this.setSize(300, 229);
|
| 261 |
} |
|
| 262 |
|
|
| 263 |
/**
|
|
| 264 |
* Initializes Today and Cancel buttons dependent on whether hideOnSelect is
|
|
| 265 |
* set; if the panel will stay open, the Cancel button is invisible.
|
|
| 266 |
*
|
|
| 267 |
* @param firstTime
|
|
| 268 |
*/
|
|
| 269 | 0 |
private void initButtons(final boolean firstTime) { |
| 270 | 0 |
final int posY = 210;
|
| 271 | 0 |
if (firstTime) {
|
| 272 | 0 |
final Insets insets = new Insets(2, 2, 2, 2);
|
| 273 |
//
|
|
| 274 | 0 |
final Dimension buttonSize = new Dimension(68, 24);
|
| 275 | 0 |
todayButton.setFont(largeFont); |
| 276 | 0 |
todayButton.setText("Today");
|
| 277 | 0 |
todayButton.setMargin(insets); |
| 278 | 0 |
todayButton.setMaximumSize(buttonSize); |
| 279 | 0 |
todayButton.setMinimumSize(buttonSize); |
| 280 | 0 |
todayButton.setPreferredSize(buttonSize); |
| 281 | 0 |
todayButton.setDefaultCapable(true);
|
| 282 | 0 |
todayButton.setSelected(true);
|
| 283 | 0 |
todayButton.addActionListener(new ActionListener() {
|
| 284 | 0 |
public void actionPerformed(final ActionEvent evt) { |
| 285 | 0 |
onToday(evt); |
| 286 |
} |
|
| 287 |
}); |
|
| 288 | 0 |
cancelButton.setFont(largeFont); |
| 289 | 0 |
cancelButton.setText("Cancel");
|
| 290 | 0 |
cancelButton.setMargin(insets); |
| 291 | 0 |
cancelButton.setMaximumSize(buttonSize); |
| 292 | 0 |
cancelButton.setMinimumSize(buttonSize); |
| 293 | 0 |
cancelButton.setPreferredSize(buttonSize); |
| 294 | 0 |
cancelButton.addActionListener(new ActionListener() {
|
| 295 | 0 |
public void actionPerformed(final ActionEvent evt) { |
| 296 | 0 |
onCancel(evt); |
| 297 |
} |
|
| 298 |
}); |
|
| 299 |
} else {
|
|
| 300 | 0 |
this.remove(todayButton);
|
| 301 | 0 |
this.remove(cancelButton);
|
| 302 |
} |
|
| 303 | 0 |
if (hideOnSelect) {
|
| 304 | 0 |
add(todayButton, new AbsoluteConstraints(50, posY, 52, -1));
|
| 305 | 0 |
add(cancelButton, new AbsoluteConstraints(112, posY, 52, -1));
|
| 306 |
} else {
|
|
| 307 | 0 |
add(todayButton, new AbsoluteConstraints(70, posY, 52, -1));
|
| 308 |
} |
|
| 309 |
} |
|
| 310 |
|
|
| 311 |
/**
|
|
| 312 |
* Returns true if the panel will be made invisible after a day is selected.
|
|
| 313 |
*
|
|
| 314 |
* @return true or false
|
|
| 315 |
*/
|
|
| 316 | 0 |
public boolean isHideOnSelect() { |
| 317 | 0 |
return hideOnSelect;
|
| 318 |
} |
|
| 319 |
|
|
| 320 |
/**
|
|
| 321 |
* Controls whether the panel will be made invisible after a day is
|
|
| 322 |
* selected.
|
|
| 323 |
*
|
|
| 324 |
* @param hideOnSelect
|
|
| 325 |
*/
|
|
| 326 | 0 |
public void setHideOnSelect(final boolean hideOnSelect) { |
| 327 | 0 |
if (this.hideOnSelect != hideOnSelect) { |
| 328 | 0 |
this.hideOnSelect = hideOnSelect;
|
| 329 | 0 |
initButtons(false);
|
| 330 |
} |
|
| 331 |
} |
|
| 332 |
|
|
| 333 |
/**
|
|
| 334 |
* Returns the currently selected date.
|
|
| 335 |
*
|
|
| 336 |
* @return date
|
|
| 337 |
*/
|
|
| 338 | 0 |
public Date getDate() {
|
| 339 | 0 |
if (null != selectedDate) |
| 340 | 0 |
return selectedDate.getTime();
|
| 341 | 0 |
return null; |
| 342 |
} |
|
| 343 |
|
|
| 344 |
/**
|
|
| 345 |
* Event handler for the Today button that sets the currently selected date
|
|
| 346 |
* to the current date.
|
|
| 347 |
*
|
|
| 348 |
* @param evt
|
|
| 349 |
*/
|
|
| 350 | 0 |
protected void onToday(final java.awt.event.ActionEvent evt) { |
| 351 | 0 |
selectedDate = getToday(); |
| 352 | 0 |
setVisible(!hideOnSelect); |
| 353 | 0 |
if (isVisible()) { // don't bother with calculation if not visible |
| 354 | 0 |
monthAndYear |
| 355 |
.setText(titleDateFormat.format(selectedDate.getTime())); |
|
| 356 | 0 |
calculateCalendar(); |
| 357 |
} |
|
| 358 |
} |
|
| 359 |
|
|
| 360 |
/**
|
|
| 361 |
* Event handler for the Cancel button that unsets the currently selected
|
|
| 362 |
* date.
|
|
| 363 |
*
|
|
| 364 |
* @param evt
|
|
| 365 |
*/
|
|
| 366 | 0 |
protected void onCancel(final ActionEvent evt) { |
| 367 | 0 |
selectedDate = originalDate; |
| 368 | 0 |
setVisible(!hideOnSelect); |
| 369 |
} |
|
| 370 |
|
|
| 371 |
/**
|
|
| 372 |
* @param diff
|
|
| 373 |
*/
|
|
| 374 | 0 |
private void moveMonth(final int diff) { |
| 375 | 0 |
final int day = selectedDate.get(Calendar.DATE);
|
| 376 | 0 |
selectedDate.set(Calendar.DATE, 1); |
| 377 | 0 |
selectedDate.add(Calendar.MONTH, diff); |
| 378 | 0 |
selectedDate.set(Calendar.DATE, Math.min(day, |
| 379 |
calculateDaysInMonth(selectedDate))); |
|
| 380 | 0 |
monthAndYear.setText(titleDateFormat.format(selectedDate.getTime())); |
| 381 | 0 |
calculateCalendar(); |
| 382 |
} |
|
| 383 |
|
|
| 384 |
/**
|
|
| 385 |
* Event handler for the forward button that increments the currently
|
|
| 386 |
* selected month.
|
|
| 387 |
*
|
|
| 388 |
* @param evt
|
|
| 389 |
*/
|
|
| 390 | 0 |
protected void onMonthForwardClicked(final java.awt.event.ActionEvent evt) { |
| 391 | 0 |
moveMonth(1); |
| 392 |
} |
|
| 393 |
|
|
| 394 |
/**
|
|
| 395 |
* Event handler for the back button that decrements the currently selected
|
|
| 396 |
* month.
|
|
| 397 |
*
|
|
| 398 |
* @param evt
|
|
| 399 |
*/
|
|
| 400 | 0 |
protected void onMonthBackClicked(final java.awt.event.ActionEvent evt) { |
| 401 | 0 |
moveMonth(-1); |
| 402 |
} |
|
| 403 |
|
|
| 404 |
/**
|
|
| 405 |
* Event handler for the forward button that increments the currently
|
|
| 406 |
* selected month.
|
|
| 407 |
*
|
|
| 408 |
* @param evt
|
|
| 409 |
*/
|
|
| 410 | 0 |
protected void onYearForwardClicked(final java.awt.event.ActionEvent evt) { |
| 411 | 0 |
moveMonth(12); |
| 412 |
} |
|
| 413 |
|
|
| 414 |
/**
|
|
| 415 |
* Event handler for the back button that decrements the currently selected
|
|
| 416 |
* month.
|
|
| 417 |
*
|
|
| 418 |
* @param evt
|
|
| 419 |
*/
|
|
| 420 | 0 |
protected void onYearBackClicked(final java.awt.event.ActionEvent evt) { |
| 421 | 0 |
moveMonth(-12); |
| 422 |
} |
|
| 423 |
|
|
| 424 |
/**
|
|
| 425 |
* Event handler that sets the currently selected date to the clicked day.
|
|
| 426 |
*
|
|
| 427 |
* @param evt
|
|
| 428 |
*/
|
|
| 429 | 0 |
protected void onDayClicked(final java.awt.event.MouseEvent evt) { |
| 430 | 0 |
final javax.swing.JTextField fld = (javax.swing.JTextField) evt |
| 431 |
.getSource(); |
|
| 432 | 0 |
if (!"".equals(fld.getText())) { |
| 433 | 0 |
if (null != selectedDay) { |
| 434 | 0 |
selectedDay.setBackground(white); |
| 435 |
} |
|
| 436 | 0 |
fld.setBackground(highlight); |
| 437 | 0 |
selectedDay = fld; |
| 438 | 0 |
selectedDate.set(Calendar.DATE, Integer.parseInt(fld.getText())); |
| 439 | 0 |
setVisible(!hideOnSelect); |
| 440 |
} |
|
| 441 |
} |
|
| 442 |
|
|
| 443 |
/**
|
|
| 444 |
* Returns the current date.
|
|
| 445 |
*
|
|
| 446 |
* @return date
|
|
| 447 |
*/
|
|
| 448 | 0 |
private static GregorianCalendar getToday() { |
| 449 | 0 |
final GregorianCalendar gc = new GregorianCalendar();
|
| 450 | 0 |
gc.set(Calendar.HOUR_OF_DAY, 0); |
| 451 | 0 |
gc.set(Calendar.MINUTE, 0); |
| 452 | 0 |
gc.set(Calendar.SECOND, 0); |
| 453 | 0 |
gc.set(Calendar.MILLISECOND, 0); |
| 454 | 0 |
return gc;
|
| 455 |
} |
|
| 456 |
|
|
| 457 |
/**
|
|
| 458 |
* Calculates the days of the month.
|
|
| 459 |
*/
|
|
| 460 | 0 |
private void calculateCalendar() { |
| 461 |
// clear the selected date
|
|
| 462 | 0 |
if (null != selectedDay) { |
| 463 | 0 |
selectedDay.setBackground(white); |
| 464 | 0 |
selectedDay = null;
|
| 465 |
} |
|
| 466 |
// get the first day of the selected year and month
|
|
| 467 | 0 |
final GregorianCalendar c = new GregorianCalendar(selectedDate
|
| 468 |
.get(Calendar.YEAR), selectedDate.get(Calendar.MONTH), 1); |
|
| 469 |
// figure out the maximum number of days in the month
|
|
| 470 | 0 |
final int maxDay = calculateDaysInMonth(c);
|
| 471 |
// figure out the day that should be selected in this month
|
|
| 472 |
// based on the previously selected day and the maximum number
|
|
| 473 |
// of days in the month
|
|
| 474 | 0 |
final int selectedDayValue = Math.min(maxDay, selectedDate
|
| 475 |
.get(Calendar.DATE)); |
|
| 476 |
// clear the days up to the first day of the month
|
|
| 477 | 0 |
int dow = c.get(Calendar.DAY_OF_WEEK);
|
| 478 | 0 |
for (int dd = 0; dd < dow; dd++) { |
| 479 | 0 |
daysInMonth[0][dd].setText("");
|
| 480 | 0 |
daysInMonth[0][dd].setBackground(gray); |
| 481 |
} |
|
| 482 |
// construct the days in the selected month
|
|
| 483 | 0 |
int week;
|
| 484 | 0 |
do {
|
| 485 | 0 |
week = c.get(Calendar.WEEK_OF_MONTH); |
| 486 | 0 |
dow = c.get(Calendar.DAY_OF_WEEK); |
| 487 | 0 |
final JTextField fld = this.daysInMonth[week - 1][dow - 1];
|
| 488 | 0 |
fld.setText(Integer.toString(c.get(Calendar.DATE))); |
| 489 | 0 |
if (selectedDayValue == c.get(Calendar.DATE)) {
|
| 490 | 0 |
fld.setBackground(highlight); |
| 491 | 0 |
this.selectedDay = fld;
|
| 492 |
} else
|
|
| 493 | 0 |
fld.setBackground(white); |
| 494 | 0 |
if (c.get(Calendar.DATE) >= maxDay)
|
| 495 | 0 |
break;
|
| 496 | 0 |
c.add(Calendar.DATE, 1); |
| 497 | 0 |
} while (c.get(Calendar.DATE) <= maxDay);
|
| 498 |
// clear all the days after the last day of the month
|
|
| 499 | 0 |
week--; |
| 500 | 0 |
for (int ww = week; ww < daysInMonth.length; ww++) { |
| 501 | 0 |
for (int dd = dow; dd < daysInMonth[ww].length; dd++) { |
| 502 | 0 |
daysInMonth[ww][dd].setText("");
|
| 503 | 0 |
daysInMonth[ww][dd].setBackground(gray); |
| 504 |
} |
|
| 505 | 0 |
dow = 0; |
| 506 |
} |
|
| 507 |
// set the currently selected date
|
|
| 508 | 0 |
c.set(Calendar.DATE, selectedDayValue); |
| 509 | 0 |
selectedDate = c; |
| 510 |
} |
|
| 511 |
|
|
| 512 |
/**
|
|
| 513 |
* Calculates the number of days in the specified month.
|
|
| 514 |
*
|
|
| 515 |
* @param c
|
|
| 516 |
* @return number of days in the month
|
|
| 517 |
*/
|
|
| 518 | 0 |
private static int calculateDaysInMonth(final Calendar c) { |
| 519 | 0 |
return DateUtils.getLastDayOfMonth(c.get(Calendar.YEAR), c
|
| 520 |
.get(Calendar.MONTH) + 1); |
|
| 521 |
} |
|
| 522 |
|
|
| 523 |
/**
|
|
| 524 |
* @return Returns the titleDateFormat.
|
|
| 525 |
*/
|
|
| 526 | 0 |
public DateFormat getTitleDateFormat() {
|
| 527 | 0 |
return titleDateFormat;
|
| 528 |
} |
|
| 529 |
|
|
| 530 |
/**
|
|
| 531 |
* @param titleDateFormat
|
|
| 532 |
* The titleDateFormat to set.
|
|
| 533 |
*/
|
|
| 534 | 0 |
public void setTitleDateFormat(DateFormat titleDateFormat) { |
| 535 | 0 |
this.titleDateFormat = titleDateFormat;
|
| 536 |
} |
|
| 537 |
} // @jve:visual-info decl-index=0 visual-constraint="10,10"
|
|
| 538 |
|
|
||||||||||