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