Make a start on exposing the StyleRecord details into HSSFCellStyle, but not fully there yet

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@695303 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-09-14 22:19:06 +00:00
parent 866ae7562a
commit d514f9b689
5 changed files with 127 additions and 1 deletions

View File

@ -774,6 +774,61 @@ public final class Workbook implements Model {
return xf;
}
/**
* Returns the StyleRecord for the given
* xfIndex, or null if that ExtendedFormat doesn't
* have a Style set.
*/
public StyleRecord getStyleRecord(int xfIndex) {
// Style records always follow after
// the ExtendedFormat records
boolean done = false;
for(int i=records.getXfpos(); i<records.size() &&
!done; i++) {
Record r = records.get(i);
if(r instanceof ExtendedFormatRecord) {
} else if(r instanceof StyleRecord) {
StyleRecord sr = (StyleRecord)r;
if(sr.getIndex() == xfIndex) {
return sr;
}
} else {
done = true;
}
}
return null;
}
/**
* Creates a new StyleRecord, for the given Extended
* Format index, and adds it onto the end of the
* records collection
*/
public StyleRecord createStyleRecord(int xfIndex) {
// Style records always follow after
// the ExtendedFormat records
StyleRecord newSR = new StyleRecord();
newSR.setIndex((short)xfIndex);
// Find the spot
int addAt = -1;
for(int i=records.getXfpos(); i<records.size() &&
addAt == -1; i++) {
Record r = records.get(i);
if(r instanceof ExtendedFormatRecord ||
r instanceof StyleRecord) {
// Keep going
} else {
addAt = i;
}
}
if(addAt == -1) {
throw new IllegalStateException("No XF Records found!");
}
records.add(addAt, newSR);
return newSR;
}
/**
* Adds a string to the SST table and returns its index (if its a duplicate
* just returns its index and update the counts) ASSUMES compressed unicode

View File

@ -160,7 +160,10 @@ public final class StyleRecord extends Record {
public void setName(String name)
{
field_4_name = name;
//TODO set name length and string options
// Fix up the length
field_2_name_length = (short)name.length();
//TODO set name string options
}
// end user defined

View File

@ -21,6 +21,7 @@ package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.FontRecord;
import org.apache.poi.hssf.record.StyleRecord;
import org.apache.poi.hssf.util.HSSFColor;
/**
@ -931,6 +932,37 @@ public class HSSFCellStyle
return format.getFillForeground();
}
/**
* Gets the name of the user defined style.
* Returns null for built in styles, and
* styles where no name has been defined
*/
public String getUserStyleName() {
StyleRecord sr = workbook.getStyleRecord(index);
if(sr == null) {
return null;
}
if(sr.getType() == StyleRecord.STYLE_BUILT_IN) {
return null;
}
return sr.getName();
}
/**
* Sets the name of the user defined style.
* Will complain if you try this on a built in style.
*/
public void setUserStyleName(String styleName) {
StyleRecord sr = workbook.getStyleRecord(index);
if(sr == null) {
sr = workbook.createStyleRecord(index);
}
if(sr.getType() == StyleRecord.STYLE_BUILT_IN) {
throw new IllegalArgumentException("Unable to set user specified style names for built in styles!");
}
sr.setName(styleName);
}
/**
* Verifies that this style belongs to the supplied Workbook.
* Will throw an exception if it belongs to a different one.

View File

@ -29,6 +29,8 @@ import java.io.*;
import java.util.*;
import junit.framework.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.util.TempFile;
/**
@ -41,6 +43,10 @@ public class TestCellStyle
extends TestCase
{
private static HSSFWorkbook openSample(String sampleFileName) {
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
}
/** Creates a new instance of TestCellStyle */
public TestCellStyle(String name)
@ -304,6 +310,36 @@ public class TestCellStyle
assertEquals(5, wbClone.getNumberOfFonts());
}
public void testStyleNames() throws Exception {
HSSFWorkbook wb = openSample("WithExtendedStyles.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFCell c1 = s.getRow(0).getCell(0);
HSSFCell c2 = s.getRow(1).getCell(0);
HSSFCell c3 = s.getRow(2).getCell(0);
HSSFCellStyle cs1 = c1.getCellStyle();
HSSFCellStyle cs2 = c2.getCellStyle();
HSSFCellStyle cs3 = c3.getCellStyle();
assertNotNull(cs1);
assertNotNull(cs2);
assertNotNull(cs3);
// Check we got the styles we'd expect
assertEquals(10, cs1.getFont(wb).getFontHeightInPoints());
assertEquals(9, cs2.getFont(wb).getFontHeightInPoints());
assertEquals(12, cs3.getFont(wb).getFontHeightInPoints());
assertEquals(15, cs1.getIndex());
assertEquals(23, cs2.getIndex());
assertEquals(24, cs3.getIndex());
// Now check the style names
// assertEquals(null, cs1.getUserStyleName());
// assertEquals("style1", cs2.getUserStyleName());
// assertEquals("style2", cs3.getUserStyleName());
}
public static void main(String [] ignored_args)
{
System.out