Update of /cvsroot/displaytag/display09/src/org/displaytag/util
In directory sc8-pr-cvs1:/tmp/cvs-serv7974/src/org/displaytag/util

Modified Files:
        RowSorter.java 
Removed Files:
        RowCellSorter.java 
Log Message:
merged RowSorter and RowCellSorter, improved sorting and use of decorators

Index: RowSorter.java
===================================================================
RCS file: /cvsroot/displaytag/display09/src/org/displaytag/util/RowSorter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** RowSorter.java      15 Jul 2003 21:48:36 -0000      1.2
--- RowSorter.java      20 Jul 2003 21:26:58 -0000      1.3
***************
*** 1,19 ****
  package org.displaytag.util;
  
- import java.lang.reflect.InvocationTargetException;
  import java.util.Comparator;
  
- import org.apache.commons.beanutils.PropertyUtils;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.displaytag.decorator.Decorator;
  import org.displaytag.decorator.TableDecorator;
  import org.displaytag.model.Row;
  
  /**
   * @author fgiust
   * @version $Revision$ ($Author$)
   */
! public class RowSorter extends Object implements Comparator
  {
  
--- 1,19 ----
  package org.displaytag.util;
  
  import java.util.Comparator;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.displaytag.decorator.Decorator;
  import org.displaytag.decorator.TableDecorator;
+ import org.displaytag.exception.ObjectLookupException;
  import org.displaytag.model.Row;
  
  /**
+  * <p>Comparator for rows</p>
   * @author fgiust
   * @version $Revision$ ($Author$)
   */
! public class RowSorter implements Comparator
  {
  
***************
*** 24,64 ****
  
        /**
!        * Field property
         */
        private String mProperty;
  
        /**
!        * Field dec
         */
!       private Decorator mDecorator;
  
        /**
!        * Field mAscending
         */
!       private int mAscending;
  
        /**
!        * BeanSorter is a decorator of sorts, you need to initialize it with
!        * the property name of the object that is to be sorted (getXXX method
!        * name).  This property should return a Comparable object.
!        * @param pProperty String
         * @param pDecorator TableDecorator
!        * @param pAscending boolean
         */
!       public RowSorter(String pProperty, TableDecorator pDecorator, boolean 
pAscending)
        {
                mProperty= pProperty;
!               mDecorator= pDecorator;
!               mAscending= pAscending ? 1 : -1;
        }
  
        /**
!        * Compares two objects by first fetching a property from each object
!        * and then comparing that value.  If there are any errors produced while
!        * trying to compare these objects then a RunTimeException will be
!        * thrown as any error found here will most likely be a programming
!        * error that needs to be quickly addressed (like trying to compare
!        * objects that are not comparable, or trying to read a property from a
!        * bean that is invalid, etc...)
         *
         * @param pObject1 Object
--- 24,67 ----
  
        /**
!        * name of the property in bean
         */
        private String mProperty;
  
        /**
!        * table decorator
         */
!       private Decorator mTableDecorator;
  
        /**
!        * sort order ascending?
         */
!       private boolean mAscending;
  
        /**
!        * index of the sorted column
!        */
!       private int mColumnIndex;
! 
!       /**
!        * initialize a new RowSorter
!        * @param pColumnIndex index of the sorted column
!        * @param pProperty name of the property. If pProperty is null column index is 
used to get a static cell value from 
!        * the row object
         * @param pDecorator TableDecorator
!        * @param pAscending boolean ascending order?
         */
!       public RowSorter(int pColumnIndex, String pProperty, TableDecorator 
pDecorator, boolean pAscending)
        {
+               mColumnIndex= pColumnIndex;
                mProperty= pProperty;
!               mTableDecorator= pDecorator;
!               mAscending= pAscending;
        }
  
        /**
!        * Compares two objects by first fetching a property from each object and then 
comparing that value.  If there are
!        * any errors produced while trying to compare these objects then a 
RunTimeException will be thrown as any error 
!        * found here will most likely be a programming error that needs to be quickly 
addressed (like trying to compare
!        * objects that are not comparable, or trying to read a property from a bean 
that is invalid, etc...)
         *
         * @param pObject1 Object
***************
*** 70,164 ****
        {
  
!               if (pObject1 instanceof Row)
!               {
!                       pObject1= ((Row) pObject1).getObject();
!               }
!               if (pObject2 instanceof Row)
!               {
!                       pObject2= ((Row) pObject2).getObject();
!               }
  
                if (mProperty == null)
                {
!                       throw new NullPointerException("Null property provided which 
prevents sorting");
!               }
! 
!               try
!               {
!                       Object lObj1= null;
!                       Object lObj2= null;
! 
!                       // If they have supplied a decorator, then make sure and use 
it for
!                       // the sorting as well... TODO - Major hack....
! 
!                       if (mDecorator != null)
                        {
!                               try
!                               {
!                                       lObj1= PropertyUtils.getProperty(mDecorator, 
mProperty);
!                                       lObj2= PropertyUtils.getProperty(mDecorator, 
mProperty);
!                               }
!                               catch (Exception e)
!                               {
!                                       // If there were any problems, then assume 
that the decorator
!                                       // does not implement that method, and instead 
fall down to
!                                       // the original object...
! 
!                                       lObj1= PropertyUtils.getProperty(pObject1, 
mProperty);
!                                       lObj2= PropertyUtils.getProperty(pObject2, 
mProperty);
!                               }
                        }
!                       else
                        {
!                               lObj1= PropertyUtils.getProperty(pObject1, mProperty);
!                               lObj2= PropertyUtils.getProperty(pObject2, mProperty);
                        }
  
!                       if (lObj1 instanceof Comparable && lObj2 instanceof Comparable)
!                       {
!                               Comparable lComparable1= (Comparable) lObj1;
!                               Comparable lComparable2= (Comparable) lObj2;
!                               return mAscending * 
lComparable1.compareTo(lComparable2);
!                       }
!                       else if (lObj1 == null && lObj2 == null)
                        {
!                               return 0;
                        }
!                       else if (lObj1 == null && lObj2 != null)
                        {
!                               return 1;
                        }
!                       else if (lObj1 != null && lObj2 == null)
                        {
!                               return -1;
                        }
!                       else
                        {
                                throw new RuntimeException(
!                                       "Object returned by property \"" + mProperty + 
"\" is not a Comparable object");
                        }
                }
!               catch (IllegalAccessException e)
                {
!                       throw new RuntimeException(
!                               "IllegalAccessException thrown while " + "trying to 
fetch property \"" + mProperty + "\" during sort");
                }
!               catch (InvocationTargetException e)
                {
!                       throw new RuntimeException(
!                               "InvocationTargetException thrown while "
!                                       + "trying to fetch property \""
!                                       + mProperty
!                                       + "\" during sort");
                }
!               catch (NoSuchMethodException e)
                {
!                       throw new RuntimeException(
!                               "NoSuchMethodException thrown while " + "trying to 
fetch property \"" + mProperty + "\" during sort");
                }
        }
  
        /**
!        * Is this Comparator the same as another one...
         * @param pObject Object
         * @return boolean
--- 73,173 ----
        {
  
!               Object lObj1= null;
!               Object lObj2= null;
  
+               // if property is null compare using two static cell objects
                if (mProperty == null)
                {
!                       if (pObject1 instanceof Row)
                        {
!                               lObj1= ((Row) 
pObject1).getCellList().get(mColumnIndex);
                        }
!                       if (pObject2 instanceof Row)
                        {
!                               lObj2= ((Row) 
pObject2).getCellList().get(mColumnIndex);
                        }
  
!                       return checkNullsAndCompare(lObj1, lObj2);
! 
!               }
!               else
!               {
!                       if (pObject1 instanceof Row)
                        {
!                               lObj1= ((Row) pObject1).getObject();
                        }
!                       if (pObject2 instanceof Row)
                        {
!                               lObj2= ((Row) pObject2).getObject();
                        }
! 
!                       try
                        {
!                               Object lResult1= null;
!                               Object lResult2= null;
! 
!                               // If they have supplied a decorator, then make sure 
and use it for the sorting as well
!                               if (mTableDecorator != null && 
mTableDecorator.hasGetterFor(mProperty))
!                               {
!                                       lResult1= 
LookupUtil.getBeanProperty(mTableDecorator, mProperty);
!                                       lResult2= 
LookupUtil.getBeanProperty(mTableDecorator, mProperty);
!                               }
!                               else
!                               {
!                                       lResult1= LookupUtil.getBeanProperty(lObj1, 
mProperty);
!                                       lResult2= LookupUtil.getBeanProperty(lObj2, 
mProperty);
!                               }
! 
!                               return checkNullsAndCompare(lResult1, lResult2);
                        }
!                       catch (ObjectLookupException e)
                        {
+                               /** @todo error handling need to be improved, can't 
throw an exception here */
+                               mLog.error(
+                                       "ObjectLookupException thrown while trying to 
fetch property \"" + mProperty + "\" during sort",
+                                       e);
                                throw new RuntimeException(
!                                       "ObjectLookupException thrown while trying to 
fetch property \"" + mProperty + "\" during sort");
                        }
                }
!       }
! 
!       /**
!        * <p>compare two given objects according to the pAscending flag</p>
!        * <p>Null values and not comparable objects are handled. Not comparable 
objects are compared using their string
!        * representation</p>
!        * @param pObject1 first object to compare
!        * @param pObject2 second object to compare
!        * @return int result
!        */
!       private int checkNullsAndCompare(Object pObject1, Object pObject2)
!       {
!               int lAscending= mAscending ? 1 : -1;
! 
!               if (pObject1 instanceof Comparable && pObject2 instanceof Comparable)
                {
!                       return lAscending * ((Comparable) 
pObject1).compareTo(pObject2);
                }
!               else if (pObject1 == null && pObject2 == null)
                {
!                       return 0;
                }
!               else if (pObject1 == null && pObject2 != null)
                {
!                       return 1;
!               }
!               else if (pObject1 != null && pObject2 == null)
!               {
!                       return -1;
!               }
!               else
!               {
!                       // if object are not null and don't implement comparable, 
compare using string values
!                       return pObject1.toString().compareTo(pObject2.toString());
                }
        }
  
        /**
!        * Is this Comparator the same as another one?
         * @param pObject Object
         * @return boolean
***************
*** 167,178 ****
        public boolean equals(Object pObject)
        {
-               if (pObject == this)
-               {
-                       return true;
-               }
- 
                if (pObject instanceof RowSorter)
                {
!                       if (this.mProperty != null)
                        {
                                return mProperty.equals(((RowSorter) 
pObject).mProperty);
--- 176,182 ----
        public boolean equals(Object pObject)
        {
                if (pObject instanceof RowSorter)
                {
!                       if (mProperty != null)
                        {
                                return mProperty.equals(((RowSorter) 
pObject).mProperty);
***************
*** 180,190 ****
                        else
                        {
!                               return false;
                        }
                }
!               else
!               {
!                       return false;
!               }
        }
  }
--- 184,193 ----
                        else
                        {
!                               return mColumnIndex == ((RowSorter) 
pObject).mColumnIndex;
                        }
                }
! 
!               return false;
! 
        }
  }

--- RowCellSorter.java DELETED ---




-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
displaytag-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-devel

Reply via email to