Update of /cvsroot/displaytag/displaytag/src/org/apache/taglibs/display
In directory sc8-pr-cvs1:/tmp/cvs-serv21795/src/org/apache/taglibs/display

Modified Files:
        TableTag.java 
Log Message:
added Zorzella's patch



Index: TableTag.java
===================================================================
RCS file: /cvsroot/displaytag/displaytag/src/org/apache/taglibs/display/TableTag.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** TableTag.java       24 Feb 2003 17:41:13 -0000      1.3
--- TableTag.java       24 Feb 2003 19:37:15 -0000      1.4
***************
*** 16,26 ****
--- 16,30 ----
  
  import java.lang.reflect.InvocationTargetException;
+ import java.sql.ResultSet;
  import java.text.MessageFormat;
  import java.util.ArrayList;
  import java.util.Arrays;
+ import java.util.Collection;
  import java.util.Collections;
+ import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.Iterator;
  import java.util.List;
+ import java.util.Map;
  import java.util.Properties;
  import java.util.StringTokenizer;
***************
*** 34,37 ****
--- 38,50 ----
  
  import org.apache.commons.beanutils.PropertyUtils;
+ import org.apache.commons.beanutils.RowSetDynaClass;
+ 
+ /* This would allow certain kinds of stuctures to be used (rather than
+  * java.util.Collections), but I did not want to introduce a dependency
+  * on struts for compilation...
+  *
+  * import org.apache.struts.util.*;
+  *
+  */
  
  /**
***************
*** 159,164 ****
     private int exportType = EXPORT_TYPE_NONE;
  
-    private List completeList = null;
- 
     // -------------------------------------------------------- Accessor methods
  
--- 172,175 ----
***************
*** 734,737 ****
--- 745,772 ----
     }
  
+     /** Given an Object, let's do our best to iterate over it
+      */
+     public static Iterator getIterator (Object o) throws JspException {
+ 
+       Iterator iterator = null;
+       
+       if( o instanceof Collection ) {
+           iterator = ( (Collection)o ).iterator();
+       } else if( o instanceof Iterator ) {
+           iterator = (Iterator)o;
+       } else if( o instanceof Map ) {
+           iterator = ( (Map)o ).entrySet().iterator();
+           /* 
+            *   This depends on importing struts.util.* -- see remarks in the import 
section of the file
+            *
+            * } else if( o instanceof Enumeration ) {
+            *     iterator = new IteratorAdapter( (Enumeration)o );
+            * }
+           */
+       } else {
+           throw new JspException( "I do not know how to iterate over '" + 
o.getClass() + "'.");
+       }
+       return iterator;
+     }
  
     /**
***************
*** 774,810 ****
  
  
!       Iterator iterator = null;
! 
!       // Todo - these needs cleaned up, we only show lists....
! 
        if( collection.getClass().isArray() )
           collection = Arrays.asList( (Object[])collection );
  
!       if( collection instanceof List ) {
!          collection = (List)collection;
!       } else {
!          Object[] objs = {collection};
!          String msg =
!             MessageFormat.format( prop.getProperty( "error.msg.invalid_bean" ), objs 
);
! 
!          throw new JspException( msg );
!       }
! 
!       // TODO - Deal with RowSets, ResultSets, Maps, Enumerations, etc...
! 
!       /*
!        iterator = ( (Collection)collection ).iterator();
!       } else if( collection instanceof Iterator ) {
!          iterator = (Iterator)collection;
!       } else if( collection instanceof Map ) {
!          iterator = ( (Map)collection ).entrySet().iterator();
!       } else if( collection instanceof Enumeration ) {
!          iterator = new IteratorAdapter( (Enumeration)collection );
!       } else {
!          throw new JspException( "Could not figure out how to iterator over " +
!             "the stuff that you gave me..." );
!       }
!       */
! 
  
        // If the user has changed the way our default behavior works, then we
--- 809,833 ----
  
  
!       /* YIKES! If we use a RowSetDynaClass to wrap a ResultSet, we'd
!        * copy the entire set to a List -- not good at all if paging
!        * (though functional -- I tested). Furthermore, RowSetDynaClass
!        * is not yet officially released, so I'm leaving this out for
!        * now. 
!        *
!        * Anyway, the proper semantics would involve a class like ResultSetDynaClass,
!        * but with some changes to the Iterator I'l trying to work on.
!        *
!        * if (collection instanceof ResultSet)
!        *   try {
!        *       collection = new RowSetDynaClass((ResultSet)collection).getRows ();
!        *   } catch (java.sql.SQLException e) {
!        *       throw new JspException( "Problems iterating over ResultSet.", e);
!        *       }
!        */
!       
        if( collection.getClass().isArray() )
           collection = Arrays.asList( (Object[])collection );
  
!       Iterator iterator = getIterator (collection);
  
        // If the user has changed the way our default behavior works, then we
***************
*** 815,823 ****
        // Load our table decorator if it is requested
        this.dec = this.loadDecorator();
!       if( this.dec != null ) this.dec.init( this.pageContext, (List)collection );
  
        if( !prop.getProperty( "sort.behavior" ).equals( "page" ) ) {
           // Sort the total list...
!          this.sortDataIfNeeded( (List)collection );
  
  
--- 838,846 ----
        // Load our table decorator if it is requested
        this.dec = this.loadDecorator();
!       if( this.dec != null ) this.dec.init( this.pageContext, collection );
  
        if( !prop.getProperty( "sort.behavior" ).equals( "page" ) ) {
           // Sort the total list...
!          this.sortDataIfNeeded(collection );
  
  
***************
*** 833,839 ****
        }
  
-       this.completeList = (List)collection;
-       iterator = ( (List)collection ).listIterator();
- 
        // If they have asked for an subset of the list via the offset or length
        // attributes, then only fetch those items out of the master list.
--- 856,859 ----
***************
*** 861,864 ****
--- 881,886 ----
        int pagesizeValue = this.getPagesizeValue();
        if( pagesizeValue > 0 ) {
+         if (! (collection instanceof List) )
+             throw new JspException( "Paging is not available for collections of type 
'" + collection.getClass() + "'.");
           helper = new SmartListHelper( (List)collection, pagesizeValue, this.prop );
           // tlaw 12-10-2001
***************
*** 890,895 ****
      */
  
!    private void sortDataIfNeeded( List viewableData )
     {
  
        // At this point we have all the objects that are supposed to be shown
--- 912,920 ----
      */
  
!    private void sortDataIfNeeded( Object viewableData )
     {
+        if (! (viewableData instanceof List))
+          throw new RuntimeException ("This function is only supported if the given 
collection is a java.util.List.");
+    
  
        // At this point we have all the objects that are supposed to be shown
***************
*** 912,916 ****
  
           if( this.sortOrder == SORT_ORDER_DECENDING ) {
!             Collections.reverse( viewableData );
           }
        }
--- 937,941 ----
  
           if( this.sortOrder == SORT_ORDER_DECENDING ) {
!             Collections.reverse( (List)viewableData );
           }
        }
***************
*** 935,939 ****
           colDecorators[c] = loadColumnDecorator( columnDecorator );
           if( colDecorators[c] != null )
!             colDecorators[c].init( this.pageContext, this.completeList );
        }
  
--- 960,964 ----
           colDecorators[c] = loadColumnDecorator( columnDecorator );
           if( colDecorators[c] != null )
!             colDecorators[c].init( this.pageContext, this.list );
        }
  
***************
*** 1192,1196 ****
  
     /**
!     * This returns a table of data in CVS format
      */
  
--- 1217,1221 ----
  
     /**
!     * This returns a table of data in CSV format
      */
  
***************
*** 1205,1209 ****
  
        if( prop.getProperty( "export.amount" ).equals( "list" ) ) {
!          iterator = this.completeList.iterator();
        }
  
--- 1230,1234 ----
  
        if( prop.getProperty( "export.amount" ).equals( "list" ) ) {
!          iterator = getIterator (this.list);
        }
  
***************
*** 1290,1294 ****
  
        if( prop.getProperty( "export.amount" ).equals( "list" ) ) {
!          iterator = this.completeList.iterator();
        }
  
--- 1315,1319 ----
  
        if( prop.getProperty( "export.amount" ).equals( "list" ) ) {
!          iterator = getIterator (this.list);
        }
  
***************
*** 1379,1383 ****
  
        if( prop.getProperty( "export.amount" ).equals( "list" ) ) {
!          iterator = this.completeList.iterator();
        }
  
--- 1404,1408 ----
  
        if( prop.getProperty( "export.amount" ).equals( "list" ) ) {
!          iterator = getIterator (this.list);
        }
  




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
displaytag-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-devel

Reply via email to