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