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