]> source.dussan.org Git - poi.git/commitdiff
Make a start on exposing the StyleRecord details into HSSFCellStyle, but not fully...
authorNick Burch <nick@apache.org>
Sun, 14 Sep 2008 22:19:06 +0000 (22:19 +0000)
committerNick Burch <nick@apache.org>
Sun, 14 Sep 2008 22:19:06 +0000 (22:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@695303 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/Workbook.java
src/java/org/apache/poi/hssf/record/StyleRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
src/testcases/org/apache/poi/hssf/data/WithExtendedStyles.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java

index f8a426108a2d3fb0d8d609e50b5e7714ea0c9919..1e37a80ca24331041ab2b1b57087cac1ba109313 100644 (file)
@@ -773,6 +773,61 @@ public final class Workbook implements Model {
         numxfs++;
         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
index 6990a5efdc3e975403423d798ada4c1679410141..c6591f550de4e83762391b8ead794a61a1a69408 100644 (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
index 6d8ae33b689416b30afb8005985156db00bd6e97..f74c38d09ea9c5cfa84ecc1f17a1fccb748bd429 100644 (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;
 
 /**
@@ -930,6 +931,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.
diff --git a/src/testcases/org/apache/poi/hssf/data/WithExtendedStyles.xls b/src/testcases/org/apache/poi/hssf/data/WithExtendedStyles.xls
new file mode 100644 (file)
index 0000000..4a80f56
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/WithExtendedStyles.xls differ
index 1dc826cc6fd74789bbd4f9723bd6fe05e1d089d0..e2783b4434cc4cdf4b35a416b752d68b9f2b27af 100644 (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)
@@ -303,6 +309,36 @@ public class TestCellStyle
        assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
        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)
     {