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 java.util.*;
59   
60   import org.apache.poi.util.LittleEndian;
61   
62   /**
63    * Title:        Selection Record<P>
64    * Description:  shows the user's selection on the sheet
65    *               for write set num refs to 0<P>
66    *
67    * TODO :  Implement reference subrecords
68    * REFERENCE:  PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
69    * @author Andrew C. Oliver (acoliver at apache dot org)
70    * @version 2.0-pre
71    */
72   
73   public class SelectionRecord
74       extends Record
75   {
76       public final static short sid = 0x1d;
77       private byte              field_1_pane;
78       private short             field_2_row_active_cell;
79       private short             field_3_col_active_cell;
80       private short             field_4_ref_active_cell;
81       private short             field_5_num_refs;
82       private ArrayList         field_6_refs;     // not used yet
83   
84       public SelectionRecord()
85       {
86       }
87   
88       /**
89        * Constructs a Selection record and sets its fields appropriately.
90        *
91        * @param id     id must be 0x1d or an exception will be throw upon validation
92        * @param size  the size of the data area of the record
93        * @param data  data of the record (should not contain sid/len)
94        */
95   
96       public SelectionRecord(short id, short size, byte [] data)
97       {
98           super(id, size, data);
99       }
100  
101      /**
102       * Constructs a Selection record and sets its fields appropriately.
103       *
104       * @param id     id must be 0x1d or an exception will be throw upon validation
105       * @param size  the size of the data area of the record
106       * @param data  data of the record (should not contain sid/len)
107       * @param offset of the record's data
108       */
109  
110      public SelectionRecord(short id, short size, byte [] data, int offset)
111      {
112          super(id, size, data, offset);
113      }
114  
115      protected void validateSid(short id)
116      {
117          if (id != sid)
118          {
119              throw new RecordFormatException("NOT A valid Selection RECORD");
120          }
121      }
122  
123      protected void fillFields(byte [] data, short size, int offset)
124      {
125          field_1_pane            = data[ 0 + offset ];
126          field_2_row_active_cell = LittleEndian.getShort(data, 1 + offset);
127          field_3_col_active_cell = LittleEndian.getShort(data, 3 + offset);
128          field_4_ref_active_cell = LittleEndian.getShort(data, 5 + offset);
129          field_5_num_refs        = LittleEndian.getShort(data, 7 + offset);
130      }
131  
132      /**
133       * set which window pane this is for
134       * @param pane
135       */
136  
137      public void setPane(byte pane)
138      {
139          field_1_pane = pane;
140      }
141  
142      /**
143       * set the active cell's row
144       * @param row number of active cell
145       */
146  
147      public void setActiveCellRow(short row)
148      {
149          field_2_row_active_cell = row;
150      }
151  
152      /**
153       * set the active cell's col
154       * @param col number of active cell
155       */
156  
157      public void setActiveCellCol(short col)
158      {
159          field_3_col_active_cell = col;
160      }
161  
162      /**
163       * set the active cell's reference number
164       * @param ref number of active cell
165       */
166  
167      public void setActiveCellRef(short ref)
168      {
169          field_4_ref_active_cell = ref;
170      }
171  
172      /**
173       * set the number of cell refs (we don't support selection so set to 0
174       * @param refs - number of references
175       */
176  
177      public void setNumRefs(short refs)
178      {
179          field_5_num_refs = refs;
180      }
181  
182      /**
183       * get which window pane this is for
184       * @return pane
185       */
186  
187      public byte getPane()
188      {
189          return field_1_pane;
190      }
191  
192      /**
193       * get the active cell's row
194       * @return row number of active cell
195       */
196  
197      public short getActiveCellRow()
198      {
199          return field_2_row_active_cell;
200      }
201  
202      /**
203       * get the active cell's col
204       * @return col number of active cell
205       */
206  
207      public short getActiveCellCol()
208      {
209          return field_3_col_active_cell;
210      }
211  
212      /**
213       * get the active cell's reference number
214       * @return ref number of active cell
215       */
216  
217      public short getActiveCellRef()
218      {
219          return field_4_ref_active_cell;
220      }
221  
222      /**
223       * get the number of cell refs (we don't support selection so set to 0
224       * @return refs - number of references
225       */
226  
227      public short getNumRefs()
228      {
229          return field_5_num_refs;
230      }
231  
232      public String toString()
233      {
234          StringBuffer buffer = new StringBuffer();
235  
236          buffer.append("[SELECTION]\n");
237          buffer.append("    .pane            = ")
238              .append(Integer.toHexString(getPane())).append("\n");
239          buffer.append("    .activecellrow   = ")
240              .append(Integer.toHexString(getActiveCellRow())).append("\n");
241          buffer.append("    .activecellcol   = ")
242              .append(Integer.toHexString(getActiveCellCol())).append("\n");
243          buffer.append("    .activecellref   = ")
244              .append(Integer.toHexString(getActiveCellRef())).append("\n");
245          buffer.append("    .numrefs         = ")
246              .append(Integer.toHexString(getNumRefs())).append("\n");
247          buffer.append("[/SELECTION]\n");
248          return buffer.toString();
249      }
250  
251  //hacked to provide one cell reference to 0,0 - 0,0
252      public int serialize(int offset, byte [] data)
253      {
254          LittleEndian.putShort(data, 0 + offset, sid);
255          LittleEndian.putShort(data, 2 + offset, ( short ) 15);
256          data[ 4 + offset ] = getPane();
257          LittleEndian.putShort(data, 5 + offset, getActiveCellRow());
258          LittleEndian.putShort(data, 7 + offset, getActiveCellCol());
259          LittleEndian.putShort(data, 9 + offset, getActiveCellRef());
260          LittleEndian.putShort(data, 11 + offset, ( short ) 1);
261          LittleEndian.putShort(data, 13 + offset, ( short ) 0);
262          LittleEndian.putShort(data, 15 + offset, ( short ) 0);
263          data[ 17 + offset ] = 0;
264          data[ 18 + offset ] = 0;
265          return getRecordSize();
266      }
267  
268      public int getRecordSize()
269      {
270          return 19;
271      }
272  
273      public short getSid()
274      {
275          return this.sid;
276      }
277  }
278