import org.apache.poi.hssf.record.EOFRecord;
import org.apache.poi.hssf.record.GridsetRecord;
import org.apache.poi.hssf.record.GutsRecord;
-import org.apache.poi.hssf.record.HorizontalPageBreakRecord;
import org.apache.poi.hssf.record.HyperlinkRecord;
import org.apache.poi.hssf.record.IndexRecord;
import org.apache.poi.hssf.record.IterationRecord;
import org.apache.poi.hssf.record.SaveRecalcRecord;
import org.apache.poi.hssf.record.SelectionRecord;
import org.apache.poi.hssf.record.UncalcedRecord;
-import org.apache.poi.hssf.record.VerticalPageBreakRecord;
import org.apache.poi.hssf.record.WindowTwoRecord;
import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable;
import org.apache.poi.hssf.record.aggregates.DataValidityTable;
import org.apache.poi.hssf.record.aggregates.MergedCellsTable;
+import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
/**
* Finds correct insert positions for records in workbook streams<p/>
if (recClass == GutsRecord.class) {
return getGutsRecordInsertPos(records);
}
- if (recClass == HorizontalPageBreakRecord.class) {
- return getPageBreakRecordInsertPos(records, true);
- }
- if (recClass == VerticalPageBreakRecord.class) {
- return getPageBreakRecordInsertPos(records, false);
+ if (recClass == PageSettingsBlock.class) {
+ return getPageBreakRecordInsertPos(records);
}
throw new RuntimeException("Unexpected record class (" + recClass.getName() + ")");
}
- private static int getPageBreakRecordInsertPos(List records, boolean isHorizonal) {
+ private static int getPageBreakRecordInsertPos(List records) {
int dimensionsIndex = getDimensionsIndex(records);
int i = dimensionsIndex-1;
while (i > 0) {
i--;
Object rb = records.get(i);
- if (isPageBreakPriorRecord(rb, isHorizonal)) {
+ if (isPageBreakPriorRecord(rb)) {
return i+1;
}
}
throw new RuntimeException("Did not find insert point for GUTS");
}
- private static boolean isPageBreakPriorRecord(Object rb, boolean newRecIsHorizontal) {
+ private static boolean isPageBreakPriorRecord(Object rb) {
if (rb instanceof Record) {
Record record = (Record) rb;
switch (record.getSid()) {
case DefaultRowHeightRecord.sid:
case 0x0081: // SHEETPR
return true;
- }
- switch (record.getSid()) {
- // page settings block
- case HorizontalPageBreakRecord.sid:
- if (!newRecIsHorizontal) {
- return true;
- }
- return false;
- case VerticalPageBreakRecord.sid:
- return false;
- // next is case HeaderRecord.sid: case FooterRecord.sid:
- // then more records in page settings block
-
+ // next is the 'Worksheet Protection Block'
}
}
return false;
return _list.get(_nextIndex).getClass();
}
+ public int peekNextSid() {
+ if(_nextIndex >= _list.size()) {
+ return -1;
+ }
+ return ((Record)_list.get(_nextIndex)).getSid();
+ }
+
public int getCountRead() {
return _countRead;
}
import java.util.List;
import org.apache.poi.hssf.record.BOFRecord;
-import org.apache.poi.hssf.record.BottomMarginRecord;
import org.apache.poi.hssf.record.CFHeaderRecord;
import org.apache.poi.hssf.record.CalcCountRecord;
import org.apache.poi.hssf.record.CalcModeRecord;
import org.apache.poi.hssf.record.FooterRecord;
import org.apache.poi.hssf.record.GridsetRecord;
import org.apache.poi.hssf.record.GutsRecord;
-import org.apache.poi.hssf.record.HCenterRecord;
import org.apache.poi.hssf.record.HeaderRecord;
-import org.apache.poi.hssf.record.HorizontalPageBreakRecord;
import org.apache.poi.hssf.record.IndexRecord;
import org.apache.poi.hssf.record.IterationRecord;
-import org.apache.poi.hssf.record.LeftMarginRecord;
-import org.apache.poi.hssf.record.Margin;
import org.apache.poi.hssf.record.MergeCellsRecord;
import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.hssf.record.ObjectProtectRecord;
-import org.apache.poi.hssf.record.PageBreakRecord;
import org.apache.poi.hssf.record.PaneRecord;
import org.apache.poi.hssf.record.PasswordRecord;
import org.apache.poi.hssf.record.PrintGridlinesRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordBase;
import org.apache.poi.hssf.record.RefModeRecord;
-import org.apache.poi.hssf.record.RightMarginRecord;
import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.hssf.record.SCLRecord;
import org.apache.poi.hssf.record.SaveRecalcRecord;
import org.apache.poi.hssf.record.ScenarioProtectRecord;
import org.apache.poi.hssf.record.SelectionRecord;
import org.apache.poi.hssf.record.StringRecord;
-import org.apache.poi.hssf.record.TopMarginRecord;
import org.apache.poi.hssf.record.UncalcedRecord;
-import org.apache.poi.hssf.record.VCenterRecord;
-import org.apache.poi.hssf.record.VerticalPageBreakRecord;
import org.apache.poi.hssf.record.WSBoolRecord;
import org.apache.poi.hssf.record.WindowTwoRecord;
import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate;
import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable;
import org.apache.poi.hssf.record.aggregates.DataValidityTable;
import org.apache.poi.hssf.record.aggregates.MergedCellsTable;
+import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
import org.apache.poi.hssf.record.aggregates.RecordAggregate;
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
protected ArrayList records = null;
int preoffset = 0; // offset of the sheet in a new file
protected int dimsloc = -1; // TODO - is it legal for dims record to be missing?
- protected DimensionsRecord dims;
- protected DefaultColWidthRecord defaultcolwidth = null;
- protected DefaultRowHeightRecord defaultrowheight = null;
+ protected PrintGridlinesRecord printGridlines = null;
protected GridsetRecord gridset = null;
private GutsRecord _gutsRecord;
- protected PrintSetupRecord printSetup = null;
- protected HeaderRecord header = null;
- protected FooterRecord footer = null;
- protected PrintGridlinesRecord printGridlines = null;
+ protected DefaultColWidthRecord defaultcolwidth = null;
+ protected DefaultRowHeightRecord defaultrowheight = null;
+ private PageSettingsBlock _psBlock;
+
+ // 'Worksheet Protection Block'
+ protected ProtectRecord protect = null;
+ protected ObjectProtectRecord objprotect = null;
+ protected ScenarioProtectRecord scenprotect = null;
+ protected PasswordRecord password = null;
+
protected WindowTwoRecord windowTwo = null;
- protected Margin[] margins = null;
- private MergedCellsTable _mergedCellsTable;
protected SelectionRecord selection = null;
+ private MergedCellsTable _mergedCellsTable;
/** always present in this POI object, not always written to Excel file */
/*package*/ColumnInfoRecordsAggregate _columnInfos;
+ protected DimensionsRecord dims;
protected RowRecordsAggregate _rowsAggregate = null;
- private Iterator rowRecIterator = null;
- protected int eofLoc = 0;
- protected ProtectRecord protect = null;
- protected PageBreakRecord _rowBreaksRecord;
- protected PageBreakRecord _columnBreaksRecord;
private DataValidityTable _dataValidityTable= null;
- protected ObjectProtectRecord objprotect = null;
- protected ScenarioProtectRecord scenprotect = null;
- protected PasswordRecord password = null;
private ConditionalFormattingTable condFormatting;
+ protected int eofLoc = 0;
+ private Iterator rowRecIterator = null;
+
/** Add an UncalcedRecord if not true indicating formulas have not been calculated */
protected boolean _isUncalced = false;
}
continue;
}
-
+
+ if (PageSettingsBlock.isComponentRecord(rec.getSid())) {
+ RecordStream rs = new RecordStream(recs, k);
+ PageSettingsBlock psb = new PageSettingsBlock(rs);
+ if (bofEofNestingLevel == 1) {
+ if (retval._psBlock == null) {
+ retval._psBlock = psb;
+ } else {
+ // more than one 'Page Settings Block' at nesting level 1 ?
+ // apparently this happens in about 15 test sample files
+ }
+ }
+ records.add(psb);
+ k += rs.getCountRead()-1;
+ continue;
+ }
if (rec.getSid() == MergeCellsRecord.sid) {
RecordStream rs = new RecordStream(recs, k);
retval._mergedCellsTable = new MergedCellsTable(rs);
records.add(retval._mergedCellsTable);
- continue; // TODO
+ continue;
}
if (rec.getSid() == BOFRecord.sid)
{
retval.gridset = (GridsetRecord) rec;
}
- else if ( rec.getSid() == HeaderRecord.sid && bofEofNestingLevel == 1)
- {
- retval.header = (HeaderRecord) rec;
- }
- else if ( rec.getSid() == FooterRecord.sid && bofEofNestingLevel == 1)
- {
- retval.footer = (FooterRecord) rec;
- }
- else if ( rec.getSid() == PrintSetupRecord.sid && bofEofNestingLevel == 1)
- {
- retval.printSetup = (PrintSetupRecord) rec;
- }
- else if ( rec.getSid() == LeftMarginRecord.sid)
- {
- retval.getMargins()[LeftMargin] = (LeftMarginRecord) rec;
- }
- else if ( rec.getSid() == RightMarginRecord.sid)
- {
- retval.getMargins()[RightMargin] = (RightMarginRecord) rec;
- }
- else if ( rec.getSid() == TopMarginRecord.sid)
- {
- retval.getMargins()[TopMargin] = (TopMarginRecord) rec;
- }
- else if ( rec.getSid() == BottomMarginRecord.sid)
- {
- retval.getMargins()[BottomMargin] = (BottomMarginRecord) rec;
- }
else if ( rec.getSid() == SelectionRecord.sid )
{
retval.selection = (SelectionRecord) rec;
{
retval.password = (PasswordRecord) rec;
}
- else if (rec.getSid() == HorizontalPageBreakRecord.sid)
- {
- retval._rowBreaksRecord = (HorizontalPageBreakRecord)rec;
- }
- else if (rec.getSid() == VerticalPageBreakRecord.sid)
- {
- retval._columnBreaksRecord = (VerticalPageBreakRecord)rec;
- }
records.add(rec);
}
records.add( retval.createWSBool() );
// 'Page Settings Block'
- retval._rowBreaksRecord = new HorizontalPageBreakRecord();
- records.add(retval._rowBreaksRecord);
- retval._columnBreaksRecord = new VerticalPageBreakRecord();
- records.add(retval._columnBreaksRecord);
-
- retval.header = createHeader();
- records.add( retval.header );
- retval.footer = createFooter();
- records.add( retval.footer );
- records.add(createHCenter() );
- records.add(createVCenter() );
- retval.printSetup = createPrintSetup();
- records.add( retval.printSetup );
-
+ retval._psBlock = new PageSettingsBlock();
+ records.add(retval._psBlock);
+
// 'Worksheet Protection Block' (after 'Page Settings Block' and before DEFCOLWIDTH)
// PROTECT record normally goes here, don't add yet since the flag is initially false
return retval;
}
- /**
- * creates the Header Record and sets it to nothing/0 length
- */
- private static HeaderRecord createHeader() {
- HeaderRecord retval = new HeaderRecord();
-
- retval.setHeaderLength(( byte ) 0);
- retval.setHeader(null);
- return retval;
- }
-
- /**
- * creates the Footer Record and sets it to nothing/0 length
- */
- private static FooterRecord createFooter() {
- FooterRecord retval = new FooterRecord();
-
- retval.setFooterLength(( byte ) 0);
- retval.setFooter(null);
- return retval;
- }
-
- /**
- * creates the HCenter Record and sets it to false (don't horizontally center)
- */
- private static HCenterRecord createHCenter() {
- HCenterRecord retval = new HCenterRecord();
-
- retval.setHCenter(false);
- return retval;
- }
-
- /**
- * creates the VCenter Record and sets it to false (don't horizontally center)
- */
- private static VCenterRecord createVCenter() {
- VCenterRecord retval = new VCenterRecord();
-
- retval.setVCenter(false);
- return retval;
- }
-
- /**
- * creates the PrintSetup Record and sets it to defaults and marks it invalid
- * @see org.apache.poi.hssf.record.PrintSetupRecord
- * @see org.apache.poi.hssf.record.Record
- * @return record containing a PrintSetupRecord
- */
- private static PrintSetupRecord createPrintSetup() {
- PrintSetupRecord retval = new PrintSetupRecord();
-
- retval.setPaperSize(( short ) 1);
- retval.setScale(( short ) 100);
- retval.setPageStart(( short ) 1);
- retval.setFitWidth(( short ) 1);
- retval.setFitHeight(( short ) 1);
- retval.setOptions(( short ) 2);
- retval.setHResolution(( short ) 300);
- retval.setVResolution(( short ) 300);
- retval.setHeaderMargin( 0.5);
- retval.setFooterMargin( 0.5);
- retval.setCopies(( short ) 0);
- return retval;
- }
/**
* creates the DefaultColWidth Record and sets it to 8
*/
public HeaderRecord getHeader ()
{
- return header;
+ return getPageSettings().getHeader();
}
public WindowTwoRecord getWindowTwo() {
*/
public void setHeader (HeaderRecord newHeader)
{
- header = newHeader;
+ getPageSettings().setHeader(newHeader);
}
/**
*/
public FooterRecord getFooter ()
{
- return footer;
+ return getPageSettings().getFooter();
}
/**
*/
public void setFooter (FooterRecord newFooter)
{
- footer = newFooter;
+ getPageSettings().setFooter(newFooter);
}
/**
*/
public PrintSetupRecord getPrintSetup ()
{
- return printSetup;
+ return getPageSettings().getPrintSetup();
}
/**
*/
public void setPrintSetup (PrintSetupRecord newPrintSetup)
{
- printSetup = newPrintSetup;
+ getPageSettings().setPrintSetup(newPrintSetup);
}
/**
* @return the size of the margin
*/
public double getMargin(short margin) {
- if (getMargins()[margin] != null)
- return margins[margin].getMargin();
- else {
- switch ( margin )
- {
- case LeftMargin:
- return .75;
- case RightMargin:
- return .75;
- case TopMargin:
- return 1.0;
- case BottomMargin:
- return 1.0;
- default :
- throw new RuntimeException( "Unknown margin constant: " + margin );
- }
- }
+ return getPageSettings().getMargin(margin);
}
/**
* @param size the size of the margin
*/
public void setMargin(short margin, double size) {
- Margin m = getMargins()[margin];
- if (m == null) {
- switch ( margin )
- {
- case LeftMargin:
- m = new LeftMarginRecord();
- records.add( getDimsLoc() + 1, m );
- break;
- case RightMargin:
- m = new RightMarginRecord();
- records.add( getDimsLoc() + 1, m );
- break;
- case TopMargin:
- m = new TopMarginRecord();
- records.add( getDimsLoc() + 1, m );
- break;
- case BottomMargin:
- m = new BottomMarginRecord();
- records.add( getDimsLoc() + 1, m );
- break;
- default :
- throw new RuntimeException( "Unknown margin constant: " + margin );
- }
- margins[margin] = m;
- }
- m.setMargin( size );
+ getPageSettings().setMargin(margin, size);
}
public int getEofLoc()
this._isUncalced = uncalced;
}
- /**
- * Returns the array of margins. If not created, will create.
- *
- * @return the array of marings.
- */
- protected Margin[] getMargins() {
- if (margins == null)
- margins = new Margin[4];
- return margins;
- }
-
/**
* Finds the DrawingRecord for our sheet, and
* attaches it to the DrawingManager (which knows about
}
}
- /**
- * Shifts all the page breaks in the range "count" number of rows/columns
- * @param breaks The page record to be shifted
- * @param start Starting "main" value to shift breaks
- * @param stop Ending "main" value to shift breaks
- * @param count number of units (rows/columns) to shift by
- */
- private static void shiftBreaks(PageBreakRecord breaks, int start, int stop, int count) {
-
- Iterator iterator = breaks.getBreaksIterator();
- List shiftedBreak = new ArrayList();
- while(iterator.hasNext())
- {
- PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
- int breakLocation = breakItem.main;
- boolean inStart = (breakLocation >= start);
- boolean inEnd = (breakLocation <= stop);
- if(inStart && inEnd)
- shiftedBreak.add(breakItem);
- }
-
- iterator = shiftedBreak.iterator();
- while (iterator.hasNext()) {
- PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
- breaks.removeBreak(breakItem.main);
- breaks.addBreak((short)(breakItem.main+count), breakItem.subFrom, breakItem.subTo);
- }
- }
-
- private PageBreakRecord getRowBreaksRecord() {
- if (_rowBreaksRecord == null) {
- _rowBreaksRecord = new HorizontalPageBreakRecord();
- RecordOrderer.addNewSheetRecord(records, _rowBreaksRecord);
- dimsloc++;
- }
- return _rowBreaksRecord;
- }
- private PageBreakRecord getColumnBreaksRecord() {
- if (_columnBreaksRecord == null) {
- _columnBreaksRecord = new VerticalPageBreakRecord();
- RecordOrderer.addNewSheetRecord(records, _columnBreaksRecord);
+ public PageSettingsBlock getPageSettings() {
+ if (_psBlock == null) {
+ _psBlock = new PageSettingsBlock();
+ RecordOrderer.addNewSheetRecord(records, _psBlock);
dimsloc++;
}
- return _columnBreaksRecord;
+ return _psBlock;
}
-
-
+
/**
* Sets a page break at the indicated row
* @param row
*/
public void setRowBreak(int row, short fromCol, short toCol) {
- getRowBreaksRecord().addBreak((short)row, fromCol, toCol);
+ getPageSettings().setRowBreak(row, fromCol, toCol);
}
/**
* @param row
*/
public void removeRowBreak(int row) {
- if (getRowBreaks() == null)
- throw new IllegalArgumentException("Sheet does not define any row breaks");
- getRowBreaksRecord().removeBreak((short)row);
+ getPageSettings().removeRowBreak(row);
}
/**
* @return true if the specified row has a page break
*/
public boolean isRowBroken(int row) {
- return getRowBreaksRecord().getBreak(row) != null;
+ return getPageSettings().isRowBroken(row);
}
/**
*
*/
public void setColumnBreak(short column, short fromRow, short toRow) {
- getColumnBreaksRecord().addBreak(column, fromRow, toRow);
+ getPageSettings().setColumnBreak(column, fromRow, toRow);
}
/**
*
*/
public void removeColumnBreak(short column) {
- getColumnBreaksRecord().removeBreak(column);
+ getPageSettings().removeColumnBreak(column);
}
/**
* @return <code>true</code> if the specified column has a page break
*/
public boolean isColumnBroken(short column) {
- return getColumnBreaksRecord().getBreak(column) != null;
+ return getPageSettings().isColumnBroken(column);
}
/**
* @param count
*/
public void shiftRowBreaks(int startingRow, int endingRow, int count) {
- shiftBreaks(getRowBreaksRecord(), startingRow, endingRow, count);
+ getPageSettings().shiftRowBreaks(startingRow, endingRow, count);
}
/**
* @param count
*/
public void shiftColumnBreaks(short startingCol, short endingCol, short count) {
- shiftBreaks(getColumnBreaksRecord(), startingCol, endingCol, count);
+ getPageSettings().shiftColumnBreaks(startingCol, endingCol, count);
}
/**
* @return all the horizontal page breaks, never <code>null</code>
*/
public int[] getRowBreaks() {
- return getRowBreaksRecord().getBreaks();
+ return getPageSettings().getRowBreaks();
}
/**
* @return the number of row page breaks
*/
public int getNumRowBreaks(){
- return getRowBreaksRecord().getNumBreaks();
+ return getPageSettings().getNumRowBreaks();
}
/**
* @return all the column page breaks, never <code>null</code>
*/
public int[] getColumnBreaks(){
- return getColumnBreaksRecord().getBreaks();
+ return getPageSettings().getColumnBreaks();
}
/**
* @return the number of column page breaks
*/
public int getNumColumnBreaks(){
- return getColumnBreaksRecord().getNumBreaks();
+ return getPageSettings().getNumColumnBreaks();
}
public void setColumnGroupCollapsed( short columnNumber, boolean collapsed )
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
package org.apache.poi.hssf.record;
import org.apache.poi.util.BitFieldFactory;
/**
- * Title: Print Setup Record<P>
- * Description: Stores print setup options -- bogus for HSSF (and marked as such)<P>
- * REFERENCE: PG 385 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
+ * Title: PAGESETUP (0x00A1)<p/>
+ * Description: Stores print setup options -- bogus for HSSF (and marked as such)<p/>
+ * REFERENCE: PG 385 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Jason Height (jheight at chariot dot net dot au)
* @version 2.0-pre
*/
-
-public class PrintSetupRecord
- extends Record
-{
- public final static short sid = 0xa1;
+public class PrintSetupRecord extends Record {
+ public final static short sid = 0x00A1;
private short field_1_paper_size;
private short field_2_scale;
private short field_3_page_start;
--- /dev/null
+/* ====================================================================
+ 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.record.aggregates;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.poi.hssf.model.RecordStream;
+import org.apache.poi.hssf.model.Sheet;
+import org.apache.poi.hssf.record.BottomMarginRecord;
+import org.apache.poi.hssf.record.FooterRecord;
+import org.apache.poi.hssf.record.HCenterRecord;
+import org.apache.poi.hssf.record.HeaderRecord;
+import org.apache.poi.hssf.record.HorizontalPageBreakRecord;
+import org.apache.poi.hssf.record.LeftMarginRecord;
+import org.apache.poi.hssf.record.Margin;
+import org.apache.poi.hssf.record.PageBreakRecord;
+import org.apache.poi.hssf.record.PrintSetupRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RightMarginRecord;
+import org.apache.poi.hssf.record.TopMarginRecord;
+import org.apache.poi.hssf.record.VCenterRecord;
+import org.apache.poi.hssf.record.VerticalPageBreakRecord;
+
+/**
+ * Groups the page settings records for a worksheet.<p/>
+ *
+ * See OOO excelfileformat.pdf sec 4.4 'Page Settings Block'
+ *
+ * @author Josh Micich
+ */
+public final class PageSettingsBlock extends RecordAggregate {
+ // Every one of these component records is optional
+ // (The whole PageSettingsBlock may not be present)
+ private PageBreakRecord _rowBreaksRecord;
+ private PageBreakRecord _columnBreaksRecord;
+ private HeaderRecord header;
+ private FooterRecord footer;
+ private HCenterRecord _hCenter;
+ private VCenterRecord _vCenter;
+ private LeftMarginRecord _leftMargin;
+ private RightMarginRecord _rightMargin;
+ private TopMarginRecord _topMargin;
+ private BottomMarginRecord _bottomMargin;
+ private Record _pls;
+ private PrintSetupRecord printSetup;
+ private Record _bitmap;
+
+ public PageSettingsBlock(RecordStream rs) {
+ while(true) {
+ if (!readARecord(rs)) {
+ break;
+ }
+ }
+ }
+
+ /**
+ * Creates a PageSettingsBlock with default settings
+ */
+ public PageSettingsBlock() {
+ _rowBreaksRecord = new HorizontalPageBreakRecord();
+ _columnBreaksRecord = new VerticalPageBreakRecord();
+ header = createHeader();
+ footer = createFooter();
+ _hCenter = createHCenter();
+ _vCenter = createVCenter();
+ printSetup = createPrintSetup();
+ }
+
+ /**
+ * @return <code>true</code> if the specified Record sid is one belonging to the
+ * 'Page Settings Block'.
+ */
+ public static boolean isComponentRecord(int sid) {
+ switch (sid) {
+ case HorizontalPageBreakRecord.sid:
+ case VerticalPageBreakRecord.sid:
+ case HeaderRecord.sid:
+ case FooterRecord.sid:
+ case HCenterRecord.sid:
+ case VCenterRecord.sid:
+ case LeftMarginRecord.sid:
+ case RightMarginRecord.sid:
+ case TopMarginRecord.sid:
+ case BottomMarginRecord.sid:
+ case 0x004D: // PLS
+ case PrintSetupRecord.sid:
+ case 0x00E9: // BITMAP
+ return true;
+ }
+ return false;
+ }
+
+ private boolean readARecord(RecordStream rs) {
+ switch (rs.peekNextSid()) {
+ case HorizontalPageBreakRecord.sid:
+ _rowBreaksRecord = (PageBreakRecord) rs.getNext();
+ break;
+ case VerticalPageBreakRecord.sid:
+ _columnBreaksRecord = (PageBreakRecord) rs.getNext();
+ break;
+ case HeaderRecord.sid:
+ header = (HeaderRecord) rs.getNext();
+ break;
+ case FooterRecord.sid:
+ footer = (FooterRecord) rs.getNext();
+ break;
+ case HCenterRecord.sid:
+ _hCenter = (HCenterRecord) rs.getNext();
+ break;
+ case VCenterRecord.sid:
+ _vCenter = (VCenterRecord) rs.getNext();
+ break;
+ case LeftMarginRecord.sid:
+ _leftMargin = (LeftMarginRecord) rs.getNext();
+ break;
+ case RightMarginRecord.sid:
+ _rightMargin = (RightMarginRecord) rs.getNext();
+ break;
+ case TopMarginRecord.sid:
+ _topMargin = (TopMarginRecord) rs.getNext();
+ break;
+ case BottomMarginRecord.sid:
+ _bottomMargin = (BottomMarginRecord) rs.getNext();
+ break;
+ case 0x004D: // PLS
+ _pls = rs.getNext();
+ break;
+ case PrintSetupRecord.sid:
+ printSetup = (PrintSetupRecord)rs.getNext();
+ break;
+ case 0x00E9: // BITMAP
+ _bitmap = rs.getNext();
+ break;
+ default:
+ // all other record types are not part of the PageSettingsBlock
+ return false;
+ }
+ return true;
+ }
+
+ private PageBreakRecord getRowBreaksRecord() {
+ if (_rowBreaksRecord == null) {
+ _rowBreaksRecord = new HorizontalPageBreakRecord();
+ }
+ return _rowBreaksRecord;
+ }
+
+ private PageBreakRecord getColumnBreaksRecord() {
+ if (_columnBreaksRecord == null) {
+ _columnBreaksRecord = new VerticalPageBreakRecord();
+ }
+ return _columnBreaksRecord;
+ }
+
+
+ /**
+ * Sets a page break at the indicated column
+ *
+ */
+ public void setColumnBreak(short column, short fromRow, short toRow) {
+ getColumnBreaksRecord().addBreak(column, fromRow, toRow);
+ }
+
+ /**
+ * Removes a page break at the indicated column
+ *
+ */
+ public void removeColumnBreak(short column) {
+ getColumnBreaksRecord().removeBreak(column);
+ }
+
+
+
+
+ public void visitContainedRecords(RecordVisitor rv) {
+ visitIfPresent(_rowBreaksRecord, rv);
+ visitIfPresent(_columnBreaksRecord, rv);
+ visitIfPresent(header, rv);
+ visitIfPresent(footer, rv);
+ visitIfPresent(_hCenter, rv);
+ visitIfPresent(_vCenter, rv);
+ visitIfPresent(_leftMargin, rv);
+ visitIfPresent(_rightMargin, rv);
+ visitIfPresent(_topMargin, rv);
+ visitIfPresent(_bottomMargin, rv);
+ visitIfPresent(_pls, rv);
+ visitIfPresent(printSetup, rv);
+ visitIfPresent(_bitmap, rv);
+ }
+ private static void visitIfPresent(Record r, RecordVisitor rv) {
+ if (r != null) {
+ rv.visitRecord(r);
+ }
+ }
+
+ /**
+ * creates the Header Record and sets it to nothing/0 length
+ */
+ private static HeaderRecord createHeader() {
+ HeaderRecord retval = new HeaderRecord();
+
+ retval.setHeaderLength(( byte ) 0);
+ retval.setHeader(null);
+ return retval;
+ }
+
+ /**
+ * creates the Footer Record and sets it to nothing/0 length
+ */
+ private static FooterRecord createFooter() {
+ FooterRecord retval = new FooterRecord();
+
+ retval.setFooterLength(( byte ) 0);
+ retval.setFooter(null);
+ return retval;
+ }
+
+ /**
+ * creates the HCenter Record and sets it to false (don't horizontally center)
+ */
+ private static HCenterRecord createHCenter() {
+ HCenterRecord retval = new HCenterRecord();
+
+ retval.setHCenter(false);
+ return retval;
+ }
+
+ /**
+ * creates the VCenter Record and sets it to false (don't horizontally center)
+ */
+ private static VCenterRecord createVCenter() {
+ VCenterRecord retval = new VCenterRecord();
+
+ retval.setVCenter(false);
+ return retval;
+ }
+
+ /**
+ * creates the PrintSetup Record and sets it to defaults and marks it invalid
+ * @see org.apache.poi.hssf.record.PrintSetupRecord
+ * @see org.apache.poi.hssf.record.Record
+ * @return record containing a PrintSetupRecord
+ */
+ private static PrintSetupRecord createPrintSetup() {
+ PrintSetupRecord retval = new PrintSetupRecord();
+
+ retval.setPaperSize(( short ) 1);
+ retval.setScale(( short ) 100);
+ retval.setPageStart(( short ) 1);
+ retval.setFitWidth(( short ) 1);
+ retval.setFitHeight(( short ) 1);
+ retval.setOptions(( short ) 2);
+ retval.setHResolution(( short ) 300);
+ retval.setVResolution(( short ) 300);
+ retval.setHeaderMargin( 0.5);
+ retval.setFooterMargin( 0.5);
+ retval.setCopies(( short ) 0);
+ return retval;
+ }
+
+
+ /**
+ * Returns the HeaderRecord.
+ * @return HeaderRecord for the sheet.
+ */
+ public HeaderRecord getHeader ()
+ {
+ return header;
+ }
+
+ /**
+ * Sets the HeaderRecord.
+ * @param newHeader The new HeaderRecord for the sheet.
+ */
+ public void setHeader (HeaderRecord newHeader)
+ {
+ header = newHeader;
+ }
+
+ /**
+ * Returns the FooterRecord.
+ * @return FooterRecord for the sheet.
+ */
+ public FooterRecord getFooter ()
+ {
+ return footer;
+ }
+
+ /**
+ * Sets the FooterRecord.
+ * @param newFooter The new FooterRecord for the sheet.
+ */
+ public void setFooter (FooterRecord newFooter)
+ {
+ footer = newFooter;
+ }
+
+ /**
+ * Returns the PrintSetupRecord.
+ * @return PrintSetupRecord for the sheet.
+ */
+ public PrintSetupRecord getPrintSetup ()
+ {
+ return printSetup;
+ }
+
+ /**
+ * Sets the PrintSetupRecord.
+ * @param newPrintSetup The new PrintSetupRecord for the sheet.
+ */
+ public void setPrintSetup (PrintSetupRecord newPrintSetup)
+ {
+ printSetup = newPrintSetup;
+ }
+
+
+ private Margin getMarginRec(int marginIndex) {
+ switch (marginIndex) {
+ case Sheet.LeftMargin: return _leftMargin;
+ case Sheet.RightMargin: return _rightMargin;
+ case Sheet.TopMargin: return _topMargin;
+ case Sheet.BottomMargin: return _bottomMargin;
+ }
+ throw new RuntimeException( "Unknown margin constant: " + marginIndex );
+ }
+
+
+ /**
+ * Gets the size of the margin in inches.
+ * @param margin which margin to get
+ * @return the size of the margin
+ */
+ public double getMargin(short margin) {
+ Margin m = getMarginRec(margin);
+ if (m != null) {
+ return m.getMargin();
+ } else {
+ switch ( margin )
+ {
+ case Sheet.LeftMargin:
+ return .75;
+ case Sheet.RightMargin:
+ return .75;
+ case Sheet.TopMargin:
+ return 1.0;
+ case Sheet.BottomMargin:
+ return 1.0;
+ }
+ throw new RuntimeException( "Unknown margin constant: " + margin );
+ }
+ }
+
+ /**
+ * Sets the size of the margin in inches.
+ * @param margin which margin to get
+ * @param size the size of the margin
+ */
+ public void setMargin(short margin, double size) {
+ Margin m = getMarginRec(margin);
+ if (m == null) {
+ switch ( margin )
+ {
+ case Sheet.LeftMargin:
+ _leftMargin = new LeftMarginRecord();
+ m = _leftMargin;
+ break;
+ case Sheet.RightMargin:
+ _rightMargin = new RightMarginRecord();
+ m = _rightMargin;
+ break;
+ case Sheet.TopMargin:
+ _topMargin = new TopMarginRecord();
+ m = _topMargin;
+ break;
+ case Sheet.BottomMargin:
+ _bottomMargin = new BottomMarginRecord();
+ m = _bottomMargin;
+ break;
+ default :
+ throw new RuntimeException( "Unknown margin constant: " + margin );
+ }
+ }
+ m.setMargin( size );
+ }
+
+ /**
+ * Shifts all the page breaks in the range "count" number of rows/columns
+ * @param breaks The page record to be shifted
+ * @param start Starting "main" value to shift breaks
+ * @param stop Ending "main" value to shift breaks
+ * @param count number of units (rows/columns) to shift by
+ */
+ private static void shiftBreaks(PageBreakRecord breaks, int start, int stop, int count) {
+
+ Iterator iterator = breaks.getBreaksIterator();
+ List shiftedBreak = new ArrayList();
+ while(iterator.hasNext())
+ {
+ PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
+ int breakLocation = breakItem.main;
+ boolean inStart = (breakLocation >= start);
+ boolean inEnd = (breakLocation <= stop);
+ if(inStart && inEnd)
+ shiftedBreak.add(breakItem);
+ }
+
+ iterator = shiftedBreak.iterator();
+ while (iterator.hasNext()) {
+ PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
+ breaks.removeBreak(breakItem.main);
+ breaks.addBreak((short)(breakItem.main+count), breakItem.subFrom, breakItem.subTo);
+ }
+ }
+
+
+ /**
+ * Sets a page break at the indicated row
+ * @param row
+ */
+ public void setRowBreak(int row, short fromCol, short toCol) {
+ getRowBreaksRecord().addBreak((short)row, fromCol, toCol);
+ }
+
+ /**
+ * Removes a page break at the indicated row
+ * @param row
+ */
+ public void removeRowBreak(int row) {
+ if (getRowBreaksRecord().getBreaks().length < 1)
+ throw new IllegalArgumentException("Sheet does not define any row breaks");
+ getRowBreaksRecord().removeBreak((short)row);
+ }
+
+ /**
+ * Queries if the specified row has a page break
+ * @param row
+ * @return true if the specified row has a page break
+ */
+ public boolean isRowBroken(int row) {
+ return getRowBreaksRecord().getBreak(row) != null;
+ }
+
+
+ /**
+ * Queries if the specified column has a page break
+ *
+ * @return <code>true</code> if the specified column has a page break
+ */
+ public boolean isColumnBroken(short column) {
+ return getColumnBreaksRecord().getBreak(column) != null;
+ }
+
+ /**
+ * Shifts the horizontal page breaks for the indicated count
+ * @param startingRow
+ * @param endingRow
+ * @param count
+ */
+ public void shiftRowBreaks(int startingRow, int endingRow, int count) {
+ shiftBreaks(getRowBreaksRecord(), startingRow, endingRow, count);
+ }
+
+ /**
+ * Shifts the vertical page breaks for the indicated count
+ * @param startingCol
+ * @param endingCol
+ * @param count
+ */
+ public void shiftColumnBreaks(short startingCol, short endingCol, short count) {
+ shiftBreaks(getColumnBreaksRecord(), startingCol, endingCol, count);
+ }
+
+ /**
+ * @return all the horizontal page breaks, never <code>null</code>
+ */
+ public int[] getRowBreaks() {
+ return getRowBreaksRecord().getBreaks();
+ }
+
+ /**
+ * @return the number of row page breaks
+ */
+ public int getNumRowBreaks(){
+ return getRowBreaksRecord().getNumBreaks();
+ }
+
+ /**
+ * @return all the column page breaks, never <code>null</code>
+ */
+ public int[] getColumnBreaks(){
+ return getColumnBreaksRecord().getBreaks();
+ }
+
+ /**
+ * @return the number of column page breaks
+ */
+ public int getNumColumnBreaks(){
+ return getColumnBreaksRecord().getNumBreaks();
+ }
+
+ public VCenterRecord getVCenter() {
+ return _vCenter;
+ }
+
+ public HCenterRecord getHCenter() {
+ return _hCenter;
+ }
+
+}
public void setVerticallyCenter(boolean value)
{
- VCenterRecord record =
- (VCenterRecord) sheet.findFirstRecordBySid(VCenterRecord.sid);
-
- record.setVCenter(value);
+ sheet.getPageSettings().getVCenter().setVCenter(value);
}
/**
*/
public boolean getVerticallyCenter()
{
- VCenterRecord record =
- (VCenterRecord) sheet.findFirstRecordBySid(VCenterRecord.sid);
-
- return record.getVCenter();
+ return sheet.getPageSettings().getVCenter().getVCenter();
}
/**
public void setHorizontallyCenter(boolean value)
{
- HCenterRecord record =
- (HCenterRecord) sheet.findFirstRecordBySid(HCenterRecord.sid);
-
- record.setHCenter(value);
+ sheet.getPageSettings().getHCenter().setHCenter(value);
}
/**
public boolean getHorizontallyCenter()
{
- HCenterRecord record =
- (HCenterRecord) sheet.findFirstRecordBySid(HCenterRecord.sid);
- return record.getHCenter();
+ return sheet.getPageSettings().getHCenter().getHCenter();
}
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.*;
+import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
import java.util.List;
}
return matchOneOrMany( records, firstRecord );
-// return matchOneOrMany( records, recordIdx );
}
private int matchRequired( int firstRecord, List records, int recordIdx )
}
return matchOneOrMany( records, firstRecord );
-// return matchOneOrMany( records, recordIdx );
}
private int matchOneOrMany( List records, int recordIdx )
new CheckRecord(GutsRecord.class, '1'),
new CheckRecord(DefaultRowHeightRecord.class, '1'),
new CheckRecord(WSBoolRecord.class, '1'),
- new CheckRecord(HeaderRecord.class, '1'),
- new CheckRecord(FooterRecord.class, '1'),
- new CheckRecord(HCenterRecord.class, '1'),
- new CheckRecord(VCenterRecord.class, '1'),
- new CheckRecord(PrintSetupRecord.class, '1'),
+ new CheckRecord(PageSettingsBlock.class, '1'),
new CheckRecord(DefaultColWidthRecord.class, '1'),
new CheckRecord(DimensionsRecord.class, '1'),
new CheckRecord(WindowTwoRecord.class, '1'),
}
} */
- private static int findFirstRecord( List records, Class record, int startIndex )
+ /* package */ static int findFirstRecord( List records, Class record, int startIndex )
{
for (int i = startIndex; i < records.size(); i++)
{
return -1;
}
-// private static int findFirstRecord( List records, Class record )
-// {
-// return findFirstRecord ( records, record, 0 );
-// }
-
void checkRecordOrder(List records, CheckRecord[] check)
{
int recordIdx = 0;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
Sheet sheet = s.getSheet();
- VCenterRecord record =
- (VCenterRecord) sheet.findFirstRecordBySid(VCenterRecord.sid);
+ VCenterRecord record = sheet.getPageSettings().getVCenter();
assertEquals(false, record.getVCenter());
s.setVerticallyCenter(true);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
Sheet sheet = s.getSheet();
- HCenterRecord record =
- (HCenterRecord) sheet.findFirstRecordBySid(HCenterRecord.sid);
+ HCenterRecord record = sheet.getPageSettings().getHCenter();
assertEquals(false, record.getHCenter());
s.setHorizontallyCenter(true);