1 17 18 19 20 package org.apache.fop.layoutmgr.table; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.fop.datatypes.PercentBaseContext; 29 import org.apache.fop.datatypes.Length; 30 31 import org.apache.fop.fo.FONode; 32 import org.apache.fop.fo.flow.Table; 33 import org.apache.fop.fo.flow.TableColumn; 34 import org.apache.fop.fo.properties.TableColLength; 35 36 39 public class ColumnSetup { 40 41 42 private static Log log = LogFactory.getLog(ColumnSetup.class); 43 44 private Table table; 45 private List columns = new java.util.ArrayList (); 46 private List colWidths = new java.util.ArrayList (); 47 48 private int maxColIndexReferenced = 0; 49 50 54 public ColumnSetup(Table table) { 55 this.table = table; 56 prepareColumns(); 57 initializeColumnWidths(); 58 } 59 60 private void prepareColumns() { 61 List rawCols = table.getColumns(); 62 if (rawCols != null) { 63 int colnum = 1; 64 ListIterator iter = rawCols.listIterator(); 65 while (iter.hasNext()) { 66 TableColumn col = (TableColumn)iter.next(); 67 if (col == null) { 68 continue; 69 } 70 colnum = col.getColumnNumber(); 71 for (int i = 0; i < col.getNumberColumnsRepeated(); i++) { 72 while (colnum > columns.size()) { 73 columns.add(null); 74 } 75 columns.set(colnum - 1, col); 76 colnum++; 77 } 78 } 79 int pos = 1; 81 ListIterator ppIter = columns.listIterator(); 82 while (ppIter.hasNext()) { 83 TableColumn col = (TableColumn)ppIter.next(); 84 if (col == null) { 85 log.error("Found a gap in the table-columns at position " + pos); 86 } 87 pos++; 88 } 89 } 90 } 91 92 98 public TableColumn getColumn(int index) { 99 int size = columns.size(); 100 if (index > size) { 101 if (index > maxColIndexReferenced) { 102 maxColIndexReferenced = index; 103 if (!(size == 1 && getColumn(1).isDefaultColumn())) { 104 log.warn(FONode.decorateWithContextInfo( 105 "There are fewer table-columns than are needed. " 106 + "Column " + index + " was accessed, but only " 107 + size + " columns have been defined. " 108 + "The last defined column will be reused." 109 , table)); 110 if (!table.isAutoLayout()) { 111 log.warn("Please note that according XSL-FO 1.0 (7.26.9) says that " 112 + "the 'column-width' property must be specified for every " 113 + "column, unless the automatic table layout is used."); 114 } 115 } 116 } 117 return (TableColumn) columns.get(size - 1); 118 } else { 119 return (TableColumn) columns.get(index - 1); 120 } 121 } 122 123 124 public String toString() { 125 return columns.toString(); 126 } 127 128 129 public int getColumnCount() { 130 if (maxColIndexReferenced > columns.size()) { 131 return maxColIndexReferenced; 132 } else { 133 return columns.size(); 134 } 135 } 136 137 138 public Iterator iterator() { 139 return this.columns.iterator(); 140 } 141 142 154 155 159 private void initializeColumnWidths() { 160 161 TableColumn col; 162 Length colWidth; 163 164 for (int i = columns.size(); --i >= 0;) { 165 if (columns.get(i) != null) { 166 col = (TableColumn) columns.get(i); 167 colWidth = col.getColumnWidth(); 168 colWidths.add(0, colWidth); 169 } 170 } 171 colWidths.add(0, null); 172 } 173 174 181 protected double computeTableUnit(TableLayoutManager tlm) { 182 183 int sumCols = 0; 184 float factors = 0; 185 double unit = 0; 186 187 191 for (Iterator i = colWidths.iterator(); i.hasNext();) { 192 Length colWidth = (Length) i.next(); 193 if (colWidth != null) { 194 sumCols += colWidth.getValue(tlm); 195 if (colWidth instanceof TableColLength) { 196 factors += 197 ((TableColLength) colWidth).getTableUnits(); 198 } 199 } 200 } 201 202 205 if (factors > 0) { 206 if (sumCols < tlm.getContentAreaIPD()) { 207 unit = (tlm.getContentAreaIPD() - sumCols) / factors; 208 } else { 209 log.warn("No space remaining to distribute over columns."); 210 } 211 } 212 213 return unit; 214 } 215 216 221 public int getXOffset(int col, PercentBaseContext context) { 222 int xoffset = 0; 223 for (int i = col; --i >= 0;) { 224 if (colWidths.get(i) != null) { 225 xoffset += ((Length) colWidths.get(i)).getValue(context); 226 } 227 } 228 return xoffset; 229 } 230 231 236 public int getSumOfColumnWidths(PercentBaseContext context) { 237 int sum = 0; 238 for (int i = 1, c = getColumnCount(); i <= c; i++) { 239 int effIndex = i; 240 if (i >= colWidths.size()) { 241 effIndex = colWidths.size() - 1; 242 } 243 if (colWidths.get(effIndex) != null) { 244 sum += ((Length) colWidths.get(effIndex)).getValue(context); 245 } 246 } 247 return sum; 248 } 249 250 } 251 | Popular Tags |