1 50 51 package com.lowagie.text; 52 53 import java.util.ArrayList ; 54 55 70 public class Row implements Element { 71 72 74 75 public static final int NULL = 0; 76 77 78 public static final int CELL = 1; 79 80 81 public static final int TABLE = 2; 82 83 85 86 protected int columns; 87 88 89 protected int currentColumn; 90 91 92 protected boolean[] reserved; 93 94 95 protected Object [] cells; 96 97 98 protected int horizontalAlignment; 99 100 102 107 protected Row(int columns) { 108 this.columns = columns; 109 reserved = new boolean[columns]; 110 cells = new Object [columns]; 111 currentColumn = 0; 112 } 113 114 116 123 public boolean process(ElementListener listener) { 124 try { 125 return listener.add(this); 126 } 127 catch(DocumentException de) { 128 return false; 129 } 130 } 131 132 137 public int type() { 138 return Element.ROW; 139 } 140 141 146 public ArrayList getChunks() { 147 return new ArrayList (); 148 } 149 150 152 158 void deleteColumn(int column) { 159 if ((column >= columns) || (column < 0)) { 160 throw new IndexOutOfBoundsException ("getCell at illegal index : " + column); 161 } 162 columns--; 163 boolean newReserved[] = new boolean[columns]; 164 Object newCells[] = new Cell[columns]; 165 166 for (int i = 0; i < column; i++) { 167 newReserved[i] = reserved[i]; 168 newCells[i] = cells[i]; 169 if (newCells[i] != null && (i + ((Cell) newCells[i]).getColspan() > column)) { 170 ((Cell) newCells[i]).setColspan(((Cell) cells[i]).getColspan() - 1); 171 } 172 } 173 for (int i = column; i < columns; i++) { 174 newReserved[i] = reserved[i + 1]; 175 newCells[i] = cells[i + 1]; 176 } 177 if (cells[column] != null && ((Cell) cells[column]).getColspan() > 1) { 178 newCells[column] = cells[column]; 179 ((Cell) newCells[column]).setColspan(((Cell) newCells[column]).getColspan() - 1); 180 } 181 reserved = newReserved; 182 cells = newCells; 183 } 184 185 187 194 int addElement(Object element) { 195 return addElement(element, currentColumn); 196 } 197 198 206 int addElement(Object element, int column) { 207 if (element == null) throw new NullPointerException ("addCell - null argument"); 208 if ((column < 0) || (column > columns)) throw new IndexOutOfBoundsException ("addCell - illegal column argument"); 209 if ( !((getObjectID(element) == CELL) || (getObjectID(element) == TABLE)) ) throw new IllegalArgumentException ("addCell - only Cells or Tables allowed"); 210 211 int lColspan = ( (Cell.class.isInstance(element)) ? ((Cell) element).getColspan() : 1); 212 213 if (!reserve(column, lColspan)) { 214 return -1; 215 } 216 217 cells[column] = element; 218 currentColumn += lColspan - 1; 219 220 return column; 221 } 222 223 229 void setElement(Object aElement, int column) { 230 if (reserved[column]) throw new IllegalArgumentException ("setElement - position already taken"); 231 232 cells[column] = aElement; 233 if (aElement != null) { 234 reserved[column] = true; 235 } 236 } 237 238 244 boolean reserve(int column) { 245 return reserve(column, 1); 246 } 247 248 249 256 boolean reserve(int column, int size) { 257 if ((column < 0) || ((column + size) > columns)) throw new IndexOutOfBoundsException ("reserve - incorrect column/size"); 258 259 for(int i=column; i < column + size; i++) 260 { 261 if (reserved[i]) { 262 for(int j=i; j >= column; j--) { 264 reserved[j] = false; 265 } 266 return false; 267 } 268 reserved[i] = true; 269 } 270 return true; 271 } 272 273 275 281 boolean isReserved(int column) { 282 return reserved[column]; 283 } 284 285 291 int getElementID(int column) { 292 if (cells[column] == null) return NULL; 293 else if (Cell.class.isInstance(cells[column])) return CELL; 294 else if (Table.class.isInstance(cells[column])) return TABLE; 295 296 return -1; 297 } 298 299 305 int getObjectID(Object element) { 306 if (element == null) return NULL; 307 else if (Cell.class.isInstance(element)) return CELL; 308 else if (Table.class.isInstance(element)) return TABLE; 309 return -1; 310 } 311 312 319 public Object getCell(int column) { 320 if ((column < 0) || (column > columns)) { 321 throw new IndexOutOfBoundsException ("getCell at illegal index :" + column + " max is " + columns); 322 } 323 return cells[column]; 324 } 325 326 331 public boolean isEmpty() { 332 for (int i = 0; i < columns; i++) { 333 if (cells[i] != null) { 334 return false; 335 } 336 } 337 return true; 338 } 339 340 345 public int getColumns() { 346 return columns; 347 } 348 349 354 public void setHorizontalAlignment(int value) { 355 horizontalAlignment = value; 356 } 357 358 363 public int getHorizontalAlignment() { 364 return horizontalAlignment; 365 } 366 } 367 | Popular Tags |