Coverage Report - org.apache.tapestry.contrib.table.model.common.AbstractTableColumn
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTableColumn
0%
0/52
0%
0/24
1.875
 
 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.common;
 16  
 
 17  
 import org.apache.tapestry.IComponent;
 18  
 import org.apache.tapestry.IRender;
 19  
 import org.apache.tapestry.IRequestCycle;
 20  
 import org.apache.tapestry.components.Block;
 21  
 import org.apache.tapestry.contrib.table.model.IAdvancedTableColumn;
 22  
 import org.apache.tapestry.contrib.table.model.ITableModelSource;
 23  
 import org.apache.tapestry.contrib.table.model.ITableRendererSource;
 24  
 import org.apache.tapestry.valid.RenderString;
 25  
 
 26  
 import java.io.Serializable;
 27  
 import java.util.Comparator;
 28  
 
 29  
 /**
 30  
  * A base implementation of
 31  
  * {@link org.apache.tapestry.contrib.table.model.ITableColumn} that allows
 32  
  * renderers to be set via aggregation.
 33  
  *
 34  
  * @see org.apache.tapestry.contrib.table.model.ITableRendererSource
 35  
  * @author mindbridge
 36  
  * @since 2.3
 37  
  */
 38  
 public class AbstractTableColumn implements IAdvancedTableColumn, Serializable
 39  
 {
 40  
     /**
 41  
      * The suffix of the name of the Block that will be used as the column
 42  
      * renderer for this column.
 43  
      */
 44  
     public static final String COLUMN_RENDERER_BLOCK_SUFFIX = "ColumnHeader";
 45  
 
 46  
     /**
 47  
      * The suffix of the name of the Block that will be used as the value
 48  
      * renderer for this column.
 49  
      */
 50  
     public static final String VALUE_RENDERER_BLOCK_SUFFIX = "ColumnValue";
 51  
 
 52  
     private static final long serialVersionUID = 1L;
 53  
 
 54  
     private String m_strColumnName;
 55  
     private boolean m_bSortable;
 56  
     private Comparator m_objComparator;
 57  
 
 58  
     private ITableRendererSource m_objColumnRendererSource;
 59  
     private ITableRendererSource m_objValueRendererSource;
 60  
 
 61  
     public AbstractTableColumn()
 62  
     {
 63  0
         this("", false, null);
 64  0
     }
 65  
 
 66  
     public AbstractTableColumn(String strColumnName, boolean bSortable,
 67  
                                Comparator objComparator)
 68  
     {
 69  0
         this(strColumnName, bSortable, objComparator, null, null);
 70  0
     }
 71  
 
 72  
     public AbstractTableColumn(String strColumnName, boolean bSortable,
 73  
                                Comparator objComparator,
 74  
                                ITableRendererSource objColumnRendererSource,
 75  
                                ITableRendererSource objValueRendererSource)
 76  0
     {
 77  0
         setColumnName(strColumnName);
 78  0
         setSortable(bSortable);
 79  0
         setComparator(objComparator);
 80  0
         setColumnRendererSource(objColumnRendererSource);
 81  0
         setValueRendererSource(objValueRendererSource);
 82  0
     }
 83  
 
 84  
     /**
 85  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnName()
 86  
      */
 87  
     public String getColumnName()
 88  
     {
 89  0
         return m_strColumnName;
 90  
     }
 91  
 
 92  
     /**
 93  
      * Sets the columnName.
 94  
      *
 95  
      * @param columnName
 96  
      *            The columnName to set
 97  
      */
 98  
     public void setColumnName(String columnName)
 99  
     {
 100  0
         m_strColumnName = columnName;
 101  0
     }
 102  
 
 103  
     /**
 104  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getSortable()
 105  
      */
 106  
     public boolean getSortable()
 107  
     {
 108  0
         return m_bSortable;
 109  
     }
 110  
 
 111  
     /**
 112  
      * Sets whether the column is sortable.
 113  
      *
 114  
      * @param sortable
 115  
      *            The sortable flag to set
 116  
      */
 117  
     public void setSortable(boolean sortable)
 118  
     {
 119  0
         m_bSortable = sortable;
 120  0
     }
 121  
 
 122  
     /**
 123  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getComparator()
 124  
      */
 125  
     public Comparator getComparator()
 126  
     {
 127  0
         return m_objComparator;
 128  
     }
 129  
 
 130  
     /**
 131  
      * Sets the comparator.
 132  
      *
 133  
      * @param comparator
 134  
      *            The comparator to set
 135  
      */
 136  
     public void setComparator(Comparator comparator)
 137  
     {
 138  0
         m_objComparator = comparator;
 139  0
     }
 140  
 
 141  
     /**
 142  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnRenderer(IRequestCycle,
 143  
      *      ITableModelSource)
 144  
      */
 145  
     public IRender getColumnRenderer(IRequestCycle objCycle,
 146  
                                      ITableModelSource objSource)
 147  
     {
 148  0
         ITableRendererSource objRendererSource = getColumnRendererSource();
 149  
 
 150  0
         if (objRendererSource == null)
 151  
         {
 152  
             // log error
 153  0
             return new RenderString("");
 154  
         }
 155  
 
 156  0
         return objRendererSource.getRenderer(objCycle, objSource, this, null);
 157  
     }
 158  
 
 159  
     /**
 160  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getValueRenderer(IRequestCycle,
 161  
      *      ITableModelSource, Object)
 162  
      */
 163  
     public IRender getValueRenderer(IRequestCycle objCycle,
 164  
                                     ITableModelSource objSource, Object objRow)
 165  
     {
 166  0
         ITableRendererSource objRendererSource = getValueRendererSource();
 167  
 
 168  0
         if (objRendererSource == null)
 169  
         {
 170  
             // log error
 171  0
             return new RenderString("");
 172  
         }
 173  
 
 174  0
         return objRendererSource.getRenderer(objCycle, objSource, this, objRow);
 175  
     }
 176  
 
 177  
     /**
 178  
      * Returns the columnRendererSource.
 179  
      *
 180  
      * @return ITableColumnRendererSource
 181  
      */
 182  
     public ITableRendererSource getColumnRendererSource()
 183  
     {
 184  0
         return m_objColumnRendererSource;
 185  
     }
 186  
 
 187  
     /**
 188  
      * Sets the columnRendererSource.
 189  
      *
 190  
      * @param columnRendererSource
 191  
      *            The columnRendererSource to set
 192  
      */
 193  
     public void setColumnRendererSource(ITableRendererSource columnRendererSource)
 194  
     {
 195  0
         m_objColumnRendererSource = columnRendererSource;
 196  0
     }
 197  
 
 198  
     /**
 199  
      * Returns the valueRendererSource.
 200  
      *
 201  
      * @return the valueRendererSource of this column
 202  
      */
 203  
     public ITableRendererSource getValueRendererSource()
 204  
     {
 205  0
         return m_objValueRendererSource;
 206  
     }
 207  
 
 208  
     /**
 209  
      * Sets the valueRendererSource.
 210  
      *
 211  
      * @param valueRendererSource
 212  
      *            The valueRendererSource to set
 213  
      */
 214  
     public void setValueRendererSource(ITableRendererSource valueRendererSource)
 215  
     {
 216  0
         m_objValueRendererSource = valueRendererSource;
 217  0
     }
 218  
 
 219  
     /**
 220  
      * Use the column name to get the column and value renderer sources from the
 221  
      * provided component. Use the column and value renderer sources for all
 222  
      * columns if necessary.
 223  
      *
 224  
      * @param container
 225  
      *            The component from which to get the settings.
 226  
      */
 227  
     public void loadSettings(IComponent container)
 228  
     {
 229  0
         String columnName = getColumnName();
 230  
         
 231  0
         IComponent objColumnRendererSource =
 232  
                 (IComponent) container.getComponents().get(columnName + COLUMN_RENDERER_BLOCK_SUFFIX);
 233  
 
 234  0
         if (objColumnRendererSource == null && columnName.indexOf(".") > -1)
 235  
         {
 236  0
             columnName = columnName.replace('.', '_');
 237  
             
 238  0
             objColumnRendererSource =
 239  
                 (IComponent) container.getComponents().get(columnName + COLUMN_RENDERER_BLOCK_SUFFIX);
 240  
         }
 241  
 
 242  0
         if (objColumnRendererSource == null)
 243  
         {
 244  0
             objColumnRendererSource = (IComponent) container.getComponents().get(COLUMN_RENDERER_BLOCK_SUFFIX);
 245  
         }
 246  
 
 247  0
         if (objColumnRendererSource != null && objColumnRendererSource instanceof Block)
 248  
         {
 249  0
             setColumnRendererSource(new BlockTableRendererSource((Block) objColumnRendererSource));
 250  
         }
 251  
 
 252  0
         IComponent objValueRendererSource =
 253  
                 (IComponent) container.getComponents().get(columnName + VALUE_RENDERER_BLOCK_SUFFIX);
 254  
 
 255  0
         if (objValueRendererSource == null && columnName.indexOf(".") > -1)
 256  
         {
 257  0
             columnName = columnName.replace('.', '_');
 258  
 
 259  0
             objValueRendererSource =
 260  
                 (IComponent) container.getComponents().get(columnName + VALUE_RENDERER_BLOCK_SUFFIX);
 261  
         }
 262  
 
 263  0
         if (objValueRendererSource == null)
 264  
         {
 265  0
             objValueRendererSource = (IComponent) container.getComponents().get(VALUE_RENDERER_BLOCK_SUFFIX);
 266  
         }
 267  
 
 268  0
         if (objValueRendererSource != null && objValueRendererSource instanceof Block)
 269  
         {
 270  0
             setValueRendererSource(new BlockTableRendererSource((Block) objValueRendererSource));
 271  
         }
 272  0
     }
 273  
 
 274  
 }