Adding changes recently added to release 2. Includes rewrite of margin code, fixes for duplicate DataFormat, and cleanup of files I noticed that was 1 line.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353242 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shawn Laubach 2003-07-25 13:39:08 +00:00
parent a3c0dfd644
commit df218dd45e
4 changed files with 487 additions and 74 deletions

View File

@ -109,6 +109,7 @@ public class Sheet implements Model
protected FooterRecord footer = null; protected FooterRecord footer = null;
protected PrintGridlinesRecord printGridlines = null; protected PrintGridlinesRecord printGridlines = null;
protected MergeCellsRecord merged = null; protected MergeCellsRecord merged = null;
protected Margin margins[] = null;
protected ArrayList mergedRecords = new ArrayList(); protected ArrayList mergedRecords = new ArrayList();
protected ArrayList mergedLocs = new ArrayList(); protected ArrayList mergedLocs = new ArrayList();
protected int numMergedRegions = 0; protected int numMergedRegions = 0;
@ -263,7 +264,23 @@ public class Sheet implements Model
{ {
retval.printSetup = (PrintSetupRecord) rec; 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;
}
if (rec != null) if (rec != null)
{ {
records.add(rec); records.add(rec);
@ -2391,82 +2408,59 @@ public class Sheet implements Model
* @param margin which margin to get * @param margin which margin to get
* @return the size of the margin * @return the size of the margin
*/ */
public double getMargin(short margin) { public double getMargin(short margin) {
Margin m; if (getMargins()[margin] != null)
switch ( margin ) return margins[margin].getMargin();
{ else {
case LeftMargin: switch ( margin )
m = (Margin) findFirstRecordBySid( LeftMarginRecord.sid ); {
if ( m == null ) case LeftMargin:
return .75; return .75;
break; case RightMargin:
case RightMargin: return .75;
m = (Margin) findFirstRecordBySid( RightMarginRecord.sid ); case TopMargin:
if ( m == null ) return 1.0;
return .75; case BottomMargin:
break; return 1.0;
case TopMargin: default :
m = (Margin) findFirstRecordBySid( TopMarginRecord.sid ); throw new RuntimeException( "Unknown margin constant: " + margin );
if ( m == null ) }
return 1.0; }
break; }
case BottomMargin:
m = (Margin) findFirstRecordBySid( BottomMarginRecord.sid );
if ( m == null )
return 1.0;
break;
default :
throw new RuntimeException( "Unknown margin constant: " + margin );
}
return m.getMargin();
}
/** /**
* Sets the size of the margin in inches. * Sets the size of the margin in inches.
* @param margin which margin to get * @param margin which margin to get
* @param size the size of the margin * @param size the size of the margin
*/ */
public void setMargin(short margin, double size) { public void setMargin(short margin, double size) {
Margin m; Margin m = getMargins()[margin];
switch ( margin ) if (m == null) {
{ switch ( margin )
case LeftMargin: {
m = (Margin) findFirstRecordBySid( LeftMarginRecord.sid ); case LeftMargin:
if ( m == null ) m = new LeftMarginRecord();
{ records.add( getDimsLoc() + 1, m );
m = new LeftMarginRecord(); break;
records.add( getDimsLoc() + 1, m ); case RightMargin:
} m = new RightMarginRecord();
break; records.add( getDimsLoc() + 1, m );
case RightMargin: break;
m = (Margin) findFirstRecordBySid( RightMarginRecord.sid ); case TopMargin:
if ( m == null ) m = new TopMarginRecord();
{ records.add( getDimsLoc() + 1, m );
m = new RightMarginRecord(); break;
records.add( getDimsLoc() + 1, m ); case BottomMargin:
} m = new BottomMarginRecord();
break; records.add( getDimsLoc() + 1, m );
case TopMargin: break;
m = (Margin) findFirstRecordBySid( TopMarginRecord.sid ); default :
if ( m == null ) throw new RuntimeException( "Unknown margin constant: " + margin );
{ }
m = new TopMarginRecord(); margins[margin] = m;
records.add( getDimsLoc() + 1, m ); }
} m.setMargin( size );
break; }
case BottomMargin:
m = (Margin) findFirstRecordBySid( BottomMarginRecord.sid );
if ( m == null )
{
m = new BottomMarginRecord();
records.add( getDimsLoc() + 1, m );
}
break;
default :
throw new RuntimeException( "Unknown margin constant: " + margin );
}
m.setMargin( size );
}
public int getEofLoc() public int getEofLoc()
{ {
@ -2590,4 +2584,15 @@ public class Sheet implements Model
this.selection = selection; this.selection = selection;
} }
/**
* 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;
}
} }

File diff suppressed because one or more lines are too long

View File

@ -140,6 +140,14 @@ public class HSSFWorkbook
* memory. * memory.
*/ */
private POIFSFileSystem poifs; private POIFSFileSystem poifs;
/**
* Used to keep track of the data formatter so that all
* createDataFormatter calls return the same one for a given
* book. This ensures that updates from one places is visible
* someplace else.
*/
private HSSFDataFormat formatter;
private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class); private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class);
@ -938,13 +946,15 @@ public class HSSFWorkbook
} }
/** /**
* Creates an instance of HSSFDataFormat. * Returns the instance of HSSFDataFormat for this workbook.
* @return the HSSFDataFormat object * @return the HSSFDataFormat object
* @see org.apache.poi.hssf.record.FormatRecord * @see org.apache.poi.hssf.record.FormatRecord
* @see org.apache.poi.hssf.record.Record * @see org.apache.poi.hssf.record.Record
*/ */
public HSSFDataFormat createDataFormat() { public HSSFDataFormat createDataFormat() {
return new HSSFDataFormat(workbook); if (formatter == null)
formatter = new HSSFDataFormat(workbook);
return formatter;
} }
/** remove the named range by his name /** remove the named range by his name

View File

@ -304,6 +304,8 @@ public class TestWorkbook
assertEquals(1.25,cell.getNumericCellValue(), 1e-10); assertEquals(1.25,cell.getNumericCellValue(), 1e-10);
assertEquals(format.getFormat(df), "0.0"); assertEquals(format.getFormat(df), "0.0");
assertEquals(format, workbook.createDataFormat());
stream.close(); stream.close();
} }