int start = tRow;
int end = bRow;
int dstStart = start + n;
//move merged regions completely if they fall within the new region boundaries when they are shifted
for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress merged = sheet.getMergedRegion(i);
int firstCol = merged.getFirstColumn();
int lastCol = merged.getLastColumn();
if (firstCol < lCol || lastCol > rCol) { //not total cover, skip
continue;
}
int firstRow = merged.getFirstRow();
int lastRow = merged.getLastRow();
CellRangeAddress[] rngs = new CellRangeAddress[2]; //[0] old, [1] new
boolean inStart= (firstRow >= start || lastRow >= start);
boolean inEnd = (firstRow <= end || lastRow <= end);
//not in moving area
if (!inStart || !inEnd) {
if (n < 0 && !inStart) { //merge area in deleted area
if (lastRow >= dstStart) {
merged.setLastRow(dstStart - 1);
if (firstRow <= merged.getLastRow()) {
if (firstRow != merged.getLastRow() || lastCol != firstCol) {
rngs[1] = merged;
}
}
rngs[0] = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
shiftedRegions.add(rngs);
sheet.removeMergedRegion(i);
i = i - 1; //back up now since we removed one
}
}
continue;
}
//moving area
if (firstRow >= start) {
merged.setFirstRow(firstRow + n);
} else if (firstRow >= dstStart) {
merged.setFirstRow(dstStart);
}
merged.setLastRow(lastRow + n);
if (merged.getFirstRow() != merged.getLastRow() || lastCol != firstCol) {
rngs[1] = merged;
}
rngs[0] = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
//have to remove/add it back
shiftedRegions.add(rngs);
sheet.removeMergedRegion(i);
i = i -1; // we have to back up now since we removed one
}
} else { //20100525, henrichen@zkoss.org: add shift columns
int start = lCol;
int end = rCol;
int dstStart = start + n;
//move merged regions completely if they fall within the new region boundaries when they are shifted
for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress merged = sheet.getMergedRegion(i);
int firstRow = merged.getFirstRow();
int lastRow = merged.getLastRow();
if (firstRow < tRow || lastRow > bRow) { //not total cover, skip
continue;
}
int firstCol = merged.getFirstColumn();
int lastCol = merged.getLastColumn();
CellRangeAddress[] rngs = new CellRangeAddress[2]; //[0] old, [1] new
boolean inStart= (merged.getFirstColumn() >= start || merged.getLastColumn() >= start);
boolean inEnd = (merged.getFirstColumn() <= end || merged.getLastColumn() <= end);
//don't check if it's not within the shifted area
if (!inStart || !inEnd) {
if (n < 0 && !inStart) { //merge area in deleted area
if (lastCol >= dstStart) {
merged.setLastColumn(dstStart - 1);
if (firstCol <= merged.getLastColumn()) {
if (firstCol != merged.getLastColumn() || lastRow != firstRow) {
rngs[1] = merged;
}
}
rngs[0] = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
shiftedRegions.add(rngs);
sheet.removeMergedRegion(i);
i = i - 1; //back up now since we removed one
}
}
continue;
}
//moving area
if (firstCol >= start) {
merged.setFirstColumn(firstCol + n);
} else if (firstCol >= dstStart) {
merged.setFirstColumn(dstStart);
}
merged.setLastColumn(lastCol + n);
if (merged.getLastColumn() != merged.getFirstColumn() || firstRow != lastRow) {
rngs[1] = merged;
}
rngs[0] = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
//have to remove/add it back
shiftedRegions.add(rngs);
sheet.removeMergedRegion(i);
i = i -1; // we have to back up now since we removed one
}
}
//read so it doesn't get shifted again
Iterator<CellRangeAddress[]> iterator = shiftedRegions.iterator();
while (iterator.hasNext()) {
CellRangeAddress region = iterator.next()[1];
if (region != null) {
sheet.addMergedRegion(region);
}
}