KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > RowRecord


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

17         
18
19 package org.apache.poi.hssf.record;
20
21 import org.apache.poi.util.BitField;
22 import org.apache.poi.util.LittleEndian;
23
24 /**
25  * Title: Row Record<P>
26  * Description: stores the row information for the sheet. <P>
27  * REFERENCE: PG 379 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
28  * @author Andrew C. Oliver (acoliver at apache dot org)
29  * @author Jason Height (jheight at chariot dot net dot au)
30  * @version 2.0-pre
31  */

32
33 public class RowRecord
34     extends Record
35     implements Comparable JavaDoc
36 {
37     public final static short sid = 0x208;
38     
39     /** The maximum row number that excel can handle (zero bazed) ie 65536 rows is
40      * max number of rows.
41      */

42     public final static int MAX_ROW_NUMBER = 65535;
43     
44     //private short field_1_row_number;
45
private int field_1_row_number;
46     private short field_2_first_col;
47     private short field_3_last_col; // plus 1
48
private short field_4_height;
49     private short field_5_optimize; // hint field for gui, can/should be set to zero
50

51     // for generated sheets.
52
private short field_6_reserved;
53     private short field_7_option_flags;
54     private BitField outlineLevel = new BitField(0x07);
55
56     // bit 3 reserved
57
private BitField colapsed = new BitField(0x10);
58     private BitField zeroHeight = new BitField(0x20);
59     private BitField badFontHeight = new BitField(0x40);
60     private BitField formatted = new BitField(0x80);
61     private short field_8_xf_index; // only if isFormatted
62

63     public RowRecord()
64     {
65     }
66
67     /**
68      * Constructs a Row record and sets its fields appropriately.
69      *
70      * @param id id must be 0x208 or an exception will be throw upon validation
71      * @param size the size of the data area of the record
72      * @param data data of the record (should not contain sid/len)
73      */

74
75     public RowRecord(short id, short size, byte [] data)
76     {
77         super(id, size, data);
78     }
79
80     /**
81      * Constructs a Row record and sets its fields appropriately.
82      *
83      * @param id id must be 0x208 or an exception will be throw upon validation
84      * @param size the size of the data area of the record
85      * @param data data of the record (should not contain sid/len)
86      * @param offset of the record data
87      */

88
89     public RowRecord(short id, short size, byte [] data, int offset)
90     {
91         super(id, size, data, offset);
92     }
93
94     protected void validateSid(short id)
95     {
96         if (id != sid)
97         {
98             throw new RecordFormatException("NOT A valid ROW RECORD");
99         }
100     }
101
102     protected void fillFields(byte [] data, short size, int offset)
103     {
104         //field_1_row_number = LittleEndian.getShort(data, 0 + offset);
105
field_1_row_number = LittleEndian.getUShort(data, 0 + offset);
106         field_2_first_col = LittleEndian.getShort(data, 2 + offset);
107         field_3_last_col = LittleEndian.getShort(data, 4 + offset);
108         field_4_height = LittleEndian.getShort(data, 6 + offset);
109         field_5_optimize = LittleEndian.getShort(data, 8 + offset);
110         field_6_reserved = LittleEndian.getShort(data, 10 + offset);
111         field_7_option_flags = LittleEndian.getShort(data, 12 + offset);
112         field_8_xf_index = LittleEndian.getShort(data, 14 + offset);
113     }
114
115     /**
116      * set the logical row number for this row (0 based index)
117      * @param row - the row number
118      */

119
120     //public void setRowNumber(short row)
121
public void setRowNumber(int row)
122     {
123         field_1_row_number = row;
124     }
125
126     /**
127      * set the logical col number for the first cell this row (0 based index)
128      * @param col - the col number
129      */

130
131     public void setFirstCol(short col)
132     {
133         field_2_first_col = col;
134     }
135
136     /**
137      * set the logical col number for the last cell this row (0 based index)
138      * @param col - the col number
139      */

140
141     public void setLastCol(short col)
142     {
143         field_3_last_col = col;
144     }
145
146     /**
147      * set the height of the row
148      * @param height of the row
149      */

150
151     public void setHeight(short height)
152     {
153         field_4_height = height;
154     }
155
156     /**
157      * set whether to optimize or not (set to 0)
158      * @param optimize (set to 0)
159      */

160
161     public void setOptimize(short optimize)
162     {
163         field_5_optimize = optimize;
164     }
165
166     /**
167      * sets the option bitmask. (use the individual bit setters that refer to this
168      * method)
169      * @param options - the bitmask
170      */

171
172     public void setOptionFlags(short options)
173     {
174         field_7_option_flags = options;
175     }
176
177     // option bitfields
178

179     /**
180      * set the outline level of this row
181      * @param ol - the outline level
182      * @see #setOptionFlags(short)
183      */

184
185     public void setOutlineLevel(short ol)
186     {
187         field_7_option_flags =
188             outlineLevel.setShortValue(field_7_option_flags, ol);
189     }
190
191     /**
192      * set whether or not to colapse this row
193      * @param c - colapse or not
194      * @see #setOptionFlags(short)
195      */

196
197     public void setColapsed(boolean c)
198     {
199         field_7_option_flags = colapsed.setShortBoolean(field_7_option_flags,
200                 c);
201     }
202
203     /**
204      * set whether or not to display this row with 0 height
205      * @param z height is zero or not.
206      * @see #setOptionFlags(short)
207      */

208
209     public void setZeroHeight(boolean z)
210     {
211         field_7_option_flags =
212             zeroHeight.setShortBoolean(field_7_option_flags, z);
213     }
214
215     /**
216      * set whether the font and row height are not compatible
217      * @param f true if they aren't compatible (damn not logic)
218      * @see #setOptionFlags(short)
219      */

220
221     public void setBadFontHeight(boolean f)
222     {
223         field_7_option_flags =
224             badFontHeight.setShortBoolean(field_7_option_flags, f);
225     }
226
227     /**
228      * set whether the row has been formatted (even if its got all blank cells)
229      * @param f formatted or not
230      * @see #setOptionFlags(short)
231      */

232
233     public void setFormatted(boolean f)
234     {
235         field_7_option_flags = formatted.setShortBoolean(field_7_option_flags,
236                 f);
237     }
238
239     // end bitfields
240

241     /**
242      * if the row is formatted then this is the index to the extended format record
243      * @see org.apache.poi.hssf.record.ExtendedFormatRecord
244      * @param index to the XF record
245      */

246
247     public void setXFIndex(short index)
248     {
249         field_8_xf_index = index;
250     }
251
252     /**
253      * get the logical row number for this row (0 based index)
254      * @return row - the row number
255      */

256
257     //public short getRowNumber()
258
public int getRowNumber()
259     {
260         return field_1_row_number;
261     }
262
263     /**
264      * get the logical col number for the first cell this row (0 based index)
265      * @return col - the col number
266      */

267
268     public short getFirstCol()
269     {
270         return field_2_first_col;
271     }
272
273     /**
274      * get the logical col number for the last cell this row (0 based index)
275      * @return col - the col number
276      */

277
278     public short getLastCol()
279     {
280         return field_3_last_col;
281     }
282
283     /**
284      * get the height of the row
285      * @return height of the row
286      */

287
288     public short getHeight()
289     {
290         return field_4_height;
291     }
292
293     /**
294      * get whether to optimize or not (set to 0)
295      * @return optimize (set to 0)
296      */

297
298     public short getOptimize()
299     {
300         return field_5_optimize;
301     }
302
303     /**
304      * gets the option bitmask. (use the individual bit setters that refer to this
305      * method)
306      * @return options - the bitmask
307      */

308
309     public short getOptionFlags()
310     {
311         return field_7_option_flags;
312     }
313
314     // option bitfields
315

316     /**
317      * get the outline level of this row
318      * @return ol - the outline level
319      * @see #getOptionFlags()
320      */

321
322     public short getOutlineLevel()
323     {
324         return outlineLevel.getShortValue(field_7_option_flags);
325     }
326
327     /**
328      * get whether or not to colapse this row
329      * @return c - colapse or not
330      * @see #getOptionFlags()
331      */

332
333     public boolean getColapsed()
334     {
335         return (colapsed.isSet(field_7_option_flags));
336     }
337
338     /**
339      * get whether or not to display this row with 0 height
340      * @return - z height is zero or not.
341      * @see #getOptionFlags()
342      */

343
344     public boolean getZeroHeight()
345     {
346         return zeroHeight.isSet(field_7_option_flags);
347     }
348
349     /**
350      * get whether the font and row height are not compatible
351      * @return - f -true if they aren't compatible (damn not logic)
352      * @see #getOptionFlags()
353      */

354
355     public boolean getBadFontHeight()
356     {
357         return badFontHeight.isSet(field_7_option_flags);
358     }
359
360     /**
361      * get whether the row has been formatted (even if its got all blank cells)
362      * @return formatted or not
363      * @see #getOptionFlags()
364      */

365
366     public boolean getFormatted()
367     {
368         return formatted.isSet(field_7_option_flags);
369     }
370
371     // end bitfields
372

373     /**
374      * if the row is formatted then this is the index to the extended format record
375      * @see org.apache.poi.hssf.record.ExtendedFormatRecord
376      * @return index to the XF record or bogus value (undefined) if isn't formatted
377      */

378
379     public short getXFIndex()
380     {
381         return field_8_xf_index;
382     }
383
384     public boolean isInValueSection()
385     {
386         return true;
387     }
388
389     public String JavaDoc toString()
390     {
391         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
392
393         buffer.append("[ROW]\n");
394         buffer.append(" .rownumber = ")
395             .append(Integer.toHexString(getRowNumber())).append("\n");
396         buffer.append(" .firstcol = ")
397             .append(Integer.toHexString(getFirstCol())).append("\n");
398         buffer.append(" .lastcol = ")
399             .append(Integer.toHexString(getLastCol())).append("\n");
400         buffer.append(" .height = ")
401             .append(Integer.toHexString(getHeight())).append("\n");
402         buffer.append(" .optimize = ")
403             .append(Integer.toHexString(getOptimize())).append("\n");
404         buffer.append(" .reserved = ")
405             .append(Integer.toHexString(field_6_reserved)).append("\n");
406         buffer.append(" .optionflags = ")
407             .append(Integer.toHexString(getOptionFlags())).append("\n");
408         buffer.append(" .outlinelvl = ")
409             .append(Integer.toHexString(getOutlineLevel())).append("\n");
410         buffer.append(" .colapsed = ").append(getColapsed())
411             .append("\n");
412         buffer.append(" .zeroheight = ").append(getZeroHeight())
413             .append("\n");
414         buffer.append(" .badfontheig= ").append(getBadFontHeight())
415             .append("\n");
416         buffer.append(" .formatted = ").append(getFormatted())
417             .append("\n");
418         buffer.append(" .xfindex = ")
419             .append(Integer.toHexString(getXFIndex())).append("\n");
420         buffer.append("[/ROW]\n");
421         return buffer.toString();
422     }
423
424     public int serialize(int offset, byte [] data)
425     {
426         LittleEndian.putShort(data, 0 + offset, sid);
427         LittleEndian.putShort(data, 2 + offset, ( short ) 16);
428         //LittleEndian.putShort(data, 4 + offset, getRowNumber());
429
LittleEndian.putShort(data, 4 + offset, ( short ) getRowNumber());
430         LittleEndian.putShort(data, 6 + offset, getFirstCol() == -1 ? (short)0 : getFirstCol());
431         LittleEndian.putShort(data, 8 + offset, getLastCol() == -1 ? (short)0 : getLastCol());
432         LittleEndian.putShort(data, 10 + offset, getHeight());
433         LittleEndian.putShort(data, 12 + offset, getOptimize());
434         LittleEndian.putShort(data, 14 + offset, field_6_reserved);
435         LittleEndian.putShort(data, 16 + offset, getOptionFlags());
436
437 // LittleEndian.putShort(data,18,getOutlineLevel());
438
LittleEndian.putShort(data, 18 + offset, getXFIndex());
439         return getRecordSize();
440     }
441
442     public int getRecordSize()
443     {
444         return 20;
445     }
446
447     public short getSid()
448     {
449         return this.sid;
450     }
451
452     public int compareTo(Object JavaDoc obj)
453     {
454         RowRecord loc = ( RowRecord ) obj;
455
456         if (this.getRowNumber() == loc.getRowNumber())
457         {
458             return 0;
459         }
460         if (this.getRowNumber() < loc.getRowNumber())
461         {
462             return -1;
463         }
464         if (this.getRowNumber() > loc.getRowNumber())
465         {
466             return 1;
467         }
468         return -1;
469     }
470
471     public boolean equals(Object JavaDoc obj)
472     {
473         if (!(obj instanceof RowRecord))
474         {
475             return false;
476         }
477         RowRecord loc = ( RowRecord ) obj;
478
479         if (this.getRowNumber() == loc.getRowNumber())
480         {
481             return true;
482         }
483         return false;
484     }
485
486     public Object JavaDoc clone() {
487       RowRecord rec = new RowRecord();
488       rec.field_1_row_number = field_1_row_number;
489       rec.field_2_first_col = field_2_first_col;
490       rec.field_3_last_col = field_3_last_col;
491       rec.field_4_height = field_4_height;
492       rec.field_5_optimize = field_5_optimize;
493       rec.field_6_reserved = field_6_reserved;
494       rec.field_7_option_flags = field_7_option_flags;
495       rec.field_8_xf_index = field_8_xf_index;
496       return rec;
497     }
498 }
499
Popular Tags