1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 24 import org.apache.poi.util.LittleEndian; 25 26 35 public class MergeCellsRecord 36 extends Record 37 { 38 public final static short sid = 0xe5; 39 private ArrayList field_2_regions; 40 41 public MergeCellsRecord() 42 { 43 } 44 45 52 53 public MergeCellsRecord(short sid, short size, byte [] data) 54 { 55 super(sid, size, data); 56 } 57 58 66 67 public MergeCellsRecord(short sid, short size, byte [] data, int offset) 68 { 69 super(sid, size, data, offset); 70 } 71 72 protected void fillFields(byte [] data, short size, int offset) 73 { 74 short numAreas = LittleEndian.getShort(data, 0 + offset); 75 field_2_regions = new ArrayList (numAreas + 10); 76 int pos = 2; 77 78 for (int k = 0; k < numAreas; k++) 79 { 80 MergedRegion region = 81 new MergedRegion(LittleEndian 82 .getShort(data, pos + offset), LittleEndian 83 .getShort(data, pos + 2 + offset), LittleEndian 84 .getShort(data, pos + 4 + offset), LittleEndian 85 .getShort(data, pos + 6 + offset)); 86 87 pos += 8; 88 field_2_regions.add(region); 89 } 90 } 91 92 97 98 public short getNumAreas() 99 { 100 if (field_2_regions == null) return 0; 102 return (short)field_2_regions.size(); 103 } 104 105 114 115 public void setNumAreas(short numareas) 116 { 117 118 } 119 120 131 132 public int addArea(int rowfrom, short colfrom, int rowto, short colto) 134 { 135 if (field_2_regions == null) 136 { 137 field_2_regions = new ArrayList (10); 138 } 139 MergedRegion region = new MergedRegion(rowfrom, rowto, colfrom, 140 colto); 141 142 field_2_regions.add(region); 143 return field_2_regions.size() - 1; 144 } 145 146 150 151 public void removeAreaAt(int area) 152 { 153 field_2_regions.remove(area); 154 } 155 156 161 162 public MergedRegion getAreaAt(int index) 163 { 164 return ( MergedRegion ) field_2_regions.get(index); 165 } 166 167 public int getRecordSize() 168 { 169 int retValue; 170 171 retValue = 6 + (8 * field_2_regions.size()); 172 return retValue; 173 } 174 175 public short getSid() 176 { 177 return sid; 178 } 179 180 public int serialize(int offset, byte [] data) 181 { 182 int recordsize = getRecordSize(); 183 int pos = 6; 184 185 LittleEndian.putShort(data, offset + 0, sid); 186 LittleEndian.putShort(data, offset + 2, ( short ) (recordsize - 4)); 187 LittleEndian.putShort(data, offset + 4, getNumAreas()); 188 for (int k = 0; k < getNumAreas(); k++) 189 { 190 MergedRegion region = getAreaAt(k); 191 192 LittleEndian.putShort(data, offset + pos, ( short ) region.row_from); 194 pos += 2; 195 LittleEndian.putShort(data, offset + pos, ( short ) region.row_to); 197 pos += 2; 198 LittleEndian.putShort(data, offset + pos, region.col_from); 199 pos += 2; 200 LittleEndian.putShort(data, offset + pos, region.col_to); 201 pos += 2; 202 } 203 return recordsize; 204 } 205 206 public String toString() 207 { 208 StringBuffer retval = new StringBuffer (); 209 210 retval.append("[MERGEDCELLS]").append("\n"); 211 retval.append(" .sid =").append(sid).append("\n"); 212 retval.append(" .numregions =").append(getNumAreas()) 213 .append("\n"); 214 for (int k = 0; k < getNumAreas(); k++) 215 { 216 MergedRegion region = ( MergedRegion ) field_2_regions.get(k); 217 218 retval.append(" .rowfrom =").append(region.row_from) 219 .append("\n"); 220 retval.append(" .colfrom =").append(region.col_from) 221 .append("\n"); 222 retval.append(" .rowto =").append(region.row_to) 223 .append("\n"); 224 retval.append(" .colto =").append(region.col_to) 225 .append("\n"); 226 } 227 retval.append("[MERGEDCELLS]").append("\n"); 228 return retval.toString(); 229 } 230 231 protected void validateSid(short id) 232 { 233 if (id != sid) 234 { 235 throw new RecordFormatException("NOT A MERGEDCELLS RECORD!! " 236 + id); 237 } 238 } 239 240 245 246 public class MergedRegion 247 { 248 249 252 253 public MergedRegion(int row_from, int row_to, short col_from, 255 short col_to) 256 { 257 this.row_from = row_from; 258 this.row_to = row_to; 259 this.col_from = col_from; 260 this.col_to = col_to; 261 } 262 263 266 267 public int row_from; 269 270 273 274 public int row_to; 276 277 280 281 public short col_from; 282 283 286 287 public short col_to; 288 } 289 290 public Object clone() { 291 MergeCellsRecord rec = new MergeCellsRecord(); 292 rec.field_2_regions = new ArrayList (); 293 Iterator iterator = field_2_regions.iterator(); 294 while (iterator.hasNext()) { 295 MergedRegion oldRegion = (MergedRegion)iterator.next(); 296 rec.addArea(oldRegion.row_from, oldRegion.col_from, oldRegion.row_to, oldRegion.col_to); 297 } 298 299 return rec; 300 } 301 } 302 | Popular Tags |