1 package com.ca.directory.jxplorer.editor; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.directory.jxplorer.HelpIDs; 5 6 import javax.swing.*; 7 import javax.swing.border.TitledBorder ; 8 import javax.swing.event.DocumentEvent ; 9 import javax.swing.event.DocumentListener ; 10 import javax.swing.text.AttributeSet ; 11 import javax.swing.text.BadLocationException ; 12 import javax.swing.text.Document ; 13 import javax.swing.text.PlainDocument ; 14 import java.awt.*; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.ActionListener ; 17 import java.text.NumberFormat ; 18 import java.text.ParseException ; 19 import java.util.Calendar ; 20 import java.util.Locale ; 21 import java.util.logging.Logger ; 22 import java.util.logging.Level ; 23 24 67 public class generalizedtimeeditor extends CBDialog 68 implements abstractstringeditor 69 { 70 73 private static final int FEBRUARY = 2; 74 75 78 private String months[] = {CBIntText.get("--"), CBIntText.get("January"), CBIntText.get("February"), 79 CBIntText.get("March"), CBIntText.get("April"), CBIntText.get("May"), 80 CBIntText.get("June"), CBIntText.get("July"), CBIntText.get("August"), 81 CBIntText.get("September"), CBIntText.get("October"), 82 CBIntText.get("November"), CBIntText.get("December")}; 83 84 87 private int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 88 89 92 private Calendar rightNow = Calendar.getInstance(); 93 94 97 private editablestring editableString = null; 98 99 102 private CBButton btnNow = new CBButton(CBIntText.get("Now"), CBIntText.get("Display the present date & time.")); 103 104 107 private JCheckBox checkBox = new JCheckBox(CBIntText.get("UTC")); 108 109 112 private JLabel yearLabel = new JLabel(CBIntText.get("Year:")); 113 114 117 private WholeNumberField yearTextField = new WholeNumberField(2001, 4); 119 122 private JLabel monthLabel = new JLabel(CBIntText.get("Month:")); 123 124 127 private CBJComboBox monthCombo = new CBJComboBox(); 128 129 132 private JLabel dayLabel = new JLabel(CBIntText.get("Day:")); 133 134 137 private CBJComboBox dayCombo = new CBJComboBox(); 138 139 142 private JLabel hourLabel = new JLabel(CBIntText.get("Hour:")); 143 144 147 private CBJComboBox hourCombo = new CBJComboBox(); 148 149 152 private JLabel minuteLabel = new JLabel(CBIntText.get("Minute:")); 153 154 157 private CBJComboBox minuteCombo = new CBJComboBox(); 158 159 162 private JLabel secondLabel = new JLabel(CBIntText.get("Second:")); 163 164 167 private CBJComboBox secondCombo = new CBJComboBox(); 168 169 172 private JLabel milliSecondLabel = new JLabel(CBIntText.get("Millisecond:")); 173 174 177 private CBJComboBox milliSecondCombo = new CBJComboBox(); 178 179 183 private int userMonth; 184 185 189 private int userYear; 190 191 195 private int userDay; 196 197 201 private int userHour; 202 203 207 private int userMinute; 208 209 213 private int userSecond; 214 215 219 private int userMilliSecond; 220 221 225 private int numDays = 0; 226 227 230 private int dateIndexBackup = 0; 231 232 235 private String value = null; 236 237 241 private boolean isTableEditor = true; 242 243 246 private String time = ""; 247 248 249 private final static Logger log = Logger.getLogger(generalizedtimeeditor.class.getName()); 250 251 259 public generalizedtimeeditor(Frame owner, String value, boolean isTableEditor) 260 { 261 super(owner, CBIntText.get("Generalized Time"), HelpIDs.ATTR_TIME); 262 263 this.isTableEditor = isTableEditor; 264 this.value = value; 265 266 display.addln(new JLabel(" ")); 268 display.makeLight(); 269 270 initDateTimeVariables(); 273 274 initMilliSeconds(); 276 initSeconds(); 277 initMinutes(); 278 initHours(); 279 initDays(31); 280 initMonths(); 281 282 if (value.indexOf("Z") != -1) 284 checkBox.setSelected(true); 285 checkBox.setToolTipText(CBIntText.get("Adds a 'Z' to the end of the generalized time.")); 286 287 btnNow.addActionListener(new ActionListener () 289 { 290 public void actionPerformed(ActionEvent e) 291 { 292 displayCurrent(); 293 } 294 }); 295 296 299 monthCombo.addActionListener(new ActionListener () 300 { 301 public void actionPerformed(ActionEvent e) 302 { 303 updateDayCombo(); 304 } 305 }); 306 307 MyDocumentListener myDocumentListener = new MyDocumentListener(); 310 yearTextField.getDocument().addDocumentListener(myDocumentListener); 311 312 CBPanel dateTimePanel = new CBPanel(); 314 315 dateTimePanel.add(dayLabel); 317 dateTimePanel.add(dayCombo); 318 319 dateTimePanel.add(monthLabel); 321 dateTimePanel.addWide(monthCombo, 2); 322 323 dateTimePanel.add(yearLabel); 325 dateTimePanel.addWide(yearTextField, 2); 326 dateTimePanel.addln(new JLabel(" ")); 327 dateTimePanel.addln(new JLabel(" ")); 328 dateTimePanel.newLine(); 329 330 dateTimePanel.add(hourLabel); 332 dateTimePanel.add(hourCombo); 333 334 dateTimePanel.add(minuteLabel); 336 dateTimePanel.add(minuteCombo); 337 dateTimePanel.add(new JLabel(" ")); 338 339 dateTimePanel.add(secondLabel); 341 dateTimePanel.add(secondCombo); 342 dateTimePanel.add(new JLabel(" ")); 343 344 dateTimePanel.add(milliSecondLabel); 346 dateTimePanel.add(milliSecondCombo); 347 348 dateTimePanel.setBorder(new TitledBorder (CBIntText.get(" Date/Time "))); 350 351 CBPanel btnPanel = new CBPanel(); 353 btnPanel.makeLight(); 354 btnPanel.addln(btnNow); 355 btnPanel.addln(checkBox); 356 357 setSelectedIndexes(); 359 360 display.makeHeavy(); 362 display.add(dateTimePanel); 363 display.makeHigh(); 364 display.add(btnPanel); 365 display.newLine(); 366 display.makeHeavy(); 367 368 display.addln(new JLabel(" ")); 370 371 setSize(500, 200); 372 373 numDays = getDaysInMonth(userMonth); 375 } 376 377 389 protected void initDateTimeVariables() 390 { 391 int size = 0; 392 393 try 394 { 395 size = value.length(); 396 } 397 catch (Exception e) 398 { 399 size = 0; 400 } 401 402 try 403 { 404 userMonth = Integer.parseInt(value.substring(4, 6)); 406 407 userYear = Integer.parseInt(value.substring(0, 4)); 409 410 userDay = Integer.parseInt(value.substring(6, 8)); 412 413 userHour = Integer.parseInt(value.substring(8, 10)); 415 416 userMinute = Integer.parseInt(value.substring(10, 12)); 418 419 if (size > 13) 421 userSecond = Integer.parseInt(value.substring(12, 14)); 422 423 if (value.indexOf(".") == 14) 425 { 426 int zIndex = value.indexOf("Z"); 428 429 String milli = null; 430 431 if (zIndex == 16) 432 milli = value.substring(15, 16) + "00"; else if (zIndex == 17) 434 milli = value.substring(15, 17) + "0"; else if (size == 18 || zIndex == 18) 436 milli = value.substring(15, 18); else if (size == 17) 438 milli = value.substring(15, 17) + "0"; else if (size == 16) 440 milli = value.substring(15, 16) + "00"; 442 userMilliSecond = Integer.parseInt(milli); 443 } 444 } 445 catch (Exception e) 446 { 447 userMonth = 0; 448 userYear = 0; 449 userDay = 0; 450 userHour = 0; 451 userMinute = 0; 452 userSecond = 0; 453 userMilliSecond = 0; 454 } 455 } 456 457 465 protected int getDaysInMonth(int month) 466 { 467 return daysInMonth[month] + ((isLeapYear(getUserYear()) && 468 (userMonth == FEBRUARY)) ? 1 : 0); 469 } 470 471 474 protected void displayCurrent() 475 { 476 rightNow = Calendar.getInstance(); 478 479 userYear = rightNow.get(Calendar.YEAR); 480 481 userMonth = rightNow.get(Calendar.MONTH) + 1; 483 userDay = rightNow.get(Calendar.DAY_OF_MONTH); 484 userHour = rightNow.get(Calendar.HOUR_OF_DAY); 485 userMinute = rightNow.get(Calendar.MINUTE); 486 userSecond = rightNow.get(Calendar.SECOND); 487 userMilliSecond = rightNow.get(Calendar.MILLISECOND); 488 489 yearTextField.setText(String.valueOf(userYear)); 490 monthCombo.setSelectedIndex(userMonth); 491 dayCombo.setSelectedIndex(userDay); 492 hourCombo.setSelectedIndex(userHour); 493 minuteCombo.setSelectedIndex(userMinute); 494 secondCombo.setSelectedIndex(userSecond); 495 milliSecondCombo.setSelectedIndex(userMilliSecond); 496 } 497 498 502 protected void setSelectedIndexes() 503 { 504 yearTextField.setText(String.valueOf(userYear)); 506 507 if (userMonth <= 13) 509 monthCombo.setSelectedIndex(userMonth); 510 else 511 userMonth = 0; 512 513 if (userDay <= getDaysInMonth(userMonth)) 515 dayCombo.setSelectedIndex(userDay); 516 517 if (userHour <= 23) 519 hourCombo.setSelectedIndex(userHour); 520 521 if (userMinute <= 59) 523 minuteCombo.setSelectedIndex(userMinute); 524 525 if (userSecond <= 59) 527 secondCombo.setSelectedIndex(userSecond); 528 529 if (userMilliSecond <= 999) 531 milliSecondCombo.setSelectedIndex(userMilliSecond); 532 } 533 534 537 protected void initMilliSeconds() 538 { 539 for (int i = 0; i < 1000; i++) 540 milliSecondCombo.addItem(new Integer (i)); 541 } 542 543 546 protected void initSeconds() 547 { 548 for (int i = 0; i < 60; i++) 549 secondCombo.addItem(new Integer (i)); 550 } 551 552 555 protected void initMinutes() 556 { 557 for (int i = 0; i < 60; i++) 558 minuteCombo.addItem(new Integer (i)); 559 } 560 561 564 protected void initHours() 565 { 566 for (int i = 0; i < 24; i++) 567 hourCombo.addItem(new Integer (i)); 568 } 569 570 575 protected void initDays(int days) 576 { 577 for (int i = 0; i <= days; i++) 578 { 579 if (i == 0) 580 dayCombo.addItem("--"); 581 else 582 dayCombo.addItem(new Integer (i)); 583 } 584 } 585 586 590 protected void initMonths() 591 { 592 for (int i = 0; i < 13; i++) 593 monthCombo.addItem(months[i]); 594 } 595 596 601 protected void updateDayCombo() 602 { 603 int dayPos = dayCombo.getSelectedIndex(); 605 606 userMonth = monthCombo.getSelectedIndex(); 608 609 dayCombo.removeAllItems(); 611 612 numDays = getDaysInMonth(userMonth); 614 615 initDays(numDays); 617 618 if (dayPos > -1) 619 { 620 setDayComboSelectedIndex(dayPos); 621 622 dateIndexBackup = dayPos; 624 } 625 else 626 { 627 setDayComboSelectedIndex(dateIndexBackup); 629 } 630 631 dayCombo.revalidate(); 632 } 633 634 640 protected int getUserYear() 641 { 642 int userYearInt = 0; 643 644 try 645 { 646 userYearInt = Integer.parseInt(yearTextField.getText(), 10); 648 } 649 catch (Exception e) 650 { 651 if (!(yearTextField.getText().length() == 0)) 653 JOptionPane.showMessageDialog(this, CBIntText.get("Please enter a valid year."), 654 CBIntText.get("Invalid Year"), JOptionPane.INFORMATION_MESSAGE); 655 userYear = 0; 656 yearTextField.setText(String.valueOf(userYear)); 657 } 658 659 if (userYearInt > 1581) 660 userYear = userYearInt; 661 662 return userYear; 663 } 664 665 671 protected void setDayComboSelectedIndex(int index) 672 { 673 try 674 { 675 dayCombo.setSelectedIndex(index); 677 } 678 catch (Exception e) 679 { 680 int items = dayCombo.getItemCount(); 681 682 dayCombo.setSelectedIndex(items - 1); 683 } 684 } 685 686 691 public boolean isLeapYear(int year) 692 { 693 if ((year % 100) == 0) 695 return ((year % 400) == 0); 696 697 return ((year % 4) == 0); 699 } 700 701 708 protected String getSelectedYear() 709 { 710 String year = yearTextField.getText(); 711 712 int len = year.length(); 713 714 switch (len) 715 { 716 case 0: 717 year = "0000"; 718 break; 719 case 1: 720 year = "000" + year; 721 break; 722 case 2: 723 year = "00" + year; 724 break; 725 case 3: 726 year = "0" + year; 727 break; 728 case 4: 729 break; default: 731 year = year.substring(0, 4); 732 } 733 return year; 734 } 735 736 740 protected String getSelectedMonth() 741 { 742 int selection = monthCombo.getSelectedIndex(); 743 744 String month = Integer.toString(selection); 745 746 if (selection < 10) 747 month = "0" + month; 748 749 return month; 750 } 751 752 756 protected String getSelectedDay() 757 { 758 int selection = dayCombo.getSelectedIndex(); 759 760 String day = Integer.toString(selection); 761 762 if (selection < 10) 763 day = "0" + day; 764 765 return day; 766 } 767 768 772 protected String getSelectedHour() 773 { 774 int selection = hourCombo.getSelectedIndex(); 775 776 String hour = Integer.toString(selection); 777 778 if (selection < 10) 779 hour = "0" + hour; 780 781 return hour; 782 } 783 784 788 protected String getSelectedMinute() 789 { 790 int selection = minuteCombo.getSelectedIndex(); 791 792 String minute = Integer.toString(selection); 793 794 if (selection < 10) 795 minute = "0" + minute; 796 797 return minute; 798 } 799 800 804 protected String getSelectedSecond() 805 { 806 int selection = secondCombo.getSelectedIndex(); 807 808 String second = Integer.toString(selection); 809 810 if (selection < 10) 811 second = "0" + second; 812 813 return second; 814 } 815 816 820 protected String getSelectedMilliSecond() 821 { 822 int selection = milliSecondCombo.getSelectedIndex(); 823 824 if (selection == 0) 828 return ""; 829 830 String milliSecond = Integer.toString(selection); 831 832 if (selection < 10) 833 milliSecond = "00" + milliSecond; 834 835 if (selection < 100 && selection > 9) 836 milliSecond = "0" + milliSecond; 837 838 return "." + milliSecond; 839 } 840 841 845 public void setStringValue(editablestring editString) 846 { 847 editableString = editString; 848 } 849 850 854 public void doOK() 855 { 856 String date = getSelectedYear() + getSelectedMonth() + 857 getSelectedDay() + getSelectedHour() + getSelectedMinute() + 858 getSelectedSecond() + getSelectedMilliSecond(); 859 860 if (checkBox.isSelected()) 862 date = date + "Z"; 863 864 if (isTableEditor) 865 editableString.setStringValue(date); 867 else 868 time = date; 869 870 setVisible(false); 871 dispose(); 872 } 873 874 878 public void doCancel() 879 { 880 if (!isTableEditor) 881 time = value; 882 super.doCancel(); 883 } 884 885 892 public String getTime() 893 { 894 return time; 895 } 896 897 903 class MyDocumentListener implements DocumentListener 904 { 905 908 public void insertUpdate(DocumentEvent e) 909 { 910 checkDate(e); 911 } 912 913 916 public void removeUpdate(DocumentEvent e) 917 { 918 checkDate(e); 919 } 920 921 public void changedUpdate(DocumentEvent e) 922 { 923 924 } 925 926 930 private void checkDate(DocumentEvent e) 931 { 932 try 933 { 934 int index = monthCombo.getSelectedIndex(); 935 936 if (index == 2) 939 updateDayCombo(); 940 } 941 catch (Exception ee) 942 { 943 log.log(Level.WARNING, "Problem getting the selected month from the drop down box in the Generalized Time editor. ", e); 944 } 945 } 946 } 947 948 952 class WholeNumberField extends JTextField 953 { 954 private NumberFormat integerFormatter; 955 956 public WholeNumberField(int value, int columns) 957 { 958 super(columns); 959 integerFormatter = NumberFormat.getNumberInstance(Locale.getDefault()); 960 integerFormatter.setParseIntegerOnly(true); 961 integerFormatter.setGroupingUsed(false); 963 setValue(value); 964 } 965 966 public int getValue() 967 { 968 int retVal = 0; 969 try 970 { 971 retVal = integerFormatter.parse(getText()).intValue(); 972 } 973 catch (ParseException e) 974 { 975 } 978 return retVal; 979 } 980 981 public void setValue(int value) 982 { 983 setText(integerFormatter.format(value)); 984 } 985 986 protected Document createDefaultModel() 987 { 988 return new WholeNumberDocument(); 989 } 990 991 protected class WholeNumberDocument extends PlainDocument 992 { 993 public void insertString(int offs, String str, AttributeSet a) 994 throws BadLocationException 995 { 996 char[] source = str.toCharArray(); 997 char[] result = new char[source.length]; 998 int j = 0; 999 1000 for (int i = 0; i < result.length; i++) 1001 { 1002 if (Character.isDigit(source[i])) 1003 result[j++] = source[i]; 1004 else 1005 log.warning("Invalid data, you can't enter '" + source[i] + "' (from " + str + ") into a year field."); 1006 } 1007 super.insertString(offs, new String (result, 0, j), a); 1008 } 1009 } 1010 } 1011} | Popular Tags |