Coverage Report - org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleTableColumn
0%
0/37
0%
0/4
1.857
SimpleTableColumn$DefaultTableComparator
0%
0/13
0%
0/10
1.857
 
 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 org.apache.tapestry.IComponent;
 18  
 import org.apache.tapestry.contrib.table.model.ITableRendererSource;
 19  
 import org.apache.tapestry.contrib.table.model.common.AbstractTableColumn;
 20  
 
 21  
 import java.io.Serializable;
 22  
 import java.util.Comparator;
 23  
 
 24  
 /**
 25  
  * A simple minimal implementation of the
 26  
  * {@link org.apache.tapestry.contrib.table.model.ITableColumn}interface that
 27  
  * provides all the basic services for displaying a column.
 28  
  *
 29  
  * @author mindbridge
 30  
  */
 31  
 public class SimpleTableColumn extends AbstractTableColumn
 32  
 {
 33  
     // TODO: Unify SimpleTableColumnRendererSource and
 34  
     // SimpleTableColumnFormRendererSource
 35  
     // and implement the configuration with HiveMind
 36  
 
 37  0
     public static final ITableRendererSource DEFAULT_COLUMN_RENDERER_SOURCE = new SimpleTableColumnRendererSource();
 38  
 
 39  0
     public static final ITableRendererSource FORM_COLUMN_RENDERER_SOURCE = new SimpleTableColumnFormRendererSource();
 40  
 
 41  0
     public static final ITableRendererSource DEFAULT_VALUE_RENDERER_SOURCE = new SimpleTableValueRendererSource();
 42  
 
 43  
     private static final long serialVersionUID = 1L;
 44  
 
 45  
     private String m_strDisplayName;
 46  
 
 47  
     private ITableColumnEvaluator m_objEvaluator;
 48  
 
 49  
     /**
 50  
      * Creates a SimpleTableColumn.
 51  
      *
 52  
      * @param strColumnName
 53  
      *            the identifying name and display name of the column
 54  
      */
 55  
     public SimpleTableColumn(String strColumnName)
 56  
     {
 57  0
         this(strColumnName, strColumnName);
 58  0
     }
 59  
 
 60  
     /**
 61  
      * Creates a SimpleTableColumn.
 62  
      *
 63  
      * @param strColumnName
 64  
      *            the identifying name and display name of the column
 65  
      * @param bSortable
 66  
      *            whether the column is sortable
 67  
      */
 68  
     public SimpleTableColumn(String strColumnName, boolean bSortable)
 69  
     {
 70  0
         this(strColumnName, strColumnName, bSortable);
 71  0
     }
 72  
 
 73  
     /**
 74  
      * Creates a SimpleTableColumn.
 75  
      *
 76  
      * @param strColumnName
 77  
      *            the identifying name and display name of the column
 78  
      * @param bSortable
 79  
      *            whether the column is sortable
 80  
      * @param objEvaluator
 81  
      *            the evaluator to extract the column value from the row
 82  
      */
 83  
     public SimpleTableColumn(String strColumnName,
 84  
                              ITableColumnEvaluator objEvaluator, boolean bSortable)
 85  
     {
 86  0
         this(strColumnName, strColumnName, objEvaluator, bSortable);
 87  0
     }
 88  
 
 89  
     /**
 90  
      * Creates a SimpleTableColumn.
 91  
      *
 92  
      * @param strColumnName
 93  
      *            the identifying name of the column
 94  
      * @param strDisplayName
 95  
      *            the display name of the column
 96  
      */
 97  
     public SimpleTableColumn(String strColumnName, String strDisplayName)
 98  
     {
 99  0
         this(strColumnName, strDisplayName, false);
 100  0
     }
 101  
 
 102  
     /**
 103  
      * Creates a SimpleTableColumn.
 104  
      *
 105  
      * @param strColumnName
 106  
      *            the identifying name of the column
 107  
      * @param strDisplayName
 108  
      *            the display name of the column
 109  
      * @param bSortable
 110  
      *            whether the column is sortable
 111  
      */
 112  
     public SimpleTableColumn(String strColumnName, String strDisplayName,
 113  
                              boolean bSortable)
 114  
     {
 115  0
         this(strColumnName, strDisplayName, null, bSortable);
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Creates a SimpleTableColumn.
 120  
      *
 121  
      * @param strColumnName
 122  
      *            the identifying name of the column
 123  
      * @param strDisplayName
 124  
      *            the display name of the column
 125  
      * @param bSortable
 126  
      *            whether the column is sortable
 127  
      * @param objEvaluator
 128  
      *            the evaluator to extract the column value from the row
 129  
      */
 130  
     public SimpleTableColumn(String strColumnName, String strDisplayName,
 131  
                              ITableColumnEvaluator objEvaluator, boolean bSortable)
 132  
     {
 133  0
         super(strColumnName, bSortable, null);
 134  
 
 135  0
         setComparator(new DefaultTableComparator());
 136  0
         setDisplayName(strDisplayName);
 137  0
         setColumnRendererSource(DEFAULT_COLUMN_RENDERER_SOURCE);
 138  0
         setValueRendererSource(DEFAULT_VALUE_RENDERER_SOURCE);
 139  0
         setEvaluator(objEvaluator);
 140  0
     }
 141  
 
 142  
     /**
 143  
      * Returns the display name of the column that will be used in the table
 144  
      * header. Override for internationalization.
 145  
      *
 146  
      * @return String the display name of the column
 147  
      */
 148  
     public String getDisplayName()
 149  
     {
 150  0
         return m_strDisplayName;
 151  
     }
 152  
 
 153  
     /**
 154  
      * Sets the displayName.
 155  
      *
 156  
      * @param displayName
 157  
      *            The displayName to set
 158  
      */
 159  
     public void setDisplayName(String displayName)
 160  
     {
 161  0
         m_strDisplayName = displayName;
 162  0
     }
 163  
 
 164  
     /**
 165  
      * Returns the evaluator.
 166  
      *
 167  
      * @return ITableColumnEvaluator
 168  
      */
 169  
     public ITableColumnEvaluator getEvaluator()
 170  
     {
 171  0
         return m_objEvaluator;
 172  
     }
 173  
 
 174  
     /**
 175  
      * Sets the evaluator.
 176  
      *
 177  
      * @param evaluator
 178  
      *            The evaluator to set
 179  
      */
 180  
     public void setEvaluator(ITableColumnEvaluator evaluator)
 181  
     {
 182  0
         m_objEvaluator = evaluator;
 183  0
     }
 184  
 
 185  
     /**
 186  
      * Sets a comparator that compares the values of this column rather than the
 187  
      * objects representing the full rows. <br>
 188  
      * This method allows easier use of standard comparators for sorting the
 189  
      * column. It simply wraps the provided comparator with a row-to-column
 190  
      * convertor and invokes the setComparator() method.
 191  
      *
 192  
      * @param comparator
 193  
      *            The column value comparator
 194  
      */
 195  
     public void setColumnComparator(Comparator comparator)
 196  
     {
 197  0
         setComparator(new ColumnComparator(this, comparator));
 198  0
     }
 199  
 
 200  
     /**
 201  
      * Extracts the value of the column from the row object.
 202  
      *
 203  
      * @param objRow
 204  
      *            the row object
 205  
      * @return Object the column value
 206  
      */
 207  
     public Object getColumnValue(Object objRow)
 208  
     {
 209  0
         ITableColumnEvaluator objEvaluator = getEvaluator();
 210  
         
 211  0
         if (objEvaluator != null)
 212  0
             return objEvaluator.getColumnValue(this, objRow);
 213  
 
 214  
         // default fallback
 215  0
         return objRow.toString();
 216  
     }
 217  
 
 218  
     /**
 219  
      * Use the column name to get the display name, as well as the column and
 220  
      * value renderer sources from the provided component.
 221  
      *
 222  
      * @param objSettingsContainer
 223  
      *            the component from which to get the settings
 224  
      */
 225  
     public void loadSettings(IComponent objSettingsContainer)
 226  
     {
 227  0
         String strDisplayName = objSettingsContainer.getMessages().getMessage(getDisplayName());
 228  
 
 229  
         // Hack! the Messages inteface needs to restore the getMessage(key,
 230  
         // default), or needs
 231  
         // to add a containsKey(key) method. Looking for the '[' used with
 232  
         // invalid/unknown keys.
 233  
 
 234  0
         if (!strDisplayName.startsWith("["))
 235  
         {
 236  0
             setDisplayName(strDisplayName);
 237  
         }
 238  
 
 239  0
         super.loadSettings(objSettingsContainer);
 240  0
     }
 241  
 
 242  
     /**
 243  
      *
 244  
      * @author mb
 245  
      */
 246  0
     public class DefaultTableComparator implements Comparator, Serializable
 247  
     {
 248  
 
 249  
         private static final long serialVersionUID = 1L;
 250  
 
 251  
         public int compare(Object objRow1, Object objRow2)
 252  
         {
 253  0
             Object objValue1 = getColumnValue(objRow1);
 254  0
             Object objValue2 = getColumnValue(objRow2);
 255  
 
 256  0
             if (objValue1 == objValue2) return 0;
 257  
 
 258  0
             boolean bComparable1 = objValue1 instanceof Comparable;
 259  0
             boolean bComparable2 = objValue2 instanceof Comparable;
 260  
 
 261  
             // non-comparable values are considered equal
 262  0
             if (!bComparable1 && !bComparable2)
 263  0
                 return 0;
 264  
 
 265  
             // non-comparable values (null included) are considered smaller
 266  
             // than the comparable ones
 267  0
             if (!bComparable1)
 268  0
                 return -1;
 269  
 
 270  0
             if (!bComparable2)
 271  0
                 return 1;
 272  
 
 273  0
             return ((Comparable) objValue1).compareTo(objValue2);
 274  
         }
 275  
     }
 276  
 
 277  
 }