KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > example > time > DateTimeBrowser


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.example.time;
17 /*
18  * Import required Java packages.
19  */

20 import java.awt.Dimension JavaDoc;
21 import java.awt.Toolkit JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.WindowAdapter JavaDoc;
24 import java.awt.event.WindowEvent JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintStream JavaDoc;
30 import java.text.DateFormat JavaDoc;
31 import java.text.ParseException JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Calendar JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.GregorianCalendar JavaDoc;
37 import java.util.TimeZone JavaDoc;
38
39 import javax.swing.AbstractAction JavaDoc;
40 import javax.swing.Action JavaDoc;
41 import javax.swing.JFileChooser JavaDoc;
42 import javax.swing.JFrame JavaDoc;
43 import javax.swing.JMenu JavaDoc;
44 import javax.swing.JMenuBar JavaDoc;
45 import javax.swing.JMenuItem JavaDoc;
46 import javax.swing.JScrollPane JavaDoc;
47 import javax.swing.JTable JavaDoc;
48 import javax.swing.WindowConstants JavaDoc;
49 import javax.swing.table.TableColumn JavaDoc;
50 import javax.swing.table.TableColumnModel JavaDoc;
51
52 import org.joda.time.DateTime;
53
54 /** DateTimeBrowser is a Java Swing application which reads a file contining
55  * strings and displays DateTime values in a JTable.<p>
56  * The input strings must be suitable for instantiation
57  * of DateTime objects. The file is read, and an attempt is made
58  * to instantiate a DateTimeObject from the input string on each file
59  * line.<p>
60  * Comments (beginning with '#') and blank lines may appear in
61  * the file.<p>
62  * Error messages may result from invalid input.<p>
63  * Values calculated from any resulting DateTime objects are placed
64  * in a JTable and displayed in a JFrame.<p>
65  *
66  * @author Guy Allard
67  * @version 1.0
68  */

69 public class DateTimeBrowser extends JFrame JavaDoc {
70     //
71
private String JavaDoc[] mainArgs = null; // Copy of args[] reference.
72
//
73
private LoadedFile currFile = null; // New ones possible at
74
// runtime.
75
private JScrollPane JavaDoc mainSP = null; // Swapped around at runtime
76
//
77
/**
78      * The getter view menu item.
79      */

80     JMenuItem JavaDoc jmiGetter = null;
81     /**
82      * The hexadecimal view menu item.
83      */

84     JMenuItem JavaDoc jmiHex = null;
85     /**
86      * The Java Date view menu item.
87      */

88     JMenuItem JavaDoc jmiDate = null;
89     /**
90      * The java calendar menu item.
91      */

92     JMenuItem JavaDoc jmiCal = null;
93     //
94
// Done deals.
95
//
96
private final JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
97     private final boolean debugf = false; // debugging flag
98
private final boolean debugt = true; // debugging flag
99

100     /**
101      * This is the main swing application method. It sets up and displays the
102      * initial GUI, and controls execution thereafter. Everything else in
103      * this class is 'private', please read the code.
104      */

105     public static void main(String JavaDoc[] args) {
106         /*
107          * Developers Notes:
108          *
109          * -No corresponding Junit test class currently
110          * provided. Test by eyeball of the output.
111          *
112          * -Add a menu with Help(About)
113          * --> TBD.
114          *
115          * -Figure out some sane way to set initial default
116          * column sizes.
117          *
118          * -Lots of inner classes here, done in order to keep
119          * all the .class files easily identifiable. Some of
120          * this code is pretty ugly, very procedural in nature.
121          * Lots of very tight coupling between all the classes,
122          * thinly disguised switch statements, etc ..... This
123          * code written on the fly, with almost no thought given
124          * to OO design.
125          *
126          * -Also, I'm not really a GUI guy, so forgive any
127          * transgressions.
128          *
129          */

130             if ( args.length < 1 ) {
131                 System.err.println("File name is required!");
132                 usage();
133                 System.exit(1);
134             }
135         /*
136          * Instantiate a DateTimeBrowser and invoke it's go method,
137          * passing the input argument list.
138          */

139         new DateTimeBrowser().go( args );
140     } // main
141

142     /*
143      * usage A private static method to display usage information to
144      * the user before an error exit.
145      */

146     private static void usage() {
147             System.err.println("Usage:");
148             System.err.print("java <options> ");
149             System.err.print(DateTimeBrowser.class.getName());
150             System.err.println(" <filename>");
151             System.err.println("<filename> contains a list of Strings");
152             System.err.println("\twhich are valid for DateTime instantiation.");
153             System.err.println("<optons>");
154             System.err.println("\t-Duse.time.zone=");
155             System.err.println("\t\tA valid timezone name. If not specified");
156             System.err.println("\t\tthe OS/user default is used. If sepcified");
157             System.err.println("\t\tincorrectly, GMT is quietly used.");
158             System.err.println("\t-Duse.view=");
159             System.err.println("\t\tAn initial view to be displayed.");
160             System.err.println("\t\tValid names are: getter, hex, date, cal");
161             System.err.println("\t\tIf incorrectly specified, getter is used.");
162             System.err.println("\t\tThis becomes the default view.");
163     } // usage
164

165     /*
166      * go This method reads the file, creates the table to display,
167      * the window to display it in, and displays the window.
168      * @param fileName the name of the file to read.
169      * @param tryLines An estimate of the number of lines in
170      * the file.
171      */

172     private void go(String JavaDoc[] args) {
173
174         mainArgs = args;
175         setDefaultTimeZone(); // let user override if needed
176
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
177
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
178         //
179
JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
180         setJMenuBar( menuBar );
181         addMenus( menuBar );
182         /*
183          * Add a fast close listener
184          */

185
186         addWindowListener( new WindowAdapter JavaDoc() {
187                     public void windowClosing(WindowEvent JavaDoc e)
188                     {
189                         setVisible( false );
190                         dispose();
191                         System.exit(0);
192                     }
193                 }
194             );
195
196         //
197
// Load current file, prime tables and JFrame.
198
//
199
currFile = new LoadedFile( mainArgs[0] );
200         TableView tView = getDefaultTableView();
201         resetDefaults( tView );
202         //
203
// Set max size at start, and display the window.
204
//
205
Dimension JavaDoc screenMax = Toolkit.getDefaultToolkit().getScreenSize();
206         setSize ( screenMax );
207         setVisible(true);
208     }
209
210     //
211
// --> Private implementation methods follow.
212
//
213

214     /*
215      * getDefaultTableView
216      */

217     private TableView getDefaultTableView() {
218         // No user input.
219
String JavaDoc viewStr = System.getProperty("use.view");
220         if ( viewStr == null ) {
221             jmiGetter.setEnabled( false );
222             return new GetterTableView( currFile );
223         }
224         // Valid user input.
225
if ( viewStr.equalsIgnoreCase("hex") ) {
226             jmiHex.setEnabled( false );
227             return new HexTableView( currFile );
228         }
229         else if ( viewStr.equalsIgnoreCase("date") ) {
230             jmiDate.setEnabled( false );
231             return new DateTableView( currFile );
232         }
233         else if ( viewStr.equalsIgnoreCase("cal") ) {
234             jmiCal.setEnabled( false );
235             return new CalTableView( currFile );
236         }
237         else if ( viewStr.equalsIgnoreCase("getter") ) {
238             jmiGetter.setEnabled( false );
239             return new GetterTableView( currFile );
240         }
241         else { // error by user
242
System.err.println("View name: " + viewStr + " invalid.");
243             jmiGetter.setEnabled( false );
244             return new GetterTableView( currFile );
245         }
246     }
247     /*
248     * setDefaultTableView
249     */

250     private void setDefaultTableView(String JavaDoc newView) {
251         System.setProperty( "use.view", newView );
252     }
253     /*
254      * setDefaultTimeZone
255      */

256     private void setDefaultTimeZone() {
257         String JavaDoc tzName = System.getProperty("use.time.zone");
258         if ( tzName == null ) return; // Use OS/user default.
259
//
260
// If tzName is bogus, not understood by the JRE,
261
// 'getTimeZone' returns GMT.
262
//
263
TimeZone JavaDoc toSet = TimeZone.getTimeZone( tzName );
264         //
265
// Set default to whatever was returned.
266
//
267
TimeZone.setDefault( toSet );
268     }
269
270     /*
271      * addMenus
272      */

273     private void addMenus( JMenuBar JavaDoc menuBar) {
274         //
275
// Create all the menus.
276
//
277
JMenu JavaDoc fileMenu = new JMenu JavaDoc("File");
278         JMenu JavaDoc viewMenu = new JMenu JavaDoc("View");
279         //
280
// Add them to the menubar in order.
281
//
282
menuBar.add( fileMenu );
283         menuBar.add( viewMenu );
284         //
285
// Create action objects and menu items.
286
//
287
Action JavaDoc open = new OpenAction();
288         JMenuItem JavaDoc jmiOpen = new JMenuItem JavaDoc( open );
289         Action JavaDoc exit = new ExitAction();
290         JMenuItem JavaDoc jmiExit = new JMenuItem JavaDoc( exit );
291         //
292
// Next Menu
293
//
294
Action JavaDoc getter = new GetterAction();
295         jmiGetter = new JMenuItem JavaDoc( getter );
296         getter.setEnabled( true );
297         //
298
Action JavaDoc hex = new HexAction();
299         jmiHex = new JMenuItem JavaDoc( hex );
300         hex.setEnabled( true );
301         //
302
Action JavaDoc date = new DateAction();
303         jmiDate = new JMenuItem JavaDoc( date );
304         date.setEnabled( true );
305         //
306
Action JavaDoc cal = new CalAction();
307         jmiCal = new JMenuItem JavaDoc( cal );
308         cal.setEnabled( true );
309         //
310
// Build the file menu.
311
//
312
fileMenu.add( jmiOpen );
313         fileMenu.addSeparator();
314         fileMenu.add( jmiExit );
315         //
316
// Build the view menu.
317
//
318
viewMenu.add( jmiGetter );
319         viewMenu.add( jmiHex );
320         viewMenu.add( jmiDate );
321         viewMenu.add( jmiCal );
322         //
323
// *temp Developer's code
324
//
325
// jmiGetter.setEnabled( false );
326
//
327
// JMenuItem getter2 = new JMenuItem( "getter2" );
328
// getter2.addActionListener( new myMouseListener() );
329
// viewMenu.add( getter2 );
330
} // end of addMenus
331

332     /*
333      * A private method to dump the arrays of Object[][]
334      * if desired by the developer
335      * @param objs The array of arrays to be dumped.
336      */

337     private void dumpObjs(Object JavaDoc[][] objs, PrintStream JavaDoc out ) {
338         for (int i = 0; i < objs.length; ++i) {
339             for (int j = 0; j < objs[i].length; ++j) {
340                 out.println(i + " " + j + " "
341                     + objs[i][j]);
342             } // for j
343
} // for i
344
}
345
346     /*
347      * enableAll
348      */

349     private void enableAllViews() {
350         jmiGetter.setEnabled( true );
351         jmiHex.setEnabled( true );
352         jmiDate.setEnabled( true );
353         jmiCal.setEnabled( true );
354     } // end of enableAllViews
355

356     /*
357      * getADate Returns a new DateTime object reference if possible,
358      * otherwise null.
359      * @return retDT A DateTime object reference.
360      */

361     private DateTime getADate(String JavaDoc s) {
362         DateTime retDT = null;
363         try
364         {
365             retDT = new DateTime( s );
366         } // the try
367
catch(IllegalArgumentException JavaDoc pe)
368         {
369             // ignore it here, caller sees null
370
} // the catch
371
return retDT;
372     } // getADate
373
//
374
private static final String JavaDoc PADCHARS = "00000000000000000000000000000000";
375
376     /*
377      * LPad Return a String, left padded with '0's as specified
378      * by the caller.
379      */

380     private String JavaDoc LPad(String JavaDoc inStr, int maxLen) {
381         if (inStr.length() >= maxLen) return inStr.toUpperCase();
382         String JavaDoc zeroes = PADCHARS.substring(0, maxLen - inStr.length());
383         String JavaDoc retVal = zeroes + inStr;
384         return retVal.toUpperCase();
385     }
386
387     /*
388      * resetDefaults
389      */

390     private void resetDefaults( TableView tView ) {
391         Object JavaDoc[] colNames = tView.getColNames();
392         Object JavaDoc[][] tableValues = tView.getCalcdValues();
393         // dumpObjs( tableValues, System.out);
394
JTable JavaDoc table = new JTable JavaDoc( tableValues, colNames );
395         tView.setViewColumnsWidth( table );
396         setTitle( tView.getViewTitle() );
397         //
398
if ( mainSP != null ) getContentPane().remove( mainSP );
399         mainSP = new JScrollPane JavaDoc( table );
400         getContentPane().add( mainSP, "Center" );
401         validate();
402     } // end of resetDefaults
403

404     //
405
// ----> Private internal classes follow.
406
//
407

408     /*
409      * LoadedFile This class represents a file that has been loaded
410      * for viewing.
411      */

412     private class LoadedFile {
413         // Instance variables
414
String JavaDoc fileName = null;
415         ArrayList JavaDoc fileStrings = null;
416         ArrayList JavaDoc dtObjects = null;
417         int lineGuess = 0;
418
419         /*
420          * LoadedFile constructor.
421          */

422         LoadedFile(String JavaDoc fileName) {
423             validateFile( fileName );
424             this.fileName = fileName;
425             //
426
fileStrings = new ArrayList JavaDoc( lineGuess );
427             dtObjects = new ArrayList JavaDoc( lineGuess );
428
429             try
430             {
431                 BufferedReader JavaDoc rdr =
432                     new BufferedReader JavaDoc( new FileReader JavaDoc( fileName ) );
433                 String JavaDoc inputLine = null;
434                 DateTime calculatedDT = null;
435                 int currLine = 0;
436                 while( (inputLine = rdr.readLine()) != null ) {
437                     currLine++;
438                     inputLine = inputLine.trim();
439                     // Ignore blank and comment lines
440
if ( inputLine.length() == 0 ) continue;
441                     if ( inputLine.charAt(0) == '#' ) continue;
442                     // Ignore lines which fail DateTime construction
443
if ( (calculatedDT = getADate(inputLine)) == null ) {
444                         System.err.println("Parse failed for: " + inputLine
445                             + " at line number " + currLine);
446                         continue;
447                     }
448                     // Add the input file string and DateTime to lists
449
fileStrings.add( inputLine );
450                     dtObjects.add( calculatedDT );
451                 }
452                 rdr.close();
453             }
454             catch(IOException JavaDoc ioe)
455             {
456                 System.err.println("Load of file: "
457                     + fileName + " failed!");
458                 ioe.printStackTrace();
459                 System.exit(100);
460             }
461
462             // Try to be efficient (?really?)
463
fileStrings.trimToSize();
464             dtObjects.trimToSize();
465             } // end of LoadedFile() constructor
466
/*
467          * General getters.
468          */

469         public String JavaDoc getFileName() { return fileName; }
470         public int getLineGuess() { return lineGuess; }
471         public ArrayList JavaDoc getFileStrings() { return fileStrings; }
472         public ArrayList JavaDoc getDtObjects() { return dtObjects; }
473         public int getLoadedFileSize() {
474             if ( dtObjects == null ) return 0;
475             return dtObjects.size();
476         }
477         /*
478          * validateFile
479          */

480         private void validateFile(String JavaDoc fileName) {
481             /*
482              * Verify the user specified file exists and can
483              * be read.
484              */

485             File JavaDoc f = new File JavaDoc( fileName );
486             if ( !f.exists() || !f.canRead() ) {
487                 System.err.println("File: " + mainArgs[0]
488                     + " does not exist or cannot be read!");
489                 usage();
490                 System.exit(2);
491             }
492             /*
493              * Try to get a reasonable estimate of the number of lines
494              * in the file.
495              */

496             // Java does not do this right IMO. The number of bytes in a
497
// file is a
498
// long, but the length of a string is an int. Why?
499
lineGuess = (int)(f.length() / (long)"YYYY-MM-DDTHH:MM:SS".length());
500             lineGuess += (lineGuess / 10);
501             //
502
// Debugging
503
//
504
if ( false ) {
505                 System.out.println("Line guess is: " + lineGuess);
506             }
507         } // end of validateFile(String)
508
} // end of class LoadedFile class
509

510     /*
511      * TableView This abstract class defines the operations
512      * necessary to create and access the Object arrays
513      * required to create a JTable.
514      */

515     private abstract class TableView {
516         protected Object JavaDoc[] colNames = null;
517         protected Object JavaDoc[][] calcdValues = null;
518         protected LoadedFile lddFile = null;
519         //
520
TableView(LoadedFile lddFile) {
521             this.lddFile = lddFile;
522         }
523         //
524
public Object JavaDoc[] getColNames() {
525             return colNames;
526         }
527         public Object JavaDoc[][] getCalcdValues() {
528             return calcdValues;
529         }
530         //
531
abstract Object JavaDoc[] genColNames();
532         abstract Object JavaDoc[][] genCalcdValues();
533         abstract String JavaDoc getViewTitle();
534         abstract void setViewColumnsWidth(JTable JavaDoc jt);
535         //
536
} // end of abstract class TableView
537

538     /*
539      * GetterTableView This class implements the operations
540      * for the GetterView of the Jtable.
541      */

542     private class GetterTableView extends TableView {
543         //
544
GetterTableView(LoadedFile lddFile) {
545             super(lddFile);
546             setDefaultTableView( "getter" );
547             colNames = genColNames();
548             calcdValues = genCalcdValues();
549         }
550
551         /*
552          * genCalcdValues is required by the base class.
553          */

554         Object JavaDoc[][] genCalcdValues() {
555             Object JavaDoc[][] retValues = null;
556             /*
557              * Create an array of Objects that will contain
558              * other arrays of Objects. (This is the 'column'
559              * array).
560              */

561             ArrayList JavaDoc fileStrings = lddFile.getFileStrings();
562             ArrayList JavaDoc dtObjects = lddFile.getDtObjects();
563             int numRows = fileStrings.size();
564             retValues = new Object JavaDoc[numRows][];
565             int numCols = colNames.length;
566             // System.err.println("NumCols : " + numCols);
567
/*
568              * Prime the array of arrays of Objects, allocating a new
569              * secondary array for each of the primary array's
570              * elements.
571              */

572             for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++ nextStrNum) {
573                 retValues[nextStrNum] = new Object JavaDoc[numCols]; // get the 'col' array
574
//****
575
//* This needs to be sync'd with the colNames array.
576
//****
577
// Current row, 1st column
578
int column = 0; // working row value
579
String JavaDoc fileString = (String JavaDoc)fileStrings.get(nextStrNum);
580                 retValues[nextStrNum][column++] = fileString;
581                 // Current row, 2nd column
582
DateTime adt = (DateTime)dtObjects.get(nextStrNum);
583                 String JavaDoc adtStr = adt.toString();
584                 retValues[nextStrNum][column++] = adtStr;
585                 // Current row, other columns.
586
// Order here must match that specified in the colNames
587
// array.
588
retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getMillisOfSecond() );
589                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getSecondOfMinute() );
590                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getMinuteOfHour() );
591                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getHourOfDay() );
592                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getDayOfWeek() );
593                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getDayOfMonth() );
594                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getDayOfYear() );
595                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getWeekOfWeekyear() );
596                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getWeekyear() );
597                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getMonthOfYear() );
598                 retValues[nextStrNum][column++] = new Integer JavaDoc( adt.getYear() );
599                 //
600
} // the for
601
if ( debugf ) dumpObjs( retValues, System.err );
602             return retValues;
603         } // end of genTBValues
604

605         /*
606          * genColNames is required by the base class.
607          */

608         Object JavaDoc[] genColNames() {
609             Object JavaDoc[] retVal = {
610                 "FileString",
611                 "toString()",
612                 "MillisOfSec",
613                 "SecOfMin",
614                 "MinOfHr",
615                 "HrOfDay",
616                 "DayOfWk",
617                 "DayOfMon",
618                 "DayOfYr",
619                 "WeekOfWY",
620                 "Weekyear",
621                 "MonOfYr",
622                 "Year"
623             };
624             return retVal;
625         }
626
627         /*
628          * getViewTitle
629          */

630         String JavaDoc getViewTitle() {
631             return "DateTime.getXXX() Method Calculations"
632                 + " : "
633                 + TimeZone.getDefault().getDisplayName()
634                 + " : "
635                 + " Record Count "
636                 + currFile.getLoadedFileSize();
637         }
638         /*
639          * setViewColumnLengths
640          */

641         void setViewColumnsWidth(JTable JavaDoc jt) {
642             /*
643              * Resize column 0, 1
644              */

645             TableColumnModel JavaDoc colmodel = jt.getColumnModel();
646             TableColumn JavaDoc col0 = colmodel.getColumn(0);
647             col0.setPreferredWidth(200);
648             TableColumn JavaDoc col1 = colmodel.getColumn(1);
649             col1.setPreferredWidth(200);
650             return;
651         }
652
653     } // end of class getterTableView
654

655     /*
656      * HexView This class implements the operations for
657      * the HexView of the file.
658      */

659     private class HexTableView extends TableView {
660         //
661
HexTableView(LoadedFile lddFile) {
662             super(lddFile);
663             setDefaultTableView( "hex" );
664             colNames = genColNames();
665             calcdValues = genCalcdValues();
666         }
667
668         /*
669          * genCalcdValues is required by the base class.
670          */

671         Object JavaDoc[][] genCalcdValues() {
672             Object JavaDoc[][] retValues = null;
673             /*
674              * Create an array of Objects that will contain
675              * other arrays of Objects. (This is the 'column'
676              * array).
677              */

678             ArrayList JavaDoc fileStrings = lddFile.getFileStrings();
679             ArrayList JavaDoc dtObjects = lddFile.getDtObjects();
680             int numRows = fileStrings.size();
681             retValues = new Object JavaDoc[numRows][];
682             int numCols = colNames.length;
683             // System.err.println("NumCols : " + numCols);
684
String JavaDoc fs = "yyyy-MM-dd'T'HH:mm:ss";
685             DateFormat JavaDoc df = new SimpleDateFormat JavaDoc( fs );
686             /*
687              * Prime the array of arrays of Objects, allocating a new
688              * secondary array for each of the primary array's
689              * elements.
690              */

691             for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++ nextStrNum) {
692                 retValues[nextStrNum] = new Object JavaDoc[numCols]; // get the 'col' array
693
//****
694
//* This needs to be sync'd with the colNames array.
695
//****
696
// Current row, 1st column
697
int column = 0;
698                 String JavaDoc fileString = (String JavaDoc)fileStrings.get(nextStrNum);
699                 retValues[nextStrNum][column++] = fileString;
700                 // Current row, 2nd column
701
DateTime adt = (DateTime)dtObjects.get(nextStrNum);
702                 String JavaDoc adtStr = adt.toString();
703                 retValues[nextStrNum][column++] = adtStr;
704                 // Current row, other columns.
705
// Order here must match that specified in the colNames
706
// array.
707
long lVal = adt.getMillis();
708                 Long JavaDoc millis = new Long JavaDoc( lVal );
709                 retValues[nextStrNum][column++] = millis;
710                 String JavaDoc hexVal = Long.toHexString( lVal );
711                 String JavaDoc octalVal = Long.toOctalString( lVal );
712                 retValues[nextStrNum][column++] = "0"+ LPad(octalVal,22);
713                 retValues[nextStrNum][column++] = "0x" + LPad(hexVal,16);
714                 //
715
Date JavaDoc javaDate = null;
716                 try
717                 {
718                     javaDate = df.parse( fileString );
719                 }
720                 catch(ParseException JavaDoc e)
721                 {
722                     System.err.println("Parse failed for : " + fileString);
723                     // pe.printStackTrace();
724
}
725                 //
726
lVal = javaDate.getTime();
727                 millis = new Long JavaDoc( lVal );
728                 hexVal = Long.toHexString( lVal );
729                 octalVal = Long.toOctalString( lVal );
730                 retValues[nextStrNum][column++] = millis;
731                 retValues[nextStrNum][column++] = "0"+ LPad(octalVal,22);
732                 retValues[nextStrNum][column++] = "0x" + LPad(hexVal,16);
733                 //
734
} // the for
735
if ( debugf ) dumpObjs( retValues, System.err );
736             return retValues;
737         } // end of genTBValues
738

739         /*
740          * genColNames is required by the base class.
741          */

742         Object JavaDoc[] genColNames() {
743             Object JavaDoc[] retVal = {
744                 "FileString",
745                 "toString()",
746                 "JDT-millis",
747                 "JDT-Oct",
748                 "JDT-Hex",
749                 "Date-millis",
750                 "Date-Oct",
751                 "Date-Hex"
752             };
753             return retVal;
754         }
755
756         /*
757          * getViewTitle
758          */

759         String JavaDoc getViewTitle() {
760             return "View the long values"
761                 + " : "
762                 + TimeZone.getDefault().getDisplayName()
763                 + " : "
764                 + " Record Count "
765                 + currFile.getLoadedFileSize();
766         }
767         /*
768          * setViewColumnLengths
769          */

770         void setViewColumnsWidth(JTable JavaDoc jt) {
771             return;
772         }
773
774     } // end of class HexTableView
775

776     /*
777      * DateTableView This class implements the operations for
778      * the java.util.Date of the file.
779      */

780     private class DateTableView extends TableView {
781         //
782
DateTableView(LoadedFile lddFile) {
783             super(lddFile);
784             setDefaultTableView( "date" );
785             colNames = genColNames();
786             calcdValues = genCalcdValues();
787         }
788
789         /*
790          * genCalcdValues is required by the base class.
791          */

792         Object JavaDoc[][] genCalcdValues() {
793             Object JavaDoc[][] retValues = null;
794             /*
795              * Create an array of Objects that will contain
796              * other arrays of Objects. (This is the 'column'
797              * array).
798              */

799             ArrayList JavaDoc fileStrings = lddFile.getFileStrings();
800             ArrayList JavaDoc dtObjects = lddFile.getDtObjects();
801             int numRows = fileStrings.size();
802             retValues = new Object JavaDoc[numRows][];
803             int numCols = colNames.length;
804             // System.err.println("NumCols : " + numCols);
805
/*
806              * Prime the array of arrays of Objects, allocating a new
807              * secondary array for each of the primary array's
808              * elements.
809              */

810             for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++ nextStrNum) {
811                 retValues[nextStrNum] = new Object JavaDoc[numCols]; // get the 'col' array
812
//****
813
//* This needs to be sync'd with the colNames array.
814
//****
815
// Current row, 1st column
816
int column = 0;
817                 String JavaDoc fileString = (String JavaDoc)fileStrings.get(nextStrNum);
818                 retValues[nextStrNum][column++] = fileString;
819                 // Current row, 2nd column
820
DateTime adt = (DateTime)dtObjects.get(nextStrNum);
821                 String JavaDoc adtStr = adt.toString();
822                 retValues[nextStrNum][column++] = adtStr;
823                 // Current row, other columns.
824
// Order here must match that specified in the colNames
825
// array.
826
long lVal = adt.getMillis();
827                 java.util.Date JavaDoc jDate = new java.util.Date JavaDoc( lVal );
828                 retValues[nextStrNum][column++] = new Integer JavaDoc( jDate.getSeconds() );
829                 retValues[nextStrNum][column++] = new Integer JavaDoc( jDate.getMinutes() );
830                 retValues[nextStrNum][column++] = new Integer JavaDoc( jDate.getHours() );
831                 retValues[nextStrNum][column++] = new Integer JavaDoc( jDate.getDay() );
832                 retValues[nextStrNum][column++] = new Integer JavaDoc( jDate.getDate() );
833                 retValues[nextStrNum][column++] = new Integer JavaDoc( jDate.getMonth() );
834                 retValues[nextStrNum][column++] = new Integer JavaDoc( jDate.getYear() );
835                 //
836
} // the for
837
if ( debugf ) dumpObjs( retValues, System.err );
838             return retValues;
839         } // end of genTBValues
840

841         /*
842          * genColNames is required by the base class.
843          */

844         Object JavaDoc[] genColNames() {
845             Object JavaDoc[] retVal = {
846                 "FileString", // 0
847
"toString()", // 1
848
"Seconds", // 2
849
"Minutes", // 3
850
"Hours", // 4
851
"Day Of Week", // 5
852
"Day Of Month", // 6
853
"Month", // 7
854
"Year" // 8
855
};
856             return retVal;
857         }
858
859         /*
860          * getViewTitle
861          */

862         String JavaDoc getViewTitle() {
863             return "java.util.Date getXXX"
864                 + " : "
865                 + TimeZone.getDefault().getDisplayName()
866                 + " : "
867                 + " Record Count "
868                 + currFile.getLoadedFileSize();
869         }
870         /*
871          * setViewColumnLengths
872          */

873         void setViewColumnsWidth(JTable JavaDoc jt) {
874             /*
875              * Resize column 0, 1
876              */

877             TableColumnModel JavaDoc colmodel = jt.getColumnModel();
878             TableColumn JavaDoc col0 = colmodel.getColumn(0);
879             col0.setPreferredWidth(150);
880             TableColumn JavaDoc col1 = colmodel.getColumn(1);
881             col1.setPreferredWidth(150);
882             return;
883         }
884
885     } // end of class DateTableView
886

887     /*
888      * CalTableView This class implements the operations for
889      * the java.util.Date of the file.
890      */

891     private class CalTableView extends TableView {
892         //
893
CalTableView(LoadedFile lddFile) {
894             super(lddFile);
895             setDefaultTableView( "cal" );
896             colNames = genColNames();
897             calcdValues = genCalcdValues();
898         }
899
900         /*
901          * genCalcdValues is required by the base class.
902          */

903         Object JavaDoc[][] genCalcdValues() {
904             Object JavaDoc[][] retValues = null;
905             /*
906              * Create an array of Objects that will contain
907              * other arrays of Objects. (This is the 'column'
908              * array).
909              */

910             ArrayList JavaDoc fileStrings = lddFile.getFileStrings();
911             ArrayList JavaDoc dtObjects = lddFile.getDtObjects();
912             int numRows = fileStrings.size();
913             retValues = new Object JavaDoc[numRows][];
914             int numCols = colNames.length;
915             // System.err.println("NumCols : " + numCols);
916
/*
917              * Prime the array of arrays of Objects, allocating a new
918              * secondary array for each of the primary array's
919              * elements.
920              */

921             for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++ nextStrNum) {
922                 retValues[nextStrNum] = new Object JavaDoc[numCols]; // get the 'col' array
923
//****
924
//* This needs to be sync'd with the colNames array.
925
//****
926
// Current row, 1st column
927
int column = 0;
928                 String JavaDoc fileString = (String JavaDoc)fileStrings.get(nextStrNum);
929                 retValues[nextStrNum][column++] = fileString;
930                 // Current row, 2nd column
931
DateTime adt = (DateTime)dtObjects.get(nextStrNum);
932                 String JavaDoc adtStr = adt.toString();
933                 retValues[nextStrNum][column++] = adtStr;
934                 // Current row, other columns.
935
// Order here must match that specified in the colNames
936
// array.
937
long lVal = adt.getMillis();
938                 GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
939                 cal.setTime( new Date JavaDoc( lVal ) );
940                 cal.setMinimalDaysInFirstWeek(4);
941                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
942                     Calendar.MILLISECOND ) );
943                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
944                     Calendar.SECOND ) );
945                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
946                     Calendar.MINUTE ) );
947                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
948                     Calendar.HOUR_OF_DAY ) );
949                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
950                     Calendar.DAY_OF_WEEK ) );
951                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
952                     Calendar.DAY_OF_MONTH ) );
953                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
954                     Calendar.DAY_OF_YEAR ) );
955                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
956                     Calendar.WEEK_OF_YEAR ) );
957                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
958                     Calendar.MONTH ) );
959                 retValues[nextStrNum][column++] = new Integer JavaDoc( cal.get(
960                     Calendar.YEAR ) );
961                 //
962
} // the for
963
if ( debugf ) dumpObjs( retValues, System.err );
964             return retValues;
965         } // end of genTBValues
966

967         /*
968          * genColNames is required by the base class.
969          */

970         Object JavaDoc[] genColNames() {
971             Object JavaDoc[] retVal = {
972                 "FileString", // 0
973
"toString()", // 1
974
"Millis", // 2
975
"Sec", // 3
976
"Min", // 4
977
"HrOfDay", // 5
978
"DayOfWeek", // 6
979
"DayOfMon", // 7
980
"DayOfYr", // 8
981
"WkofYr", // 9
982
"MonOfYr", // 10
983
"Year" // 11
984
};
985             return retVal;
986         }
987
988         /*
989          * getViewTitle
990          */

991         String JavaDoc getViewTitle() {
992             return "java.util.Calendar.get(int)"
993                 + " : "
994                 + TimeZone.getDefault().getDisplayName()
995                 + " : "
996                 + " Record Count "
997                 + currFile.getLoadedFileSize();
998         }
999         /*
1000         * setViewColumnLengths
1001         */

1002        void setViewColumnsWidth(JTable JavaDoc jt) {
1003            /*
1004             * Resize column 0, 1
1005             */

1006            TableColumnModel JavaDoc colmodel = jt.getColumnModel();
1007            TableColumn JavaDoc col0 = colmodel.getColumn(0);
1008            col0.setPreferredWidth(175);
1009            TableColumn JavaDoc col1 = colmodel.getColumn(1);
1010            col1.setPreferredWidth(175);
1011            return;
1012        }
1013
1014    } // end of class CalTableView
1015

1016    /*
1017     * OpenAction
1018     */

1019    private class OpenAction extends AbstractAction JavaDoc {
1020        /*
1021         * Constructor
1022         */

1023        public OpenAction() {
1024            super("Open");
1025        } // end of ctor
1026

1027        /*
1028         * actionPerformed
1029         */

1030        public void actionPerformed(ActionEvent JavaDoc e) {
1031            int result = chooser.showOpenDialog( DateTimeBrowser.this );
1032            String JavaDoc canPath = null;
1033            if ( result == JFileChooser.APPROVE_OPTION ) {
1034                File JavaDoc chosenFile = chooser.getSelectedFile();
1035                try
1036                {
1037                    canPath = chosenFile.getCanonicalPath();
1038                }
1039                catch(IOException JavaDoc ioe)
1040                {
1041                    System.err.println( "I/O Error on file: "
1042                        + chosenFile );
1043                    // Ignore it for now.
1044
}
1045                enableAllViews();
1046                currFile = new LoadedFile( canPath );
1047                TableView tView = getDefaultTableView();
1048                resetDefaults( tView );
1049            } // end of if a file actually chosen.
1050
} // end of actionPerformed
1051
} // end of class OpenAction
1052

1053    /*
1054     * ExitAction
1055     */

1056    private class ExitAction extends AbstractAction JavaDoc {
1057        /*
1058         * Constructor
1059         */

1060        public ExitAction() {
1061            super("Exit");
1062        } // end of ctor
1063

1064        /*
1065         * actionPerformed
1066         */

1067        public void actionPerformed(ActionEvent JavaDoc e) {
1068            DateTimeBrowser.this.setVisible( false );
1069            DateTimeBrowser.this.dispose();
1070            System.exit(0);
1071        } // end of actionPerformed
1072
} // end of class OpenAction
1073

1074    /*
1075     * GetterAction
1076     */

1077    private class GetterAction extends AbstractAction JavaDoc {
1078        /*
1079         * Constructor
1080         */

1081        public GetterAction() {
1082            super("Getter");
1083        } // end of ctor
1084

1085        /*
1086         * actionPerformed
1087         */

1088        public void actionPerformed(ActionEvent JavaDoc e) {
1089            TableView tView = new GetterTableView( currFile );
1090            resetDefaults( tView );
1091            enableAllViews();
1092            jmiGetter.setEnabled( false );
1093        } // end of actionPerformed
1094
} // end of class OpenAction
1095

1096    /*
1097     * HexAction
1098     */

1099    private class HexAction extends AbstractAction JavaDoc {
1100        /*
1101         * Constructor
1102         */

1103        public HexAction() {
1104            super("Hex");
1105        } // end of ctor
1106

1107        /*
1108         * actionPerformed
1109         */

1110        public void actionPerformed(ActionEvent JavaDoc e) {
1111            TableView tView = new HexTableView( currFile );
1112            resetDefaults( tView );
1113            enableAllViews();
1114            jmiHex.setEnabled( false );
1115        } // end of actionPerformed
1116
} // end of class OpenAction
1117

1118    /*
1119     * DateAction
1120     */

1121    private class DateAction extends AbstractAction JavaDoc {
1122        /*
1123         * Constructor
1124         */

1125        public DateAction() {
1126            super("Date");
1127        } // end of ctor
1128

1129        /*
1130         * actionPerformed
1131         */

1132        public void actionPerformed(ActionEvent JavaDoc e) {
1133            TableView tView = new DateTableView( currFile );
1134            resetDefaults( tView );
1135            enableAllViews();
1136            jmiDate.setEnabled( false );
1137        } // end of actionPerformed
1138
} // end of class DateAction
1139

1140    /*
1141     * CalAction
1142     */

1143    private class CalAction extends AbstractAction JavaDoc {
1144        /*
1145         * Constructor
1146         */

1147        public CalAction() {
1148            super("Calendar");
1149        } // end of ctor
1150

1151        /*
1152         * actionPerformed
1153         */

1154        public void actionPerformed(ActionEvent JavaDoc e) {
1155            TableView tView = new CalTableView( currFile );
1156            resetDefaults( tView );
1157            enableAllViews();
1158            jmiCal.setEnabled( false );
1159        } // end of actionPerformed
1160
} // end of class CalAction
1161

1162} // class DateTimeBrowser
1163
Popular Tags