diff options
author | Javen O'Neal <onealj@apache.org> | 2016-06-20 04:16:32 +0000 |
---|---|---|
committer | Javen O'Neal <onealj@apache.org> | 2016-06-20 04:16:32 +0000 |
commit | 958597462653e9a53486c78fcdcada642e1b561e (patch) | |
tree | 9d78a52d5f411c86114c54f930ee825053217f43 /src/java/org/apache/poi | |
parent | 4dba923a565d1d3828907702f7e34255a7a850ce (diff) | |
download | poi-958597462653e9a53486c78fcdcada642e1b561e.tar.gz poi-958597462653e9a53486c78fcdcada642e1b561e.zip |
bug 59731: start migrating methods relating to row-shifting in HSSFSheet to HSSFRowShifter; consolidate with XSSFRowShifter into RowShifter
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749262 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi')
3 files changed, 197 insertions, 43 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 9b10b70afd..89b3c9e997 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -50,6 +50,7 @@ import org.apache.poi.hssf.record.aggregates.DataValidityTable; import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock; +import org.apache.poi.hssf.usermodel.helpers.HSSFRowShifter; import org.apache.poi.hssf.util.PaneInformation; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.FormulaShifter; @@ -64,6 +65,7 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationHelper; import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.helpers.RowShifter; import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddressList; @@ -1485,50 +1487,11 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { * @param endRow the end-index of the rows to shift, zero-based * @param n how far to shift, negative to shift up * @param isRow unused, kept for backwards compatibility - * @deprecated POI 3.15 beta 2. This will be made private in future releases. + * @deprecated POI 3.15 beta 2. Use {@link HSSFRowShifter#shiftMergedRegions(int, int, int)}. */ protected void shiftMerged(int startRow, int endRow, int n, boolean isRow) { - shiftMerged(startRow, endRow, n); - } - - /** - * Shifts, grows, or shrinks the merged regions due to a row shift - * - * @param startRow the start-index of the rows to shift, zero-based - * @param endRow the end-index of the rows to shift, zero-based - * @param n how far to shift, negative to shift up - * This should be kept in sync with {@link org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter#shiftMerged(int, int, int)} - */ - private void shiftMerged(int startRow, int endRow, int n) { - List<CellRangeAddress> shiftedRegions = new ArrayList<CellRangeAddress>(); - //move merged regions completely if they fall within the new region boundaries when they are shifted - for (int i = 0; i < getNumMergedRegions(); i++) { - CellRangeAddress merged = getMergedRegion(i); - - boolean inStart = (merged.getFirstRow() >= startRow || merged.getLastRow() >= startRow); - boolean inEnd = (merged.getFirstRow() <= endRow || merged.getLastRow() <= endRow); - - //don't check if it's not within the shifted area - if (!inStart || !inEnd) { - continue; - } - - //only shift if the region outside the shifted rows is not merged too - if (!merged.containsRow(startRow - 1) && - !merged.containsRow(endRow + 1)) { - merged.setFirstRow(merged.getFirstRow() + n); - merged.setLastRow(merged.getLastRow() + n); - //have to remove/add it back - shiftedRegions.add(merged); - removeMergedRegion(i); - i = i - 1; // we have to back up now since we removed one - } - } - - //read so it doesn't get shifted again - for (CellRangeAddress region : shiftedRegions) { - this.addMergedRegion(region); - } + RowShifter rowShifter = new HSSFRowShifter(this); + rowShifter.shiftMergedRegions(startRow, endRow, n); } /** @@ -1607,6 +1570,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { // Nothing to do return; } + + RowShifter rowShifter = new HSSFRowShifter(this); // Shift comments if (moveComments) { @@ -1614,7 +1579,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { } // Shift Merged Regions - shiftMerged(startRow, endRow, n); + rowShifter.shiftMergedRegions(startRow, endRow, n); // Shift Row Breaks _sheet.getPageSettings().shiftRowBreaks(startRow, endRow, n); diff --git a/src/java/org/apache/poi/hssf/usermodel/helpers/HSSFRowShifter.java b/src/java/org/apache/poi/hssf/usermodel/helpers/HSSFRowShifter.java new file mode 100644 index 0000000000..88a6f9b731 --- /dev/null +++ b/src/java/org/apache/poi/hssf/usermodel/helpers/HSSFRowShifter.java @@ -0,0 +1,68 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.usermodel.helpers; + +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.ss.formula.FormulaShifter; +import org.apache.poi.ss.formula.eval.NotImplementedException; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.helpers.RowShifter; +import org.apache.poi.util.Internal; +import org.apache.poi.util.NotImplemented; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; + +/** + * Helper for shifting rows up or down + * + * When possible, code should be implemented in the RowShifter abstract class to avoid duplication with {@link org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter} + */ +public final class HSSFRowShifter extends RowShifter { + private static final POILogger logger = POILogFactory.getLogger(HSSFRowShifter.class); + + public HSSFRowShifter(HSSFSheet sh) { + super(sh); + } + + @NotImplemented + public void updateNamedRanges(FormulaShifter shifter) { + throw new NotImplementedException("HSSFRowShifter.updateNamedRanges"); + } + + @NotImplemented + public void updateFormulas(FormulaShifter shifter) { + throw new NotImplementedException("updateFormulas"); + } + + @Internal + @NotImplemented + public void updateRowFormulas(Row row, FormulaShifter shifter) { + throw new NotImplementedException("updateRowFormulas"); + } + + @NotImplemented + public void updateConditionalFormatting(FormulaShifter shifter) { + throw new NotImplementedException("updateConditionalFormatting"); + } + + @NotImplemented + public void updateHyperlinks(FormulaShifter shifter) { + throw new NotImplementedException("updateHyperlinks"); + } + +} diff --git a/src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java b/src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java new file mode 100644 index 0000000000..a168080004 --- /dev/null +++ b/src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java @@ -0,0 +1,121 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel.helpers; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.poi.ss.formula.FormulaShifter; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.util.Internal; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; + +/** + * Helper for shifting rows up or down + * + * This abstract class exists to consolidate duplicated code between XSSFRowShifter and HSSFRowShifter (currently methods sprinkled throughout HSSFSheet) + */ +public abstract class RowShifter { + private static final POILogger logger = POILogFactory.getLogger(RowShifter.class); + protected final Sheet sheet; + + public RowShifter(Sheet sh) { + sheet = sh; + } + + /** + * Shifts, grows, or shrinks the merged regions due to a row shift + * + * @param startRow the row to start shifting + * @param endRow the row to end shifting + * @param n the number of rows to shift + * @return an array of affected merged regions + */ + public List<CellRangeAddress> shiftMergedRegions(int startRow, int endRow, int n) { + List<CellRangeAddress> shiftedRegions = new ArrayList<CellRangeAddress>(); + Set<Integer> removedIndices = new HashSet<Integer>(); + //move merged regions completely if they fall within the new region boundaries when they are shifted + int size = sheet.getNumMergedRegions(); + for (int i = 0; i < size; i++) { + CellRangeAddress merged = sheet.getMergedRegion(i); + + boolean inStart = (merged.getFirstRow() >= startRow || merged.getLastRow() >= startRow); + boolean inEnd = (merged.getFirstRow() <= endRow || merged.getLastRow() <= endRow); + + //don't check if it's not within the shifted area + if (!inStart || !inEnd) { + continue; + } + + //only shift if the region outside the shifted rows is not merged too + if (!merged.containsRow(startRow - 1) && !merged.containsRow(endRow + 1)) { + merged.setFirstRow(merged.getFirstRow() + n); + merged.setLastRow(merged.getLastRow() + n); + //have to remove/add it back + shiftedRegions.add(merged); + removedIndices.add(i); + } + } + + if(!removedIndices.isEmpty()) { + sheet.removeMergedRegions(removedIndices); + } + + //read so it doesn't get shifted again + for (CellRangeAddress region : shiftedRegions) { + sheet.addMergedRegion(region); + } + return shiftedRegions; + } + + /** + * Updated named ranges + */ + public abstract void updateNamedRanges(FormulaShifter shifter); + + /** + * Update formulas. + */ + public abstract void updateFormulas(FormulaShifter shifter); + + /** + * Update the formulas in specified row using the formula shifting policy specified by shifter + * + * @param row the row to update the formulas on + * @param shifter the formula shifting policy + */ + @Internal + public abstract void updateRowFormulas(Row row, FormulaShifter shifter); + + public abstract void updateConditionalFormatting(FormulaShifter shifter); + + /** + * Shift the Hyperlink anchors (not the hyperlink text, even if the hyperlink + * is of type LINK_DOCUMENT and refers to a cell that was shifted). Hyperlinks + * do not track the content they point to. + * + * @param shifter + */ + public abstract void updateHyperlinks(FormulaShifter shifter); + +} |