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