KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFRow


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 /*
18  * HSSFRow.java
19  *
20  * Created on September 30, 2001, 3:44 PM
21  */

22 package org.apache.poi.hssf.usermodel;
23
24 import org.apache.poi.hssf.model.Sheet;
25 import org.apache.poi.hssf.model.Workbook;
26 import org.apache.poi.hssf.record.CellValueRecordInterface;
27 import org.apache.poi.hssf.record.RowRecord;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * High level representation of a row of a spreadsheet.
34  *
35  * Only rows that have cells should be added to a Sheet.
36  * @version 1.0-pre
37  * @author Andrew C. Oliver (acoliver at apache dot org)
38  * @author Glen Stampoultzis (glens at apache.org)
39  */

40
41 public class HSSFRow
42         implements Comparable JavaDoc
43 {
44
45     // used for collections
46
public final static int INITIAL_CAPACITY = 5;
47     //private short rowNum;
48
private int rowNum;
49     private HashMap JavaDoc cells;
50 // private short firstcell = -1;
51
// private short lastcell = -1;
52

53     /**
54      * reference to low level representation
55      */

56
57     private RowRecord row;
58
59     /**
60      * reference to containing low level Workbook
61      */

62
63     private Workbook book;
64
65     /**
66      * reference to containing Sheet
67      */

68
69     private Sheet sheet;
70
71     protected HSSFRow()
72     {
73     }
74
75     /**
76      * Creates new HSSFRow from scratch. Only HSSFSheet should do this.
77      *
78      * @param book low-level Workbook object containing the sheet that contains this row
79      * @param sheet low-level Sheet object that contains this Row
80      * @param rowNum the row number of this row (0 based)
81      * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int)
82      */

83
84     //protected HSSFRow(Workbook book, Sheet sheet, short rowNum)
85
protected HSSFRow(Workbook book, Sheet sheet, int rowNum)
86     {
87         cells = new HashMap JavaDoc(10); // new ArrayList(INITIAL_CAPACITY);
88
this.book = book;
89         this.sheet = sheet;
90         row = new RowRecord();
91         row.setOptionFlags( (short)0x100 ); // seems necessary for outlining to work.
92
row.setHeight((short) 0xff);
93         row.setLastCol((short) -1);
94         row.setFirstCol((short) -1);
95
96         setRowNum(rowNum);
97     }
98
99     /**
100      * Creates an HSSFRow from a low level RowRecord object. Only HSSFSheet should do
101      * this. HSSFSheet uses this when an existing file is read in.
102      *
103      * @param book low-level Workbook object containing the sheet that contains this row
104      * @param sheet low-level Sheet object that contains this Row
105      * @param record the low level api object this row should represent
106      * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int)
107      */

108
109     protected HSSFRow(Workbook book, Sheet sheet, RowRecord record)
110     {
111         cells = new HashMap JavaDoc(); // ArrayList(INITIAL_CAPACITY);
112
this.book = book;
113         this.sheet = sheet;
114         row = record;
115
116         setRowNum(record.getRowNumber());
117     }
118
119     /**
120      * Use this to create new cells within the row and return it.
121      * <p>
122      * The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
123      * either through calling <code>setCellValue</code> or <code>setCellType</code>.
124      *
125      * @param column - the column number this cell represents
126      *
127      * @return HSSFCell a high level representation of the created cell.
128      */

129
130     public HSSFCell createCell(short column)
131     {
132         HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), column);
133
134         addCell(cell);
135         sheet.addValueRecord(getRowNum(), cell.getCellValueRecord());
136         return cell;
137     }
138
139     /**
140      * Use this to create new cells within the row and return it.
141      * <p>
142      * The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
143      * either through calling setCellValue or setCellType.
144      *
145      * @param column - the column number this cell represents
146      *
147      * @return HSSFCell a high level representation of the created cell.
148      * @deprecated As of 22-Jan-2002 use createCell(short) and use setCellValue to
149      * specify the type lazily.
150      */

151
152     public HSSFCell createCell(short column, int type)
153     {
154         HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), column, type);
155
156         addCell(cell);
157         sheet.addValueRecord(getRowNum(), cell.getCellValueRecord());
158         return cell;
159     }
160
161     /**
162      * remove the HSSFCell from this row.
163      * @param cell to remove
164      */

165     public void removeCell(HSSFCell cell)
166     {
167         CellValueRecordInterface cval = cell.getCellValueRecord();
168
169         sheet.removeValueRecord(getRowNum(), cval);
170         cells.remove(new Integer JavaDoc(cell.getCellNum()));
171
172         if (cell.getCellNum() == row.getLastCol())
173         {
174             row.setLastCol(findLastCell(row.getLastCol()));
175         }
176         if (cell.getCellNum() == row.getFirstCol())
177         {
178             row.setFirstCol(findFirstCell(row.getFirstCol()));
179         }
180     }
181
182     /**
183      * create a high level HSSFCell object from an existing low level record. Should
184      * only be called from HSSFSheet or HSSFRow itself.
185      * @param cell low level cell to create the high level representation from
186      * @return HSSFCell representing the low level record passed in
187      */

188
189     protected HSSFCell createCellFromRecord(CellValueRecordInterface cell)
190     {
191         HSSFCell hcell = new HSSFCell(book, sheet, getRowNum(), cell);
192
193         addCell(hcell);
194
195         // sheet.addValueRecord(getRowNum(),cell.getCellValueRecord());
196
return hcell;
197     }
198
199     /**
200      * set the row number of this row.
201      * @param rowNum the row number (0-based)
202      * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
203      */

204
205     //public void setRowNum(short rowNum)
206
public void setRowNum(int rowNum)
207     {
208         if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER))
209           throw new IndexOutOfBoundsException JavaDoc("Row number must be between 0 and "+RowRecord.MAX_ROW_NUMBER+", was <"+rowNum+">");
210         this.rowNum = rowNum;
211         if (row != null)
212         {
213             row.setRowNumber(rowNum); // used only for KEY comparison (HSSFRow)
214
}
215     }
216
217     /**
218      * get row number this row represents
219      * @return the row number (0 based)
220      */

221
222     //public short getRowNum()
223
public int getRowNum()
224     {
225         return rowNum;
226     }
227
228     /**
229      * used internally to add a cell.
230      */

231
232     private void addCell(HSSFCell cell)
233     {
234         if (row.getFirstCol() == -1)
235         {
236             row.setFirstCol(cell.getCellNum());
237         }
238         if (row.getLastCol() == -1)
239         {
240             row.setLastCol(cell.getCellNum());
241         }
242         cells.put(new Integer JavaDoc(cell.getCellNum()), cell);
243
244         if (cell.getCellNum() < row.getFirstCol())
245         {
246             row.setFirstCol(cell.getCellNum());
247         }
248         if (cell.getCellNum() > row.getLastCol())
249         {
250             row.setLastCol(cell.getCellNum());
251         }
252     }
253
254     /**
255      * get the hssfcell representing a given column (logical cell) 0-based. If you
256      * ask for a cell that is not defined....you get a null.
257      *
258      * @param cellnum 0 based column number
259      * @return HSSFCell representing that column or null if undefined.
260      */

261
262     public HSSFCell getCell(short cellnum)
263     {
264
265 /* for (int k = 0; k < cells.size(); k++)
266         {
267             HSSFCell cell = ( HSSFCell ) cells.get(k);
268
269             if (cell.getCellNum() == cellnum)
270             {
271                 return cell;
272             }
273         }*/

274         return (HSSFCell) cells.get(new Integer JavaDoc(cellnum));
275     }
276
277     /**
278      * get the number of the first cell contained in this row.
279      * @return short representing the first logical cell in the row
280      */

281
282     public short getFirstCellNum()
283     {
284         if (getPhysicalNumberOfCells() == 0)
285             return -1;
286         else
287             return row.getFirstCol();
288     }
289
290     /**
291      * get the number of the last cell contained in this row.
292      * @return short representing the last logical cell in the row
293      */

294
295     public short getLastCellNum()
296     {
297         if (getPhysicalNumberOfCells() == 0)
298             return -1;
299         else
300             return row.getLastCol();
301     }
302
303
304     /**
305      * gets the number of defined cells (NOT number of cells in the actual row!).
306      * That is to say if only columns 0,4,5 have values then there would be 3.
307      * @return int representing the number of defined cells in the row.
308      */

309
310     public int getPhysicalNumberOfCells()
311     {
312         if (cells == null)
313         {
314             return 0; // shouldn't be possible but it is due to missing API support for BLANK/MULBLANK
315
}
316         return cells.size();
317     }
318
319     /**
320      * set the row's height or set to ff (-1) for undefined/default-height. Set the height in "twips" or
321      * 1/20th of a point.
322      * @param height rowheight or 0xff for undefined (use sheet default)
323      */

324
325     public void setHeight(short height)
326     {
327
328         // row.setOptionFlags(
329
row.setBadFontHeight(true);
330         row.setHeight(height);
331     }
332
333     /**
334      * set whether or not to display this row with 0 height
335      * @param zHeight height is zero or not.
336      */

337     public void setZeroHeight(boolean zHeight) {
338         row.setZeroHeight(zHeight);
339     }
340   
341     /**
342      * get whether or not to display this row with 0 height
343      * @return - zHeight height is zero or not.
344      */

345     public boolean getZeroHeight() {
346         return row.getZeroHeight();
347     }
348
349     /**
350      * set the row's height in points.
351      * @param height row height in points
352      */

353
354     public void setHeightInPoints(float height)
355     {
356
357         // row.setOptionFlags(
358
row.setBadFontHeight(true);
359         row.setHeight((short) (height * 20));
360     }
361
362     /**
363      * get the row's height or ff (-1) for undefined/default-height in twips (1/20th of a point)
364      * @return rowheight or 0xff for undefined (use sheet default)
365      */

366
367     public short getHeight()
368     {
369         return row.getHeight();
370     }
371
372     /**
373      * get the row's height or ff (-1) for undefined/default-height in points (20*getHeight())
374      * @return rowheight or 0xff for undefined (use sheet default)
375      */

376
377     public float getHeightInPoints()
378     {
379         return (row.getHeight() / 20);
380     }
381
382     /**
383      * get the lowlevel RowRecord represented by this object - should only be called
384      * by other parts of the high level API
385      *
386      * @return RowRecord this row represents
387      */

388
389     protected RowRecord getRowRecord()
390     {
391         return row;
392     }
393
394     /**
395      * used internally to refresh the "last cell" when the last cell is removed.
396      */

397
398     private short findLastCell(short lastcell)
399     {
400         short cellnum = (short) (lastcell - 1);
401         HSSFCell r = getCell(cellnum);
402
403         while (r == null && cellnum >= 0)
404         {
405             r = getCell(--cellnum);
406         }
407         return cellnum;
408     }
409
410     /**
411      * used internally to refresh the "first cell" when the first cell is removed.
412      */

413
414     private short findFirstCell(short firstcell)
415     {
416         short cellnum = (short) (firstcell + 1);
417         HSSFCell r = getCell(cellnum);
418
419         while (r == null && cellnum <= getLastCellNum())
420         {
421             r = getCell(++cellnum);
422         }
423         if (cellnum > getLastCellNum())
424             return -1;
425         return cellnum;
426     }
427
428     /**
429      * @return cell iterator of the physically defined cells. Note element 4 may
430      * actually be row cell depending on how many are defined!
431      */

432
433     public Iterator JavaDoc cellIterator()
434     {
435         return cells.values().iterator();
436     }
437
438     public int compareTo(Object JavaDoc obj)
439     {
440         HSSFRow loc = (HSSFRow) obj;
441
442         if (this.getRowNum() == loc.getRowNum())
443         {
444             return 0;
445         }
446         if (this.getRowNum() < loc.getRowNum())
447         {
448             return -1;
449         }
450         if (this.getRowNum() > loc.getRowNum())
451         {
452             return 1;
453         }
454         return -1;
455     }
456
457     public boolean equals(Object JavaDoc obj)
458     {
459         if (!(obj instanceof HSSFRow))
460         {
461             return false;
462         }
463         HSSFRow loc = (HSSFRow) obj;
464
465         if (this.getRowNum() == loc.getRowNum())
466         {
467             return true;
468         }
469         return false;
470     }
471 }
472
Popular Tags