home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / FileTableModel.java < prev    next >
Text File  |  1998-10-07  |  7KB  |  228 lines

  1. package com.symantec.itools.swing.models;
  2.  
  3. import java.io.*;
  4. import java.net.URL;
  5. import java.math.*;
  6. import java.util.Vector;
  7. import java.util.StringTokenizer;
  8.  
  9. // A table model that uses a tab, comma or space - delimited file as input.
  10. // Excess column headers beyond rowCount, which is pinned to the widest data
  11. // row, are ignored by JTable and not shown.
  12. public class FileTableModel
  13.     extends com.sun.java.swing.table.AbstractTableModel
  14.     implements Serializable
  15. {
  16.     public static final int TAB_DELIMITED = 1;
  17.     public static final int COMMA_DELIMITED = 2;
  18.     public static final int SPACE_DELIMITED = 3;
  19.     protected static final int POSSIBLE_DELIMITERS = 3;
  20.  
  21.     protected int rowCount = 0;
  22.     protected int colCount = 0;
  23.     protected int numHeaders = 0;
  24.  
  25.     protected Vector dataVector = new Vector(rowCount);
  26.     protected Vector headerVector = new Vector(colCount);
  27.     protected URL dataURL;
  28.  
  29.     protected int delimiter = TAB_DELIMITED;
  30.     protected String delimiterString = "\t";
  31.  
  32.     //
  33.     // com.sun.java.swing.table.TableModel implementation
  34.     //
  35.  
  36.     // Returns number of rows of data in table.
  37.     public int getRowCount()
  38.     {
  39.         return rowCount;
  40.     }
  41.     // Returns the number of columns of data (not headers)
  42.     public int getColumnCount()
  43.     {
  44.         return colCount;
  45.     }
  46.     //Returns columns header; a single space if it has been padded.
  47.     public String getColumnName(int column)
  48.     {
  49.        return (String)headerVector.elementAt(column);
  50.     }
  51.  
  52.     // JTable can hold any object, but in this model  will always
  53.     // return a class of  String.
  54.     public Class getColumnClass(int columnIndex)
  55.     {
  56.         return String.class;
  57.     }
  58.  
  59.     // Return value from a specified cell.
  60.     public Object getValueAt(int row, int column)
  61.     {
  62.         return (((Vector)dataVector.elementAt(row)).elementAt(column));
  63.     }
  64.     // Set value in a specified cell.
  65.     // Does not filter out non-String objects.
  66.     public void setValueAt(Object aValue, int row, int column)
  67.     {
  68.         ((Vector)dataVector.elementAt(row)).setElementAt(aValue, column);
  69.         fireTableCellUpdated(row,column);
  70.     }
  71.     // Return false
  72.     public boolean isCellEditable(int rowIndex, int columnIndex)
  73.     {
  74.         return false;
  75.     }
  76.  
  77.     //
  78.     // Properties
  79.     //
  80.  
  81.     // Return the URL for this models associated file.
  82.     public URL getItems()
  83.     {
  84.        return dataURL;
  85.     }
  86.  
  87.     // Takes a URL to a tab, comma or space-delimited text file.
  88.     // Pads all rows to length of longest row.
  89.     // Pads headers with spaces to length of longest row.
  90.     public void setItems(URL newItems)
  91.     {
  92.         dataVector = new Vector();
  93.         dataURL = newItems;
  94.         if(dataURL != null)
  95.         {
  96.             try
  97.             {
  98.                 colCount = 0;
  99.                 BufferedReader input = new BufferedReader(new InputStreamReader((InputStream)newItems.getContent()));
  100.                 StringTokenizer currentLine;
  101.                 Vector newRow = new Vector();
  102.                 String nextLine;
  103.                 while((nextLine = input.readLine())!= null)
  104.                 {
  105.                     currentLine = new StringTokenizer(nextLine,delimiterString);
  106.                     newRow = new Vector();
  107.                     while(currentLine.hasMoreTokens())
  108.                     {
  109.                         newRow.addElement(currentLine.nextToken());
  110.                         if (newRow.size() > numHeaders)
  111.                         {
  112.                             //pad headers if needed
  113.                             headerVector.addElement(" ");
  114.                             numHeaders++;
  115.                         }
  116.                     }
  117.                      colCount = Math.max(newRow.size(),colCount);
  118.                      dataVector.addElement(newRow);
  119.                 }
  120.                 rowCount = dataVector.size();
  121.  
  122.                 // pad any short rows
  123.                 for(int i = 0; i < dataVector.size();i++)
  124.                 {
  125.                     int difference = colCount - ((Vector)dataVector.elementAt(i)).size();
  126.                     if(difference >0)
  127.                         for(int j = 0; j< difference; j++)
  128.                         {
  129.                          ((Vector)dataVector.elementAt(i)).addElement("");
  130.                         }
  131.  
  132.                 }
  133.  
  134.                 input.close();
  135.             }
  136.             catch (java.io.IOException e)
  137.             {
  138.                 e.printStackTrace();
  139.             }
  140.             fireTableStructureChanged();
  141.             fireTableDataChanged();
  142.  
  143.         }
  144.         else
  145.         {
  146.           Vector emptyRow = new Vector(colCount);
  147.           for(int i = 0; i < Math.max(colCount,numHeaders);i++)
  148.             emptyRow.addElement("");
  149.           dataVector.addElement(emptyRow);
  150.           fireTableStructureChanged();
  151.           fireTableDataChanged();
  152.         }
  153.     }
  154.  
  155.     // Return column headers as a comma-delimited String.
  156.     public String getColumnHeaders()
  157.     {
  158.         StringBuffer temp = new StringBuffer();
  159.         for(int i = 0; i < headerVector.size();i++)
  160.         {
  161.             if (temp.length() > 0)
  162.             {
  163.                 temp.append(",");
  164.             }
  165.             temp.append(headerVector.elementAt(i));
  166.         }
  167.     return temp.toString();
  168.     }
  169.  
  170.  
  171.     // Takes a comma-delimited string and converts it too column headers.
  172.     // If headers are shorter than data (colCount) they are padded with a space.
  173.     public void setColumnHeaders(String newHeaders)
  174.     {
  175.         if (newHeaders == null)
  176.             newHeaders = "";
  177.  
  178.         StringTokenizer headers = new StringTokenizer(newHeaders,",");
  179.         headerVector = new Vector();
  180.         numHeaders = headers.countTokens();
  181.  
  182.         for (int j = 0; j < Math.max(colCount,numHeaders);j++)
  183.         {
  184.             if (headers.hasMoreTokens())
  185.                 headerVector.addElement( headers.nextToken());
  186.             else  // Rod, leave the space or the headers won't draw.
  187.                 headerVector.addElement(" ");
  188.         }
  189.  
  190.         fireTableStructureChanged();
  191.         fireTableDataChanged();
  192.     }
  193.  
  194.     // Return the deliimiter.
  195.     // One of TAB_DELIMITED, COMMA_DELIMITED or SPACE_DELIMITED.
  196.     public int getDelimiter()
  197.     {
  198.         return delimiter;
  199.     }
  200.  
  201.     // Set the deliimiter.
  202.     // One of TAB_DELIMITED, COMMA_DELIMITED or SPACE_DELIMITED.
  203.     public void setDelimiter(int d)
  204.         throws IllegalArgumentException
  205.     {
  206.         if(0 < d && d < (POSSIBLE_DELIMITERS+1))
  207.             delimiter = d;
  208.         else
  209.             throw new IllegalArgumentException("delimiter must be between 1 and "+POSSIBLE_DELIMITERS+" inclusive");
  210.  
  211.         switch(delimiter)
  212.         {
  213.             case TAB_DELIMITED: {delimiterString = "\t";break;}
  214.             case COMMA_DELIMITED: {delimiterString = ",";break;}
  215.             case SPACE_DELIMITED: {delimiterString = " ";break;}
  216.             default : {delimiterString = "\t";break;}
  217.         }
  218.         setColumnHeaders(getColumnHeaders());
  219.         setItems(getItems());
  220.         fireTableStructureChanged();
  221.         fireTableDataChanged();
  222.     }
  223.  
  224.  
  225.         //{{DECLARE_CONTROLS
  226.         //}}
  227. }
  228.