From 2619dbf5690c56f398013bc27874372b84f5ea8b Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Thu, 15 May 2008 23:30:08 +0000 Subject: [PATCH] 44523 - fixed workbook sheet selection and focus git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@656893 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + src/java/org/apache/poi/hssf/model/Sheet.java | 3 + .../poi/hssf/record/WindowOneRecord.java | 75 ++++++--- .../poi/hssf/record/WindowTwoRecord.java | 30 ++-- .../apache/poi/hssf/usermodel/HSSFSheet.java | 23 ++- .../poi/hssf/usermodel/HSSFWorkbook.java | 155 +++++++++++++----- 7 files changed, 206 insertions(+), 82 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 2f0c4280ca..7a44109601 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 44523 - fixed workbook sheet selection and focus 45000 - Fixed NPE in ListLevel when numberText is null 44985 - Properly update TextSpecInfoAtom when the parent text is changed 41187 - fixed HSSFSheet to properly read xls files without ROW records diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 854df3b0e2..200c62e127 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 44523 - fixed workbook sheet selection and focus 45000 - Fixed NPE in ListLevel when numberText is null 44985 - Properly update TextSpecInfoAtom when the parent text is changed 41187 - fixed HSSFSheet to properly read xls files without ROW records diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 2dac8b1d06..45090e85cf 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -2131,6 +2131,9 @@ public final class Sheet implements Model { return header; } + public WindowTwoRecord getWindowTwo() { + return windowTwo; + } /** * Sets the HeaderRecord. * @param newHeader The new HeaderRecord for the sheet. diff --git a/src/java/org/apache/poi/hssf/record/WindowOneRecord.java b/src/java/org/apache/poi/hssf/record/WindowOneRecord.java index f101cb59af..4c836f2ee8 100644 --- a/src/java/org/apache/poi/hssf/record/WindowOneRecord.java +++ b/src/java/org/apache/poi/hssf/record/WindowOneRecord.java @@ -57,8 +57,8 @@ public class WindowOneRecord BitFieldFactory.getInstance(0x20); // display tabs at the bottom // all the rest are "reserved" - private short field_6_selected_tab; - private short field_7_displayed_tab; + private int field_6_active_sheet; + private int field_7_first_visible_tab; private short field_8_num_selected_tabs; private short field_9_tab_width_ratio; @@ -91,8 +91,8 @@ public class WindowOneRecord field_3_width = in.readShort(); field_4_height = in.readShort(); field_5_options = in.readShort(); - field_6_selected_tab = in.readShort(); - field_7_displayed_tab = in.readShort(); + field_6_active_sheet = in.readShort(); + field_7_first_visible_tab = in.readShort(); field_8_num_selected_tabs = in.readShort(); field_9_tab_width_ratio = in.readShort(); } @@ -202,24 +202,33 @@ public class WindowOneRecord // end bitfields + public void setActiveSheetIndex(int index) { + field_6_active_sheet = index; + } /** - * set the selected tab number - * @param s tab number + * deprecated May 2008 + * @deprecated - Misleading name - use setActiveSheetIndex() */ - public void setSelectedTab(short s) { - field_6_selected_tab = s; + setActiveSheetIndex(s); } /** - * set the displayed tab number - * @param t tab number + * Sets the first visible sheet in the worksheet tab-bar. This method does not + * hide, select or focus sheets. It just sets the scroll position in the tab-bar. + * @param t the sheet index of the tab that will become the first in the tab-bar */ + public void setFirstVisibleTab(int t) { + field_7_first_visible_tab = t; + } - public void setDisplayedTab(short t) - { - field_7_displayed_tab = t; + /** + * deprecated May 2008 + * @deprecated - Misleading name - use setFirstVisibleTab() + */ + public void setDisplayedTab(short t) { + setFirstVisibleTab(t); } /** @@ -347,24 +356,36 @@ public class WindowOneRecord // end options bitfields + /** - * get the selected tab number - * @return Tab number + * @return the index of the currently displayed sheet + */ + public int getActiveSheetIndex() { + return field_6_active_sheet; + } + /** + * deprecated May 2008 + * @deprecated - Misleading name - use getActiveSheetIndex() */ - public short getSelectedTab() { - return field_6_selected_tab; + return (short) getActiveSheetIndex(); } /** - * get the displayed tab number - * @return Tab number + * @return the first visible sheet in the worksheet tab-bar. + * I.E. the scroll position of the tab-bar. + */ + public int getFirstVisibleTab() { + return field_7_first_visible_tab; + } + /** + * deprecated May 2008 + * @deprecated - Misleading name - use getFirstVisibleTab() */ - public short getDisplayedTab() { - return field_7_displayed_tab; + return (short) getFirstVisibleTab(); } /** @@ -412,10 +433,10 @@ public class WindowOneRecord .append(getDisplayVerticalScrollbar()).append("\n"); buffer.append(" .tabs = ").append(getDisplayTabs()) .append("\n"); - buffer.append(" .selectedtab = ") - .append(Integer.toHexString(getSelectedTab())).append("\n"); - buffer.append(" .displayedtab = ") - .append(Integer.toHexString(getDisplayedTab())).append("\n"); + buffer.append(" .activeSheet = ") + .append(Integer.toHexString(getActiveSheetIndex())).append("\n"); + buffer.append(" .firstVisibleTab = ") + .append(Integer.toHexString(getFirstVisibleTab())).append("\n"); buffer.append(" .numselectedtabs = ") .append(Integer.toHexString(getNumSelectedTabs())).append("\n"); buffer.append(" .tabwidthratio = ") @@ -434,8 +455,8 @@ public class WindowOneRecord LittleEndian.putShort(data, 8 + offset, getWidth()); LittleEndian.putShort(data, 10 + offset, getHeight()); LittleEndian.putShort(data, 12 + offset, getOptions()); - LittleEndian.putShort(data, 14 + offset, getSelectedTab()); - LittleEndian.putShort(data, 16 + offset, getDisplayedTab()); + LittleEndian.putUShort(data, 14 + offset, getActiveSheetIndex()); + LittleEndian.putUShort(data, 16 + offset, getFirstVisibleTab()); LittleEndian.putShort(data, 18 + offset, getNumSelectedTabs()); LittleEndian.putShort(data, 20 + offset, getTabWidthRatio()); return getRecordSize(); diff --git a/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java b/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java index f6b51996d5..e2bb6d5609 100644 --- a/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java +++ b/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java @@ -54,7 +54,7 @@ public class WindowTwoRecord private BitField displayGuts = BitFieldFactory.getInstance(0x80); private BitField freezePanesNoSplit = BitFieldFactory.getInstance(0x100); private BitField selected = BitFieldFactory.getInstance(0x200); - private BitField paged = BitFieldFactory.getInstance(0x400); + private BitField active = BitFieldFactory.getInstance(0x400); private BitField savedInPageBreakPreview = BitFieldFactory.getInstance(0x800); // 4-7 reserved @@ -222,12 +222,16 @@ public class WindowTwoRecord * is the sheet currently displayed in the window * @param p displayed or not */ - - public void setPaged(boolean p) - { - field_1_options = paged.setShortBoolean(field_1_options, p); + public void setActive(boolean p) { + field_1_options = active.setShortBoolean(field_1_options, p); + } + /** + * deprecated May 2008 + * @deprecated use setActive() + */ + public void setPaged(boolean p) { + setActive(p); } - /** * was the sheet saved in page break view * @param p pagebreaksaved or not @@ -416,9 +420,15 @@ public class WindowTwoRecord * @return displayed or not */ - public boolean getPaged() - { - return paged.isSet(field_1_options); + public boolean isActive() { + return active.isSet(field_1_options); + } + /** + * deprecated May 2008 + * @deprecated use isActive() + */ + public boolean getPaged() { + return isActive(); } /** @@ -520,7 +530,7 @@ public class WindowTwoRecord .append(getFreezePanesNoSplit()).append("\n"); buffer.append(" .selected = ").append(getSelected()) .append("\n"); - buffer.append(" .paged = ").append(getPaged()) + buffer.append(" .active = ").append(isActive()) .append("\n"); buffer.append(" .svdinpgbrkpv= ") .append(getSavedInPageBreakPreview()).append("\n"); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 44185cdfd8..ced8719809 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -977,13 +977,34 @@ public final class HSSFSheet { return new HSSFFooter( getSheet().getFooter() ); } + /** + * Note - this is not the same as whether the sheet is focused (isActive) + * @return true if this sheet is currently selected + */ + public boolean isSelected() { + return getSheet().getWindowTwo().getSelected(); + } /** * Sets whether sheet is selected. * @param sel Whether to select the sheet or deselect the sheet. */ public void setSelected( boolean sel ) { - getSheet().setSelected( sel ); + getSheet().getWindowTwo().setSelected(sel); + } + /** + * @return true if this sheet is currently focused + */ + public boolean isActive() { + return getSheet().getWindowTwo().isActive(); + } + /** + * Sets whether sheet is selected. + * @param sel Whether to select the sheet or deselect the sheet. + */ + public void setActive(boolean sel ) + { + getSheet().getWindowTwo().setActive(sel); } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 78fe260136..d49c601dcb 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -365,16 +365,66 @@ public class HSSFWorkbook extends POIDocument workbook.setSheetOrder(sheetname, pos); } + private void validateSheetIndex(int index) { + int lastSheetIx = sheets.size() - 1; + if (index < 0 || index > lastSheetIx) { + throw new IllegalArgumentException("Sheet index (" + + index +") is out of range (0.." + lastSheetIx + ")"); + } + } + /** - * 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 + * Selects a single sheet. This may be different to + * the 'active' sheet (which is the sheet with focus). + */ + public void setSelectedTab(int index) { + + validateSheetIndex(index); + int nSheets = sheets.size(); + for (int i=0; i