0001: /*
0002: * Copyright 2001-2005 Stephen Colebourne
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.joda.example.time;
0017:
0018: /*
0019: * Import required Java packages.
0020: */
0021: import java.awt.Dimension;
0022: import java.awt.Toolkit;
0023: import java.awt.event.ActionEvent;
0024: import java.awt.event.WindowAdapter;
0025: import java.awt.event.WindowEvent;
0026: import java.io.BufferedReader;
0027: import java.io.File;
0028: import java.io.FileReader;
0029: import java.io.IOException;
0030: import java.io.PrintStream;
0031: import java.text.DateFormat;
0032: import java.text.ParseException;
0033: import java.text.SimpleDateFormat;
0034: import java.util.ArrayList;
0035: import java.util.Calendar;
0036: import java.util.Date;
0037: import java.util.GregorianCalendar;
0038: import java.util.TimeZone;
0039:
0040: import javax.swing.AbstractAction;
0041: import javax.swing.Action;
0042: import javax.swing.JFileChooser;
0043: import javax.swing.JFrame;
0044: import javax.swing.JMenu;
0045: import javax.swing.JMenuBar;
0046: import javax.swing.JMenuItem;
0047: import javax.swing.JScrollPane;
0048: import javax.swing.JTable;
0049: import javax.swing.WindowConstants;
0050: import javax.swing.table.TableColumn;
0051: import javax.swing.table.TableColumnModel;
0052:
0053: import org.joda.time.DateTime;
0054:
0055: /** DateTimeBrowser is a Java Swing application which reads a file contining
0056: * strings and displays DateTime values in a JTable.<p>
0057: * The input strings must be suitable for instantiation
0058: * of DateTime objects. The file is read, and an attempt is made
0059: * to instantiate a DateTimeObject from the input string on each file
0060: * line.<p>
0061: * Comments (beginning with '#') and blank lines may appear in
0062: * the file.<p>
0063: * Error messages may result from invalid input.<p>
0064: * Values calculated from any resulting DateTime objects are placed
0065: * in a JTable and displayed in a JFrame.<p>
0066: *
0067: * @author Guy Allard
0068: * @version 1.0
0069: */
0070: public class DateTimeBrowser extends JFrame {
0071: //
0072: private String[] mainArgs = null; // Copy of args[] reference.
0073: //
0074: private LoadedFile currFile = null; // New ones possible at
0075: // runtime.
0076: private JScrollPane mainSP = null; // Swapped around at runtime
0077: //
0078: /**
0079: * The getter view menu item.
0080: */
0081: JMenuItem jmiGetter = null;
0082: /**
0083: * The hexadecimal view menu item.
0084: */
0085: JMenuItem jmiHex = null;
0086: /**
0087: * The Java Date view menu item.
0088: */
0089: JMenuItem jmiDate = null;
0090: /**
0091: * The java calendar menu item.
0092: */
0093: JMenuItem jmiCal = null;
0094: //
0095: // Done deals.
0096: //
0097: private final JFileChooser chooser = new JFileChooser();
0098: private final boolean debugf = false; // debugging flag
0099: private final boolean debugt = true; // debugging flag
0100:
0101: /**
0102: * This is the main swing application method. It sets up and displays the
0103: * initial GUI, and controls execution thereafter. Everything else in
0104: * this class is 'private', please read the code.
0105: */
0106: public static void main(String[] args) {
0107: /*
0108: * Developers Notes:
0109: *
0110: * -No corresponding Junit test class currently
0111: * provided. Test by eyeball of the output.
0112: *
0113: * -Add a menu with Help(About)
0114: * --> TBD.
0115: *
0116: * -Figure out some sane way to set initial default
0117: * column sizes.
0118: *
0119: * -Lots of inner classes here, done in order to keep
0120: * all the .class files easily identifiable. Some of
0121: * this code is pretty ugly, very procedural in nature.
0122: * Lots of very tight coupling between all the classes,
0123: * thinly disguised switch statements, etc ..... This
0124: * code written on the fly, with almost no thought given
0125: * to OO design.
0126: *
0127: * -Also, I'm not really a GUI guy, so forgive any
0128: * transgressions.
0129: *
0130: */
0131: if (args.length < 1) {
0132: System.err.println("File name is required!");
0133: usage();
0134: System.exit(1);
0135: }
0136: /*
0137: * Instantiate a DateTimeBrowser and invoke it's go method,
0138: * passing the input argument list.
0139: */
0140: new DateTimeBrowser().go(args);
0141: } // main
0142:
0143: /*
0144: * usage A private static method to display usage information to
0145: * the user before an error exit.
0146: */
0147: private static void usage() {
0148: System.err.println("Usage:");
0149: System.err.print("java <options> ");
0150: System.err.print(DateTimeBrowser.class.getName());
0151: System.err.println(" <filename>");
0152: System.err.println("<filename> contains a list of Strings");
0153: System.err
0154: .println("\twhich are valid for DateTime instantiation.");
0155: System.err.println("<optons>");
0156: System.err.println("\t-Duse.time.zone=");
0157: System.err
0158: .println("\t\tA valid timezone name. If not specified");
0159: System.err
0160: .println("\t\tthe OS/user default is used. If sepcified");
0161: System.err.println("\t\tincorrectly, GMT is quietly used.");
0162: System.err.println("\t-Duse.view=");
0163: System.err.println("\t\tAn initial view to be displayed.");
0164: System.err
0165: .println("\t\tValid names are: getter, hex, date, cal");
0166: System.err
0167: .println("\t\tIf incorrectly specified, getter is used.");
0168: System.err.println("\t\tThis becomes the default view.");
0169: } // usage
0170:
0171: /*
0172: * go This method reads the file, creates the table to display,
0173: * the window to display it in, and displays the window.
0174: * @param fileName the name of the file to read.
0175: * @param tryLines An estimate of the number of lines in
0176: * the file.
0177: */
0178: private void go(String[] args) {
0179:
0180: mainArgs = args;
0181: setDefaultTimeZone(); // let user override if needed
0182: // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0183: setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
0184: //
0185: JMenuBar menuBar = new JMenuBar();
0186: setJMenuBar(menuBar);
0187: addMenus(menuBar);
0188: /*
0189: * Add a fast close listener
0190: */
0191:
0192: addWindowListener(new WindowAdapter() {
0193: public void windowClosing(WindowEvent e) {
0194: setVisible(false);
0195: dispose();
0196: System.exit(0);
0197: }
0198: });
0199:
0200: //
0201: // Load current file, prime tables and JFrame.
0202: //
0203: currFile = new LoadedFile(mainArgs[0]);
0204: TableView tView = getDefaultTableView();
0205: resetDefaults(tView);
0206: //
0207: // Set max size at start, and display the window.
0208: //
0209: Dimension screenMax = Toolkit.getDefaultToolkit()
0210: .getScreenSize();
0211: setSize(screenMax);
0212: setVisible(true);
0213: }
0214:
0215: //
0216: // --> Private implementation methods follow.
0217: //
0218:
0219: /*
0220: * getDefaultTableView
0221: */
0222: private TableView getDefaultTableView() {
0223: // No user input.
0224: String viewStr = System.getProperty("use.view");
0225: if (viewStr == null) {
0226: jmiGetter.setEnabled(false);
0227: return new GetterTableView(currFile);
0228: }
0229: // Valid user input.
0230: if (viewStr.equalsIgnoreCase("hex")) {
0231: jmiHex.setEnabled(false);
0232: return new HexTableView(currFile);
0233: } else if (viewStr.equalsIgnoreCase("date")) {
0234: jmiDate.setEnabled(false);
0235: return new DateTableView(currFile);
0236: } else if (viewStr.equalsIgnoreCase("cal")) {
0237: jmiCal.setEnabled(false);
0238: return new CalTableView(currFile);
0239: } else if (viewStr.equalsIgnoreCase("getter")) {
0240: jmiGetter.setEnabled(false);
0241: return new GetterTableView(currFile);
0242: } else { // error by user
0243: System.err.println("View name: " + viewStr + " invalid.");
0244: jmiGetter.setEnabled(false);
0245: return new GetterTableView(currFile);
0246: }
0247: }
0248:
0249: /*
0250: * setDefaultTableView
0251: */
0252: private void setDefaultTableView(String newView) {
0253: System.setProperty("use.view", newView);
0254: }
0255:
0256: /*
0257: * setDefaultTimeZone
0258: */
0259: private void setDefaultTimeZone() {
0260: String tzName = System.getProperty("use.time.zone");
0261: if (tzName == null)
0262: return; // Use OS/user default.
0263: //
0264: // If tzName is bogus, not understood by the JRE,
0265: // 'getTimeZone' returns GMT.
0266: //
0267: TimeZone toSet = TimeZone.getTimeZone(tzName);
0268: //
0269: // Set default to whatever was returned.
0270: //
0271: TimeZone.setDefault(toSet);
0272: }
0273:
0274: /*
0275: * addMenus
0276: */
0277: private void addMenus(JMenuBar menuBar) {
0278: //
0279: // Create all the menus.
0280: //
0281: JMenu fileMenu = new JMenu("File");
0282: JMenu viewMenu = new JMenu("View");
0283: //
0284: // Add them to the menubar in order.
0285: //
0286: menuBar.add(fileMenu);
0287: menuBar.add(viewMenu);
0288: //
0289: // Create action objects and menu items.
0290: //
0291: Action open = new OpenAction();
0292: JMenuItem jmiOpen = new JMenuItem(open);
0293: Action exit = new ExitAction();
0294: JMenuItem jmiExit = new JMenuItem(exit);
0295: //
0296: // Next Menu
0297: //
0298: Action getter = new GetterAction();
0299: jmiGetter = new JMenuItem(getter);
0300: getter.setEnabled(true);
0301: //
0302: Action hex = new HexAction();
0303: jmiHex = new JMenuItem(hex);
0304: hex.setEnabled(true);
0305: //
0306: Action date = new DateAction();
0307: jmiDate = new JMenuItem(date);
0308: date.setEnabled(true);
0309: //
0310: Action cal = new CalAction();
0311: jmiCal = new JMenuItem(cal);
0312: cal.setEnabled(true);
0313: //
0314: // Build the file menu.
0315: //
0316: fileMenu.add(jmiOpen);
0317: fileMenu.addSeparator();
0318: fileMenu.add(jmiExit);
0319: //
0320: // Build the view menu.
0321: //
0322: viewMenu.add(jmiGetter);
0323: viewMenu.add(jmiHex);
0324: viewMenu.add(jmiDate);
0325: viewMenu.add(jmiCal);
0326: //
0327: // *temp Developer's code
0328: //
0329: // jmiGetter.setEnabled( false );
0330: //
0331: // JMenuItem getter2 = new JMenuItem( "getter2" );
0332: // getter2.addActionListener( new myMouseListener() );
0333: // viewMenu.add( getter2 );
0334: } // end of addMenus
0335:
0336: /*
0337: * A private method to dump the arrays of Object[][]
0338: * if desired by the developer
0339: * @param objs The array of arrays to be dumped.
0340: */
0341: private void dumpObjs(Object[][] objs, PrintStream out) {
0342: for (int i = 0; i < objs.length; ++i) {
0343: for (int j = 0; j < objs[i].length; ++j) {
0344: out.println(i + " " + j + " " + objs[i][j]);
0345: } // for j
0346: } // for i
0347: }
0348:
0349: /*
0350: * enableAll
0351: */
0352: private void enableAllViews() {
0353: jmiGetter.setEnabled(true);
0354: jmiHex.setEnabled(true);
0355: jmiDate.setEnabled(true);
0356: jmiCal.setEnabled(true);
0357: } // end of enableAllViews
0358:
0359: /*
0360: * getADate Returns a new DateTime object reference if possible,
0361: * otherwise null.
0362: * @return retDT A DateTime object reference.
0363: */
0364: private DateTime getADate(String s) {
0365: DateTime retDT = null;
0366: try {
0367: retDT = new DateTime(s);
0368: } // the try
0369: catch (IllegalArgumentException pe) {
0370: // ignore it here, caller sees null
0371: } // the catch
0372: return retDT;
0373: } // getADate
0374:
0375: //
0376: private static final String PADCHARS = "00000000000000000000000000000000";
0377:
0378: /*
0379: * LPad Return a String, left padded with '0's as specified
0380: * by the caller.
0381: */
0382: private String LPad(String inStr, int maxLen) {
0383: if (inStr.length() >= maxLen)
0384: return inStr.toUpperCase();
0385: String zeroes = PADCHARS.substring(0, maxLen - inStr.length());
0386: String retVal = zeroes + inStr;
0387: return retVal.toUpperCase();
0388: }
0389:
0390: /*
0391: * resetDefaults
0392: */
0393: private void resetDefaults(TableView tView) {
0394: Object[] colNames = tView.getColNames();
0395: Object[][] tableValues = tView.getCalcdValues();
0396: // dumpObjs( tableValues, System.out);
0397: JTable table = new JTable(tableValues, colNames);
0398: tView.setViewColumnsWidth(table);
0399: setTitle(tView.getViewTitle());
0400: //
0401: if (mainSP != null)
0402: getContentPane().remove(mainSP);
0403: mainSP = new JScrollPane(table);
0404: getContentPane().add(mainSP, "Center");
0405: validate();
0406: } // end of resetDefaults
0407:
0408: //
0409: // ----> Private internal classes follow.
0410: //
0411:
0412: /*
0413: * LoadedFile This class represents a file that has been loaded
0414: * for viewing.
0415: */
0416: private class LoadedFile {
0417: // Instance variables
0418: String fileName = null;
0419: ArrayList fileStrings = null;
0420: ArrayList dtObjects = null;
0421: int lineGuess = 0;
0422:
0423: /*
0424: * LoadedFile constructor.
0425: */
0426: LoadedFile(String fileName) {
0427: validateFile(fileName);
0428: this .fileName = fileName;
0429: //
0430: fileStrings = new ArrayList(lineGuess);
0431: dtObjects = new ArrayList(lineGuess);
0432:
0433: try {
0434: BufferedReader rdr = new BufferedReader(new FileReader(
0435: fileName));
0436: String inputLine = null;
0437: DateTime calculatedDT = null;
0438: int currLine = 0;
0439: while ((inputLine = rdr.readLine()) != null) {
0440: currLine++;
0441: inputLine = inputLine.trim();
0442: // Ignore blank and comment lines
0443: if (inputLine.length() == 0)
0444: continue;
0445: if (inputLine.charAt(0) == '#')
0446: continue;
0447: // Ignore lines which fail DateTime construction
0448: if ((calculatedDT = getADate(inputLine)) == null) {
0449: System.err.println("Parse failed for: "
0450: + inputLine + " at line number "
0451: + currLine);
0452: continue;
0453: }
0454: // Add the input file string and DateTime to lists
0455: fileStrings.add(inputLine);
0456: dtObjects.add(calculatedDT);
0457: }
0458: rdr.close();
0459: } catch (IOException ioe) {
0460: System.err.println("Load of file: " + fileName
0461: + " failed!");
0462: ioe.printStackTrace();
0463: System.exit(100);
0464: }
0465:
0466: // Try to be efficient (?really?)
0467: fileStrings.trimToSize();
0468: dtObjects.trimToSize();
0469: } // end of LoadedFile() constructor
0470:
0471: /*
0472: * General getters.
0473: */
0474: public String getFileName() {
0475: return fileName;
0476: }
0477:
0478: public int getLineGuess() {
0479: return lineGuess;
0480: }
0481:
0482: public ArrayList getFileStrings() {
0483: return fileStrings;
0484: }
0485:
0486: public ArrayList getDtObjects() {
0487: return dtObjects;
0488: }
0489:
0490: public int getLoadedFileSize() {
0491: if (dtObjects == null)
0492: return 0;
0493: return dtObjects.size();
0494: }
0495:
0496: /*
0497: * validateFile
0498: */
0499: private void validateFile(String fileName) {
0500: /*
0501: * Verify the user specified file exists and can
0502: * be read.
0503: */
0504: File f = new File(fileName);
0505: if (!f.exists() || !f.canRead()) {
0506: System.err.println("File: " + mainArgs[0]
0507: + " does not exist or cannot be read!");
0508: usage();
0509: System.exit(2);
0510: }
0511: /*
0512: * Try to get a reasonable estimate of the number of lines
0513: * in the file.
0514: */
0515: // Java does not do this right IMO. The number of bytes in a
0516: // file is a
0517: // long, but the length of a string is an int. Why?
0518: lineGuess = (int) (f.length() / (long) "YYYY-MM-DDTHH:MM:SS"
0519: .length());
0520: lineGuess += (lineGuess / 10);
0521: //
0522: // Debugging
0523: //
0524: if (false) {
0525: System.out.println("Line guess is: " + lineGuess);
0526: }
0527: } // end of validateFile(String)
0528: } // end of class LoadedFile class
0529:
0530: /*
0531: * TableView This abstract class defines the operations
0532: * necessary to create and access the Object arrays
0533: * required to create a JTable.
0534: */
0535: private abstract class TableView {
0536: protected Object[] colNames = null;
0537: protected Object[][] calcdValues = null;
0538: protected LoadedFile lddFile = null;
0539:
0540: //
0541: TableView(LoadedFile lddFile) {
0542: this .lddFile = lddFile;
0543: }
0544:
0545: //
0546: public Object[] getColNames() {
0547: return colNames;
0548: }
0549:
0550: public Object[][] getCalcdValues() {
0551: return calcdValues;
0552: }
0553:
0554: //
0555: abstract Object[] genColNames();
0556:
0557: abstract Object[][] genCalcdValues();
0558:
0559: abstract String getViewTitle();
0560:
0561: abstract void setViewColumnsWidth(JTable jt);
0562: //
0563: } // end of abstract class TableView
0564:
0565: /*
0566: * GetterTableView This class implements the operations
0567: * for the GetterView of the Jtable.
0568: */
0569: private class GetterTableView extends TableView {
0570: //
0571: GetterTableView(LoadedFile lddFile) {
0572: super (lddFile);
0573: setDefaultTableView("getter");
0574: colNames = genColNames();
0575: calcdValues = genCalcdValues();
0576: }
0577:
0578: /*
0579: * genCalcdValues is required by the base class.
0580: */
0581: Object[][] genCalcdValues() {
0582: Object[][] retValues = null;
0583: /*
0584: * Create an array of Objects that will contain
0585: * other arrays of Objects. (This is the 'column'
0586: * array).
0587: */
0588: ArrayList fileStrings = lddFile.getFileStrings();
0589: ArrayList dtObjects = lddFile.getDtObjects();
0590: int numRows = fileStrings.size();
0591: retValues = new Object[numRows][];
0592: int numCols = colNames.length;
0593: // System.err.println("NumCols : " + numCols);
0594: /*
0595: * Prime the array of arrays of Objects, allocating a new
0596: * secondary array for each of the primary array's
0597: * elements.
0598: */
0599: for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++nextStrNum) {
0600: retValues[nextStrNum] = new Object[numCols]; // get the 'col' array
0601: //****
0602: //* This needs to be sync'd with the colNames array.
0603: //****
0604: // Current row, 1st column
0605: int column = 0; // working row value
0606: String fileString = (String) fileStrings
0607: .get(nextStrNum);
0608: retValues[nextStrNum][column++] = fileString;
0609: // Current row, 2nd column
0610: DateTime adt = (DateTime) dtObjects.get(nextStrNum);
0611: String adtStr = adt.toString();
0612: retValues[nextStrNum][column++] = adtStr;
0613: // Current row, other columns.
0614: // Order here must match that specified in the colNames
0615: // array.
0616: retValues[nextStrNum][column++] = new Integer(adt
0617: .getMillisOfSecond());
0618: retValues[nextStrNum][column++] = new Integer(adt
0619: .getSecondOfMinute());
0620: retValues[nextStrNum][column++] = new Integer(adt
0621: .getMinuteOfHour());
0622: retValues[nextStrNum][column++] = new Integer(adt
0623: .getHourOfDay());
0624: retValues[nextStrNum][column++] = new Integer(adt
0625: .getDayOfWeek());
0626: retValues[nextStrNum][column++] = new Integer(adt
0627: .getDayOfMonth());
0628: retValues[nextStrNum][column++] = new Integer(adt
0629: .getDayOfYear());
0630: retValues[nextStrNum][column++] = new Integer(adt
0631: .getWeekOfWeekyear());
0632: retValues[nextStrNum][column++] = new Integer(adt
0633: .getWeekyear());
0634: retValues[nextStrNum][column++] = new Integer(adt
0635: .getMonthOfYear());
0636: retValues[nextStrNum][column++] = new Integer(adt
0637: .getYear());
0638: //
0639: } // the for
0640: if (debugf)
0641: dumpObjs(retValues, System.err);
0642: return retValues;
0643: } // end of genTBValues
0644:
0645: /*
0646: * genColNames is required by the base class.
0647: */
0648: Object[] genColNames() {
0649: Object[] retVal = { "FileString", "toString()",
0650: "MillisOfSec", "SecOfMin", "MinOfHr", "HrOfDay",
0651: "DayOfWk", "DayOfMon", "DayOfYr", "WeekOfWY",
0652: "Weekyear", "MonOfYr", "Year" };
0653: return retVal;
0654: }
0655:
0656: /*
0657: * getViewTitle
0658: */
0659: String getViewTitle() {
0660: return "DateTime.getXXX() Method Calculations" + " : "
0661: + TimeZone.getDefault().getDisplayName() + " : "
0662: + " Record Count " + currFile.getLoadedFileSize();
0663: }
0664:
0665: /*
0666: * setViewColumnLengths
0667: */
0668: void setViewColumnsWidth(JTable jt) {
0669: /*
0670: * Resize column 0, 1
0671: */
0672: TableColumnModel colmodel = jt.getColumnModel();
0673: TableColumn col0 = colmodel.getColumn(0);
0674: col0.setPreferredWidth(200);
0675: TableColumn col1 = colmodel.getColumn(1);
0676: col1.setPreferredWidth(200);
0677: return;
0678: }
0679:
0680: } // end of class getterTableView
0681:
0682: /*
0683: * HexView This class implements the operations for
0684: * the HexView of the file.
0685: */
0686: private class HexTableView extends TableView {
0687: //
0688: HexTableView(LoadedFile lddFile) {
0689: super (lddFile);
0690: setDefaultTableView("hex");
0691: colNames = genColNames();
0692: calcdValues = genCalcdValues();
0693: }
0694:
0695: /*
0696: * genCalcdValues is required by the base class.
0697: */
0698: Object[][] genCalcdValues() {
0699: Object[][] retValues = null;
0700: /*
0701: * Create an array of Objects that will contain
0702: * other arrays of Objects. (This is the 'column'
0703: * array).
0704: */
0705: ArrayList fileStrings = lddFile.getFileStrings();
0706: ArrayList dtObjects = lddFile.getDtObjects();
0707: int numRows = fileStrings.size();
0708: retValues = new Object[numRows][];
0709: int numCols = colNames.length;
0710: // System.err.println("NumCols : " + numCols);
0711: String fs = "yyyy-MM-dd'T'HH:mm:ss";
0712: DateFormat df = new SimpleDateFormat(fs);
0713: /*
0714: * Prime the array of arrays of Objects, allocating a new
0715: * secondary array for each of the primary array's
0716: * elements.
0717: */
0718: for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++nextStrNum) {
0719: retValues[nextStrNum] = new Object[numCols]; // get the 'col' array
0720: //****
0721: //* This needs to be sync'd with the colNames array.
0722: //****
0723: // Current row, 1st column
0724: int column = 0;
0725: String fileString = (String) fileStrings
0726: .get(nextStrNum);
0727: retValues[nextStrNum][column++] = fileString;
0728: // Current row, 2nd column
0729: DateTime adt = (DateTime) dtObjects.get(nextStrNum);
0730: String adtStr = adt.toString();
0731: retValues[nextStrNum][column++] = adtStr;
0732: // Current row, other columns.
0733: // Order here must match that specified in the colNames
0734: // array.
0735: long lVal = adt.getMillis();
0736: Long millis = new Long(lVal);
0737: retValues[nextStrNum][column++] = millis;
0738: String hexVal = Long.toHexString(lVal);
0739: String octalVal = Long.toOctalString(lVal);
0740: retValues[nextStrNum][column++] = "0"
0741: + LPad(octalVal, 22);
0742: retValues[nextStrNum][column++] = "0x"
0743: + LPad(hexVal, 16);
0744: //
0745: Date javaDate = null;
0746: try {
0747: javaDate = df.parse(fileString);
0748: } catch (ParseException e) {
0749: System.err.println("Parse failed for : "
0750: + fileString);
0751: // pe.printStackTrace();
0752: }
0753: //
0754: lVal = javaDate.getTime();
0755: millis = new Long(lVal);
0756: hexVal = Long.toHexString(lVal);
0757: octalVal = Long.toOctalString(lVal);
0758: retValues[nextStrNum][column++] = millis;
0759: retValues[nextStrNum][column++] = "0"
0760: + LPad(octalVal, 22);
0761: retValues[nextStrNum][column++] = "0x"
0762: + LPad(hexVal, 16);
0763: //
0764: } // the for
0765: if (debugf)
0766: dumpObjs(retValues, System.err);
0767: return retValues;
0768: } // end of genTBValues
0769:
0770: /*
0771: * genColNames is required by the base class.
0772: */
0773: Object[] genColNames() {
0774: Object[] retVal = { "FileString", "toString()",
0775: "JDT-millis", "JDT-Oct", "JDT-Hex", "Date-millis",
0776: "Date-Oct", "Date-Hex" };
0777: return retVal;
0778: }
0779:
0780: /*
0781: * getViewTitle
0782: */
0783: String getViewTitle() {
0784: return "View the long values" + " : "
0785: + TimeZone.getDefault().getDisplayName() + " : "
0786: + " Record Count " + currFile.getLoadedFileSize();
0787: }
0788:
0789: /*
0790: * setViewColumnLengths
0791: */
0792: void setViewColumnsWidth(JTable jt) {
0793: return;
0794: }
0795:
0796: } // end of class HexTableView
0797:
0798: /*
0799: * DateTableView This class implements the operations for
0800: * the java.util.Date of the file.
0801: */
0802: private class DateTableView extends TableView {
0803: //
0804: DateTableView(LoadedFile lddFile) {
0805: super (lddFile);
0806: setDefaultTableView("date");
0807: colNames = genColNames();
0808: calcdValues = genCalcdValues();
0809: }
0810:
0811: /*
0812: * genCalcdValues is required by the base class.
0813: */
0814: Object[][] genCalcdValues() {
0815: Object[][] retValues = null;
0816: /*
0817: * Create an array of Objects that will contain
0818: * other arrays of Objects. (This is the 'column'
0819: * array).
0820: */
0821: ArrayList fileStrings = lddFile.getFileStrings();
0822: ArrayList dtObjects = lddFile.getDtObjects();
0823: int numRows = fileStrings.size();
0824: retValues = new Object[numRows][];
0825: int numCols = colNames.length;
0826: // System.err.println("NumCols : " + numCols);
0827: /*
0828: * Prime the array of arrays of Objects, allocating a new
0829: * secondary array for each of the primary array's
0830: * elements.
0831: */
0832: for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++nextStrNum) {
0833: retValues[nextStrNum] = new Object[numCols]; // get the 'col' array
0834: //****
0835: //* This needs to be sync'd with the colNames array.
0836: //****
0837: // Current row, 1st column
0838: int column = 0;
0839: String fileString = (String) fileStrings
0840: .get(nextStrNum);
0841: retValues[nextStrNum][column++] = fileString;
0842: // Current row, 2nd column
0843: DateTime adt = (DateTime) dtObjects.get(nextStrNum);
0844: String adtStr = adt.toString();
0845: retValues[nextStrNum][column++] = adtStr;
0846: // Current row, other columns.
0847: // Order here must match that specified in the colNames
0848: // array.
0849: long lVal = adt.getMillis();
0850: java.util.Date jDate = new java.util.Date(lVal);
0851: retValues[nextStrNum][column++] = new Integer(jDate
0852: .getSeconds());
0853: retValues[nextStrNum][column++] = new Integer(jDate
0854: .getMinutes());
0855: retValues[nextStrNum][column++] = new Integer(jDate
0856: .getHours());
0857: retValues[nextStrNum][column++] = new Integer(jDate
0858: .getDay());
0859: retValues[nextStrNum][column++] = new Integer(jDate
0860: .getDate());
0861: retValues[nextStrNum][column++] = new Integer(jDate
0862: .getMonth());
0863: retValues[nextStrNum][column++] = new Integer(jDate
0864: .getYear());
0865: //
0866: } // the for
0867: if (debugf)
0868: dumpObjs(retValues, System.err);
0869: return retValues;
0870: } // end of genTBValues
0871:
0872: /*
0873: * genColNames is required by the base class.
0874: */
0875: Object[] genColNames() {
0876: Object[] retVal = { "FileString", // 0
0877: "toString()", // 1
0878: "Seconds", // 2
0879: "Minutes", // 3
0880: "Hours", // 4
0881: "Day Of Week", // 5
0882: "Day Of Month", // 6
0883: "Month", // 7
0884: "Year" // 8
0885: };
0886: return retVal;
0887: }
0888:
0889: /*
0890: * getViewTitle
0891: */
0892: String getViewTitle() {
0893: return "java.util.Date getXXX" + " : "
0894: + TimeZone.getDefault().getDisplayName() + " : "
0895: + " Record Count " + currFile.getLoadedFileSize();
0896: }
0897:
0898: /*
0899: * setViewColumnLengths
0900: */
0901: void setViewColumnsWidth(JTable jt) {
0902: /*
0903: * Resize column 0, 1
0904: */
0905: TableColumnModel colmodel = jt.getColumnModel();
0906: TableColumn col0 = colmodel.getColumn(0);
0907: col0.setPreferredWidth(150);
0908: TableColumn col1 = colmodel.getColumn(1);
0909: col1.setPreferredWidth(150);
0910: return;
0911: }
0912:
0913: } // end of class DateTableView
0914:
0915: /*
0916: * CalTableView This class implements the operations for
0917: * the java.util.Date of the file.
0918: */
0919: private class CalTableView extends TableView {
0920: //
0921: CalTableView(LoadedFile lddFile) {
0922: super (lddFile);
0923: setDefaultTableView("cal");
0924: colNames = genColNames();
0925: calcdValues = genCalcdValues();
0926: }
0927:
0928: /*
0929: * genCalcdValues is required by the base class.
0930: */
0931: Object[][] genCalcdValues() {
0932: Object[][] retValues = null;
0933: /*
0934: * Create an array of Objects that will contain
0935: * other arrays of Objects. (This is the 'column'
0936: * array).
0937: */
0938: ArrayList fileStrings = lddFile.getFileStrings();
0939: ArrayList dtObjects = lddFile.getDtObjects();
0940: int numRows = fileStrings.size();
0941: retValues = new Object[numRows][];
0942: int numCols = colNames.length;
0943: // System.err.println("NumCols : " + numCols);
0944: /*
0945: * Prime the array of arrays of Objects, allocating a new
0946: * secondary array for each of the primary array's
0947: * elements.
0948: */
0949: for (int nextStrNum = 0; nextStrNum < fileStrings.size(); ++nextStrNum) {
0950: retValues[nextStrNum] = new Object[numCols]; // get the 'col' array
0951: //****
0952: //* This needs to be sync'd with the colNames array.
0953: //****
0954: // Current row, 1st column
0955: int column = 0;
0956: String fileString = (String) fileStrings
0957: .get(nextStrNum);
0958: retValues[nextStrNum][column++] = fileString;
0959: // Current row, 2nd column
0960: DateTime adt = (DateTime) dtObjects.get(nextStrNum);
0961: String adtStr = adt.toString();
0962: retValues[nextStrNum][column++] = adtStr;
0963: // Current row, other columns.
0964: // Order here must match that specified in the colNames
0965: // array.
0966: long lVal = adt.getMillis();
0967: GregorianCalendar cal = new GregorianCalendar();
0968: cal.setTime(new Date(lVal));
0969: cal.setMinimalDaysInFirstWeek(4);
0970: retValues[nextStrNum][column++] = new Integer(cal
0971: .get(Calendar.MILLISECOND));
0972: retValues[nextStrNum][column++] = new Integer(cal
0973: .get(Calendar.SECOND));
0974: retValues[nextStrNum][column++] = new Integer(cal
0975: .get(Calendar.MINUTE));
0976: retValues[nextStrNum][column++] = new Integer(cal
0977: .get(Calendar.HOUR_OF_DAY));
0978: retValues[nextStrNum][column++] = new Integer(cal
0979: .get(Calendar.DAY_OF_WEEK));
0980: retValues[nextStrNum][column++] = new Integer(cal
0981: .get(Calendar.DAY_OF_MONTH));
0982: retValues[nextStrNum][column++] = new Integer(cal
0983: .get(Calendar.DAY_OF_YEAR));
0984: retValues[nextStrNum][column++] = new Integer(cal
0985: .get(Calendar.WEEK_OF_YEAR));
0986: retValues[nextStrNum][column++] = new Integer(cal
0987: .get(Calendar.MONTH));
0988: retValues[nextStrNum][column++] = new Integer(cal
0989: .get(Calendar.YEAR));
0990: //
0991: } // the for
0992: if (debugf)
0993: dumpObjs(retValues, System.err);
0994: return retValues;
0995: } // end of genTBValues
0996:
0997: /*
0998: * genColNames is required by the base class.
0999: */
1000: Object[] genColNames() {
1001: Object[] retVal = { "FileString", // 0
1002: "toString()", // 1
1003: "Millis", // 2
1004: "Sec", // 3
1005: "Min", // 4
1006: "HrOfDay", // 5
1007: "DayOfWeek", // 6
1008: "DayOfMon", // 7
1009: "DayOfYr", // 8
1010: "WkofYr", // 9
1011: "MonOfYr", // 10
1012: "Year" // 11
1013: };
1014: return retVal;
1015: }
1016:
1017: /*
1018: * getViewTitle
1019: */
1020: String getViewTitle() {
1021: return "java.util.Calendar.get(int)" + " : "
1022: + TimeZone.getDefault().getDisplayName() + " : "
1023: + " Record Count " + currFile.getLoadedFileSize();
1024: }
1025:
1026: /*
1027: * setViewColumnLengths
1028: */
1029: void setViewColumnsWidth(JTable jt) {
1030: /*
1031: * Resize column 0, 1
1032: */
1033: TableColumnModel colmodel = jt.getColumnModel();
1034: TableColumn col0 = colmodel.getColumn(0);
1035: col0.setPreferredWidth(175);
1036: TableColumn col1 = colmodel.getColumn(1);
1037: col1.setPreferredWidth(175);
1038: return;
1039: }
1040:
1041: } // end of class CalTableView
1042:
1043: /*
1044: * OpenAction
1045: */
1046: private class OpenAction extends AbstractAction {
1047: /*
1048: * Constructor
1049: */
1050: public OpenAction() {
1051: super ("Open");
1052: } // end of ctor
1053:
1054: /*
1055: * actionPerformed
1056: */
1057: public void actionPerformed(ActionEvent e) {
1058: int result = chooser.showOpenDialog(DateTimeBrowser.this );
1059: String canPath = null;
1060: if (result == JFileChooser.APPROVE_OPTION) {
1061: File chosenFile = chooser.getSelectedFile();
1062: try {
1063: canPath = chosenFile.getCanonicalPath();
1064: } catch (IOException ioe) {
1065: System.err.println("I/O Error on file: "
1066: + chosenFile);
1067: // Ignore it for now.
1068: }
1069: enableAllViews();
1070: currFile = new LoadedFile(canPath);
1071: TableView tView = getDefaultTableView();
1072: resetDefaults(tView);
1073: } // end of if a file actually chosen.
1074: } // end of actionPerformed
1075: } // end of class OpenAction
1076:
1077: /*
1078: * ExitAction
1079: */
1080: private class ExitAction extends AbstractAction {
1081: /*
1082: * Constructor
1083: */
1084: public ExitAction() {
1085: super ("Exit");
1086: } // end of ctor
1087:
1088: /*
1089: * actionPerformed
1090: */
1091: public void actionPerformed(ActionEvent e) {
1092: DateTimeBrowser.this .setVisible(false);
1093: DateTimeBrowser.this .dispose();
1094: System.exit(0);
1095: } // end of actionPerformed
1096: } // end of class OpenAction
1097:
1098: /*
1099: * GetterAction
1100: */
1101: private class GetterAction extends AbstractAction {
1102: /*
1103: * Constructor
1104: */
1105: public GetterAction() {
1106: super ("Getter");
1107: } // end of ctor
1108:
1109: /*
1110: * actionPerformed
1111: */
1112: public void actionPerformed(ActionEvent e) {
1113: TableView tView = new GetterTableView(currFile);
1114: resetDefaults(tView);
1115: enableAllViews();
1116: jmiGetter.setEnabled(false);
1117: } // end of actionPerformed
1118: } // end of class OpenAction
1119:
1120: /*
1121: * HexAction
1122: */
1123: private class HexAction extends AbstractAction {
1124: /*
1125: * Constructor
1126: */
1127: public HexAction() {
1128: super ("Hex");
1129: } // end of ctor
1130:
1131: /*
1132: * actionPerformed
1133: */
1134: public void actionPerformed(ActionEvent e) {
1135: TableView tView = new HexTableView(currFile);
1136: resetDefaults(tView);
1137: enableAllViews();
1138: jmiHex.setEnabled(false);
1139: } // end of actionPerformed
1140: } // end of class OpenAction
1141:
1142: /*
1143: * DateAction
1144: */
1145: private class DateAction extends AbstractAction {
1146: /*
1147: * Constructor
1148: */
1149: public DateAction() {
1150: super ("Date");
1151: } // end of ctor
1152:
1153: /*
1154: * actionPerformed
1155: */
1156: public void actionPerformed(ActionEvent e) {
1157: TableView tView = new DateTableView(currFile);
1158: resetDefaults(tView);
1159: enableAllViews();
1160: jmiDate.setEnabled(false);
1161: } // end of actionPerformed
1162: } // end of class DateAction
1163:
1164: /*
1165: * CalAction
1166: */
1167: private class CalAction extends AbstractAction {
1168: /*
1169: * Constructor
1170: */
1171: public CalAction() {
1172: super ("Calendar");
1173: } // end of ctor
1174:
1175: /*
1176: * actionPerformed
1177: */
1178: public void actionPerformed(ActionEvent e) {
1179: TableView tView = new CalTableView(currFile);
1180: resetDefaults(tView);
1181: enableAllViews();
1182: jmiCal.setEnabled(false);
1183: } // end of actionPerformed
1184: } // end of class CalAction
1185:
1186: } // class DateTimeBrowser
|