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:        Window Two Record<P>
63    * Description:  sheet window settings<P>
64    * REFERENCE:  PG 422 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 WindowTwoRecord
70       extends Record
71   {
72       public final static short sid = 0x23e;
73       private short             field_1_options;
74   
75       // bitfields
76       private BitField          displayFormulas         = new BitField(0x01);
77       private BitField          displayGridlines        = new BitField(0x02);
78       private BitField          displayRowColHeadings   = new BitField(0x04);
79       private BitField          freezePanes             = new BitField(0x08);
80       private BitField          displayZeros            = new BitField(0x10);
81       private BitField          defaultHeader           =
82           new BitField(0x20);   // if false use color in field 4
83   
84       // if true use default foreground
85       // for headers
86       private BitField          arabic                  =
87           new BitField(0x40);   // for our desert dwelling friends
88       private BitField          displayGuts             = new BitField(0x80);
89       private BitField          freezePanesNoSplit      = new BitField(0x100);
90       private BitField          selected                = new BitField(0x200);
91       private BitField          paged                   = new BitField(0x400);
92       private BitField          savedInPageBreakPreview = new BitField(0x800);
93   
94       // 4-7 reserved
95       // end bitfields
96       private short             field_2_top_row;
97       private short             field_3_left_col;
98       private int               field_4_header_color;
99       private short             field_5_page_break_zoom;
100      private short             field_6_normal_zoom;
101      private int               field_7_reserved;
102  
103      public WindowTwoRecord()
104      {
105      }
106  
107      /**
108       * Constructs a WindowTwo record and sets its fields appropriately.
109       *
110       * @param id     id must be 0x23e or an exception will be throw upon validation
111       * @param size  the size of the data area of the record
112       * @param data  data of the record (should not contain sid/len)
113       */
114  
115      public WindowTwoRecord(short id, short size, byte [] data)
116      {
117          super(id, size, data);
118      }
119  
120      /**
121       * Constructs a WindowTwo record and sets its fields appropriately.
122       *
123       * @param id     id must be 0x23e or an exception will be throw upon validation
124       * @param size  the size of the data area of the record
125       * @param data  data of the record (should not contain sid/len)
126       * @param offset of the record's data
127       */
128  
129      public WindowTwoRecord(short id, short size, byte [] data, int offset)
130      {
131          super(id, size, data, offset);
132      }
133  
134      protected void validateSid(short id)
135      {
136          if (id != sid)
137          {
138              throw new RecordFormatException("NOT A valid WindowTwo RECORD");
139          }
140      }
141  
142      protected void fillFields(byte [] data, short size, int offset)
143      {
144          field_1_options      = LittleEndian.getShort(data, 0 + offset);
145          field_2_top_row      = LittleEndian.getShort(data, 2 + offset);
146          field_3_left_col     = LittleEndian.getShort(data, 4 + offset);
147          field_4_header_color = LittleEndian.getInt(data, 6 + offset);
148          if (size > 10)
149          {
150              field_5_page_break_zoom = LittleEndian.getShort(data,
151                      10 + offset);
152              field_6_normal_zoom     = LittleEndian.getShort(data,
153                      12 + offset);
154          }
155          if (size > 14)
156          {   // there is a special case of this record that has only 14 bytes...undocumented!
157              field_7_reserved = LittleEndian.getInt(data, 14 + offset);
158          }
159      }
160  
161      /**
162       * set the options bitmask or just use the bit setters.
163       * @param options
164       */
165  
166      public void setOptions(short options)
167      {
168          field_1_options = options;
169      }
170  
171      // option bitfields
172  
173      /**
174       * set whether the window should display formulas
175       * @param formulas or not
176       */
177  
178      public void setDisplayFormulas(boolean formulas)
179      {
180          field_1_options = displayFormulas.setShortBoolean(field_1_options, formulas);
181      }
182  
183      /**
184       * set whether the window should display gridlines
185       * @param gridlines or not
186       */
187  
188      public void setDisplayGridlines(boolean gridlines)
189      {
190          field_1_options = displayGridlines.setShortBoolean(field_1_options, gridlines);
191      }
192  
193      /**
194       * set whether the window should display row and column headings
195       * @param headings or not
196       */
197  
198      public void setDisplayRowColHeadings(boolean headings)
199      {
200          field_1_options = displayRowColHeadings.setShortBoolean(field_1_options, headings);
201      }
202  
203      /**
204       * set whether the window should freeze panes
205       * @param freezepanes  freeze panes or not
206       */
207  
208      public void setFreezePanes(boolean freezepanes)
209      {
210          field_1_options = freezePanes.setShortBoolean(field_1_options, freezepanes);
211      }
212  
213      /**
214       * set whether the window should display zero values
215       * @param zeros or not
216       */
217  
218      public void setDisplayZeros(boolean zeros)
219      {
220          field_1_options = displayZeros.setShortBoolean(field_1_options, zeros);
221      }
222  
223      /**
224       * set whether the window should display a default header
225       * @param header or not
226       */
227  
228      public void setDefaultHeader(boolean header)
229      {
230          field_1_options = defaultHeader.setShortBoolean(field_1_options, header);
231      }
232  
233      /**
234       * is this arabic?
235       * @param isarabic  arabic or not
236       */
237  
238      public void setArabic(boolean isarabic)
239      {
240          field_1_options = arabic.setShortBoolean(field_1_options, isarabic);
241      }
242  
243      /**
244       * set whether the outline symbols are displaed
245       * @param guts  symbols or not
246       */
247  
248      public void setDisplayGuts(boolean guts)
249      {
250          field_1_options = displayGuts.setShortBoolean(field_1_options, guts);
251      }
252  
253      /**
254       * freeze unsplit panes or not
255       * @param freeze or not
256       */
257  
258      public void setFreezePanesNoSplit(boolean freeze)
259      {
260          field_1_options = freezePanesNoSplit.setShortBoolean(field_1_options, freeze);
261      }
262  
263      /**
264       * sheet tab is selected
265       * @param sel  selected or not
266       */
267  
268      public void setSelected(boolean sel)
269      {
270          field_1_options = selected.setShortBoolean(field_1_options, sel);
271      }
272  
273      /**
274       * is the sheet currently displayed in the window
275       * @param p  displayed or not
276       */
277  
278      public void setPaged(boolean p)
279      {
280          field_1_options = paged.setShortBoolean(field_1_options, p);
281      }
282  
283      /**
284       * was the sheet saved in page break view
285       * @param p  pagebreaksaved or not
286       */
287  
288      public void setSavedInPageBreakPreview(boolean p)
289      {
290          field_1_options = savedInPageBreakPreview.setShortBoolean(field_1_options, p);
291      }
292  
293      // end of bitfields.
294  
295      /**
296       * set the top row visible in the window
297       * @param topRow  top row visible
298       */
299  
300      public void setTopRow(short topRow)
301      {
302          field_2_top_row = topRow;
303      }
304  
305      /**
306       * set the leftmost column displayed in the window
307       * @param leftCol  leftmost column
308       */
309  
310      public void setLeftCol(short leftCol)
311      {
312          field_3_left_col = leftCol;
313      }
314  
315      /**
316       * set the palette index for the header color
317       * @param color
318       */
319  
320      public void setHeaderColor(int color)
321      {
322          field_4_header_color = color;
323      }
324  
325      /**
326       * zoom magification in page break view
327       * @param zoom
328       */
329  
330      public void setPageBreakZoom(short zoom)
331      {
332          field_5_page_break_zoom = zoom;
333      }
334  
335      /**
336       * set the zoom magnification in normal view
337       * @param zoom
338       */
339  
340      public void setNormalZoom(short zoom)
341      {
342          field_6_normal_zoom = zoom;
343      }
344  
345      /**
346       * set the reserved (don't do this) value
347       */
348  
349      public void setReserved(int reserved)
350      {
351          field_7_reserved = reserved;
352      }
353  
354      /**
355       * get the options bitmask or just use the bit setters.
356       * @return options
357       */
358  
359      public short getOptions()
360      {
361          return field_1_options;
362      }
363  
364      // option bitfields
365  
366      /**
367       * get whether the window should display formulas
368       * @return formulas or not
369       */
370  
371      public boolean getDisplayFormulas()
372      {
373          return displayFormulas.isSet(field_1_options);
374      }
375  
376      /**
377       * get whether the window should display gridlines
378       * @return gridlines or not
379       */
380  
381      public boolean getDisplayGridlines()
382      {
383          return displayGridlines.isSet(field_1_options);
384      }
385  
386      /**
387       * get whether the window should display row and column headings
388       * @return headings or not
389       */
390  
391      public boolean getDisplayRowColHeadings()
392      {
393          return displayRowColHeadings.isSet(field_1_options);
394      }
395  
396      /**
397       * get whether the window should freeze panes
398       * @return freeze panes or not
399       */
400  
401      public boolean getFreezePanes()
402      {
403          return freezePanes.isSet(field_1_options);
404      }
405  
406      /**
407       * get whether the window should display zero values
408       * @return zeros or not
409       */
410  
411      public boolean getDisplayZeros()
412      {
413          return displayZeros.isSet(field_1_options);
414      }
415  
416      /**
417       * get whether the window should display a default header
418       * @return header or not
419       */
420  
421      public boolean getDefaultHeader()
422      {
423          return defaultHeader.isSet(field_1_options);
424      }
425  
426      /**
427       * is this arabic?
428       * @return arabic or not
429       */
430  
431      public boolean getArabic()
432      {
433          return arabic.isSet(field_1_options);
434      }
435  
436      /**
437       * get whether the outline symbols are displaed
438       * @return symbols or not
439       */
440  
441      public boolean getDisplayGuts()
442      {
443          return displayGuts.isSet(field_1_options);
444      }
445  
446      /**
447       * freeze unsplit panes or not
448       * @return freeze or not
449       */
450  
451      public boolean getFreezePanesNoSplit()
452      {
453          return freezePanesNoSplit.isSet(field_1_options);
454      }
455  
456      /**
457       * sheet tab is selected
458       * @return selected or not
459       */
460  
461      public boolean getSelected()
462      {
463          return selected.isSet(field_1_options);
464      }
465  
466      /**
467       * is the sheet currently displayed in the window
468       * @return displayed or not
469       */
470  
471      public boolean getPaged()
472      {
473          return paged.isSet(field_1_options);
474      }
475  
476      /**
477       * was the sheet saved in page break view
478       * @return pagebreaksaved or not
479       */
480  
481      public boolean getSavedInPageBreakPreview()
482      {
483          return savedInPageBreakPreview.isSet(field_1_options);
484      }
485  
486      // end of bitfields.
487  
488      /**
489       * get the top row visible in the window
490       * @return toprow
491       */
492  
493      public short getTopRow()
494      {
495          return field_2_top_row;
496      }
497  
498      /**
499       * get the leftmost column displayed in the window
500       * @return leftmost
501       */
502  
503      public short getLeftCol()
504      {
505          return field_3_left_col;
506      }
507  
508      /**
509       * get the palette index for the header color
510       * @return color
511       */
512  
513      public int getHeaderColor()
514      {
515          return field_4_header_color;
516      }
517  
518      /**
519       * zoom magification in page break view
520       * @return zoom
521       */
522  
523      public short getPageBreakZoom()
524      {
525          return field_5_page_break_zoom;
526      }
527  
528      /**
529       * get the zoom magnification in normal view
530       * @return zoom
531       */
532  
533      public short getNormalZoom()
534      {
535          return field_6_normal_zoom;
536      }
537  
538      /**
539       * get the reserved bits - why would you do this?
540       * @return reserved stuff -probably garbage
541       */
542  
543      public int getReserved()
544      {
545          return field_7_reserved;
546      }
547  
548      public String toString()
549      {
550          StringBuffer buffer = new StringBuffer();
551  
552          buffer.append("[WINDOW2]\n");
553          buffer.append("    .options        = ")
554              .append(Integer.toHexString(getOptions())).append("\n");
555          buffer.append("       .dispformulas= ").append(getDisplayFormulas())
556              .append("\n");
557          buffer.append("       .dispgridlins= ").append(getDisplayGridlines())
558              .append("\n");
559          buffer.append("       .disprcheadin= ")
560              .append(getDisplayRowColHeadings()).append("\n");
561          buffer.append("       .freezepanes = ").append(getFreezePanes())
562              .append("\n");
563          buffer.append("       .displayzeros= ").append(getDisplayZeros())
564              .append("\n");
565          buffer.append("       .defaultheadr= ").append(getDefaultHeader())
566              .append("\n");
567          buffer.append("       .arabic      = ").append(getArabic())
568              .append("\n");
569          buffer.append("       .displayguts = ").append(getDisplayGuts())
570              .append("\n");
571          buffer.append("       .frzpnsnosplt= ")
572              .append(getFreezePanesNoSplit()).append("\n");
573          buffer.append("       .selected    = ").append(getSelected())
574              .append("\n");
575          buffer.append("       .paged       = ").append(getPaged())
576              .append("\n");
577          buffer.append("       .svdinpgbrkpv= ")
578              .append(getSavedInPageBreakPreview()).append("\n");
579          buffer.append("    .toprow         = ")
580              .append(Integer.toHexString(getTopRow())).append("\n");
581          buffer.append("    .leftcol        = ")
582              .append(Integer.toHexString(getLeftCol())).append("\n");
583          buffer.append("    .headercolor    = ")
584              .append(Integer.toHexString(getHeaderColor())).append("\n");
585          buffer.append("    .pagebreakzoom  = ")
586              .append(Integer.toHexString(getPageBreakZoom())).append("\n");
587          buffer.append("    .normalzoom     = ")
588              .append(Integer.toHexString(getNormalZoom())).append("\n");
589          buffer.append("    .reserved       = ")
590              .append(Integer.toHexString(getReserved())).append("\n");
591          buffer.append("[/WINDOW2]\n");
592          return buffer.toString();
593      }
594  
595      public int serialize(int offset, byte [] data)
596      {
597          LittleEndian.putShort(data, 0 + offset, sid);
598          LittleEndian.putShort(data, 2 + offset, ( short ) 18);
599          LittleEndian.putShort(data, 4 + offset, getOptions());
600          LittleEndian.putShort(data, 6 + offset, getTopRow());
601          LittleEndian.putShort(data, 8 + offset, getLeftCol());
602          LittleEndian.putInt(data, 10 + offset, getHeaderColor());
603          LittleEndian.putShort(data, 14 + offset, getPageBreakZoom());
604          LittleEndian.putShort(data, 16 + offset, getNormalZoom());
605          LittleEndian.putInt(data, 18 + offset, getReserved());
606          return getRecordSize();
607      }
608  
609      public int getRecordSize()
610      {
611          return 22;
612      }
613  
614      public short getSid()
615      {
616          return this.sid;
617      }
618  }
619