View | Details | Raw Unified | Return to bug 15537
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/model/Sheet.java (-1 / +69 lines)
Lines 81-86 Link Here
81
 * @author  Glen Stampoultzis (glens at apache.org)
81
 * @author  Glen Stampoultzis (glens at apache.org)
82
 * @author  Shawn Laubach (slaubach at apache dot org) Gridlines, Headers, Footers, and PrintSetup
82
 * @author  Shawn Laubach (slaubach at apache dot org) Gridlines, Headers, Footers, and PrintSetup
83
 * @author Jason Height (jheight at chariot dot net dot au) Clone support
83
 * @author Jason Height (jheight at chariot dot net dot au) Clone support
84
 * @author  Brian Sanders (kestrel at burdell dot org) Active Cell support
84
 *
85
 *
85
 * @see org.apache.poi.hssf.model.Workbook
86
 * @see org.apache.poi.hssf.model.Workbook
86
 * @see org.apache.poi.hssf.usermodel.HSSFSheet
87
 * @see org.apache.poi.hssf.usermodel.HSSFSheet
Lines 108-113 Link Here
108
    protected FooterRecord              footer           = null;
109
    protected FooterRecord              footer           = null;
109
    protected PrintGridlinesRecord      printGridlines   = null;
110
    protected PrintGridlinesRecord      printGridlines   = null;
110
    protected MergeCellsRecord          merged           = null;
111
    protected MergeCellsRecord          merged           = null;
112
    protected SelectionRecord           selection        = null;
111
    protected int                       mergedloc        = 0;
113
    protected int                       mergedloc        = 0;
112
    private static POILogger            log              = POILogFactory.getLogger(Sheet.class);
114
    private static POILogger            log              = POILogFactory.getLogger(Sheet.class);
113
    private ArrayList                   columnSizes      = null;  // holds column info
115
    private ArrayList                   columnSizes      = null;  // holds column info
Lines 252-257 Link Here
252
            {
254
            {
253
                retval.printSetup = (PrintSetupRecord) rec;
255
                retval.printSetup = (PrintSetupRecord) rec;
254
            }
256
            }
257
            else if ( rec.getSid() == SelectionRecord.sid )
258
            {
259
                retval.selection = (SelectionRecord) rec;
260
            }
255
261
256
            if (rec != null)
262
            if (rec != null)
257
            {
263
            {
Lines 376-382 Link Here
376
        records.add(retval.dims);
382
        records.add(retval.dims);
377
        records.add(retval.createWindowTwo());
383
        records.add(retval.createWindowTwo());
378
        retval.setLoc(records.size() - 1);
384
        retval.setLoc(records.size() - 1);
379
        records.add(retval.createSelection());
385
        retval.selection = 
386
                (SelectionRecord) retval.createSelection();
387
        records.add(retval.selection);
380
        records.add(retval.createEOF());
388
        records.add(retval.createEOF());
381
        retval.records = records;
389
        retval.records = records;
382
        log.log(log.DEBUG, "Sheet createsheet from scratch exit");
390
        log.log(log.DEBUG, "Sheet createsheet from scratch exit");
Lines 1934-1939 Link Here
1934
        retval.setActiveCellRow(( short ) 0x0);
1942
        retval.setActiveCellRow(( short ) 0x0);
1935
        retval.setNumRefs(( short ) 0x0);
1943
        retval.setNumRefs(( short ) 0x0);
1936
        return retval;
1944
        return retval;
1945
    }
1946
    
1947
    /**
1948
     * Returns the active row
1949
     *
1950
     * @see org.apache.poi.hssf.record.SelectionRecord
1951
     * @return row the active row index
1952
     */
1953
    public int getActiveCellRow()
1954
    {
1955
        if (selection == null)
1956
        {
1957
            return 0;
1958
        }
1959
        return selection.getActiveCellRow();
1960
    }
1961
    
1962
    /**
1963
     * Sets the active row
1964
     *
1965
     * @param row the row index
1966
     * @see org.apache.poi.hssf.record.SelectionRecord
1967
     */
1968
    public void setActiveCellRow(int row)
1969
    {
1970
        //shouldn't have a sheet w/o a SelectionRecord, but best to guard anyway
1971
        if (selection != null)
1972
        {
1973
            selection.setActiveCellRow(row);
1974
        }
1975
    }
1976
    
1977
    /**
1978
     * Returns the active column
1979
     *
1980
     * @see org.apache.poi.hssf.record.SelectionRecord
1981
     * @return row the active column index
1982
     */
1983
    public short getActiveCellCol()
1984
    {
1985
        if (selection == null)
1986
        {
1987
            return (short) 0;
1988
        }
1989
        return selection.getActiveCellCol();
1990
    }
1991
    
1992
    /**
1993
     * Sets the active column
1994
     *
1995
     * @param col the column index
1996
     * @see org.apache.poi.hssf.record.SelectionRecord
1997
     */
1998
    public void setActiveCellCol(short col)
1999
    {
2000
        //shouldn't have a sheet w/o a SelectionRecord, but best to guard anyway
2001
        if (selection != null)
2002
        {
2003
            selection.setActiveCellCol(col);
2004
        }
1937
    }
2005
    }
1938
2006
1939
    protected Record createMergedCells()
2007
    protected Record createMergedCells()
(-)src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (+10 lines)
Lines 95-100 Link Here
95
 *
95
 *
96
 * @author  Andrew C. Oliver (acoliver at apache dot org)
96
 * @author  Andrew C. Oliver (acoliver at apache dot org)
97
 * @author  Dan Sherman (dsherman at isisph.com)
97
 * @author  Dan Sherman (dsherman at isisph.com)
98
 * @author  Brian Sanders (kestrel at burdell dot org) Active Cell support
98
 * @version 1.0-pre
99
 * @version 1.0-pre
99
 */
100
 */
100
101
Lines 972-976 Link Here
972
      else if (cellNum < 0) {
973
      else if (cellNum < 0) {
973
          throw new RuntimeException("You cannot reference columns with an index of less then 0.");
974
          throw new RuntimeException("You cannot reference columns with an index of less then 0.");
974
      }
975
      }
976
    }
977
    
978
    /**
979
     * Sets this cell as the active cell for the worksheet
980
     */
981
    public void setAsActiveCell()
982
    {
983
        this.sheet.setActiveCellRow(this.row);
984
        this.sheet.setActiveCellCol(this.cellNum);
975
    }
985
    }
976
}
986
}
(-)src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java (+48 lines)
Lines 176-181 Link Here
176
                              .getDateCellValue().getTime());
176
                              .getDateCellValue().getTime());
177
        stream.close();
177
        stream.close();
178
    }
178
    }
179
    
180
    /**
181
     * Tests that the active cell can be correctly read and set
182
     */
183
    public void testActiveCell() throws Exception
184
    {
185
        //read in sample
186
        String dir = System.getProperty("HSSF.testdata.path");
187
        File sample = new File(dir + "/Simple.xls");
188
        assertTrue("Simple.xls exists and is readable", sample.canRead());
189
        FileInputStream fis = new FileInputStream(sample);
190
        HSSFWorkbook book = new HSSFWorkbook(fis);
191
        fis.close();
192
        
193
        //check initial position
194
        HSSFSheet umSheet = book.getSheetAt(0);
195
        Sheet s = umSheet.getSheet();
196
        assertEquals("Initial active cell should be in col 0",
197
            (short) 0, s.getActiveCellCol());
198
        assertEquals("Initial active cell should be on row 1",
199
            1, s.getActiveCellRow());
200
        
201
        //modify position through HSSFCell
202
        HSSFCell cell = umSheet.createRow(3).createCell((short) 2);
203
        cell.setAsActiveCell();
204
        assertEquals("After modify, active cell should be in col 2",
205
            (short) 2, s.getActiveCellCol());
206
        assertEquals("After modify, active cell should be on row 3",
207
            3, s.getActiveCellRow());
208
        
209
        //write book to temp file; read and verify that position is serialized
210
        File temp = File.createTempFile("testActiveCell", ".xls");
211
        FileOutputStream fos = new FileOutputStream(temp);
212
        book.write(fos);
213
        fos.close();
214
        
215
        fis = new FileInputStream(temp);
216
        book = new HSSFWorkbook(fis);
217
        fis.close();
218
        temp.delete();
219
        umSheet = book.getSheetAt(0);
220
        s = umSheet.getSheet();
221
        
222
        assertEquals("After serialize, active cell should be in col 2",
223
            (short) 2, s.getActiveCellCol());
224
        assertEquals("After serialize, active cell should be on row 3",
225
            3, s.getActiveCellRow());
226
    }
179
227
180
    public static void main(String [] args) {
228
    public static void main(String [] args) {
181
        System.out
229
        System.out

Return to bug 15537