poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r656893 - in /poi/trunk/src: documentation/content/xdocs/ java/org/apache/poi/hssf/model/ java/org/apache/poi/hssf/record/ java/org/apache/poi/hssf/usermodel/
Date Thu, 15 May 2008 23:30:08 GMT
Author: josh
Date: Thu May 15 16:30:08 2008
New Revision: 656893

URL: http://svn.apache.org/viewvc?rev=656893&view=rev
Log:
44523 - fixed workbook sheet selection and focus

Modified:
    poi/trunk/src/documentation/content/xdocs/changes.xml
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/hssf/model/Sheet.java
    poi/trunk/src/java/org/apache/poi/hssf/record/WindowOneRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java

Modified: poi/trunk/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=656893&r1=656892&r2=656893&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/changes.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/changes.xml Thu May 15 16:30:08 2008
@@ -37,6 +37,7 @@
 
 		<!-- Don't forget to update status.xml too! -->
         <release version="3.1-beta2" date="2008-05-??">
+           <action dev="POI-DEVELOPERS" type="fix">44523 - fixed workbook sheet selection
and focus</action>
            <action dev="POI-DEVELOPERS" type="fix">45000 - Fixed NPE in ListLevel when
numberText is null</action>
            <action dev="POI-DEVELOPERS" type="fix">44985 - Properly update TextSpecInfoAtom
when the parent text is changed</action>
            <action dev="POI-DEVELOPERS" type="fix">41187 - fixed HSSFSheet to properly
read xls files without ROW records</action>

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=656893&r1=656892&r2=656893&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Thu May 15 16:30:08 2008
@@ -34,6 +34,7 @@
 	<!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1-beta2" date="2008-05-??">
+           <action dev="POI-DEVELOPERS" type="fix">44523 - fixed workbook sheet selection
and focus</action>
            <action dev="POI-DEVELOPERS" type="fix">45000 - Fixed NPE in ListLevel when
numberText is null</action>
            <action dev="POI-DEVELOPERS" type="fix">44985 - Properly update TextSpecInfoAtom
when the parent text is changed</action>
            <action dev="POI-DEVELOPERS" type="fix">41187 - fixed HSSFSheet to properly
read xls files without ROW records</action>

Modified: poi/trunk/src/java/org/apache/poi/hssf/model/Sheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/model/Sheet.java?rev=656893&r1=656892&r2=656893&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/model/Sheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/model/Sheet.java Thu May 15 16:30:08 2008
@@ -2131,6 +2131,9 @@
     return header;
     }
 
+    public WindowTwoRecord getWindowTwo() {
+        return windowTwo;
+    }
     /**
      * Sets the HeaderRecord.
      * @param newHeader The new HeaderRecord for the sheet.

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/WindowOneRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/WindowOneRecord.java?rev=656893&r1=656892&r2=656893&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/WindowOneRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/WindowOneRecord.java Thu May 15 16:30:08
2008
@@ -57,8 +57,8 @@
         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 @@
         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 @@
 
     // 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 <b>not</b>
+     *  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 @@
 
     // 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 @@
             .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 @@
         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();

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java?rev=656893&r1=656892&r2=656893&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/WindowTwoRecord.java Thu May 15 16:30:08
2008
@@ -54,7 +54,7 @@
     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 @@
      * 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 @@
      * @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 @@
             .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");

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java?rev=656893&r1=656892&r2=656893&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java Thu May 15 16:30:08 2008
@@ -978,12 +978,33 @@
     }
 
     /**
+     * Note - this is not the same as whether the sheet is focused (isActive)
+     * @return <code>true</code> 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 <code>true</code> 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);
     }
 
     /**

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java?rev=656893&r1=656892&r2=656893&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java Thu May 15 16:30:08
2008
@@ -365,16 +365,66 @@
         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<nSheets; i++) {
+               getSheetAt(i).setSelected(i == index);
+        }
+        workbook.getWindowOne().setNumSelectedTabs((short)1);
+    }
+    /**
+     * deprecated May 2008
+     * @deprecated use setSelectedTab(int)
      */
     public void setSelectedTab(short index) {
-        workbook.getWindowOne().setSelectedTab(index);
+        setSelectedTab((int)index);
+    }
+    public void setSelectedTabs(int[] indexes) {
+        
+        for (int i = 0; i < indexes.length; i++) {
+            validateSheetIndex(indexes[i]);
+        }
+        int nSheets = sheets.size();
+        for (int i=0; i<nSheets; i++) {
+            boolean bSelect = false;
+            for (int j = 0; j < indexes.length; j++) {
+                if (indexes[j] == i) {
+                    bSelect = true;
+                    break;
+                }
+                
+            }
+               getSheetAt(i).setSelected(bSelect);
+        }
+        workbook.getWindowOne().setNumSelectedTabs((short)indexes.length);
+    }
+    /**
+     * Convenience method to set the active sheet.  The active sheet is is the sheet
+     * which is currently displayed when the workbook is viewed in Excel.
+     * 'Selected' sheet(s) is a distinct concept.
+     */
+    public void setActiveSheet(int index) {
+        
+        validateSheetIndex(index);
+        int nSheets = sheets.size();
+        for (int i=0; i<nSheets; i++) {
+             getSheetAt(i).setActive(i == index);
+        }
+        workbook.getWindowOne().setActiveSheetIndex(index);
     }
 
     /**
@@ -384,25 +434,46 @@
      * in the tabs (at the bottom).
      * @see org.apache.poi.hssf.usermodel.HSSFSheet#setSelected(boolean)
      */
+    public int getActiveSheetIndex() {
+        return workbook.getWindowOne().getActiveSheetIndex();
+    }
+    /**
+     * deprecated May 2008
+     * @deprecated - Misleading name - use getActiveSheetIndex() 
+     */
     public short getSelectedTab() {
-        return workbook.getWindowOne().getSelectedTab();
+        return (short) getActiveSheetIndex();
     }
 
+    
     /**
      * sets the first tab that is displayed in the list of tabs
      * in excel.
      * @param index
      */
+    public void setFirstVisibleTab(int index) {
+        workbook.getWindowOne().setFirstVisibleTab(index);
+    }
+    /**
+     * deprecated May 2008
+     * @deprecated - Misleading name - use setFirstVisibleTab() 
+     */
     public void setDisplayedTab(short index) {
-        workbook.getWindowOne().setDisplayedTab(index);
+       setFirstVisibleTab(index);
     }
 
     /**
-     * sets the first tab that is displayed in the list of tabs
-     * in excel.
+     * sets the first tab that is displayed in the list of tabs in excel.
+     */
+    public int getFirstVisibleTab() {
+        return workbook.getWindowOne().getFirstVisibleTab();
+    }
+    /**
+     * deprecated May 2008
+     * @deprecated - Misleading name - use getFirstVisibleTab() 
      */
     public short getDisplayedTab() {
-        return workbook.getWindowOne().getDisplayedTab();
+        return (short) getFirstVisibleTab();
     }
 
     /**
@@ -563,17 +634,13 @@
 
     public HSSFSheet createSheet()
     {
-
-//        if (getNumberOfSheets() == 3)
-//            throw new RuntimeException("You cannot have more than three sheets in HSSF
1.0");
         HSSFSheet sheet = new HSSFSheet(this);
 
         sheets.add(sheet);
-        workbook.setSheetName(sheets.size() - 1,
-                "Sheet" + (sheets.size() - 1));
-        WindowTwoRecord windowTwo = (WindowTwoRecord) sheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
-        windowTwo.setSelected(sheets.size() == 1);
-        windowTwo.setPaged(sheets.size() == 1);
+        workbook.setSheetName(sheets.size() - 1, "Sheet" + (sheets.size() - 1));
+        boolean isOnlySheet = sheets.size() == 1;
+        sheet.setSelected(isOnlySheet);
+        sheet.setActive(isOnlySheet);
         return sheet;
     }
 
@@ -584,23 +651,24 @@
      */
 
     public HSSFSheet cloneSheet(int sheetNum) {
-      HSSFSheet srcSheet = (HSSFSheet)sheets.get(sheetNum);
-      String srcName = workbook.getSheetName(sheetNum);
-      if (srcSheet != null) {
+        validateSheetIndex(sheetNum);
+        HSSFSheet srcSheet = (HSSFSheet) sheets.get(sheetNum);
+        String srcName = workbook.getSheetName(sheetNum);
         HSSFSheet clonedSheet = srcSheet.cloneSheet(this);
-        WindowTwoRecord windowTwo = (WindowTwoRecord) clonedSheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
-        windowTwo.setSelected(sheets.size() == 1);
-        windowTwo.setPaged(sheets.size() == 1);
+        clonedSheet.setSelected(false);
+        clonedSheet.setActive(false);
 
         sheets.add(clonedSheet);
-        int i=1;
+        int i = 1;
         while (true) {
-            //Try and find the next sheet name that is unique
+            // Try and find the next sheet name that is unique
             String name = srcName;
             String index = Integer.toString(i++);
-            if (name.length()+index.length()+2<31)
-              name = name + "("+index+")";
-            else name = name.substring(0, 31-index.length()-2)+"("+index+")";
+            if (name.length() + index.length() + 2 < 31) {
+                name = name + "(" + index + ")";
+            } else {
+                name = name.substring(0, 31 - index.length() - 2) + "(" + index + ")";
+            }
 
             //If the sheet name is unique, then set it otherwise move on to the next number.
             if (workbook.getSheetIndex(name) == -1) {
@@ -609,18 +677,18 @@
             }
         }
         return clonedSheet;
-      }
-      return null;
     }
 
     /**
-     * create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns
-     * the high level representation.  Use this to create new sheets.
-     *
-     * @param sheetname     sheetname to set for the sheet.
+     * create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and
+     * returns the high level representation. Use this to create new sheets.
+     * 
+     * @param sheetname
+     *            sheetname to set for the sheet.
      * @return HSSFSheet representing the new sheet.
-     * @throws IllegalArgumentException if there is already a sheet present with a case-insensitive
-     *  match for the specified name.
+     * @throws IllegalArgumentException
+     *             if there is already a sheet present with a case-insensitive
+     *             match for the specified name.
      */
 
     public HSSFSheet createSheet(String sheetname)
@@ -632,9 +700,9 @@
 
         sheets.add(sheet);
         workbook.setSheetName(sheets.size() - 1, sheetname);
-        WindowTwoRecord windowTwo = (WindowTwoRecord) sheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
-        windowTwo.setSelected(sheets.size() == 1);
-        windowTwo.setPaged(sheets.size() == 1);
+        boolean isOnlySheet = sheets.size() == 1;
+        sheet.setSelected(isOnlySheet);
+        sheet.setActive(isOnlySheet);
         return sheet;
     }
 
@@ -834,8 +902,7 @@
         HSSFPrintSetup printSetup = sheet.getPrintSetup();
         printSetup.setValidSettings(false);
 
-        WindowTwoRecord w2 = (WindowTwoRecord) sheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
-        w2.setPaged(true);
+        sheet.setActive(true);
     }
 
     private NameRecord findExistingRowColHeaderNameRecord( int sheetIndex )



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message