Index: src/java/org/apache/poi/hssf/model/Sheet.java =================================================================== RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/model/Sheet.java,v retrieving revision 1.21 diff -u -r1.21 Sheet.java --- src/java/org/apache/poi/hssf/model/Sheet.java 16 Dec 2002 11:16:41 -0000 1.21 +++ src/java/org/apache/poi/hssf/model/Sheet.java 20 Dec 2002 16:14:08 -0000 @@ -81,6 +81,7 @@ * @author Glen Stampoultzis (glens at apache.org) * @author Shawn Laubach (slaubach at apache dot org) Gridlines, Headers, Footers, and PrintSetup * @author Jason Height (jheight at chariot dot net dot au) Clone support + * @author Brian Sanders (kestrel at burdell dot org) Active Cell support * * @see org.apache.poi.hssf.model.Workbook * @see org.apache.poi.hssf.usermodel.HSSFSheet @@ -108,6 +109,7 @@ protected FooterRecord footer = null; protected PrintGridlinesRecord printGridlines = null; protected MergeCellsRecord merged = null; + protected SelectionRecord selection = null; protected int mergedloc = 0; private static POILogger log = POILogFactory.getLogger(Sheet.class); private ArrayList columnSizes = null; // holds column info @@ -252,6 +254,10 @@ { retval.printSetup = (PrintSetupRecord) rec; } + else if ( rec.getSid() == SelectionRecord.sid ) + { + retval.selection = (SelectionRecord) rec; + } if (rec != null) { @@ -376,7 +382,9 @@ records.add(retval.dims); records.add(retval.createWindowTwo()); retval.setLoc(records.size() - 1); - records.add(retval.createSelection()); + retval.selection = + (SelectionRecord) retval.createSelection(); + records.add(retval.selection); records.add(retval.createEOF()); retval.records = records; log.log(log.DEBUG, "Sheet createsheet from scratch exit"); @@ -1934,6 +1942,66 @@ retval.setActiveCellRow(( short ) 0x0); retval.setNumRefs(( short ) 0x0); return retval; + } + + /** + * Returns the active row + * + * @see org.apache.poi.hssf.record.SelectionRecord + * @return row the active row index + */ + public int getActiveCellRow() + { + if (selection == null) + { + return 0; + } + return selection.getActiveCellRow(); + } + + /** + * Sets the active row + * + * @param row the row index + * @see org.apache.poi.hssf.record.SelectionRecord + */ + public void setActiveCellRow(int row) + { + //shouldn't have a sheet w/o a SelectionRecord, but best to guard anyway + if (selection != null) + { + selection.setActiveCellRow(row); + } + } + + /** + * Returns the active column + * + * @see org.apache.poi.hssf.record.SelectionRecord + * @return row the active column index + */ + public short getActiveCellCol() + { + if (selection == null) + { + return (short) 0; + } + return selection.getActiveCellCol(); + } + + /** + * Sets the active column + * + * @param col the column index + * @see org.apache.poi.hssf.record.SelectionRecord + */ + public void setActiveCellCol(short col) + { + //shouldn't have a sheet w/o a SelectionRecord, but best to guard anyway + if (selection != null) + { + selection.setActiveCellCol(col); + } } protected Record createMergedCells() Index: src/java/org/apache/poi/hssf/usermodel/HSSFCell.java =================================================================== RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java,v retrieving revision 1.19 diff -u -r1.19 HSSFCell.java --- src/java/org/apache/poi/hssf/usermodel/HSSFCell.java 28 Nov 2002 19:32:52 -0000 1.19 +++ src/java/org/apache/poi/hssf/usermodel/HSSFCell.java 20 Dec 2002 16:14:09 -0000 @@ -95,6 +95,7 @@ * * @author Andrew C. Oliver (acoliver at apache dot org) * @author Dan Sherman (dsherman at isisph.com) + * @author Brian Sanders (kestrel at burdell dot org) Active Cell support * @version 1.0-pre */ @@ -972,5 +973,14 @@ else if (cellNum < 0) { throw new RuntimeException("You cannot reference columns with an index of less then 0."); } + } + + /** + * Sets this cell as the active cell for the worksheet + */ + public void setAsActiveCell() + { + this.sheet.setActiveCellRow(this.row); + this.sheet.setActiveCellCol(this.cellNum); } } Index: src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java =================================================================== RCS file: /home/cvspublic/jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java,v retrieving revision 1.3 diff -u -r1.3 TestHSSFCell.java --- src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java 12 Dec 2002 09:02:26 -0000 1.3 +++ src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java 20 Dec 2002 16:14:09 -0000 @@ -176,6 +176,54 @@ .getDateCellValue().getTime()); stream.close(); } + + /** + * Tests that the active cell can be correctly read and set + */ + public void testActiveCell() throws Exception + { + //read in sample + String dir = System.getProperty("HSSF.testdata.path"); + File sample = new File(dir + "/Simple.xls"); + assertTrue("Simple.xls exists and is readable", sample.canRead()); + FileInputStream fis = new FileInputStream(sample); + HSSFWorkbook book = new HSSFWorkbook(fis); + fis.close(); + + //check initial position + HSSFSheet umSheet = book.getSheetAt(0); + Sheet s = umSheet.getSheet(); + assertEquals("Initial active cell should be in col 0", + (short) 0, s.getActiveCellCol()); + assertEquals("Initial active cell should be on row 1", + 1, s.getActiveCellRow()); + + //modify position through HSSFCell + HSSFCell cell = umSheet.createRow(3).createCell((short) 2); + cell.setAsActiveCell(); + assertEquals("After modify, active cell should be in col 2", + (short) 2, s.getActiveCellCol()); + assertEquals("After modify, active cell should be on row 3", + 3, s.getActiveCellRow()); + + //write book to temp file; read and verify that position is serialized + File temp = File.createTempFile("testActiveCell", ".xls"); + FileOutputStream fos = new FileOutputStream(temp); + book.write(fos); + fos.close(); + + fis = new FileInputStream(temp); + book = new HSSFWorkbook(fis); + fis.close(); + temp.delete(); + umSheet = book.getSheetAt(0); + s = umSheet.getSheet(); + + assertEquals("After serialize, active cell should be in col 2", + (short) 2, s.getActiveCellCol()); + assertEquals("After serialize, active cell should be on row 3", + 3, s.getActiveCellRow()); + } public static void main(String [] args) { System.out