Coverage Report - org.apache.tapestry.contrib.table.model.common.AbstractTableColumn
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTableColumn
0% 
0% 
1.562
 
 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
         if (columnName != null)
 101  0
             columnName = columnName.replace('.', '_');
 102  
         
 103  0
         m_strColumnName = columnName;
 104  0
     }
 105  
 
 106  
     /**
 107  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getSortable()
 108  
      */
 109  
     public boolean getSortable()
 110  
     {
 111  0
         return m_bSortable;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Sets whether the column is sortable.
 116  
      *
 117  
      * @param sortable
 118  
      *            The sortable flag to set
 119  
      */
 120  
     public void setSortable(boolean sortable)
 121  
     {
 122  0
         m_bSortable = sortable;
 123  0
     }
 124  
 
 125  
     /**
 126  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getComparator()
 127  
      */
 128  
     public Comparator getComparator()
 129  
     {
 130  0
         return m_objComparator;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Sets the comparator.
 135  
      *
 136  
      * @param comparator
 137  
      *            The comparator to set
 138  
      */
 139  
     public void setComparator(Comparator comparator)
 140  
     {
 141  0
         m_objComparator = comparator;
 142  0
     }
 143  
 
 144  
     /**
 145  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnRenderer(IRequestCycle,
 146  
      *      ITableModelSource)
 147  
      */
 148  
     public IRender getColumnRenderer(IRequestCycle objCycle,
 149  
                                      ITableModelSource objSource)
 150  
     {
 151  0
         ITableRendererSource objRendererSource = getColumnRendererSource();
 152  
 
 153  0
         if (objRendererSource == null)
 154  
         {
 155  
             // log error
 156  0
             return new RenderString("");
 157  
         }
 158  
 
 159  0
         return objRendererSource.getRenderer(objCycle, objSource, this, null);
 160  
     }
 161  
 
 162  
     /**
 163  
      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getValueRenderer(IRequestCycle,
 164  
      *      ITableModelSource, Object)
 165  
      */
 166  
     public IRender getValueRenderer(IRequestCycle objCycle,
 167  
                                     ITableModelSource objSource, Object objRow)
 168  
     {
 169  0
         ITableRendererSource objRendererSource = getValueRendererSource();
 170  
 
 171  0
         if (objRendererSource == null)
 172  
         {
 173  
             // log error
 174  0
             return new RenderString("");
 175  
         }
 176  
 
 177  0
         return objRendererSource.getRenderer(objCycle, objSource, this, objRow);
 178  
     }
 179  
 
 180  
     /**
 181  
      * Returns the columnRendererSource.
 182  
      *
 183  
      * @return ITableColumnRendererSource
 184  
      */
 185  
     public ITableRendererSource getColumnRendererSource()
 186  
     {
 187  0
         return m_objColumnRendererSource;
 188  
     }
 189  
 
 190  
     /**
 191  
      * Sets the columnRendererSource.
 192  
      *
 193  
      * @param columnRendererSource
 194  
      *            The columnRendererSource to set
 195  
      */
 196  
     public void setColumnRendererSource(
 197  
       ITableRendererSource columnRendererSource)
 198  
     {
 199  0
         m_objColumnRendererSource = columnRendererSource;
 200  0
     }
 201  
 
 202  
     /**
 203  
      * Returns the valueRendererSource.
 204  
      *
 205  
      * @return the valueRendererSource of this column
 206  
      */
 207  
     public ITableRendererSource getValueRendererSource()
 208  
     {
 209  0
         return m_objValueRendererSource;
 210  
     }
 211  
 
 212  
     /**
 213  
      * Sets the valueRendererSource.
 214  
      *
 215  
      * @param valueRendererSource
 216  
      *            The valueRendererSource to set
 217  
      */
 218  
     public void setValueRendererSource(ITableRendererSource valueRendererSource)
 219  
     {
 220  0
         m_objValueRendererSource = valueRendererSource;
 221  0
     }
 222  
 
 223  
     /**
 224  
      * Use the column name to get the column and value renderer sources from the
 225  
      * provided component. Use the column and value renderer sources for all
 226  
      * columns if necessary.
 227  
      *
 228  
      * @param container
 229  
      *            the component from which to get the settings
 230  
      */
 231  
     public void loadSettings(IComponent container)
 232  
     {
 233  0
         IComponent objColumnRendererSource =
 234  
           (IComponent) container.getComponents().get(getColumnName() + COLUMN_RENDERER_BLOCK_SUFFIX);
 235  
 
 236  0
         if (objColumnRendererSource == null)
 237  0
             objColumnRendererSource = (IComponent) container.getComponents().get(COLUMN_RENDERER_BLOCK_SUFFIX);
 238  
 
 239  0
         if (objColumnRendererSource != null
 240  
             && objColumnRendererSource instanceof Block)
 241  
         {
 242  0
             setColumnRendererSource(new BlockTableRendererSource((Block) objColumnRendererSource));
 243  
         }
 244  
 
 245  0
         IComponent objValueRendererSource =
 246  
           (IComponent) container.getComponents().get(getColumnName() + VALUE_RENDERER_BLOCK_SUFFIX);
 247  
 
 248  0
         if (objValueRendererSource == null)
 249  
         {
 250  0
             objValueRendererSource = (IComponent) container.getComponents().get(VALUE_RENDERER_BLOCK_SUFFIX);
 251  
         }
 252  
 
 253  0
         if (objValueRendererSource != null
 254  
             && objValueRendererSource instanceof Block)
 255  
         {
 256  0
             setValueRendererSource(new BlockTableRendererSource((Block) objValueRendererSource));
 257  
         }
 258  0
     }
 259  
 
 260  
 }