From f3df5ee67f58dae36f4405ac95237075b8c31769 Mon Sep 17 00:00:00 2001 From: "Amol S. Deshmukh" Date: Wed, 24 Aug 2005 16:26:20 +0000 Subject: [PATCH] Fix for Bug#28328 git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353773 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/model/Workbook.java | 18 +++++++- .../poi/hssf/usermodel/HSSFWorkbook.java | 42 +++++++++++++++++++ .../poi/hssf/usermodel/TestHSSFWorkbook.java | 20 +++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index 75328e980..4bc93eb96 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -100,6 +100,7 @@ public class Workbook implements Model private boolean uses1904datewindowing = false; // whether 1904 date windowing is being used private DrawingManager2 drawingManager; private List escherBSERecords = new ArrayList(); // EscherBSERecord + private WindowOneRecord windowOne; private static POILogger log = POILogFactory.getLogger(Workbook.class); @@ -215,6 +216,10 @@ public class Workbook implements Model if (log.check( POILogger.DEBUG )) log.log(DEBUG, "found palette record at " + k); retval.records.setPalettepos( k ); + case WindowOneRecord.sid: + if (log.check( POILogger.DEBUG )) + log.log(DEBUG, "found WindowOneRecord at " + k); + retval.windowOne = (WindowOneRecord) rec; default : } records.add(rec); @@ -226,6 +231,10 @@ public class Workbook implements Model // } retval.records.setRecords(records); + + if (retval.windowOne == null) { + retval.windowOne = (WindowOneRecord) retval.createWindowOne(); + } if (log.check( POILogger.DEBUG )) log.log(DEBUG, "exit create workbook from existing file function"); return retval; @@ -259,7 +268,8 @@ public class Workbook implements Model records.add( retval.createPassword() ); records.add( retval.createProtectionRev4() ); records.add( retval.createPasswordRev4() ); - records.add( retval.createWindowOne() ); + retval.windowOne = (WindowOneRecord) retval.createWindowOne(); + records.add( retval.windowOne ); records.add( retval.createBackup() ); retval.records.setBackuppos( records.size() - 1 ); records.add( retval.createHideObj() ); @@ -2164,7 +2174,11 @@ public class Workbook implements Model } } - + + public WindowOneRecord getWindowOne() { + return windowOne; + } + public int addBSERecord(EscherBSERecord e) { createDrawingGroup(); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 8b16dc099..414ff24a1 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -251,6 +251,48 @@ public class HSSFWorkbook public void setSheetOrder(String sheetname, int pos ) { workbook.setSheetOrder(sheetname, pos); } + + /** + * sets the tab whose data is actually seen when the sheet is opened. + * This may be different from the "selected sheet" since excel seems to + * allow you to show the data of one sheet when another is seen "selected" + * in the tabs (at the bottom). + * @see org.apache.poi.hssf.usermodel.HSSFSheet#setSelected(boolean) + * @param index + */ + public void setSelectedTab(short index) { + workbook.getWindowOne().setSelectedTab(index); + } + + /** + * gets the tab whose data is actually seen when the sheet is opened. + * This may be different from the "selected sheet" since excel seems to + * allow you to show the data of one sheet when another is seen "selected" + * in the tabs (at the bottom). + * @see org.apache.poi.hssf.usermodel.HSSFSheet#setSelected(boolean) + * @return + */ + public short getSelectedTab() { + return workbook.getWindowOne().getSelectedTab(); + } + + /** + * sets the first tab that is displayed in the list of tabs + * in excel. + * @param index + */ + public void setDisplayedTab(short index) { + workbook.getWindowOne().setDisplayedTab(index); + } + + /** + * sets the first tab that is displayed in the list of tabs + * in excel. + * @return + */ + public short getDisplayedTab() { + return workbook.getWindowOne().getDisplayedTab(); + } public final static byte ENCODING_COMPRESSED_UNICODE = 0; public final static byte ENCODING_UTF_16 = 1; diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index d07ed38b0..e07fe2647 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -64,4 +64,24 @@ public class TestHSSFWorkbook extends TestCase c.createSheet("Sheet4"); } + + public void testWindowOneDefaults() { + HSSFWorkbook b = new HSSFWorkbook( ); + try { + assertEquals(b.getSelectedTab(), 0); + assertEquals(b.getDisplayedTab(), 0); + } catch (NullPointerException npe) { + fail("WindowOneRecord in Workbook is probably not initialized"); + } + } + + public void testSheetSelection() { + HSSFWorkbook b = new HSSFWorkbook(); + b.createSheet("Sheet One"); + HSSFSheet s = b.createSheet("Sheet Two"); + b.setSelectedTab((short) 1); + b.setDisplayedTab((short) 1); + assertEquals(b.getSelectedTab(), 1); + assertEquals(b.getDisplayedTab(), 1); + } } \ No newline at end of file