1 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 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 51 public SimpleTableColumn(String strColumnName) 52 { 53 this(strColumnName, strColumnName); 54 } 55 56 64 public SimpleTableColumn(String strColumnName, boolean bSortable) 65 { 66 this(strColumnName, strColumnName, bSortable); 67 } 68 69 79 public SimpleTableColumn(String strColumnName, ITableColumnEvaluator objEvaluator, 80 boolean bSortable) 81 { 82 this(strColumnName, strColumnName, objEvaluator, bSortable); 83 } 84 85 93 public SimpleTableColumn(String strColumnName, String strDisplayName) 94 { 95 this(strColumnName, strDisplayName, false); 96 } 97 98 108 public SimpleTableColumn(String strColumnName, String strDisplayName, boolean bSortable) 109 { 110 this(strColumnName, strDisplayName, null, bSortable); 111 } 112 113 125 public SimpleTableColumn(String strColumnName, String strDisplayName, 126 ITableColumnEvaluator objEvaluator, boolean bSortable) 127 { 128 super(strColumnName, bSortable, null); 129 setComparator(new DefaultTableComparator()); 130 setDisplayName(strDisplayName); 131 setColumnRendererSource(DEFAULT_COLUMN_RENDERER_SOURCE); 132 setValueRendererSource(DEFAULT_VALUE_RENDERER_SOURCE); 133 setEvaluator(objEvaluator); 134 } 135 136 142 public String getDisplayName() 143 { 144 return m_strDisplayName; 145 } 146 147 153 public void setDisplayName(String displayName) 154 { 155 m_strDisplayName = displayName; 156 } 157 158 163 public ITableColumnEvaluator getEvaluator() 164 { 165 return m_objEvaluator; 166 } 167 168 174 public void setEvaluator(ITableColumnEvaluator evaluator) 175 { 176 m_objEvaluator = evaluator; 177 } 178 179 189 public void setColumnComparator(Comparator comparator) 190 { 191 setComparator(new ColumnComparator(this, comparator)); 192 } 193 194 201 public Object getColumnValue(Object objRow) 202 { 203 ITableColumnEvaluator objEvaluator = getEvaluator(); 204 if (objEvaluator != null) 205 return objEvaluator.getColumnValue(this, objRow); 206 207 return objRow.toString(); 209 } 210 211 218 public void loadSettings(IComponent objSettingsContainer) 219 { 220 String strDisplayName = objSettingsContainer.getMessages().getMessage(getColumnName()); 221 222 225 if (!strDisplayName.startsWith("[")) 226 setDisplayName(strDisplayName); 227 228 super.loadSettings(objSettingsContainer); 229 } 230 231 public class DefaultTableComparator implements Comparator , Serializable 232 { 233 private static final long serialVersionUID = 1L; 234 235 public int compare(Object objRow1, Object objRow2) 236 { 237 Object objValue1 = getColumnValue(objRow1); 238 Object objValue2 = getColumnValue(objRow2); 239 240 if (objValue1 == objValue2) 241 return 0; 242 243 boolean bComparable1 = objValue1 instanceof Comparable ; 244 boolean bComparable2 = objValue2 instanceof Comparable ; 245 246 if (!bComparable1 && !bComparable2) 248 return 0; 249 250 if (!bComparable1) 253 return -1; 254 255 if (!bComparable2) 256 return 1; 257 258 return ((Comparable ) objValue1).compareTo(objValue2); 259 } 260 } 261 262 } | Popular Tags |