1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Color ; 34 import java.awt.Component ; 35 import java.awt.Container ; 36 import java.awt.Dimension ; 37 import java.awt.Font ; 38 import java.awt.Frame ; 39 import java.awt.GridBagConstraints ; 40 import java.awt.GridBagLayout ; 41 import java.awt.GridLayout ; 42 import java.awt.Insets ; 43 import java.awt.Point ; 44 import java.awt.event.ActionEvent ; 45 import java.awt.event.ActionListener ; 46 import java.awt.event.KeyEvent ; 47 import java.awt.event.KeyListener ; 48 import java.awt.event.MouseEvent ; 49 import java.awt.event.MouseListener ; 50 import java.io.Serializable ; 51 import java.text.SimpleDateFormat ; 52 import java.util.Calendar ; 53 import java.util.Date ; 54 import java.util.GregorianCalendar ; 55 import java.util.HashMap ; 56 57 import javax.swing.BorderFactory ; 58 import javax.swing.Icon ; 59 import javax.swing.JButton ; 60 import javax.swing.JComponent ; 61 import javax.swing.JDialog ; 62 import javax.swing.JLabel ; 63 import javax.swing.JPanel ; 64 import javax.swing.JTextField ; 65 import javax.swing.SwingConstants ; 66 67 import com.genimen.djeneric.language.Messages; 68 import com.genimen.djeneric.util.DjLogger; 69 70 86 87 public class DateChooser extends JComponent implements Serializable , ActionListener , MouseListener , KeyListener 88 { 89 90 private static final long serialVersionUID = 1L; 91 static boolean _onJdk13 = System.getProperty("java.specification.version").startsWith("1.3"); 92 93 99 private boolean isEditable = false; 100 101 109 private int monthScrollRate = 5; 110 111 118 private Date selectedDate; 119 120 128 private SimpleDateFormat returnDateFormatter = new SimpleDateFormat ("MMM dd, yyyy"); 129 130 133 private JTextField componentTextField; 134 private JButton componentButton; 135 private int componentMargin = 0; 136 private JDialog popupCalendar; 137 private JButton todayButton, noneButton; 138 private JButton prevButton, nextButton; 139 private javax.swing.Timer nextMonthTimer, prevMonthTimer; 140 private JLabel monthButton; 141 private SimpleDateFormat monthyearFormatter = new SimpleDateFormat ("MMMM yyyy"); 142 private JPanel dayHeaderPanel, dayPanel; 143 private JLabel [] dayLabel = new JLabel [42]; 144 private Date todaysDate, displayedDate; 145 private GregorianCalendar displayedCalendar, todaysCalendar, selectedCalendar; 146 private int todaysYear, todaysMonth, todaysDay, selectedYear, selectedMonth, selectedDay; 147 private HashMap dayMap = new HashMap (); 148 149 153 156 public DateChooser() 157 { 158 159 layoutControl(); 160 layoutCalendarWindow(); 161 } 162 163 169 public DateChooser(int cols) 170 { 171 172 layoutControl(); 173 layoutCalendarWindow(); 174 componentTextField.setColumns(cols); 175 } 176 177 184 public DateChooser(Date aDate) 185 { 186 187 layoutControl(); 188 layoutCalendarWindow(); 189 190 selectedDate = aDate; 191 selectedCalendar.setTime(selectedDate); 192 selectedYear = selectedCalendar.get(Calendar.YEAR); 193 selectedMonth = selectedCalendar.get(Calendar.MONTH); 194 selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH); 195 displayedDate = aDate; 196 displayedCalendar.setTime(displayedDate); 197 updateCalendarDisplay(); 198 componentTextField.setText(returnDateFormatter.format(selectedDate)); 199 200 } 201 202 209 public DateChooser(Date aDate, int cols) 210 { 211 212 layoutControl(); 213 layoutCalendarWindow(); 214 215 selectedDate = aDate; 216 selectedCalendar.setTime(selectedDate); 217 selectedYear = selectedCalendar.get(Calendar.YEAR); 218 selectedMonth = selectedCalendar.get(Calendar.MONTH); 219 selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH); 220 displayedDate = aDate; 221 displayedCalendar.setTime(displayedDate); 222 updateCalendarDisplay(); 223 componentTextField.setText(returnDateFormatter.format(selectedDate)); 224 componentTextField.setColumns(cols); 225 226 } 227 228 232 public Date getDate() 233 { 234 return selectedDate; 235 } 236 237 public SimpleDateFormat getDateFormat() 238 { 239 return returnDateFormatter; 240 } 241 242 public String getDateFormatPattern() 243 { 244 String myPattern = returnDateFormatter.toPattern(); 245 return myPattern; 246 } 247 248 public int getMonthScrollRate() 249 { 250 return monthScrollRate; 251 } 252 253 public Font getPopupFont() 254 { 255 Font myFont = monthButton.getFont(); 256 return myFont; 257 } 258 259 public Font getTextFont() 260 { 261 Font myFont = componentTextField.getFont(); 262 return myFont; 263 } 264 265 public boolean isEditable() 266 { 267 return isEditable; 268 } 269 270 public void setDate(Date myDate) 271 { 272 280 setDateEx(myDate); 281 componentTextField.setText(returnDateFormatter.format(myDate)); 282 } 283 284 private void setDateEx(Date myDate) 285 { 286 selectedDate = myDate; 287 selectedCalendar.setTime(selectedDate); 288 selectedYear = selectedCalendar.get(Calendar.YEAR); 289 selectedMonth = selectedCalendar.get(Calendar.MONTH); 290 selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH); 291 displayedCalendar.setTime(selectedDate); 292 updateCalendarDisplay(); 293 } 294 295 public void setDateFormat(SimpleDateFormat mySimpleFormat) 296 { 297 returnDateFormatter = mySimpleFormat; 298 } 299 300 public void setDateFormatPattern(String pattern) 301 { 302 returnDateFormatter.applyPattern(pattern); 303 } 304 305 public void setEditable(boolean myEditable) 306 { 307 componentTextField.setEditable(myEditable); 308 isEditable = myEditable; 309 } 310 311 public void setMonthScrollRate(int myMonthScrollRate) 312 { 313 monthScrollRate = myMonthScrollRate; 314 } 315 316 public void setPopupFont(Font myFont) 317 { 318 monthButton.setFont(myFont); 319 todayButton.setFont(myFont); 320 noneButton.setFont(myFont); 321 Component [] dayHeaderLabels = dayHeaderPanel.getComponents(); 322 for (int i = 0; i < dayHeaderLabels.length; i++) 323 { 324 dayHeaderLabels[i].setFont(myFont); 325 } 326 Component [] dayLabels = dayPanel.getComponents(); 327 for (int i = 0; i < dayLabels.length; i++) 328 { 329 dayLabels[i].setFont(myFont); 330 } 331 } 332 333 public void setTextFont(Font myFont) 334 { 335 componentTextField.setFont(myFont); 336 } 337 338 342 public void actionPerformed(ActionEvent evt) 343 { 344 Object source = evt.getSource(); 345 346 if (source == componentButton) 347 { 348 Point myOrigin = componentButton.getLocationOnScreen(); 349 Dimension buttonSize = componentButton.getSize(); 350 popupCalendar.pack(); 351 Dimension mySize = popupCalendar.getSize(); 352 myOrigin.translate((buttonSize.width - mySize.width), (buttonSize.height)); 353 popupCalendar.setLocation(myOrigin); 354 popupCalendar.setVisible(true); 355 componentTextField.requestFocus(); 356 } 357 if (source == todayButton) 358 { 359 selectedDate = todaysCalendar.getTime(); 360 selectedCalendar.setTime(selectedDate); 361 selectedYear = selectedCalendar.get(Calendar.YEAR); 362 selectedMonth = selectedCalendar.get(Calendar.MONTH); 363 selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH); 364 componentTextField.setText(returnDateFormatter.format(selectedDate)); 365 popupCalendar.setVisible(false); 366 displayedCalendar.setTime(selectedDate); 367 updateCalendarDisplay(); 368 } 369 if (source == noneButton) 370 { 371 componentTextField.setText(null); 372 selectedDate = null; 373 popupCalendar.setVisible(false); 374 selectedYear = 0; 375 selectedMonth = 0; 376 selectedDay = 0; 377 updateCalendarDisplay(); 378 379 } 380 } 381 382 public void mousePressed(MouseEvent evt) 383 { 384 Object source = evt.getSource(); 385 if (dayMap.containsKey(source)) 386 { 387 selectedDate = (Date ) dayMap.get(source); 388 selectedCalendar.setTime(selectedDate); 389 selectedYear = selectedCalendar.get(Calendar.YEAR); 390 selectedMonth = selectedCalendar.get(Calendar.MONTH); 391 selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH); 392 componentTextField.setText(returnDateFormatter.format(selectedDate)); 393 popupCalendar.setVisible(false); 394 updateCalendarDisplay(); 395 } 396 else if (source == prevButton) 397 { 398 displayedCalendar.roll(Calendar.MONTH, -1); 399 if (displayedCalendar.get(Calendar.MONTH) == Calendar.DECEMBER) 400 { 401 displayedCalendar.roll(Calendar.YEAR, -1); 402 } 403 updateCalendarDisplay(); 404 prevMonthTimer.start(); 405 } 406 else if (source == nextButton) 407 { 408 displayedCalendar.roll(Calendar.MONTH, 1); 409 if (displayedCalendar.get(Calendar.MONTH) == Calendar.JANUARY) 410 { 411 displayedCalendar.roll(Calendar.YEAR, 1); 412 } 413 updateCalendarDisplay(); 414 nextMonthTimer.start(); 415 } 416 else 417 { 418 } 420 } 421 422 public void mouseReleased(MouseEvent evt) 423 { 424 Object source = evt.getSource(); 425 if (source == prevButton) 426 { 427 prevMonthTimer.stop(); 428 } 429 else if (source == nextButton) 430 { 431 nextMonthTimer.stop(); 432 } 433 else 434 { 435 } 437 } 438 439 public void mouseEntered(MouseEvent evt) 440 { 441 } 443 444 public void mouseExited(MouseEvent evt) 445 { 446 Object source = evt.getSource(); 447 if (source == prevButton) 448 { 449 prevMonthTimer.stop(); 450 } 451 else if (source == nextButton) 452 { 453 nextMonthTimer.stop(); 454 } 455 else 456 { 457 } 459 } 460 461 public void mouseClicked(MouseEvent evt) 462 { 463 } 465 466 ActionListener monthScrollListener = new ActionListener () 467 { 468 public void actionPerformed(ActionEvent evt) 469 { 470 Object source = evt.getSource(); 471 if (source == prevMonthTimer) 472 { 473 displayedCalendar.roll(Calendar.MONTH, -1); 474 if (displayedCalendar.get(Calendar.MONTH) == Calendar.DECEMBER) 475 { 476 displayedCalendar.roll(Calendar.YEAR, -1); 477 } 478 updateCalendarDisplay(); 479 } 480 else if (source == nextMonthTimer) 481 { 482 displayedCalendar.roll(Calendar.MONTH, 1); 483 if (displayedCalendar.get(Calendar.MONTH) == Calendar.JANUARY) 484 { 485 displayedCalendar.roll(Calendar.YEAR, 1); 486 } 487 updateCalendarDisplay(); 488 } 489 else 490 { 491 } 493 } 494 }; 495 496 500 private void layoutControl() 501 { 502 BorderLayout componentLayout = new BorderLayout (componentMargin, 0); 504 setLayout(componentLayout); 505 componentTextField = new JTextField (12); 506 componentTextField.addKeyListener(this); 507 add(componentTextField, BorderLayout.CENTER); 508 componentButton = new JButton (); 509 componentButton.setPreferredSize(new Dimension (21, 21)); 510 componentButton.setText("..."); 511 componentButton.addActionListener(this); 512 componentButton.setFocusPainted(false); 513 add(componentButton, BorderLayout.EAST); 514 setBorder(BorderFactory.createEmptyBorder(componentMargin, componentMargin, componentMargin, componentMargin)); 515 addMouseListener(this); 516 } 517 518 private void layoutCalendarWindow() 519 { 520 todaysDate = new Date (); 521 todaysCalendar = new GregorianCalendar (); 522 todaysCalendar.setTime(todaysDate); 523 todaysYear = todaysCalendar.get(Calendar.YEAR); 524 todaysMonth = todaysCalendar.get(Calendar.MONTH); 525 todaysDay = todaysCalendar.get(Calendar.DAY_OF_MONTH); 526 527 selectedDate = null; 528 selectedCalendar = new GregorianCalendar (); 529 selectedYear = 0; 530 selectedMonth = 0; 531 selectedDay = 0; 532 533 displayedDate = new Date (); 534 displayedCalendar = new GregorianCalendar (); 535 displayedCalendar.setTime(displayedDate); 536 537 popupCalendar = new JDialog ((Frame ) this.getTopLevelAncestor(), true); 538 539 if (!_onJdk13) popupCalendar.setUndecorated(true); 540 541 JPanel monthPanel = new JPanel (new BorderLayout ()); 542 monthPanel.setBorder(BorderFactory.createEtchedBorder()); 543 Icon leftButtonIcon = new ArrowIcon(SwingConstants.LEFT); 544 prevButton = new JButton (leftButtonIcon); 545 prevButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 546 prevButton.setPressedIcon(prevButton.getIcon()); 547 prevButton.setFocusPainted(false); 548 prevButton.addMouseListener(this); 549 prevMonthTimer = new javax.swing.Timer ((1000 / monthScrollRate), monthScrollListener); 550 prevMonthTimer.setInitialDelay(1000); 551 monthPanel.add(prevButton, BorderLayout.WEST); 552 553 monthButton = new JLabel ("SEPTEMBER 8888", JLabel.CENTER); 554 monthButton.setBorder(null); 555 monthButton.addMouseListener(this); 556 monthPanel.add(monthButton, BorderLayout.CENTER); 557 558 Icon rightButtonIcon = new ArrowIcon(SwingConstants.RIGHT); 559 nextButton = new JButton (rightButtonIcon); 560 nextButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 561 nextButton.setPressedIcon(nextButton.getIcon()); 562 nextButton.setFocusPainted(false); 563 nextButton.addMouseListener(this); 564 nextMonthTimer = new javax.swing.Timer ((1000 / monthScrollRate), monthScrollListener); 565 nextMonthTimer.setInitialDelay(1000); 566 monthPanel.add(nextButton, BorderLayout.EAST); 567 568 dayHeaderPanel = new JPanel (new GridLayout (1, 7, 2, 2)); 569 dayHeaderPanel.setBackground(Color.white); 570 JLabel dayTitleLabel1 = new JLabel ("S", JLabel.RIGHT); 571 dayTitleLabel1.setBackground(Color.white); 572 dayTitleLabel1.setForeground(Color.black); 573 dayHeaderPanel.add(dayTitleLabel1); 574 JLabel dayTitleLabel2 = new JLabel ("M", JLabel.RIGHT); 575 dayTitleLabel2.setBackground(Color.white); 576 dayTitleLabel2.setForeground(Color.black); 577 dayHeaderPanel.add(dayTitleLabel2); 578 JLabel dayTitleLabel3 = new JLabel ("T", JLabel.RIGHT); 579 dayTitleLabel3.setBackground(Color.white); 580 dayTitleLabel3.setForeground(Color.black); 581 dayHeaderPanel.add(dayTitleLabel3); 582 JLabel dayTitleLabel4 = new JLabel ("W", JLabel.RIGHT); 583 dayTitleLabel4.setBackground(Color.white); 584 dayTitleLabel4.setForeground(Color.black); 585 dayHeaderPanel.add(dayTitleLabel4); 586 JLabel dayTitleLabel5 = new JLabel ("T", JLabel.RIGHT); 587 dayTitleLabel5.setBackground(Color.white); 588 dayTitleLabel5.setForeground(Color.black); 589 dayHeaderPanel.add(dayTitleLabel5); 590 JLabel dayTitleLabel6 = new JLabel ("F", JLabel.RIGHT); 591 dayTitleLabel6.setBackground(Color.white); 592 dayTitleLabel6.setForeground(Color.black); 593 dayHeaderPanel.add(dayTitleLabel6); 594 JLabel dayTitleLabel7 = new JLabel ("S", JLabel.RIGHT); 595 dayTitleLabel7.setBackground(Color.white); 596 dayTitleLabel7.setForeground(Color.black); 597 dayHeaderPanel.add(dayTitleLabel7); 598 599 dayPanel = new JPanel (new GridLayout (6, 7, 2, 2)); 600 dayPanel.setBackground(Color.white); 601 dayPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.black)); 602 603 for (int i = 0; i < dayLabel.length; i++) 604 { 605 dayLabel[i] = new JLabel ("0", JLabel.RIGHT); 606 dayLabel[i].setOpaque(true); 607 dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); 608 dayLabel[i].addMouseListener(this); 609 dayPanel.add(dayLabel[i]); 610 } 611 612 updateCalendarDisplay(); 613 614 JPanel buttonPanel = new JPanel (new GridLayout (1, 2, 6, 6)); 615 buttonPanel.setBackground(Color.white); 616 buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 617 todayButton = new JButton (Messages.getString("DateChooser.Today")); 618 todayButton.setMargin(new Insets (2, 2, 2, 2)); 619 todayButton.setFocusPainted(false); 620 todayButton.addActionListener(this); 621 buttonPanel.add(todayButton); 622 623 noneButton = new JButton (Messages.getString("global.None")); 624 noneButton.setMargin(new Insets (2, 2, 2, 2)); 625 noneButton.setFocusPainted(false); 626 noneButton.addActionListener(this); 627 buttonPanel.add(noneButton); 628 629 GridBagLayout bottomPanelGridBag = new GridBagLayout (); 630 JPanel bottomPanel = new JPanel (bottomPanelGridBag); 631 bottomPanel.setBackground(Color.white); 632 bottomPanel.setBorder(BorderFactory.createEmptyBorder(0, 15, 0, 15)); 633 GridBagConstraints bottomPanelConstraints = new GridBagConstraints (); 634 buildConstraints(bottomPanelConstraints, 0, 0, 1, 1, 1, 0); 635 bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL; 636 bottomPanelConstraints.anchor = GridBagConstraints.CENTER; 637 bottomPanelGridBag.setConstraints(dayHeaderPanel, bottomPanelConstraints); 638 bottomPanel.add(dayHeaderPanel); 639 try 640 { 641 String dateString = componentTextField.getText(); 642 if (dateString.trim().length() > 0) 643 { 644 Date dt = returnDateFormatter.parse(dateString); 645 this.setDate(dt); 646 } 647 } 648 catch (java.text.ParseException ex) 649 { 650 DjLogger.log(ex); 651 } 652 653 buildConstraints(bottomPanelConstraints, 0, 1, 1, 1, 1, 0); 654 bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL; 655 bottomPanelConstraints.anchor = GridBagConstraints.CENTER; 656 bottomPanelGridBag.setConstraints(dayPanel, bottomPanelConstraints); 657 bottomPanel.add(dayPanel); 658 659 buildConstraints(bottomPanelConstraints, 0, 2, 1, 1, 0, 0); 660 bottomPanelConstraints.fill = GridBagConstraints.BOTH; 661 bottomPanelGridBag.setConstraints(buttonPanel, bottomPanelConstraints); 662 bottomPanel.add(buttonPanel); 663 664 GridBagLayout mainPanelGridBag = new GridBagLayout (); 665 JPanel mainPanel = new JPanel (mainPanelGridBag); 666 mainPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black)); 668 GridBagConstraints mainPanelConstraints = new GridBagConstraints (); 669 buildConstraints(mainPanelConstraints, 0, 0, 1, 1, 0, 0); 670 mainPanelConstraints.fill = GridBagConstraints.BOTH; 671 mainPanelGridBag.setConstraints(monthPanel, mainPanelConstraints); 672 mainPanel.add(monthPanel); 673 674 buildConstraints(mainPanelConstraints, 0, 1, 1, 1, 1, 0); 675 mainPanelConstraints.fill = GridBagConstraints.HORIZONTAL; 676 mainPanelConstraints.anchor = GridBagConstraints.CENTER; 677 mainPanelGridBag.setConstraints(bottomPanel, mainPanelConstraints); 678 mainPanel.add(bottomPanel); 679 680 Container contentPane = popupCalendar.getContentPane(); 681 contentPane.add(mainPanel, BorderLayout.CENTER); 682 683 popupCalendar.pack(); 684 } 685 686 private void updateCalendarDisplay() 687 { 688 monthButton.setText(monthyearFormatter.format(displayedCalendar.getTime())); 689 690 displayedCalendar.set(Calendar.DAY_OF_MONTH, 1); 691 int firstDayOfDisplayedMonth = displayedCalendar.get(Calendar.DAY_OF_WEEK); 692 int daysInDisplayedMonth = displayedCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); 693 displayedCalendar.roll(Calendar.MONTH, -1); 694 int daysInPrevDisplayedMonth = displayedCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); 695 displayedCalendar.set(Calendar.DAY_OF_MONTH, daysInPrevDisplayedMonth); 696 697 dayMap.clear(); 698 699 if (firstDayOfDisplayedMonth > 1) 700 { 701 for (int i = (firstDayOfDisplayedMonth - 2); i > (-1); i--) 702 { 703 dayMap.put(dayLabel[i], displayedCalendar.getTime()); 704 int thisYear = displayedCalendar.get(Calendar.YEAR); 705 int thisMonth = displayedCalendar.get(Calendar.MONTH); 706 int thisDay = displayedCalendar.get(Calendar.DAY_OF_MONTH); 707 dayLabel[i].setText(String.valueOf(thisDay)); 708 if (thisYear == selectedYear && thisMonth == selectedMonth && thisDay == selectedDay) 709 { 710 dayLabel[i].setBackground(Color.lightGray); 711 dayLabel[i].setForeground(Color.white); 712 } 713 else 714 { 715 dayLabel[i].setBackground(Color.white); 716 dayLabel[i].setForeground(Color.lightGray); 717 } 718 if (thisYear == todaysYear && thisMonth == todaysMonth && thisDay == todaysDay) 719 { 720 dayLabel[i].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black)); 721 } 722 else 723 { 724 dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); 725 } 726 displayedCalendar.roll(Calendar.DAY_OF_MONTH, -1); 727 } 728 } 729 730 displayedCalendar.roll(Calendar.MONTH, 1); 731 displayedCalendar.set(Calendar.DAY_OF_MONTH, 1); 732 733 for (int i = (firstDayOfDisplayedMonth - 1); i < (firstDayOfDisplayedMonth + daysInDisplayedMonth - 1); i++) 734 { 735 dayMap.put(dayLabel[i], displayedCalendar.getTime()); 736 int thisYear = displayedCalendar.get(Calendar.YEAR); 737 int thisMonth = displayedCalendar.get(Calendar.MONTH); 738 int thisDay = displayedCalendar.get(Calendar.DAY_OF_MONTH); 739 dayLabel[i].setText(String.valueOf(thisDay)); 740 dayLabel[i].setForeground(Color.black); 741 if (thisYear == selectedYear && thisMonth == selectedMonth && thisDay == selectedDay) 742 { 743 dayLabel[i].setBackground(Color.lightGray); 744 dayLabel[i].setForeground(Color.white); 745 } 746 else 747 { 748 dayLabel[i].setBackground(Color.white); 749 dayLabel[i].setForeground(Color.black); 750 } 751 if (thisYear == todaysYear && thisMonth == todaysMonth && thisDay == todaysDay) 752 { 753 dayLabel[i].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black)); 754 } 755 else 756 { 757 dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); 758 } 759 displayedCalendar.roll(Calendar.DAY_OF_MONTH, 1); 760 } 761 762 displayedCalendar.roll(Calendar.MONTH, 1); 763 displayedCalendar.set(Calendar.DAY_OF_MONTH, 1); 764 765 for (int i = (firstDayOfDisplayedMonth + daysInDisplayedMonth - 1); i < dayLabel.length; i++) 766 { 767 dayMap.put(dayLabel[i], displayedCalendar.getTime()); 768 int thisYear = displayedCalendar.get(Calendar.YEAR); 769 int thisMonth = displayedCalendar.get(Calendar.MONTH); 770 int thisDay = displayedCalendar.get(Calendar.DAY_OF_MONTH); 771 dayLabel[i].setText(String.valueOf(thisDay)); 772 dayLabel[i].setForeground(Color.lightGray); 773 if (thisYear == selectedYear && thisMonth == selectedMonth && thisDay == selectedDay) 774 { 775 dayLabel[i].setBackground(Color.lightGray); 776 dayLabel[i].setForeground(Color.white); 777 } 778 else 779 { 780 dayLabel[i].setBackground(Color.white); 781 dayLabel[i].setForeground(Color.lightGray); 782 } 783 if (thisYear == todaysYear && thisMonth == todaysMonth && thisDay == todaysDay) 784 { 785 dayLabel[i].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black)); 786 } 787 else 788 { 789 dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); 790 } 791 displayedCalendar.roll(Calendar.DAY_OF_MONTH, 1); 792 } 793 displayedCalendar.roll(Calendar.MONTH, -1); 794 displayedCalendar.set(Calendar.DAY_OF_MONTH, 1); 795 } 796 797 private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) 798 { 799 gbc.gridx = gx; 800 gbc.gridy = gy; 801 gbc.gridwidth = gw; 802 gbc.gridheight = gh; 803 gbc.weightx = wx; 804 gbc.weighty = wy; 805 } 806 807 public void selectAll() 808 { 809 componentTextField.selectAll(); 810 } 811 812 public void clear() 813 { 814 componentTextField.setText(""); 815 } 816 817 public void keyPressed(KeyEvent e) 818 { 819 } 820 821 public void keyReleased(KeyEvent e) 822 { 823 try 824 { 825 if (!componentTextField.getText().equals("")) 826 { 827 Date dt = returnDateFormatter.parse(componentTextField.getText()); 828 setDateEx(dt); 829 } 830 } 831 catch (java.text.ParseException ex) 832 { 833 834 } 835 } 836 837 public void keyTyped(KeyEvent e) 838 { 839 840 } 841 842 public JTextField getTextField() 843 { 844 return componentTextField; 845 } 846 847 } | Popular Tags |