KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > editor > generalizedtimeeditor


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 JavaDoc;
8 import javax.swing.event.DocumentEvent JavaDoc;
9 import javax.swing.event.DocumentListener JavaDoc;
10 import javax.swing.text.AttributeSet JavaDoc;
11 import javax.swing.text.BadLocationException JavaDoc;
12 import javax.swing.text.Document JavaDoc;
13 import javax.swing.text.PlainDocument JavaDoc;
14 import java.awt.*;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.text.NumberFormat JavaDoc;
18 import java.text.ParseException JavaDoc;
19 import java.util.Calendar JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.logging.Logger JavaDoc;
22 import java.util.logging.Level JavaDoc;
23
24 /**
25  * The GeneralizedTimeEditor is used for editing generalizedTime attributes within the
26  * directory. It is initiated when the user clicks on a generalizedTime attribute in
27  * the Table Editor of JXplorer or when the user selects a generalizedTime attribute in
28  * the search dialog.
29  * <p/>
30  * The GeneralizedTimeEditor displays a date/time dialog with combo boxes for the following:
31  * <p/>
32  * Milli seconds (possible values: 0-999).
33  * Seconds of the minute (possible values: 0-59).
34  * Minutes of the hour (possible values: 0-59).
35  * Hour of the day (possible values: 0-23).
36  * Day of the month (possible values: --, 1-31).
37  * Month of the year (possible values: --, January - Decemeber inclusive).
38  * <p/>
39  * Note: it is possible to enter the following generalizedTime 000000000000, this is why a value of -- (0) is
40  * included in day of the month and month of the year combos.
41  * <p/>
42  * The year is displayed and edited through a restricted (whole number) field. Year can only be of 4 characters.
43  * The UTC (or GTM or zulu) is selected via a check box.
44  * <p/>
45  * This class follows the generalizedTime format of year-month-day-hour-minute (yyyymmddhhmm) as manditory.
46  * Optional values in the generalizedTime are second-millisecond-UTC (ss.mmmZ). Milliseconds require seconds
47  * to be added. Examples of a correct generalizeTime are
48  * <P>
49  * 20011231235959.999Z
50  * 20011231235959.999
51  * 20011231235959.99Z
52  * 20011231235959.99
53  * 20011231235959.9Z
54  * 20011231235959.9
55  * 19960229235959
56  * 19960229235959Z
57  * 199702282359
58  * 199702282359Z
59  * <p/>
60  * The dialog has a 'Now' button for displaying the current date/time of the user's machine.
61  * <p/>
62  * The dialog enforces date constraints such as leap years. For example if a leap year is entered in the
63  * year field and february is selected, the days displayed will be 0-29. The correct days for the month that
64  * is selected will be displayed also. For example if April is selected the days will be 0-30.
65  * @author Trudi.
66  */

67 public class generalizedtimeeditor extends CBDialog
68         implements abstractstringeditor
69 {
70     /**
71      * Index of feb.
72      */

73     private static final int FEBRUARY = 2;
74
75     /**
76      * List of months (first element is '--').
77      */

78     private String JavaDoc 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     /**
85      * Max days in a month, in order of months.
86      */

87     private int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
88
89     /**
90      * The date and time right now.
91      */

92     private Calendar JavaDoc rightNow = Calendar.getInstance();
93
94     /**
95      * The time value (from the directory).
96      */

97     private editablestring editableString = null;
98
99     /**
100      * The Button that when clicked loads the current date and time into the display.
101      */

102     private CBButton btnNow = new CBButton(CBIntText.get("Now"), CBIntText.get("Display the present date & time."));
103
104     /**
105      * If checked will add a 'Z' to the generalized time (20010918235959.999Z).
106      */

107     private JCheckBox checkBox = new JCheckBox(CBIntText.get("UTC"));
108
109     /**
110      * Year label.
111      */

112     private JLabel yearLabel = new JLabel(CBIntText.get("Year:"));
113
114     /**
115      * Year field.
116      */

117     private WholeNumberField yearTextField = new WholeNumberField(2001, 4); // was 5 columns? -CB
118

119     /**
120      * Month label.
121      */

122     private JLabel monthLabel = new JLabel(CBIntText.get("Month:"));
123
124     /**
125      * Month combo box.
126      */

127     private CBJComboBox monthCombo = new CBJComboBox();
128
129     /**
130      * Day label.
131      */

132     private JLabel dayLabel = new JLabel(CBIntText.get("Day:"));
133
134     /**
135      * Day combo box.
136      */

137     private CBJComboBox dayCombo = new CBJComboBox();
138
139     /**
140      * Hour label.
141      */

142     private JLabel hourLabel = new JLabel(CBIntText.get("Hour:"));
143
144     /**
145      * Hour combo box.
146      */

147     private CBJComboBox hourCombo = new CBJComboBox();
148
149     /**
150      * Minute label.
151      */

152     private JLabel minuteLabel = new JLabel(CBIntText.get("Minute:"));
153
154     /**
155      * Minuet combo box.
156      */

157     private CBJComboBox minuteCombo = new CBJComboBox();
158
159     /**
160      * Second label.
161      */

162     private JLabel secondLabel = new JLabel(CBIntText.get("Second:"));
163
164     /**
165      * Second combo box.
166      */

167     private CBJComboBox secondCombo = new CBJComboBox();
168
169     /**
170      * Millisecond label.
171      */

172     private JLabel milliSecondLabel = new JLabel(CBIntText.get("Millisecond:"));
173
174     /**
175      * Millisecond combo box.
176      */

177     private CBJComboBox milliSecondCombo = new CBJComboBox();
178
179     /**
180      * Month as entered by the user.
181      * Possible values: 0-12.
182      */

183     private int userMonth;
184
185     /**
186      * Year as entered by the user.
187      * Example: 2001.
188      */

189     private int userYear;
190
191     /**
192      * Date as entered by the user.
193      * Possible values: 0-31.
194      */

195     private int userDay;
196
197     /**
198      * Hour as entered by the user.
199      * Possible values: 0-23.
200      */

201     private int userHour;
202
203     /**
204      * Minute as entered by the user.
205      * Possible values: 0-59.
206      */

207     private int userMinute;
208
209     /**
210      * Second as entered by the user.
211      * Possible values: 0-59.
212      */

213     private int userSecond;
214
215     /**
216      * Millisecond as entered by the user.
217      * Possible values: 0-999.
218      */

219     private int userMilliSecond;
220
221     /**
222      * Number of days in month.
223      * Possible values: 0, 28, 29, 30 or 31.
224      */

225     private int numDays = 0;
226
227     /**
228      * A backup of the selected index in the date combo.
229      */

230     private int dateIndexBackup = 0;
231
232     /**
233      * Attribute value as a string.
234      */

235     private String JavaDoc value = null;
236
237     /**
238      * This class is normally used by the table editor. If used elsewhere
239      * set this to false and get the time value from the getTime() method.
240      */

241     private boolean isTableEditor = true;
242
243     /**
244      * Used if isTableEditor = false to return the set time via getTime().
245      */

246     private String JavaDoc time = "";
247
248
249     private final static Logger JavaDoc log = Logger.getLogger(generalizedtimeeditor.class.getName());
250
251     /**
252      * Sets up the dialog components, displaying the supplied attribute value. If there is
253      * no value to display, the fields are all set to zero.
254      * @param owner the parent frame, usually JXplorer.
255      * @param value the value of the generalized time attribute.
256      * @param isTableEditor this class is normally used by the table editor.
257      * If used elsewhere set this to false and get the time value from the getTime() method.
258      */

259     public generalizedtimeeditor(Frame owner, String JavaDoc 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         // Padding at top...
267
display.addln(new JLabel(" "));
268         display.makeLight();
269
270         // Initiate the date and time (integer) variables with the
271
// attribute values (or 0 if value is not present)...
272
initDateTimeVariables();
273
274         // Populate the combos...
275
initMilliSeconds();
276         initSeconds();
277         initMinutes();
278         initHours();
279         initDays(31);
280         initMonths();
281
282         // Sets the state of the check box & sets the tooltip of the check box...
283
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         // Adds an action listener to the 'Now' button...
288
btnNow.addActionListener(new ActionListener JavaDoc()
289         {
290             public void actionPerformed(ActionEvent JavaDoc e)
291             {
292                 displayCurrent();
293             }
294         });
295
296         /**
297          * Listener for month changes. Updates the day combo depending on month selected.
298          */

299         monthCombo.addActionListener(new ActionListener JavaDoc()
300         {
301             public void actionPerformed(ActionEvent JavaDoc e)
302             {
303                 updateDayCombo();
304             }
305         });
306
307         // Adds a document listener on the year field to do a leap year
308
// check if feb is selected in month combo...
309
MyDocumentListener myDocumentListener = new MyDocumentListener();
310         yearTextField.getDocument().addDocumentListener(myDocumentListener);
311
312         // New panel for drop down boxes...
313
CBPanel dateTimePanel = new CBPanel();
314
315         // Add DAY components...
316
dateTimePanel.add(dayLabel);
317         dateTimePanel.add(dayCombo);
318
319         // Add MONTH components...
320
dateTimePanel.add(monthLabel);
321         dateTimePanel.addWide(monthCombo, 2);
322
323         // Add YEAR components...
324
dateTimePanel.add(yearLabel);
325         dateTimePanel.addWide(yearTextField, 2);
326         dateTimePanel.addln(new JLabel(" "));
327         dateTimePanel.addln(new JLabel(" "));
328         dateTimePanel.newLine();
329
330         // Add HOUR components...
331
dateTimePanel.add(hourLabel);
332         dateTimePanel.add(hourCombo);
333
334         // Add MINUTE components...
335
dateTimePanel.add(minuteLabel);
336         dateTimePanel.add(minuteCombo);
337         dateTimePanel.add(new JLabel(" "));
338
339         // Add SECOND components...
340
dateTimePanel.add(secondLabel);
341         dateTimePanel.add(secondCombo);
342         dateTimePanel.add(new JLabel(" "));
343
344         // Add MILLISECOND components...
345
dateTimePanel.add(milliSecondLabel);
346         dateTimePanel.add(milliSecondCombo);
347
348         // Add border...
349
dateTimePanel.setBorder(new TitledBorder JavaDoc(CBIntText.get(" Date/Time ")));
350
351         // Add 'Now' button and a check box to a new panel...
352
CBPanel btnPanel = new CBPanel();
353         btnPanel.makeLight();
354         btnPanel.addln(btnNow);
355         btnPanel.addln(checkBox);
356
357         // Set selected indexes and field text...
358
setSelectedIndexes();
359
360         // Add panels to main display panel...
361
display.makeHeavy();
362         display.add(dateTimePanel);
363         display.makeHigh();
364         display.add(btnPanel);
365         display.newLine();
366         display.makeHeavy();
367
368         // Padding at bottom...
369
display.addln(new JLabel(" "));
370
371         setSize(500, 200);
372
373         // Get the number of days in the selected month...
374
numDays = getDaysInMonth(userMonth);
375     }
376
377     /**
378      * Initialises the date and time variables depending on the current attribute value.
379      * For example: 20010920235959.999 would display...
380      * year - 2001
381      * month - September
382      * day - 20
383      * hour - 23 (11pm)
384      * minute - 59
385      * second - 59
386      * millisecond - 999
387      * If there is no value the date and time variables default to zero (0).
388      */

389     protected void initDateTimeVariables()
390     {
391         int size = 0;
392
393         try
394         {
395             size = value.length();
396         }
397         catch (Exception JavaDoc e)
398         {
399             size = 0;
400         }
401
402         try
403         {
404             // Init month...
405
userMonth = Integer.parseInt(value.substring(4, 6));
406
407             // Init year...
408
userYear = Integer.parseInt(value.substring(0, 4));
409
410             // Init day...
411
userDay = Integer.parseInt(value.substring(6, 8));
412
413             // Init hour...
414
userHour = Integer.parseInt(value.substring(8, 10));
415
416             // Init minute...
417
userMinute = Integer.parseInt(value.substring(10, 12));
418
419             // Init second...
420
if (size > 13)
421                 userSecond = Integer.parseInt(value.substring(12, 14));
422
423             // Init milli-seconds...
424
if (value.indexOf(".") == 14)
425             {
426                 // Where is the Z?...
427
int zIndex = value.indexOf("Z");
428
429                 String JavaDoc milli = null;
430
431                 if (zIndex == 16)
432                     milli = value.substring(15, 16) + "00"; // .9Z.
433
else if (zIndex == 17)
434                     milli = value.substring(15, 17) + "0"; // .99Z.
435
else if (size == 18 || zIndex == 18)
436                     milli = value.substring(15, 18); // .999 or .999Z.
437
else if (size == 17)
438                     milli = value.substring(15, 17) + "0"; // .99.
439
else if (size == 16)
440                     milli = value.substring(15, 16) + "00"; // .9.
441

442                 userMilliSecond = Integer.parseInt(milli);
443             }
444         }
445         catch (Exception JavaDoc 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     /**
458      * Returns the days in the month that is supplied.
459      * @param month an integer representation of the month
460      * for example: 1 = Jan, 2 = Feb...12 = Dec.
461      * 0 = an unspecified month which has 0 days.
462      * @return the days in the month for example:
463      * Jan = 31, Feb = 28 (29 if leap) etc.
464      */

465     protected int getDaysInMonth(int month)
466     {
467         return daysInMonth[month] + ((isLeapYear(getUserYear()) &&
468                 (userMonth == FEBRUARY)) ? 1 : 0);
469     }
470
471     /**
472      * Gets the current date and time from the user's machine and displays it.
473      */

474     protected void displayCurrent()
475     {
476         // Get the date and time of right now...
477
rightNow = Calendar.getInstance();
478
479         userYear = rightNow.get(Calendar.YEAR);
480
481         // Jan is meant to be 1 but is it actually is 0 and so on dec = 11...
482
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     /**
499      * Sets the index of the combo boxes and the year field to the already initiated
500      * date an time variables.
501      */

502     protected void setSelectedIndexes()
503     {
504         // Year...
505
yearTextField.setText(String.valueOf(userYear));
506
507         // Month...
508
if (userMonth <= 13)
509             monthCombo.setSelectedIndex(userMonth);
510         else
511             userMonth = 0;
512
513         // Day...
514
if (userDay <= getDaysInMonth(userMonth))
515             dayCombo.setSelectedIndex(userDay);
516
517         // Hour...
518
if (userHour <= 23)
519             hourCombo.setSelectedIndex(userHour);
520
521         // Minute...
522
if (userMinute <= 59)
523             minuteCombo.setSelectedIndex(userMinute);
524
525         // Second...
526
if (userSecond <= 59)
527             secondCombo.setSelectedIndex(userSecond);
528
529         // Millisecond...
530
if (userMilliSecond <= 999)
531             milliSecondCombo.setSelectedIndex(userMilliSecond);
532     }
533
534     /**
535      * Initialises the milli-second combo with integers from 0-999.
536      */

537     protected void initMilliSeconds()
538     {
539         for (int i = 0; i < 1000; i++)
540             milliSecondCombo.addItem(new Integer JavaDoc(i));
541     }
542
543     /**
544      * Initialises the second combo with integers from 0-59.
545      */

546     protected void initSeconds()
547     {
548         for (int i = 0; i < 60; i++)
549             secondCombo.addItem(new Integer JavaDoc(i));
550     }
551
552     /**
553      * Initialises the minute combo with integers from 0-59.
554      */

555     protected void initMinutes()
556     {
557         for (int i = 0; i < 60; i++)
558             minuteCombo.addItem(new Integer JavaDoc(i));
559     }
560
561     /**
562      * Initialises the hour combo with integers from 0-23.
563      */

564     protected void initHours()
565     {
566         for (int i = 0; i < 24; i++)
567             hourCombo.addItem(new Integer JavaDoc(i));
568     }
569
570     /**
571      * Initialises the second combo with integers from 0-31, 0-30, 0-29 or 0-28
572      * depending on the number of days supplied.
573      * @param days the maximum number of days to display. Can be 0, 28, 29, 30 ,31.
574      */

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 JavaDoc(i));
583         }
584     }
585
586     /**
587      * Initialises the month combo with months of the year.
588      * For example: --, January, Febuarary...December.
589      */

590     protected void initMonths()
591     {
592         for (int i = 0; i < 13; i++)
593             monthCombo.addItem(months[i]);
594     }
595
596     /**
597      * Re-populates the day combo to reflect the number of
598      * days of the month that is selected in the month combo.
599      * Accounts for leap year also.
600      */

601     protected void updateDayCombo()
602     {
603         // Remember the selected position in the day combo...
604
int dayPos = dayCombo.getSelectedIndex();
605
606         // Get the index of the selected month...
607
userMonth = monthCombo.getSelectedIndex();
608
609         // Empty the day combo...
610
dayCombo.removeAllItems();
611
612         // Get the days in the month (account for leap year)...
613
numDays = getDaysInMonth(userMonth);
614
615         // Add the days to the day combo...
616
initDays(numDays);
617
618         if (dayPos > -1)
619         {
620             setDayComboSelectedIndex(dayPos);
621
622             // Make a backup of the last day index incase something goes wrong......
623
dateIndexBackup = dayPos;
624         }
625         else
626         {
627             // Something has gone wrong so set the day combo to the backup index...
628
setDayComboSelectedIndex(dateIndexBackup);
629         }
630
631         dayCombo.revalidate();
632     }
633
634     /**
635      * Returns the year from the year field as an integer. If there is a problem
636      * parsing the value (which there shouldn't be because the year field is restricted to
637      * whole numbers), the year is set to zero (0).
638      * @return the year as displayed in the year field.
639      */

640     protected int getUserYear()
641     {
642         int userYearInt = 0;
643
644         try
645         {
646             // Get the year from the text field and parse it...
647
userYearInt = Integer.parseInt(yearTextField.getText(), 10);
648         }
649         catch (Exception JavaDoc e)
650         {
651             // If the year is something weird (other than blank) show the message...
652
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     /**
666      * Displays the value in the day combo at the index supplied.
667      * If there is a problem with the index being out of bounds (or the like),
668      * the index displayed is the last available in the combo.
669      * @param index the index of the value to display in the day combo.
670      */

671     protected void setDayComboSelectedIndex(int index)
672     {
673         try
674         {
675             // Set the position of the day combo box...
676
dayCombo.setSelectedIndex(index);
677         }
678         catch (Exception JavaDoc e)
679         {
680             int items = dayCombo.getItemCount();
681
682             dayCombo.setSelectedIndex(items - 1);
683         }
684     }
685
686     /**
687      * Determines if given year is a leap year.
688      * @param year = given year after 1582 (start of the Gregorian calendar).
689      * @return TRUE if given year is leap year, FALSE if not.
690      */

691     public boolean isLeapYear(int year)
692     {
693         // If multiple of 100, leap year if multiple of 400...
694
if ((year % 100) == 0)
695             return ((year % 400) == 0);
696
697         // Otherwise leap year iff multiple of 4...
698
return ((year % 4) == 0);
699     }
700
701     /**
702      * Returns a string representation of the year as entered in the year field.
703      * The year can only have four characters therefore any characters after the
704      * first for are ignored. If there is less than four characters the space(s)
705      * to the left are filled with a zero (0).
706      * @return the year as a string for example 2001.
707      */

708     protected String JavaDoc getSelectedYear()
709     {
710         String JavaDoc 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; // year = year;
730
default:
731                 year = year.substring(0, 4);
732         }
733         return year;
734     }
735
736     /**
737      * Returns a string representation of the month as entered in the month combo.
738      * @return the month as a string for example 01 (January).
739      */

740     protected String JavaDoc getSelectedMonth()
741     {
742         int selection = monthCombo.getSelectedIndex();
743
744         String JavaDoc month = Integer.toString(selection);
745
746         if (selection < 10)
747             month = "0" + month;
748
749         return month;
750     }
751
752     /**
753      * Returns a string representation of the day as entered in the day combo.
754      * @return the day as a string for example 31.
755      */

756     protected String JavaDoc getSelectedDay()
757     {
758         int selection = dayCombo.getSelectedIndex();
759
760         String JavaDoc day = Integer.toString(selection);
761
762         if (selection < 10)
763             day = "0" + day;
764
765         return day;
766     }
767
768     /**
769      * Returns a string representation of the hour as entered in the hour combo.
770      * @return the hour as a string for example 23.
771      */

772     protected String JavaDoc getSelectedHour()
773     {
774         int selection = hourCombo.getSelectedIndex();
775
776         String JavaDoc hour = Integer.toString(selection);
777
778         if (selection < 10)
779             hour = "0" + hour;
780
781         return hour;
782     }
783
784     /**
785      * Returns a string representation of the minute as entered in the minute combo.
786      * @return the minute as a string for example 59.
787      */

788     protected String JavaDoc getSelectedMinute()
789     {
790         int selection = minuteCombo.getSelectedIndex();
791
792         String JavaDoc minute = Integer.toString(selection);
793
794         if (selection < 10)
795             minute = "0" + minute;
796
797         return minute;
798     }
799
800     /**
801      * Returns a string representation of the second as entered in the second combo.
802      * @return the second as a string for example 59.
803      */

804     protected String JavaDoc getSelectedSecond()
805     {
806         int selection = secondCombo.getSelectedIndex();
807
808         String JavaDoc second = Integer.toString(selection);
809
810         if (selection < 10)
811             second = "0" + second;
812
813         return second;
814     }
815
816     /**
817      * Returns a string representation of the milliSecond as entered in the milliSecond combo.
818      * @return the milliSecond as a string for example '.999', or an empty string if milliseconds equals 0.
819      */

820     protected String JavaDoc getSelectedMilliSecond()
821     {
822         int selection = milliSecondCombo.getSelectedIndex();
823
824         // special handling for '0' milliseconds - some directories do not like milliseconds
825
// DDDDDDDDDD.mmm format, and cannot handle the decimal '.mmm' bit - to allow users to still
826
// get by, we'll leave it out altogether if milliseconds are unspecified.
827
if (selection == 0)
828             return "";
829
830         String JavaDoc 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     /**
842      * Sets the EditableString of this class to the supplied EditableString.
843      * @param editString
844      */

845     public void setStringValue(editablestring editString)
846     {
847         editableString = editString;
848     }
849
850     /**
851      * Sets the changed (or unchanged) attribute value from the attribute editor in the table.
852      * Disposes of the dialog.
853      */

854     public void doOK()
855     {
856         String JavaDoc date = getSelectedYear() + getSelectedMonth() +
857                 getSelectedDay() + getSelectedHour() + getSelectedMinute() +
858                 getSelectedSecond() + getSelectedMilliSecond();
859
860         // Add the Z if check box is selected...
861
if (checkBox.isSelected())
862             date = date + "Z";
863
864         if (isTableEditor)
865             // Sets the attribute value in the table editor to reflect the changes made...
866
editableString.setStringValue(date);
867         else
868             time = date;
869
870         setVisible(false);
871         dispose();
872     }
873
874     /**
875      * When the user hits 'cancel', the window is shut down.
876      * Sets the 'time' (accessable via getTime()) to the initial 'value'.
877      */

878     public void doCancel()
879     {
880         if (!isTableEditor)
881             time = value;
882         super.doCancel();
883     }
884
885     /**
886      * Returns the time set in the GUI. This is a hack so that the Search dialog can
887      * use this editor for time attributes. Instead of setting the value
888      * via editableString.setStringValue(), a global variable should have
889      * been set with the date/time.
890      * @return the date/time set by the user.
891      */

892     public String JavaDoc getTime()
893     {
894         return time;
895     }
896
897     /**
898      * Listener that monitors any insert or removal updates of a document.
899      * This is used on the year text field to spawn a leap year check if
900      * february is selected in the month combo.
901      * @author Trudi.
902      */

903     class MyDocumentListener implements DocumentListener JavaDoc
904     {
905         /**
906          * Does a date check if the user inserts a value in to the document (year field).
907          */

908         public void insertUpdate(DocumentEvent JavaDoc e)
909         {
910             checkDate(e);
911         }
912
913         /**
914          * Does a date check if the user removes a value from the document (year field).
915          */

916         public void removeUpdate(DocumentEvent JavaDoc e)
917         {
918             checkDate(e);
919         }
920
921         public void changedUpdate(DocumentEvent JavaDoc e)
922         {
923             /* we won't ever get this with PlainDocument */
924         }
925
926         /**
927          * Triggers an update in the day combo if the month selected in the month combo is february.
928          * Basically it is checking that the correct days are displayed if the year is a leap or not.
929          */

930         private void checkDate(DocumentEvent JavaDoc e)
931         {
932             try
933             {
934                 int index = monthCombo.getSelectedIndex();
935
936                 // If the month that is selected is feb, make sure the number of days
937
// in the day combo is correct (leap year)...
938
if (index == 2)
939                     updateDayCombo();
940             }
941             catch (Exception JavaDoc 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     /**
949      * This class extends JTextField to only allow whole
950      * numbers to be entered into the text field.
951      */

952     class WholeNumberField extends JTextField
953     {
954         private NumberFormat JavaDoc 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); // prevents error where formatter turns 2007 into '2,007'
962

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 JavaDoc e)
974             {
975                 // This should never happen because insertString allows
976
// only properly formatted data to get in the field.
977
}
978             return retVal;
979         }
980
981         public void setValue(int value)
982         {
983             setText(integerFormatter.format(value));
984         }
985
986         protected Document JavaDoc createDefaultModel()
987         {
988             return new WholeNumberDocument();
989         }
990
991         protected class WholeNumberDocument extends PlainDocument JavaDoc
992         {
993             public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc a)
994                     throws BadLocationException JavaDoc
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 JavaDoc(result, 0, j), a);
1008            }
1009        }
1010    }
1011}
Popular Tags