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   /*
57    * ColumnInfoRecord.java
58    *
59    * Created on December 8, 2001, 8:44 AM
60    */
61   package org.apache.poi.hssf.record;
62   
63   import org.apache.poi.util.LittleEndian;
64   import org.apache.poi.util.BitField;
65   
66   /**
67    * Title: ColumnInfo Record<P>
68    * Description:  Defines with width and formatting for a range of columns<P>
69    * REFERENCE:  PG 293 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
70    * @author Andrew C. Oliver (acoliver at apache dot org)
71    * @version 2.0-pre
72    */
73   
74   public class ColumnInfoRecord
75       extends Record
76   {
77       public static final short     sid = 0x7d;
78       private short                 field_1_first_col;
79       private short                 field_2_last_col;
80       private short                 field_3_col_width;
81       private short                 field_4_xf_index;
82       private short                 field_5_options;
83       static final private BitField hidden    = new BitField(0x01);
84       static final private BitField outlevel  = new BitField(0x0700);
85       static final private BitField collapsed = new BitField(0x1000);
86       private short                 field_6_reserved;
87   
88       public ColumnInfoRecord()
89       {
90       }
91   
92       /**
93        * Constructs a ColumnInfo record and sets its fields appropriately
94        *
95        * @param id     id must be 0x7d or an exception will be throw upon validation
96        * @param size  the size of the data area of the record
97        * @param data  data of the record (should not contain sid/len)
98        */
99   
100      public ColumnInfoRecord(short id, short size, byte [] data)
101      {
102          super(id, size, data);
103      }
104  
105      /**
106       * Constructs a ColumnInfo record and sets its fields appropriately
107       *
108       * @param id     id must be 0x7d or an exception will be throw upon validation
109       * @param size  the size of the data area of the record
110       * @param data  data of the record (should not contain sid/len)
111       * @param offset of the record's data
112       */
113  
114      public ColumnInfoRecord(short id, short size, byte [] data, int offset)
115      {
116          super(id, size, data);
117      }
118  
119      protected void fillFields(byte [] data, short size, int offset)
120      {
121          field_1_first_col = LittleEndian.getShort(data, 0 + offset);
122          field_2_last_col  = LittleEndian.getShort(data, 2 + offset);
123          field_3_col_width = LittleEndian.getShort(data, 4 + offset);
124          field_4_xf_index  = LittleEndian.getShort(data, 6 + offset);
125          field_5_options   = LittleEndian.getShort(data, 8 + offset);
126          field_6_reserved  = data[ 10 + offset ];
127      }
128  
129      protected void validateSid(short id)
130      {
131          if (id != sid)
132          {
133              throw new RecordFormatException("NOT A COLINFO RECORD!!");
134          }
135      }
136  
137      /**
138       * set the first column this record defines formatting info for
139       * @param fc - the first column index (0-based)
140       */
141  
142      public void setFirstColumn(short fc)
143      {
144          field_1_first_col = fc;
145      }
146  
147      /**
148       * set the last column this record defines formatting info for
149       * @param lc - the last column index (0-based)
150       */
151  
152      public void setLastColumn(short lc)
153      {
154          field_2_last_col = lc;
155      }
156  
157      /**
158       * set the columns' width in 1/256 of a character width
159       * @param cw - column width
160       */
161  
162      public void setColumnWidth(short cw)
163      {
164          field_3_col_width = cw;
165      }
166  
167      /**
168       * set the columns' default format info
169       * @param xfi - the extended format index
170       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
171       */
172  
173      public void setXFIndex(short xfi)
174      {
175          field_4_xf_index = xfi;
176      }
177  
178      /**
179       * set the options bitfield - use the bitsetters instead
180       * @param options - the bitfield raw value
181       */
182  
183      public void setOptions(short options)
184      {
185          field_5_options = options;
186      }
187  
188      // start options bitfield
189  
190      /**
191       * set whether or not these cells are hidden
192       * @param ishidden - whether the cells are hidden.
193       * @see #setOptions(short)
194       */
195  
196      public void setHidden(boolean ishidden)
197      {
198          field_5_options = hidden.setShortBoolean(field_5_options, ishidden);
199      }
200  
201      /**
202       * set the outline level for the cells
203       * @see #setOptions(short)
204       * @param olevel -outline level for the cells
205       */
206  
207      public void setOutlineLevel(short olevel)
208      {
209          field_5_options = outlevel.setShortValue(field_5_options, olevel);
210      }
211  
212      /**
213       * set whether the cells are collapsed
214       * @param iscollapsed - wether the cells are collapsed
215       * @see #setOptions(short)
216       */
217  
218      public void setCollapsed(boolean iscollapsed)
219      {
220          field_5_options = collapsed.setShortBoolean(field_5_options,
221                                                      iscollapsed);
222      }
223  
224      // end options bitfield
225  
226      /**
227       * get the first column this record defines formatting info for
228       * @return the first column index (0-based)
229       */
230  
231      public short getFirstColumn()
232      {
233          return field_1_first_col;
234      }
235  
236      /**
237       * get the last column this record defines formatting info for
238       * @return the last column index (0-based)
239       */
240  
241      public short getLastColumn()
242      {
243          return field_2_last_col;
244      }
245  
246      /**
247       * get the columns' width in 1/256 of a character width
248       * @return column width
249       */
250  
251      public short getColumnWidth()
252      {
253          return field_3_col_width;
254      }
255  
256      /**
257       * get the columns' default format info
258       * @return the extended format index
259       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
260       */
261  
262      public short getXFIndex()
263      {
264          return field_4_xf_index;
265      }
266  
267      /**
268       * get the options bitfield - use the bitsetters instead
269       * @return the bitfield raw value
270       */
271  
272      public short getOptions()
273      {
274          return field_5_options;
275      }
276  
277      // start options bitfield
278  
279      /**
280       * get whether or not these cells are hidden
281       * @return whether the cells are hidden.
282       * @see #setOptions(short)
283       */
284  
285      public boolean getHidden()
286      {
287          return hidden.isSet(field_5_options);
288      }
289  
290      /**
291       * get the outline level for the cells
292       * @see #setOptions(short)
293       * @return outline level for the cells
294       */
295  
296      public short getOutlineLevel()
297      {
298          return outlevel.getShortValue(field_5_options);
299      }
300  
301      /**
302       * get whether the cells are collapsed
303       * @return wether the cells are collapsed
304       * @see #setOptions(short)
305       */
306  
307      public boolean getCollapsed()
308      {
309          return collapsed.isSet(field_5_options);
310      }
311  
312      // end options bitfield
313      public short getSid()
314      {
315          return sid;
316      }
317  
318      public int serialize(int offset, byte [] data)
319      {
320          LittleEndian.putShort(data, 0 + offset, sid);
321          LittleEndian.putShort(data, 2 + offset, ( short ) 12);
322          LittleEndian.putShort(data, 4 + offset, getFirstColumn());
323          LittleEndian.putShort(data, 6 + offset, getLastColumn());
324          LittleEndian.putShort(data, 8 + offset, getColumnWidth());
325          LittleEndian.putShort(data, 10 + offset, getXFIndex());
326          LittleEndian.putShort(data, 12 + offset, getOptions());
327          LittleEndian.putShort(data, 14 + offset,
328                                ( short ) 0x0);   // retval[14] = 0;
329          return getRecordSize();
330      }
331  
332      public int getRecordSize()
333      {
334          return 16;
335      }
336  
337      public String toString()
338      {
339          StringBuffer buffer = new StringBuffer();
340  
341          buffer.append("[COLINFO]\n");
342          buffer.append("colfirst       = ").append(getFirstColumn())
343              .append("\n");
344          buffer.append("collast        = ").append(getLastColumn())
345              .append("\n");
346          buffer.append("colwidth       = ").append(getColumnWidth())
347              .append("\n");
348          buffer.append("xfindex        = ").append(getXFIndex()).append("\n");
349          buffer.append("options        = ").append(getOptions()).append("\n");
350          buffer.append("  hidden       = ").append(getHidden()).append("\n");
351          buffer.append("  olevel       = ").append(getOutlineLevel())
352              .append("\n");
353          buffer.append("  collapsed    = ").append(getCollapsed())
354              .append("\n");
355          buffer.append("[/COLINFO]\n");
356          return buffer.toString();
357      }
358  }
359