1 19 20 package jxl.write.biff; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.io.IOException ; 25 26 import common.Assert; 27 import common.Logger; 28 29 import jxl.Range; 30 import jxl.Cell; 31 import jxl.CellType; 32 import jxl.write.WritableSheet; 33 import jxl.write.WriteException; 34 import jxl.write.Blank; 35 import jxl.biff.SheetRangeImpl; 36 import jxl.write.biff.File; 37 41 class MergedCells 42 { 43 46 private static Logger logger = Logger.getLogger(MergedCells.class); 47 48 51 private ArrayList ranges; 52 53 56 private WritableSheet sheet; 57 58 61 private static final int maxRangesPerSheet = 1020; 62 63 66 public MergedCells(WritableSheet ws) 67 { 68 ranges = new ArrayList (); 69 sheet = ws; 70 } 71 72 78 void add(Range r) 79 { 80 ranges.add(r); 81 } 82 83 86 void insertRow(int row) 87 { 88 SheetRangeImpl sr = null; 90 Iterator i = ranges.iterator(); 91 while (i.hasNext()) 92 { 93 sr = (SheetRangeImpl) i.next(); 94 sr.insertRow(row); 95 } 96 } 97 98 101 void insertColumn(int col) 102 { 103 SheetRangeImpl sr = null; 104 Iterator i = ranges.iterator(); 105 while (i.hasNext()) 106 { 107 sr = (SheetRangeImpl) i.next(); 108 sr.insertColumn(col); 109 } 110 } 111 112 115 void removeColumn(int col) 116 { 117 SheetRangeImpl sr = null; 118 Iterator i = ranges.iterator(); 119 while (i.hasNext()) 120 { 121 sr = (SheetRangeImpl) i.next(); 122 if (sr.getTopLeft().getColumn() == col && 123 sr.getBottomRight().getColumn() == col) 124 { 125 ranges.remove(ranges.indexOf(sr)); 128 } 129 else 130 { 131 sr.removeColumn(col); 132 } 133 } 134 } 135 136 139 void removeRow(int row) 140 { 141 SheetRangeImpl sr = null; 142 Iterator i = ranges.iterator(); 143 while (i.hasNext()) 144 { 145 sr = (SheetRangeImpl) i.next(); 146 if (sr.getTopLeft().getRow() == row && 147 sr.getBottomRight().getRow() == row) 148 { 149 i.remove(); 152 } 153 else 154 { 155 sr.removeRow(row); 156 } 157 } 158 } 159 160 165 Range[] getMergedCells() 166 { 167 Range[] cells = new Range[ranges.size()]; 168 169 for (int i=0; i < cells.length; i++) 170 { 171 cells[i] = (Range) ranges.get(i); 172 } 173 174 return cells; 175 } 176 177 183 void unmergeCells(Range r) 184 { 185 int index = ranges.indexOf(r); 186 187 if (index != -1) 188 { 189 ranges.remove(index); 190 } 191 } 192 193 196 void checkIntersections() 197 { 198 ArrayList newcells = new ArrayList (ranges.size()); 199 200 for (Iterator mci = ranges.iterator(); mci.hasNext() ; ) 201 { 202 SheetRangeImpl r = (SheetRangeImpl) mci.next(); 203 204 Iterator i = newcells.iterator(); 206 SheetRangeImpl range = null; 207 boolean intersects = false; 208 while (i.hasNext() && !intersects) 209 { 210 range = (SheetRangeImpl) i.next(); 211 212 if (range.intersects(r)) 213 { 214 logger.warn("Could not merge cells " + r + 215 " as they clash with an existing set of merged cells."); 216 217 intersects = true; 218 } 219 } 220 221 if (!intersects) 222 { 223 newcells.add(r); 224 } 225 } 226 227 ranges = newcells; 228 } 229 230 234 private void checkRanges() 235 { 236 try 237 { 238 SheetRangeImpl range = null; 239 240 for (int i = 0; i < ranges.size(); i++) 242 { 243 range = (SheetRangeImpl) ranges.get(i); 244 245 Cell tl = range.getTopLeft(); 247 Cell br = range.getBottomRight(); 248 boolean found = false; 249 250 for (int c = tl.getColumn(); c <= br.getColumn(); c++) 251 { 252 for (int r = tl.getRow(); r <= br.getRow(); r++) 253 { 254 Cell cell = sheet.getCell(c, r); 255 if (cell.getType() != CellType.EMPTY) 256 { 257 if (!found) 258 { 259 found = true; 260 } 261 else 262 { 263 logger.warn("Range " + range + 264 " contains more than one data cell. " + 265 "Setting the other cells to blank."); 266 Blank b = new Blank(c, r); 267 sheet.addCell(b); 268 } 269 } 270 } 271 } 272 } 273 } 274 catch (WriteException e) 275 { 276 Assert.verify(false); 278 } 279 } 280 281 void write(File outputFile) throws IOException 282 { 283 if (ranges.size() == 0) 284 { 285 return; 286 } 287 288 checkIntersections(); 289 checkRanges(); 290 291 if (ranges.size() < maxRangesPerSheet) 294 { 295 MergedCellsRecord mcr = new MergedCellsRecord(ranges); 296 outputFile.write(mcr); 297 return; 298 } 299 300 int numRecordsRequired = ranges.size() / maxRangesPerSheet + 1; 301 int pos = 0; 302 303 for (int i = 0 ; i < numRecordsRequired ; i++) 304 { 305 int numranges = Math.min(maxRangesPerSheet, ranges.size() - pos); 306 307 ArrayList cells = new ArrayList (numranges); 308 for (int j = 0 ; j < numranges ; j++) 309 { 310 cells.add(ranges.get(pos+j)); 311 } 312 313 MergedCellsRecord mcr = new MergedCellsRecord(cells); 314 outputFile.write(mcr); 315 316 pos += numranges; 317 } 318 } 319 } 320 | Popular Tags |