Clover coverage report - Code Coverage for tapestry-contrib release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:12:41 EDT
file stats: LOC: 259   Methods: 14
NCLOC: 98   Classes: 2
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
SimpleTableColumn.java 0% 0% 0% 0%
coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.contrib.table.model.simple;
 16   
 
 17   
 import java.io.Serializable;
 18   
 import java.util.Comparator;
 19   
 
 20   
 import org.apache.hivemind.HiveMind;
 21   
 import org.apache.tapestry.IComponent;
 22   
 import org.apache.tapestry.contrib.table.model.ITableRendererSource;
 23   
 import org.apache.tapestry.contrib.table.model.common.AbstractTableColumn;
 24   
 
 25   
 /**
 26   
  * A simple minimal implementation of the
 27   
  * {@link org.apache.tapestry.contrib.table.model.ITableColumn}interface that provides all the
 28   
  * basic services for displaying a column.
 29   
  * 
 30   
  * @author mindbridge
 31   
  */
 32   
 public class SimpleTableColumn extends AbstractTableColumn
 33   
 {
 34   
     public static final ITableRendererSource DEFAULT_COLUMN_RENDERER_SOURCE = new SimpleTableColumnRendererSource();
 35   
 
 36   
     public static final ITableRendererSource FORM_COLUMN_RENDERER_SOURCE = new SimpleTableColumnFormRendererSource();
 37   
 
 38   
     public static final ITableRendererSource DEFAULT_VALUE_RENDERER_SOURCE = new SimpleTableValueRendererSource();
 39   
 
 40   
     private String m_strDisplayName;
 41   
 
 42   
     private ITableColumnEvaluator m_objEvaluator;
 43   
 
 44   
     /**
 45   
      * Creates a SimpleTableColumn
 46   
      * 
 47   
      * @param strColumnName
 48   
      *            the identifying name and display name of the column
 49   
      */
 50  0
     public SimpleTableColumn(String strColumnName)
 51   
     {
 52  0
         this(strColumnName, strColumnName);
 53   
     }
 54   
 
 55   
     /**
 56   
      * Creates a SimpleTableColumn
 57   
      * 
 58   
      * @param strColumnName
 59   
      *            the identifying name and display name of the column
 60   
      * @param bSortable
 61   
      *            whether the column is sortable
 62   
      */
 63  0
     public SimpleTableColumn(String strColumnName, boolean bSortable)
 64   
     {
 65  0
         this(strColumnName, strColumnName, bSortable);
 66   
     }
 67   
 
 68   
     /**
 69   
      * Creates a SimpleTableColumn
 70   
      * 
 71   
      * @param strColumnName
 72   
      *            the identifying name and display name of the column
 73   
      * @param bSortable
 74   
      *            whether the column is sortable
 75   
      * @param objEvaluator
 76   
      *            the evaluator to extract the column value from the row
 77   
      */
 78  0
     public SimpleTableColumn(String strColumnName, ITableColumnEvaluator objEvaluator,
 79   
             boolean bSortable)
 80   
     {
 81  0
         this(strColumnName, strColumnName, objEvaluator, bSortable);
 82   
     }
 83   
 
 84   
     /**
 85   
      * Creates a SimpleTableColumn
 86   
      * 
 87   
      * @param strColumnName
 88   
      *            the identifying name of the column
 89   
      * @param strDisplayName
 90   
      *            the display name of the column
 91   
      */
 92  0
     public SimpleTableColumn(String strColumnName, String strDisplayName)
 93   
     {
 94  0
         this(strColumnName, strDisplayName, false);
 95   
     }
 96   
 
 97   
     /**
 98   
      * Creates a SimpleTableColumn
 99   
      * 
 100   
      * @param strColumnName
 101   
      *            the identifying name of the column
 102   
      * @param strDisplayName
 103   
      *            the display name of the column
 104   
      * @param bSortable
 105   
      *            whether the column is sortable
 106   
      */
 107  0
     public SimpleTableColumn(String strColumnName, String strDisplayName, boolean bSortable)
 108   
     {
 109  0
         this(strColumnName, strDisplayName, null, bSortable);
 110   
     }
 111   
 
 112   
     /**
 113   
      * Creates a SimpleTableColumn
 114   
      * 
 115   
      * @param strColumnName
 116   
      *            the identifying name of the column
 117   
      * @param strDisplayName
 118   
      *            the display name of the column
 119   
      * @param bSortable
 120   
      *            whether the column is sortable
 121   
      * @param objEvaluator
 122   
      *            the evaluator to extract the column value from the row
 123   
      */
 124  0
     public SimpleTableColumn(String strColumnName, String strDisplayName,
 125   
             ITableColumnEvaluator objEvaluator, boolean bSortable)
 126   
     {
 127  0
         super(strColumnName, bSortable, null);
 128  0
         setComparator(new DefaultTableComparator());
 129  0
         setDisplayName(strDisplayName);
 130  0
         setColumnRendererSource(DEFAULT_COLUMN_RENDERER_SOURCE);
 131  0
         setValueRendererSource(DEFAULT_VALUE_RENDERER_SOURCE);
 132  0
         setEvaluator(objEvaluator);
 133   
     }
 134   
 
 135   
     /**
 136   
      * Returns the display name of the column that will be used in the table header. Override for
 137   
      * internationalization.
 138   
      * 
 139   
      * @return String the display name of the column
 140   
      */
 141  0
     public String getDisplayName()
 142   
     {
 143  0
         return m_strDisplayName;
 144   
     }
 145   
 
 146   
     /**
 147   
      * Sets the displayName.
 148   
      * 
 149   
      * @param displayName
 150   
      *            The displayName to set
 151   
      */
 152  0
     public void setDisplayName(String displayName)
 153   
     {
 154  0
         m_strDisplayName = displayName;
 155   
     }
 156   
 
 157   
     /**
 158   
      * Returns the evaluator.
 159   
      * 
 160   
      * @return ITableColumnEvaluator
 161   
      */
 162  0
     public ITableColumnEvaluator getEvaluator()
 163   
     {
 164  0
         return m_objEvaluator;
 165   
     }
 166   
 
 167   
     /**
 168   
      * Sets the evaluator.
 169   
      * 
 170   
      * @param evaluator
 171   
      *            The evaluator to set
 172   
      */
 173  0
     public void setEvaluator(ITableColumnEvaluator evaluator)
 174   
     {
 175  0
         m_objEvaluator = evaluator;
 176   
     }
 177   
 
 178   
     /**
 179   
      * Sets a comparator that compares the values of this column rather than the objects
 180   
      * representing the full rows. <br>
 181   
      * This method allows easier use of standard comparators for sorting the column. It simply wraps
 182   
      * the provided comparator with a row-to-column convertor and invokes the setComparator()
 183   
      * method.
 184   
      * 
 185   
      * @param comparator
 186   
      *            The column value comparator
 187   
      */
 188  0
     public void setColumnComparator(Comparator comparator)
 189   
     {
 190  0
         setComparator(new ColumnComparator(this, comparator));
 191   
     }
 192   
 
 193   
     /**
 194   
      * Extracts the value of the column from the row object
 195   
      * 
 196   
      * @param objRow
 197   
      *            the row object
 198   
      * @return Object the column value
 199   
      */
 200  0
     public Object getColumnValue(Object objRow)
 201   
     {
 202  0
         ITableColumnEvaluator objEvaluator = getEvaluator();
 203  0
         if (objEvaluator != null)
 204  0
             return objEvaluator.getColumnValue(this, objRow);
 205   
 
 206   
         // default fallback
 207  0
         return objRow.toString();
 208   
     }
 209   
 
 210   
     /**
 211   
      * Use the column name to get the display name, as well as the column and value renderer sources
 212   
      * from the provided component.
 213   
      * 
 214   
      * @param objSettingsContainer
 215   
      *            the component from which to get the settings
 216   
      */
 217  0
     public void loadSettings(IComponent objSettingsContainer)
 218   
     {
 219  0
         String strDisplayName = objSettingsContainer.getMessages().getMessage(getColumnName());
 220   
 
 221   
         // Hack! the Messages inteface needs to restore the getMessage(key, default), or needs
 222   
         // to add a containsKey(key) method. Looking for the '[' used with invalid/unknown keys.
 223   
 
 224  0
         if (!strDisplayName.startsWith("["))
 225  0
             setDisplayName(strDisplayName);
 226   
 
 227  0
         super.loadSettings(objSettingsContainer);
 228   
     }
 229   
 
 230   
     public class DefaultTableComparator implements Comparator, Serializable
 231   
     {
 232  0
         public int compare(Object objRow1, Object objRow2)
 233   
         {
 234  0
             Object objValue1 = getColumnValue(objRow1);
 235  0
             Object objValue2 = getColumnValue(objRow2);
 236   
 
 237  0
             if (objValue1 == objValue2)
 238  0
                 return 0;
 239   
 
 240  0
             boolean bComparable1 = objValue1 instanceof Comparable;
 241  0
             boolean bComparable2 = objValue2 instanceof Comparable;
 242   
 
 243   
             // non-comparable values are considered equal
 244  0
             if (!bComparable1 && !bComparable2)
 245  0
                 return 0;
 246   
 
 247   
             // non-comparable values (null included) are considered smaller
 248   
             // than the comparable ones
 249  0
             if (!bComparable1)
 250  0
                 return -1;
 251   
 
 252  0
             if (!bComparable2)
 253  0
                 return 1;
 254   
 
 255  0
             return ((Comparable) objValue1).compareTo(objValue2);
 256   
         }
 257   
     }
 258   
 
 259   
 }