KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > SelectionRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.record;
20
21 import java.util.*;
22
23 import org.apache.poi.util.LittleEndian;
24
25 /**
26  * Title: Selection Record<P>
27  * Description: shows the user's selection on the sheet
28  * for write set num refs to 0<P>
29  *
30  * TODO : Fully implement reference subrecords.
31  * REFERENCE: PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
32  * @author Andrew C. Oliver (acoliver at apache dot org)
33  * @author Jason Height (jheight at chariot dot net dot au)
34  * @author Glen Stampoultzis (glens at apache.org)
35  */

36
37 public class SelectionRecord
38     extends Record
39 {
40     public final static short sid = 0x1d;
41     private byte field_1_pane;
42     //private short field_2_row_active_cell;
43
private int field_2_row_active_cell;
44     private short field_3_col_active_cell;
45     private short field_4_ref_active_cell;
46     private short field_5_num_refs;
47     private ArrayList field_6_refs; // not used yet
48

49     public SelectionRecord()
50     {
51     }
52
53     /**
54      * Constructs a Selection record and sets its fields appropriately.
55      *
56      * @param id id must be 0x1d or an exception will be throw upon validation
57      * @param size the size of the data area of the record
58      * @param data data of the record (should not contain sid/len)
59      */

60
61     public SelectionRecord(short id, short size, byte [] data)
62     {
63         super(id, size, data);
64     }
65
66     /**
67      * Constructs a Selection record and sets its fields appropriately.
68      *
69      * @param id id must be 0x1d or an exception will be throw upon validation
70      * @param size the size of the data area of the record
71      * @param data data of the record (should not contain sid/len)
72      * @param offset of the record's data
73      */

74
75     public SelectionRecord(short id, short size, byte [] data, int offset)
76     {
77         super(id, size, data, offset);
78     }
79
80     protected void validateSid(short id)
81     {
82         if (id != sid)
83         {
84             throw new RecordFormatException("NOT A valid Selection RECORD");
85         }
86     }
87
88     protected void fillFields(byte [] data, short size, int offset)
89     {
90         field_1_pane = data[ 0 + offset ];
91         //field_2_row_active_cell = LittleEndian.getShort(data, 1 + offset);
92
field_2_row_active_cell = LittleEndian.getUShort(data, 1 + offset);
93         field_3_col_active_cell = LittleEndian.getShort(data, 3 + offset);
94         field_4_ref_active_cell = LittleEndian.getShort(data, 5 + offset);
95         field_5_num_refs = LittleEndian.getShort(data, 7 + offset);
96     }
97
98     /**
99      * set which window pane this is for
100      * @param pane
101      */

102
103     public void setPane(byte pane)
104     {
105         field_1_pane = pane;
106     }
107
108     /**
109      * set the active cell's row
110      * @param row number of active cell
111      */

112
113     //public void setActiveCellRow(short row)
114
public void setActiveCellRow(int row)
115     {
116         field_2_row_active_cell = row;
117     }
118
119     /**
120      * set the active cell's col
121      * @param col number of active cell
122      */

123
124     public void setActiveCellCol(short col)
125     {
126         field_3_col_active_cell = col;
127     }
128
129     /**
130      * set the active cell's reference number
131      * @param ref number of active cell
132      */

133
134     public void setActiveCellRef(short ref)
135     {
136         field_4_ref_active_cell = ref;
137     }
138
139     /**
140      * set the number of cell refs (we don't support selection so set to 0
141      * @param refs - number of references
142      */

143
144     public void setNumRefs(short refs)
145     {
146         field_5_num_refs = refs;
147     }
148
149     /**
150      * get which window pane this is for
151      * @return pane
152      */

153
154     public byte getPane()
155     {
156         return field_1_pane;
157     }
158
159     /**
160      * get the active cell's row
161      * @return row number of active cell
162      */

163
164     //public short getActiveCellRow()
165
public int getActiveCellRow()
166     {
167         return field_2_row_active_cell;
168     }
169
170     /**
171      * get the active cell's col
172      * @return col number of active cell
173      */

174
175     public short getActiveCellCol()
176     {
177         return field_3_col_active_cell;
178     }
179
180     /**
181      * get the active cell's reference number
182      * @return ref number of active cell
183      */

184
185     public short getActiveCellRef()
186     {
187         return field_4_ref_active_cell;
188     }
189
190     /**
191      * get the number of cell refs (we don't support selection so set to 0
192      * @return refs - number of references
193      */

194
195     public short getNumRefs()
196     {
197         return field_5_num_refs;
198     }
199
200     public String JavaDoc toString()
201     {
202         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
203
204         buffer.append("[SELECTION]\n");
205         buffer.append(" .pane = ")
206             .append(Integer.toHexString(getPane())).append("\n");
207         buffer.append(" .activecellrow = ")
208             .append(Integer.toHexString(getActiveCellRow())).append("\n");
209         buffer.append(" .activecellcol = ")
210             .append(Integer.toHexString(getActiveCellCol())).append("\n");
211         buffer.append(" .activecellref = ")
212             .append(Integer.toHexString(getActiveCellRef())).append("\n");
213         buffer.append(" .numrefs = ")
214             .append(Integer.toHexString(getNumRefs())).append("\n");
215         buffer.append("[/SELECTION]\n");
216         return buffer.toString();
217     }
218
219 //hacked to provide one cell reference to 0,0 - 0,0
220
public int serialize(int offset, byte [] data)
221     {
222         LittleEndian.putShort(data, 0 + offset, sid);
223         LittleEndian.putShort(data, 2 + offset, ( short ) 15);
224         data[ 4 + offset ] = getPane();
225         //LittleEndian.putShort(data, 5 + offset, getActiveCellRow());
226
LittleEndian.putShort(data, 5 + offset, ( short ) getActiveCellRow());
227         LittleEndian.putShort(data, 7 + offset, getActiveCellCol());
228         LittleEndian.putShort(data, 9 + offset, getActiveCellRef());
229         LittleEndian.putShort(data, 11 + offset, ( short ) 1);
230         LittleEndian.putShort(data, 13 + offset, ( short ) getActiveCellRow());
231         LittleEndian.putShort(data, 15 + offset, ( short ) getActiveCellRow());
232         data[ 17 + offset ] = (byte)getActiveCellCol();
233         data[ 18 + offset ] = (byte)getActiveCellCol();
234         return getRecordSize();
235     }
236
237     public int getRecordSize()
238     {
239         return 19;
240     }
241
242     public short getSid()
243     {
244         return this.sid;
245     }
246
247     public Object JavaDoc clone() {
248       SelectionRecord rec = new SelectionRecord();
249       rec.field_1_pane = field_1_pane;
250       rec.field_2_row_active_cell = field_2_row_active_cell;
251       rec.field_3_col_active_cell = field_3_col_active_cell;
252       rec.field_4_ref_active_cell = field_4_ref_active_cell;
253       rec.field_5_num_refs = field_5_num_refs;
254       rec.field_6_refs = field_6_refs;
255       return rec;
256     }
257 }
258
Popular Tags