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