code refactoring

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1368211 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Evgeniy Berlog 2012-08-01 19:34:08 +00:00
parent 462647cd42
commit b04e0e9381
5 changed files with 488 additions and 407 deletions

View File

@ -1482,6 +1482,7 @@ public final class InternalSheet {
* if none currently exist
* @param drawingManager The DrawingManager2 for our workbook
* @param createIfMissing Should one be created if missing?
* @return location of EscherAggregate record. if no EscherAggregate record is found return -1
*/
public int aggregateDrawingRecords(DrawingManager2 drawingManager, boolean createIfMissing) {
int loc = findFirstRecordLocBySid(DrawingRecord.sid);

View File

@ -539,14 +539,18 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
//First record in drawing layer MUST be DrawingRecord
if (writtenEscherBytes + drawingData.length > RecordInputStream.MAX_RECORD_DATA_SIZE && i != 1) {
for (int j = 0; j < drawingData.length; j += RecordInputStream.MAX_RECORD_DATA_SIZE) {
ContinueRecord drawing = new ContinueRecord(Arrays.copyOfRange(drawingData, j, Math.min(j + RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length)));
byte[] buf = new byte[Math.min(RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length - j)];
System.arraycopy(drawingData, j, buf, 0, Math.min(RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length - j));
ContinueRecord drawing = new ContinueRecord(buf);
temp += drawing.serialize(pos + temp, data);
}
} else {
for (int j = 0; j < drawingData.length; j += RecordInputStream.MAX_RECORD_DATA_SIZE) {
if (j == 0) {
DrawingRecord drawing = new DrawingRecord();
drawing.setData(Arrays.copyOfRange(drawingData, j, Math.min(j + RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length)));
byte[] buf = new byte[Math.min(RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length - j)];
System.arraycopy(drawingData, j, buf, 0, Math.min(RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length - j));
drawing.setData(buf);
temp += drawing.serialize(pos + temp, data);
} else {
ContinueRecord drawing = new ContinueRecord(Arrays.copyOfRange(drawingData, j, Math.min(j + RecordInputStream.MAX_RECORD_DATA_SIZE, drawingData.length)));

View File

@ -51,4 +51,6 @@ public interface HSSFShapeContainer extends Iterable<HSSFShape>
public int getX2();
public int getY2();
public boolean removeShape(HSSFShape shape);
}

View File

@ -24,6 +24,7 @@ import java.util.List;
import java.util.TreeMap;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.hssf.model.DrawingManager2;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.model.InternalWorkbook;
@ -52,6 +53,7 @@ import org.apache.poi.util.POILogger;
/**
* High level representation of a worksheet.
*
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Glen Stampoultzis (glens at apache.org)
* @author Libin Roman (romal at vistaportal.com)
@ -76,7 +78,9 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* reference to the low level {@link InternalSheet} object
*/
private final InternalSheet _sheet;
/** stores rows by zero-based row number */
/**
* stores rows by zero-based row number
*/
private final TreeMap<Integer, HSSFRow> _rows;
protected final InternalWorkbook _book;
protected final HSSFWorkbook _workbook;
@ -205,8 +209,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @see org.apache.poi.hssf.usermodel.HSSFRow
* @see #removeRow(org.apache.poi.ss.usermodel.Row)
*/
public HSSFRow createRow(int rownum)
{
public HSSFRow createRow(int rownum) {
HSSFRow row = new HSSFRow(_workbook, this, rownum);
// new rows inherit default height from the sheet
row.setHeight(getDefaultRowHeight());
@ -218,12 +221,12 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Used internally to create a high level Row object from a low level row object.
* USed when reading an existing file
*
* @param row low level record to represent as a high level Row and add to sheet
* @return HSSFRow high level representation
*/
private HSSFRow createRowFromRecord(RowRecord row)
{
private HSSFRow createRowFromRecord(RowRecord row) {
HSSFRow hrow = new HSSFRow(_workbook, this, row);
addRow(hrow, false);
@ -255,12 +258,10 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
//should not happen if the input argument is valid
throw new IllegalArgumentException("Specified row does not belong to this sheet");
}
if (hrow.getRowNum() == getLastRowNum())
{
if (hrow.getRowNum() == getLastRowNum()) {
_lastrow = findLastRow(_lastrow);
}
if (hrow.getRowNum() == getFirstRowNum())
{
if (hrow.getRowNum() == getFirstRowNum()) {
_firstrow = findFirstRow(_firstrow);
}
_sheet.removeRow(hrow.getRowRecord());
@ -290,13 +291,11 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* used internally to refresh the "first row" when the first row is removed.
*/
private int findFirstRow(int firstrow)
{
private int findFirstRow(int firstrow) {
int rownum = firstrow + 1;
HSSFRow r = getRow(rownum);
while (r == null && rownum <= getLastRowNum())
{
while (r == null && rownum <= getLastRowNum()) {
r = getRow(++rownum);
}
@ -312,20 +311,16 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @param addLow whether to add the row to the low level model - false if its already there
*/
private void addRow(HSSFRow row, boolean addLow)
{
private void addRow(HSSFRow row, boolean addLow) {
_rows.put(Integer.valueOf(row.getRowNum()), row);
if (addLow)
{
if (addLow) {
_sheet.addRow(row.getRowRecord());
}
boolean firstRow = _rows.size() == 1;
if (row.getRowNum() > getLastRowNum() || firstRow)
{
if (row.getRowNum() > getLastRowNum() || firstRow) {
_lastrow = row.getRowNum();
}
if (row.getRowNum() < getFirstRowNum() || firstRow)
{
if (row.getRowNum() < getFirstRowNum() || firstRow) {
_firstrow = row.getRowNum();
}
}
@ -333,6 +328,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Returns the logical row (not physical) 0-based. If you ask for a row that is not
* defined you get a null. This is to say row 4 represents the fifth row on a sheet.
*
* @param rowIndex row to get
* @return HSSFRow representing the row number or null if its not defined on the sheet
*/
@ -349,6 +345,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Gets the first row on the sheet
*
* @return the number of the first logical row on the sheet, zero based
*/
public int getFirstRowNum() {
@ -365,6 +362,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* call {@link #getPhysicalNumberOfRows()} to
* tell if there is a row at position zero
* or not.
*
* @return the number of the last row contained in this sheet, zero based.
*/
public int getLastRowNum() {
@ -424,6 +422,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Get the visibility state for a given column.
*
* @param columnIndex - the column to get (0-based)
* @param hidden - the visiblity state of the column
*/
@ -433,6 +432,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Get the hidden state for a given column.
*
* @param columnIndex - the column to set (0-based)
* @return hidden - <code>false</code> if the column is visible
*/
@ -442,13 +442,13 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Set the width (in units of 1/256th of a character width)
*
* <p/>
* <p>
* The maximum column width for an individual cell is 255 characters.
* This value represents the number of characters that can be displayed
* in a cell that is formatted with the standard font (first font in the workbook).
* </p>
*
* <p/>
* <p>
* Character width is defined as the maximum digit width
* of the numbers <code>0, 1, 2, ... 9</code> as rendered
@ -457,7 +457,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* Unless you are using a very special font, the default character is '0' (zero),
* this is true for Arial (default font font in HSSF) and Calibri (default font in XSSF)
* </p>
*
* <p/>
* <p>
* Please note, that the width set by this method includes 4 pixels of margin padding (two on each side),
* plus 1 pixel padding for the gridlines (Section 3.3.1.12 of the OOXML spec).
@ -475,9 +475,9 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* If you set a column width to be eight characters wide, e.g. <code>setColumnWidth(columnIndex, 8*256)</code>,
* then the actual value of visible characters (the value shown in Excel) is derived from the following equation:
* <code>
Truncate([numChars*7+5]/7*256)/256 = 8;
* Truncate([numChars*7+5]/7*256)/256 = 8;
* </code>
*
* <p/>
* which gives <code>7.29</code>.
*
* @param columnIndex - the column to set (0-based)
@ -490,6 +490,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* get the width (in units of 1/256th of a character width )
*
* @param columnIndex - the column to set (0-based)
* @return width - the width in units of 1/256th of a character width
*/
@ -500,14 +501,17 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* get the default column width for the sheet (if the columns do not define their own width) in
* characters
*
* @return default column width
*/
public int getDefaultColumnWidth() {
return _sheet.getDefaultColumnWidth();
}
/**
* set the default column width for the sheet (if the columns do not define their own width) in
* characters
*
* @param width default column width
*/
public void setDefaultColumnWidth(int width) {
@ -530,30 +534,29 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @return default row height in points
*/
public float getDefaultRowHeightInPoints()
{
public float getDefaultRowHeightInPoints() {
return ((float) _sheet.getDefaultRowHeight() / 20);
}
/**
* set the default row height for the sheet (if the rows do not define their own height) in
* twips (1/20 of a point)
*
* @param height default row height
*/
public void setDefaultRowHeight(short height)
{
public void setDefaultRowHeight(short height) {
_sheet.setDefaultRowHeight(height);
}
/**
* set the default row height for the sheet (if the rows do not define their own height) in
* points
*
* @param height default row height
*/
public void setDefaultRowHeightInPoints(float height)
{
public void setDefaultRowHeightInPoints(float height) {
_sheet.setDefaultRowHeight((short) (height * 20));
}
@ -576,42 +579,42 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* get whether gridlines are printed.
*
* @return true if printed
*/
public boolean isGridsPrinted()
{
public boolean isGridsPrinted() {
return _sheet.isGridsPrinted();
}
/**
* set whether gridlines printed.
*
* @param value false if not printed.
*/
public void setGridsPrinted(boolean value)
{
public void setGridsPrinted(boolean value) {
_sheet.setGridsPrinted(value);
}
/**
* @deprecated (Aug-2008) use <tt>CellRangeAddress</tt> instead of <tt>Region</tt>
*/
public int addMergedRegion(org.apache.poi.ss.util.Region region)
{
public int addMergedRegion(org.apache.poi.ss.util.Region region) {
return _sheet.addMergedRegion(region.getRowFrom(),
region.getColumnFrom(),
//(short) region.getRowTo(),
region.getRowTo(),
region.getColumnTo());
}
/**
* adds a merged region of cells (hence those cells form one)
*
* @param region (rowfrom/colfrom-rowto/colto) to merge
* @return index of this region
*/
public int addMergedRegion(CellRangeAddress region)
{
public int addMergedRegion(CellRangeAddress region) {
region.validate(SpreadsheetVersion.EXCEL97);
// throw IllegalStateException if the argument CellRangeAddress intersects with
@ -655,50 +658,51 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Control if Excel should be asked to recalculate all formulas on this sheet
* when the workbook is opened.
*
* <p/>
* <p>
* Calculating the formula values with {@link org.apache.poi.ss.usermodel.FormulaEvaluator} is the
* recommended solution, but this may be used for certain cases where
* evaluation in POI is not possible.
* </p>
*
* <p/>
* <p>
* It is recommended to force recalcuation of formulas on workbook level using
* {@link org.apache.poi.ss.usermodel.Workbook#setForceFormulaRecalculation(boolean)}
* to ensure that all cross-worksheet formuals and external dependencies are updated.
* </p>
*
* @param value true if the application will perform a full recalculation of
* this worksheet values when the workbook is opened
*
* @see org.apache.poi.ss.usermodel.Workbook#setForceFormulaRecalculation(boolean)
*/
public void setForceFormulaRecalculation(boolean value)
{
public void setForceFormulaRecalculation(boolean value) {
_sheet.setUncalced(value);
}
/**
* Whether a record must be inserted or not at generation to indicate that
* formula must be recalculated when workbook is opened.
*
* @return true if an uncalced record must be inserted or not at generation
*/
public boolean getForceFormulaRecalculation()
{
public boolean getForceFormulaRecalculation() {
return _sheet.getUncalced();
}
/**
* determines whether the output is vertically centered on the page.
*
* @param value true to vertically center, false otherwise.
*/
public void setVerticallyCenter(boolean value)
{
public void setVerticallyCenter(boolean value) {
_sheet.getPageSettings().getVCenter().setVCenter(value);
}
/**
* TODO: Boolean not needed, remove after next release
*
* @deprecated (Mar-2008) use getVerticallyCenter() instead
*/
public boolean getVerticallyCenter(boolean value) {
@ -708,18 +712,17 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Determine whether printed output for this sheet will be vertically centered.
*/
public boolean getVerticallyCenter()
{
public boolean getVerticallyCenter() {
return _sheet.getPageSettings().getVCenter().getVCenter();
}
/**
* determines whether the output is horizontally centered on the page.
*
* @param value true to horizontally center, false otherwise.
*/
public void setHorizontallyCenter(boolean value)
{
public void setHorizontallyCenter(boolean value) {
_sheet.getPageSettings().getHCenter().setHCenter(value);
}
@ -727,8 +730,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* Determine whether printed output for this sheet will be horizontally centered.
*/
public boolean getHorizontallyCenter()
{
public boolean getHorizontallyCenter() {
return _sheet.getPageSettings().getHCenter().getHCenter();
}
@ -738,8 +740,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
*
* @param value true for right to left, false otherwise.
*/
public void setRightToLeft(boolean value)
{
public void setRightToLeft(boolean value) {
_sheet.getWindowTwo().setArabic(value);
}
@ -748,28 +749,27 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
*
* @return whether the text is displayed in right-to-left mode in the window
*/
public boolean isRightToLeft()
{
public boolean isRightToLeft() {
return _sheet.getWindowTwo().getArabic();
}
/**
* removes a merged region of cells (hence letting them free)
*
* @param index of the region to unmerge
*/
public void removeMergedRegion(int index)
{
public void removeMergedRegion(int index) {
_sheet.removeMergedRegion(index);
}
/**
* returns the number of merged regions
*
* @return number of merged regions
*/
public int getNumMergedRegions()
{
public int getNumMergedRegions() {
return _sheet.getNumMergedRegions();
}
@ -782,6 +782,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
return new org.apache.poi.hssf.util.Region(cra.getFirstRow(), (short) cra.getFirstColumn(),
cra.getLastRow(), (short) cra.getLastColumn());
}
/**
* @return the merged region at the specified index
*/
@ -799,6 +800,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
Iterator<Row> result = (Iterator<Row>) (Iterator<? extends Row>) _rows.values().iterator();
return result;
}
/**
* Alias for {@link #rowIterator()} to allow
* foreach loops
@ -1041,26 +1043,32 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Note - this is not the same as whether the sheet is focused (isActive)
*
* @return <code>true</code> if this sheet is currently selected
*/
public boolean isSelected() {
return getSheet().getWindowTwo().getSelected();
}
/**
* Sets whether sheet is selected.
*
* @param sel Whether to select the sheet or deselect the sheet.
*/
public void setSelected(boolean sel) {
getSheet().getWindowTwo().setSelected(sel);
}
/**
* @return <code>true</code> if this sheet is currently focused
*/
public boolean isActive() {
return getSheet().getWindowTwo().isActive();
}
/**
* Sets whether sheet is selected.
*
* @param sel Whether to select the sheet or deselect the sheet.
*/
public void setActive(boolean sel) {
@ -1069,6 +1077,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Gets the size of the margin in inches.
*
* @param margin which margin to get
* @return the size of the margin
*/
@ -1085,6 +1094,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Sets the size of the margin in inches.
*
* @param margin which margin to get
* @param size the size of the margin
*/
@ -1104,8 +1114,10 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
private WorksheetProtectionBlock getProtectionBlock() {
return _sheet.getProtectionBlock();
}
/**
* Answer whether protection is enabled or disabled
*
* @return true => protection enabled; false => protection disabled
*/
public boolean getProtect() {
@ -1121,6 +1133,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Answer whether object protection is enabled or disabled
*
* @return true => protection enabled; false => protection disabled
*/
public boolean getObjectProtect() {
@ -1129,13 +1142,16 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Answer whether scenario protection is enabled or disabled
*
* @return true => protection enabled; false => protection disabled
*/
public boolean getScenarioProtect() {
return getProtectionBlock().isScenarioProtected();
}
/**
* Sets the protection enabled as well as the password
*
* @param password to set for protection. Pass <code>null</code> to remove protection
*/
public void protectSheet(String password) {
@ -1150,8 +1166,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @param numerator The numerator for the zoom magnification.
* @param denominator The denominator for the zoom magnification.
*/
public void setZoom( int numerator, int denominator)
{
public void setZoom(int numerator, int denominator) {
if (numerator < 1 || numerator > 65535)
throw new IllegalArgumentException("Numerator must be greater than 1 and less than 65536");
if (denominator < 1 || denominator > 65535)
@ -1166,6 +1181,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* The top row in the visible view when the sheet is
* first viewed after opening it in a viewer
*
* @return short indicating the rownum (0 based) of the top row
*/
public short getTopRow() {
@ -1175,6 +1191,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* The left col in the visible view when the sheet is
* first viewed after opening it in a viewer
*
* @return short indicating the rownum (0 based) of the top row
*/
public short getLeftCol() {
@ -1184,6 +1201,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Sets desktop window pane display area, when the
* file is first opened in a viewer.
*
* @param toprow the top row to show in desktop window pane
* @param leftcol the left column to show in desktop window pane
*/
@ -1194,8 +1212,9 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Shifts the merged regions left or right depending on mode
* <p>
* <p/>
* TODO: MODE , this is only row specific
*
* @param startRow
* @param endRow
* @param n
@ -1240,12 +1259,13 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* Shifts rows between startRow and endRow n number of rows.
* If you use a negative number, it will shift rows up.
* Code ensures that rows don't wrap around.
*
* <p/>
* Calls shiftRows(startRow, endRow, n, false, false);
*
* <p>
* <p/>
* <p/>
* Additionally shifts merged regions that are completely defined in these
* rows (ie. merged 2 cells on a row to be shifted).
*
* @param startRow the row to start shifting
* @param endRow the row to end shifting
* @param n the number of rows to shift
@ -1258,12 +1278,13 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* Shifts rows between startRow and endRow n number of rows.
* If you use a negative number, it will shift rows up.
* Code ensures that rows don't wrap around
*
* <p>
* <p/>
* <p/>
* Additionally shifts merged regions that are completely defined in these
* rows (ie. merged 2 cells on a row to be shifted).
* <p>
* <p/>
* TODO Might want to add bounds checking here
*
* @param startRow the row to start shifting
* @param endRow the row to end shifting
* @param n the number of rows to shift
@ -1278,12 +1299,13 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* Shifts rows between startRow and endRow n number of rows.
* If you use a negative number, it will shift rows up.
* Code ensures that rows don't wrap around
*
* <p>
* <p/>
* <p/>
* Additionally shifts merged regions that are completely defined in these
* rows (ie. merged 2 cells on a row to be shifted).
* <p>
* <p/>
* TODO Might want to add bounds checking here
*
* @param startRow the row to start shifting
* @param endRow the row to end shifting
* @param n the number of rows to shift
@ -1455,7 +1477,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
*
* <p/>
* <p>
* If both colSplit and rowSplit are zero then the existing freeze pane is removed
* </p>
@ -1468,14 +1490,16 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
public void createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow) {
validateColumn(colSplit);
validateRow(rowSplit);
if (leftmostColumn < colSplit) throw new IllegalArgumentException("leftmostColumn parameter must not be less than colSplit parameter");
if (topRow < rowSplit) throw new IllegalArgumentException("topRow parameter must not be less than leftmostColumn parameter");
if (leftmostColumn < colSplit)
throw new IllegalArgumentException("leftmostColumn parameter must not be less than colSplit parameter");
if (topRow < rowSplit)
throw new IllegalArgumentException("topRow parameter must not be less than leftmostColumn parameter");
getSheet().createFreezePane(colSplit, rowSplit, topRow, leftmostColumn);
}
/**
* Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
*
* <p/>
* <p>
* If both colSplit and rowSplit are zero then the existing freeze pane is removed
* </p>
@ -1489,6 +1513,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Creates a split pane. Any existing freezepane or split pane is overwritten.
*
* @param xSplitPos Horizonatal position of split (in 1/20th of a point).
* @param ySplitPos Vertical position of split (in 1/20th of a point).
* @param topRow Top row visible in bottom pane
@ -1506,6 +1531,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Returns the information regarding the currently configured pane (split or freeze).
*
* @return null if no pane configured, or the pane information.
*/
public PaneInformation getPaneInformation() {
@ -1514,6 +1540,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Sets whether the gridlines are shown in a viewer.
*
* @param show whether to show gridlines or not
*/
public void setDisplayGridlines(boolean show) {
@ -1522,6 +1549,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Returns if gridlines are displayed.
*
* @return whether gridlines are displayed
*/
public boolean isDisplayGridlines() {
@ -1530,6 +1558,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Sets whether the formulas are shown in a viewer.
*
* @param show whether to show formulas or not
*/
public void setDisplayFormulas(boolean show) {
@ -1538,6 +1567,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Returns if formulas are displayed.
*
* @return whether formulas are displayed
*/
public boolean isDisplayFormulas() {
@ -1546,6 +1576,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Sets whether the RowColHeadings are shown in a viewer.
*
* @param show whether to show RowColHeadings or not
*/
public void setDisplayRowColHeadings(boolean show) {
@ -1554,6 +1585,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Returns if RowColHeadings are displayed.
*
* @return whether RowColHeadings are displayed
*/
public boolean isDisplayRowColHeadings() {
@ -1563,7 +1595,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Sets a page break at the indicated row
* Breaks occur above the specified row and left of the specified column inclusive.
*
* <p/>
* For example, <code>sheet.setColumnBreak(2);</code> breaks the sheet into two parts
* with columns A,B,C in the first and D,E,... in the second. Simuilar, <code>sheet.setRowBreak(2);</code>
* breaks the sheet into two parts with first three rows (rownum=1...3) in the first part
@ -1610,7 +1642,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Sets a page break at the indicated column.
* Breaks occur above the specified row and left of the specified column inclusive.
*
* <p/>
* For example, <code>sheet.setColumnBreak(2);</code> breaks the sheet into two parts
* with columns A,B,C in the first and D,E,... in the second. Simuilar, <code>sheet.setRowBreak(2);</code>
* breaks the sheet into two parts with first three rows (rownum=1...3) in the first part
@ -1625,6 +1657,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Determines if there is a page break at the indicated column
*
* @param column FIXME: Document this!
* @return FIXME: Document this!
*/
@ -1634,6 +1667,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Removes a page break at the indicated column
*
* @param column
*/
public void removeColumnBreak(int column) {
@ -1642,6 +1676,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Runs a bounds check for row numbers
*
* @param row
*/
protected void validateRow(int row) {
@ -1652,6 +1687,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Runs a bounds check for column numbers
*
* @param column
*/
protected void validateColumn(int column) {
@ -1681,36 +1717,9 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
w.flush();
}
/**
* Creates the top-level drawing patriarch. This will have
* the effect of removing any existing drawings on this
* sheet.
* This may then be used to add graphics or charts
* @return The new patriarch.
*/
public HSSFPatriarch createDrawingPatriarch() {
if(_patriarch == null){
// Create the drawing group if it doesn't already exist.
_workbook.initDrawings();
if(_patriarch == null){
_sheet.aggregateDrawingRecords(_book.getDrawingManager(), true);
EscherAggregate agg = (EscherAggregate) _sheet.findFirstRecordBySid(EscherAggregate.sid);
_patriarch = new HSSFPatriarch(this, agg);
_patriarch.afterCreate();
// agg.setPatriarch(_patriarch);
}
}
return _patriarch;
}
/**
* Returns the agregate escher records for this sheet,
* it there is one.
* WARNING - calling this will trigger a parsing of the
* associated escher records. Any that aren't supported
* (such as charts and complex drawing types) will almost
* certainly be lost or corrupted when written out.
*/
public EscherAggregate getDrawingEscherAggregate() {
_book.findDrawingGroup();
@ -1735,51 +1744,76 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
}
/**
* Returns the top-level drawing patriach, if there is
* one.
* This will hold any graphics or charts for the sheet.
* WARNING - calling this will trigger a parsing of the
* associated escher records. Any that aren't supported
* (such as charts and complex drawing types) will almost
* certainly be lost or corrupted when written out. Only
* use this with simple drawings, otherwise call
* {@link HSSFSheet#createDrawingPatriarch()} and
* start from scratch!
*
* @return the top-level drawing patriarch, if there is one, else returns null
*/
public HSSFPatriarch getDrawingPatriarch() {
if(_patriarch != null) return _patriarch;
EscherAggregate agg = getDrawingEscherAggregate();
if(agg == null) return null;
_patriarch = new HSSFPatriarch(this, agg);
// _patriarch.buildShapeTree();
//HSSFShapeFactory.createShapeTree();
//agg.setPatriarch(_patriarch);
//EscherAggregate.createShapeTree(EscherAggregate.getMainSpgrContainer(agg), agg.getPatriarch(), agg);
// Have it process the records into high level objects
// as best it can do (this step may eat anything
// that isn't supported, you were warned...)
// agg.convertRecordsToUserModel();
// Return what we could cope with
_patriarch = getPatriarch(false);
return _patriarch;
}
/**
* Creates the top-level drawing patriarch. This will have
* the effect of removing any existing drawings on this
* sheet.
* This may then be used to add graphics or charts
*
* @return The new patriarch.
*/
public HSSFPatriarch createDrawingPatriarch() {
_patriarch = getPatriarch(true);
return _patriarch;
}
private HSSFPatriarch getPatriarch(boolean createIfMissing) {
HSSFPatriarch patriarch = null;
if (_patriarch != null) {
return _patriarch;
}
DrawingManager2 dm = _book.findDrawingGroup();
if (null == dm) {
if (!createIfMissing) {
return null;
} else {
_book.createDrawingGroup();
dm = _book.getDrawingManager();
}
}
EscherAggregate agg = (EscherAggregate) _sheet.findFirstRecordBySid(EscherAggregate.sid);
if (null == agg) {
int pos = _sheet.aggregateDrawingRecords(dm, false);
if (-1 == pos) {
if (createIfMissing) {
pos = _sheet.aggregateDrawingRecords(dm, true);
agg = (EscherAggregate) _sheet.getRecords().get(pos);
patriarch = new HSSFPatriarch(this, agg);
patriarch.afterCreate();
return patriarch;
} else {
return null;
}
}
agg = (EscherAggregate) _sheet.getRecords().get(pos);
}
return new HSSFPatriarch(this, agg);
}
/**
* @deprecated (Sep 2008) use {@link #setColumnGroupCollapsed(int, boolean)}
*/
public void setColumnGroupCollapsed(short columnNumber, boolean collapsed) {
setColumnGroupCollapsed(columnNumber & 0xFFFF, collapsed);
}
/**
* @deprecated (Sep 2008) use {@link #groupColumn(int, int)}
*/
public void groupColumn(short fromColumn, short toColumn) {
groupColumn(fromColumn & 0xFFFF, toColumn & 0xFFFF);
}
/**
* @deprecated (Sep 2008) use {@link #ungroupColumn(int, int)}
*/
@ -1845,7 +1879,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Adjusts the column width to fit the contents.
*
* <p/>
* This process can be relatively slow on large sheets, so this should
* normally only be called once per column, at the end of your
* processing.
@ -1858,11 +1892,11 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
/**
* Adjusts the column width to fit the contents.
*
* <p/>
* This process can be relatively slow on large sheets, so this should
* normally only be called once per column, at the end of your
* processing.
*
* <p/>
* You can specify whether the content of merged cells should be considered or ignored.
* Default is to ignore merged cells.
*

View File

@ -0,0 +1,40 @@
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.ddf.EscherDgRecord;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.EscherAggregate;
/**
* @author Evgeniy Berlog
* @date 01.08.12
*/
public class TestPatriarch extends TestCase {
public void testGetPatriarch(){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sh = wb.createSheet();
assertNull(sh.getDrawingPatriarch());
HSSFPatriarch patriarch = sh.createDrawingPatriarch();
assertNotNull(patriarch);
patriarch.createSimpleShape(new HSSFClientAnchor());
patriarch.createSimpleShape(new HSSFClientAnchor());
assertSame(patriarch, sh.getDrawingPatriarch());
EscherAggregate agg = patriarch._getBoundAggregate();
EscherDgRecord dg = agg.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
int lastId = dg.getLastMSOSPID();
assertSame(patriarch, sh.createDrawingPatriarch());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sh = wb.getSheetAt(0);
patriarch = sh.createDrawingPatriarch();
dg = patriarch._getBoundAggregate().getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
assertEquals(lastId, dg.getLastMSOSPID());
}
}