home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / Source.bin / MatrixEnumeration.java < prev    next >
Encoding:
Java Source  |  1998-09-14  |  4.8 KB  |  226 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.util.Enumeration;
  5. import java.text.MessageFormat;
  6. import java.util.ResourceBundle;
  7.  
  8.  
  9. //     01/30/97    RKM    Integrated fix to hasMoreElements from flash@tansu.slab.tnr.sharp.co.jp (George Rome Borden IV)
  10. //     03/27/97    RKM    Made class public, so peole could use it
  11. //                    Made class final
  12. //                    Tried to make some sense of protection on methods & members
  13. //     04/17/97    LAB Added Don Haney's (haney@tiac.net) fix that addressed a NullPointerException in hasMoreElements().
  14.  
  15. /**
  16.  * This class is used to enumerate Matrix elements. Enumeration is used to
  17.  * sequentially scan through all the elements in a Matrix one at a time.
  18.  * @see symantec.itools.awt.Matrix
  19.  * @see java.util.Enumeration
  20.  *
  21.  * @version 1.0, Nov 26, 1996
  22.  *
  23.  * @author    Symantec
  24.  *
  25.  */
  26.  
  27. public final class MatrixEnumeration
  28.     implements Enumeration
  29. {
  30.     //--------------------------------------------------
  31.     // public constructors
  32.     //--------------------------------------------------
  33.  
  34.     /**
  35.      * Constructs a matrix enumerator for the given Matrix.
  36.      */
  37.     public MatrixEnumeration(Matrix matrix)
  38.     {        
  39.         try
  40.         {
  41.             errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  42.         }
  43.         catch(Throwable ex)
  44.         {
  45.             errors = new symantec.itools.resources.ErrorsBundle();
  46.         }
  47.         
  48.         m = matrix;
  49.         started = false;
  50.     }
  51.  
  52.     //--------------------------------------------------
  53.     // public members
  54.     //--------------------------------------------------
  55.  
  56.     /**
  57.      * Returns true if more elements are available to enumerate.
  58.      * This is a standard Java Enumeration interface method.
  59.      * @return true if more elements available, false if done enumerating
  60.      */
  61.     public boolean hasMoreElements()
  62.     {
  63.         if (m == null)
  64.             return false;
  65.         if (started == false && m.o != null) return true;
  66.         if (m.nextElt != null) return true;
  67.         return hasMoreRows();
  68.     }
  69.  
  70.     /**
  71.      * Returns true if more rows are available to enumerate.
  72.      */
  73.     public boolean hasMoreRows()
  74.     {
  75.         if (m.nextRow == null)
  76.         {
  77.             return false;
  78.         }
  79.  
  80.         Matrix n = m.nextRow;
  81.  
  82.         //find next row
  83.         while(n.o == null && n.nextElt == null)
  84.         {
  85.             if ((n = n.nextRow) == null)  return false;
  86.         }
  87.  
  88.         return true;
  89.     }
  90.  
  91.     /**
  92.      * Returns the next element.
  93.      * This is a standard Java Enumeration interface method.
  94.      */
  95.     public Object nextElement()
  96.     {
  97.         if (!started)
  98.         {
  99.             started = true;
  100.  
  101.             if (m.o != null)
  102.             {
  103.                 return m.o;
  104.             }
  105.         }
  106.  
  107.         if (m == null)
  108.         {
  109.             return null;
  110.         }
  111.  
  112.         if (m.nextElt != null)
  113.         {
  114.             m = m.nextElt;
  115.         } else {
  116.  
  117.                 if ((m = findNextRow()) == null)
  118.                 {
  119.                     return null;
  120.                 }
  121.                 }
  122.  
  123.         return m.o;
  124.     }
  125.  
  126.     /**
  127.      * Returns the row number of the current element.
  128.      */
  129.     public int currRow()
  130.     {
  131.         return m.row;
  132.     }
  133.  
  134.     /**
  135.      * Returns the column number of the current element.
  136.      */
  137.     public int currCol()
  138.     {
  139.         return m.col;
  140.     }
  141.  
  142.     /**
  143.      * Skips enumeration ahead to the start of the next row.
  144.      * @return the first element in the next row.
  145.      */
  146.     public Object nextRow()
  147.     {
  148.         m = findNextRow();
  149.  
  150.         return m.o;
  151.     }
  152.  
  153.     /**
  154.      * Skips enumeration ahead to the start of the given row.
  155.      * @param r the row to skip ahead to. Must be greater than the current row
  156.      * @return the first element in the given row
  157.      * @exception IllegalArgumentException if r is less than or equal to the
  158.      * current row
  159.      */
  160.     public Object advanceTo(int r)
  161.         throws IllegalArgumentException
  162.     {
  163.         started = true;
  164.  
  165.         if (r < m.row || r == m.row)
  166.         {
  167.             Object[] args = { new Integer(r), new Integer(m.row) };
  168.             throw new
  169.                IllegalArgumentException(MessageFormat.format(errors.getString("MustBeGreaterThanCurrentRow"), args));
  170.         }
  171.  
  172.         Matrix n = m;
  173.  
  174.         while (m.row < r)
  175.         {
  176.             m = findNextRow();
  177.  
  178.             if (m == null)
  179.             {
  180.                 m = n;
  181.                 Object[] args = { new Integer(r) };
  182.                 throw new
  183.                    IllegalArgumentException(MessageFormat.format(errors.getString("RowTooLarge"), args));
  184.             }
  185.         }
  186.  
  187.         return m.o;
  188.     }
  189.  
  190.     //--------------------------------------------------
  191.     // private members
  192.     //--------------------------------------------------
  193.  
  194.     private Matrix findNextRow()
  195.     {
  196.         started = true;
  197.         Matrix n = m.nextRow;
  198.  
  199.         if (n == null)
  200.         {
  201.             return null;
  202.         }
  203.  
  204.         //find next row
  205.         while(n.o == null && n.nextElt == null)
  206.         {
  207.             if ((n = n.nextRow) == null)
  208.             {
  209.                 return null;
  210.             }
  211.         }
  212.  
  213.         n = n.o != null  ?n  :n.nextElt;
  214.         return n;
  215.     }
  216.  
  217.     transient protected ResourceBundle errors;
  218.  
  219.     //--------------------------------------------------
  220.     // private members
  221.     //--------------------------------------------------
  222.  
  223.     private Matrix        m;
  224.     private boolean    started;
  225. }
  226.