1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.hssf.record;
57   
58   import org.apache.poi.util.BitField;
59   import org.apache.poi.util.LittleEndian;
60   
61   /**
62    * Title:        Row Record<P>
63    * Description:  stores the row information for the sheet. <P>
64    * REFERENCE:  PG 379 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
65    * @author Andrew C. Oliver (acoliver at apache dot org)
66    * @version 2.0-pre
67    */
68   
69   public class RowRecord
70       extends Record
71       implements Comparable
72   {
73       public final static short sid = 0x208;
74       private short             field_1_row_number;
75       private short             field_2_first_col;
76       private short             field_3_last_col;   // plus 1
77       private short             field_4_height;
78       private short             field_5_optimize;   // hint field for gui, can/should be set to zero
79   
80       // for generated sheets.
81       private short             field_6_reserved;
82       private short             field_7_option_flags;
83       private BitField          outlineLevel  = new BitField(0x07);
84   
85       // bit 3 reserved
86       private BitField          colapsed      = new BitField(0x10);
87       private BitField          zeroHeight    = new BitField(0x20);
88       private BitField          badFontHeight = new BitField(0x40);
89       private BitField          formatted     = new BitField(0x80);
90       private short             field_8_xf_index;   // only if isFormatted
91   
92       public RowRecord()
93       {
94       }
95   
96       /**
97        * Constructs a Row record and sets its fields appropriately.
98        *
99        * @param id     id must be 0x208 or an exception will be throw upon validation
100       * @param size  the size of the data area of the record
101       * @param data  data of the record (should not contain sid/len)
102       */
103  
104      public RowRecord(short id, short size, byte [] data)
105      {
106          super(id, size, data);
107      }
108  
109      /**
110       * Constructs a Row record and sets its fields appropriately.
111       *
112       * @param id     id must be 0x208 or an exception will be throw upon validation
113       * @param size  the size of the data area of the record
114       * @param data  data of the record (should not contain sid/len)
115       * @param offset of the record data
116       */
117  
118      public RowRecord(short id, short size, byte [] data, int offset)
119      {
120          super(id, size, data, offset);
121      }
122  
123      protected void validateSid(short id)
124      {
125          if (id != sid)
126          {
127              throw new RecordFormatException("NOT A valid ROW RECORD");
128          }
129      }
130  
131      protected void fillFields(byte [] data, short size, int offset)
132      {
133          field_1_row_number   = LittleEndian.getShort(data, 0 + offset);
134          field_2_first_col    = LittleEndian.getShort(data, 2 + offset);
135          field_3_last_col     = LittleEndian.getShort(data, 4 + offset);
136          field_4_height       = LittleEndian.getShort(data, 6 + offset);
137          field_5_optimize     = LittleEndian.getShort(data, 8 + offset);
138          field_6_reserved     = LittleEndian.getShort(data, 10 + offset);
139          field_7_option_flags = LittleEndian.getShort(data, 12 + offset);
140          field_8_xf_index     = LittleEndian.getShort(data, 14 + offset);
141      }
142  
143      /**
144       * set the logical row number for this row (0 based index)
145       * @param row - the row number
146       */
147  
148      public void setRowNumber(short row)
149      {
150          field_1_row_number = row;
151      }
152  
153      /**
154       * set the logical col number for the first cell this row (0 based index)
155       * @param col - the col number
156       */
157  
158      public void setFirstCol(short col)
159      {
160          field_2_first_col = col;
161      }
162  
163      /**
164       * set the logical col number for the last cell this row (0 based index)
165       * @param col - the col number
166       */
167  
168      public void setLastCol(short col)
169      {
170          field_3_last_col = col;
171      }
172  
173      /**
174       * set the height of the row
175       * @param height of the row
176       */
177  
178      public void setHeight(short height)
179      {
180          field_4_height = height;
181      }
182  
183      /**
184       * set whether to optimize or not (set to 0)
185       * @param optimize (set to 0)
186       */
187  
188      public void setOptimize(short optimize)
189      {
190          field_5_optimize = optimize;
191      }
192  
193      /**
194       * sets the option bitmask.  (use the individual bit setters that refer to this
195       * method)
196       * @param options - the bitmask
197       */
198  
199      public void setOptionFlags(short options)
200      {
201          field_7_option_flags = options;
202      }
203  
204      // option bitfields
205  
206      /**
207       * set the outline level of this row
208       * @param ol - the outline level
209       * @see #setOptionFlags(short)
210       */
211  
212      public void setOutlineLevel(short ol)
213      {
214          field_7_option_flags =
215              outlineLevel.setShortValue(field_7_option_flags, ol);
216      }
217  
218      /**
219       * set whether or not to colapse this row
220       * @param c - colapse or not
221       * @see #setOptionFlags(short)
222       */
223  
224      public void setColapsed(boolean c)
225      {
226          field_7_option_flags = colapsed.setShortBoolean(field_7_option_flags,
227                  c);
228      }
229  
230      /**
231       * set whether or not to display this row with 0 height
232       * @param z  height is zero or not.
233       * @see #setOptionFlags(short)
234       */
235  
236      public void setZeroHeight(boolean z)
237      {
238          field_7_option_flags =
239              zeroHeight.setShortBoolean(field_7_option_flags, z);
240      }
241  
242      /**
243       * set whether the font and row height are not compatible
244       * @param  f  true if they aren't compatible (damn not logic)
245       * @see #setOptionFlags(short)
246       */
247  
248      public void setBadFontHeight(boolean f)
249      {
250          field_7_option_flags =
251              badFontHeight.setShortBoolean(field_7_option_flags, f);
252      }
253  
254      /**
255       * set whether the row has been formatted (even if its got all blank cells)
256       * @param f  formatted or not
257       * @see #setOptionFlags(short)
258       */
259  
260      public void setFormatted(boolean f)
261      {
262          field_7_option_flags = formatted.setShortBoolean(field_7_option_flags,
263                  f);
264      }
265  
266      // end bitfields
267  
268      /**
269       * if the row is formatted then this is the index to the extended format record
270       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
271       * @param index to the XF record
272       */
273  
274      public void setXFIndex(short index)
275      {
276          field_8_xf_index = index;
277      }
278  
279      /**
280       * get the logical row number for this row (0 based index)
281       * @return row - the row number
282       */
283  
284      public short getRowNumber()
285      {
286          return field_1_row_number;
287      }
288  
289      /**
290       * get the logical col number for the first cell this row (0 based index)
291       * @return col - the col number
292       */
293  
294      public short getFirstCol()
295      {
296          return field_2_first_col;
297      }
298  
299      /**
300       * get the logical col number for the last cell this row (0 based index)
301       * @return col - the col number
302       */
303  
304      public short getLastCol()
305      {
306          return field_3_last_col;
307      }
308  
309      /**
310       * get the height of the row
311       * @return height of the row
312       */
313  
314      public short getHeight()
315      {
316          return field_4_height;
317      }
318  
319      /**
320       * get whether to optimize or not (set to 0)
321       * @return optimize (set to 0)
322       */
323  
324      public short getOptimize()
325      {
326          return field_5_optimize;
327      }
328  
329      /**
330       * gets the option bitmask.  (use the individual bit setters that refer to this
331       * method)
332       * @return options - the bitmask
333       */
334  
335      public short getOptionFlags()
336      {
337          return field_7_option_flags;
338      }
339  
340      // option bitfields
341  
342      /**
343       * get the outline level of this row
344       * @return ol - the outline level
345       * @see #getOptionFlags()
346       */
347  
348      public short getOutlineLevel()
349      {
350          return outlineLevel.getShortValue(field_7_option_flags);
351      }
352  
353      /**
354       * get whether or not to colapse this row
355       * @return c - colapse or not
356       * @see #getOptionFlags()
357       */
358  
359      public boolean getColapsed()
360      {
361          return (colapsed.isSet(field_7_option_flags));
362      }
363  
364      /**
365       * get whether or not to display this row with 0 height
366       * @return - z height is zero or not.
367       * @see #getOptionFlags()
368       */
369  
370      public boolean getZeroHeight()
371      {
372          return zeroHeight.isSet(field_7_option_flags);
373      }
374  
375      /**
376       * get whether the font and row height are not compatible
377       * @return - f -true if they aren't compatible (damn not logic)
378       * @see #getOptionFlags()
379       */
380  
381      public boolean getBadFontHeight()
382      {
383          return badFontHeight.isSet(field_7_option_flags);
384      }
385  
386      /**
387       * get whether the row has been formatted (even if its got all blank cells)
388       * @return formatted or not
389       * @see #getOptionFlags()
390       */
391  
392      public boolean getFormatted()
393      {
394          return formatted.isSet(field_7_option_flags);
395      }
396  
397      // end bitfields
398  
399      /**
400       * if the row is formatted then this is the index to the extended format record
401       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
402       * @return index to the XF record or bogus value (undefined) if isn't formatted
403       */
404  
405      public short getXFIndex()
406      {
407          return field_8_xf_index;
408      }
409  
410      public boolean isInValueSection()
411      {
412          return true;
413      }
414  
415      public String toString()
416      {
417          StringBuffer buffer = new StringBuffer();
418  
419          buffer.append("[ROW]\n");
420          buffer.append("    .rownumber      = ")
421              .append(Integer.toHexString(getRowNumber())).append("\n");
422          buffer.append("    .firstcol       = ")
423              .append(Integer.toHexString(getFirstCol())).append("\n");
424          buffer.append("    .lastcol        = ")
425              .append(Integer.toHexString(getLastCol())).append("\n");
426          buffer.append("    .height         = ")
427              .append(Integer.toHexString(getHeight())).append("\n");
428          buffer.append("    .optimize       = ")
429              .append(Integer.toHexString(getOptimize())).append("\n");
430          buffer.append("    .reserved       = ")
431              .append(Integer.toHexString(field_6_reserved)).append("\n");
432          buffer.append("    .optionflags    = ")
433              .append(Integer.toHexString(getOptionFlags())).append("\n");
434          buffer.append("        .outlinelvl = ")
435              .append(Integer.toHexString(getOutlineLevel())).append("\n");
436          buffer.append("        .colapsed   = ").append(getColapsed())
437              .append("\n");
438          buffer.append("        .zeroheight = ").append(getZeroHeight())
439              .append("\n");
440          buffer.append("        .badfontheig= ").append(getBadFontHeight())
441              .append("\n");
442          buffer.append("        .formatted  = ").append(getFormatted())
443              .append("\n");
444          buffer.append("    .xfindex        = ")
445              .append(Integer.toHexString(getXFIndex())).append("\n");
446          buffer.append("[/ROW]\n");
447          return buffer.toString();
448      }
449  
450      public int serialize(int offset, byte [] data)
451      {
452          LittleEndian.putShort(data, 0 + offset, sid);
453          LittleEndian.putShort(data, 2 + offset, ( short ) 16);
454          LittleEndian.putShort(data, 4 + offset, getRowNumber());
455          LittleEndian.putShort(data, 6 + offset, getFirstCol() == -1 ? (short)0 : getFirstCol());
456          LittleEndian.putShort(data, 8 + offset, getLastCol() == -1 ? (short)0 : getLastCol());
457          LittleEndian.putShort(data, 10 + offset, getHeight());
458          LittleEndian.putShort(data, 12 + offset, getOptimize());
459          LittleEndian.putShort(data, 14 + offset, field_6_reserved);
460          LittleEndian.putShort(data, 16 + offset, getOptionFlags());
461  
462  //    LittleEndian.putShort(data,18,getOutlineLevel());
463          LittleEndian.putShort(data, 18 + offset, getXFIndex());
464          return getRecordSize();
465      }
466  
467      public int getRecordSize()
468      {
469          return 20;
470      }
471  
472      public short getSid()
473      {
474          return this.sid;
475      }
476  
477      public int compareTo(Object obj)
478      {
479          RowRecord loc = ( RowRecord ) obj;
480  
481          if (this.getRowNumber() == loc.getRowNumber())
482          {
483              return 0;
484          }
485          if (this.getRowNumber() < loc.getRowNumber())
486          {
487              return -1;
488          }
489          if (this.getRowNumber() > loc.getRowNumber())
490          {
491              return 1;
492          }
493          return -1;
494      }
495  
496      public boolean equals(Object obj)
497      {
498          if (!(obj instanceof RowRecord))
499          {
500              return false;
501          }
502          RowRecord loc = ( RowRecord ) obj;
503  
504          if (this.getRowNumber() == loc.getRowNumber())
505          {
506              return true;
507          }
508          return false;
509      }
510  }
511