001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.record;
019:
020: import java.util.*;
021:
022: import org.apache.poi.util.LittleEndian;
023:
024: /**
025: * Title: Selection Record<P>
026: * Description: shows the user's selection on the sheet
027: * for write set num refs to 0<P>
028: *
029: * TODO : Fully implement reference subrecords.
030: * REFERENCE: PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
031: * @author Andrew C. Oliver (acoliver at apache dot org)
032: * @author Jason Height (jheight at chariot dot net dot au)
033: * @author Glen Stampoultzis (glens at apache.org)
034: */
035:
036: public class SelectionRecord extends Record {
037: public final static short sid = 0x1d;
038: private byte field_1_pane;
039: //private short field_2_row_active_cell;
040: private int field_2_row_active_cell;
041: private short field_3_col_active_cell;
042: private short field_4_ref_active_cell;
043: private short field_5_num_refs;
044: private ArrayList field_6_refs; // not used yet
045:
046: public class Reference {
047: private short field_1_first_row;
048: private short field_2_last_row;
049: private byte field_3_first_column;
050: private byte field_4_last_column;
051:
052: Reference(RecordInputStream in) {
053: field_1_first_row = in.readShort();
054: field_2_last_row = in.readShort();
055: field_3_first_column = in.readByte();
056: field_4_last_column = in.readByte();
057: }
058:
059: public short getFirstRow() {
060: return field_1_first_row;
061: }
062:
063: public short getLastRow() {
064: return field_2_last_row;
065: }
066:
067: public byte getFirstColumn() {
068: return field_3_first_column;
069: }
070:
071: public byte getLastColumn() {
072: return field_4_last_column;
073: }
074: }
075:
076: public SelectionRecord() {
077: }
078:
079: /**
080: * Constructs a Selection record and sets its fields appropriately.
081: * @param in the RecordInputstream to read the record from
082: */
083:
084: public SelectionRecord(RecordInputStream in) {
085: super (in);
086: }
087:
088: protected void validateSid(short id) {
089: if (id != sid) {
090: throw new RecordFormatException(
091: "NOT A valid Selection RECORD");
092: }
093: }
094:
095: protected void fillFields(RecordInputStream in) {
096: field_1_pane = in.readByte();
097: //field_2_row_active_cell = LittleEndian.getShort(data, 1 + offset);
098: field_2_row_active_cell = in.readUShort();
099: field_3_col_active_cell = in.readShort();
100: field_4_ref_active_cell = in.readShort();
101: field_5_num_refs = in.readShort();
102:
103: field_6_refs = new ArrayList(field_5_num_refs);
104: for (int i = 0; i < field_5_num_refs; i++) {
105: field_6_refs.add(new Reference(in));
106: }
107: }
108:
109: /**
110: * set which window pane this is for
111: * @param pane
112: */
113:
114: public void setPane(byte pane) {
115: field_1_pane = pane;
116: }
117:
118: /**
119: * set the active cell's row
120: * @param row number of active cell
121: */
122:
123: //public void setActiveCellRow(short row)
124: public void setActiveCellRow(int row) {
125: field_2_row_active_cell = row;
126: }
127:
128: /**
129: * set the active cell's col
130: * @param col number of active cell
131: */
132:
133: public void setActiveCellCol(short col) {
134: field_3_col_active_cell = col;
135: }
136:
137: /**
138: * set the active cell's reference number
139: * @param ref number of active cell
140: */
141:
142: public void setActiveCellRef(short ref) {
143: field_4_ref_active_cell = ref;
144: }
145:
146: /**
147: * set the number of cell refs (we don't support selection so set to 0
148: * @param refs - number of references
149: */
150:
151: public void setNumRefs(short refs) {
152: field_5_num_refs = refs;
153: }
154:
155: /**
156: * get which window pane this is for
157: * @return pane
158: */
159:
160: public byte getPane() {
161: return field_1_pane;
162: }
163:
164: /**
165: * get the active cell's row
166: * @return row number of active cell
167: */
168:
169: //public short getActiveCellRow()
170: public int getActiveCellRow() {
171: return field_2_row_active_cell;
172: }
173:
174: /**
175: * get the active cell's col
176: * @return col number of active cell
177: */
178:
179: public short getActiveCellCol() {
180: return field_3_col_active_cell;
181: }
182:
183: /**
184: * get the active cell's reference number
185: * @return ref number of active cell
186: */
187:
188: public short getActiveCellRef() {
189: return field_4_ref_active_cell;
190: }
191:
192: /**
193: * get the number of cell refs (we don't support selection so set to 0
194: * @return refs - number of references
195: */
196:
197: public short getNumRefs() {
198: return field_5_num_refs;
199: }
200:
201: public String toString() {
202: StringBuffer buffer = new StringBuffer();
203:
204: buffer.append("[SELECTION]\n");
205: buffer.append(" .pane = ").append(
206: Integer.toHexString(getPane())).append("\n");
207: buffer.append(" .activecellrow = ").append(
208: Integer.toHexString(getActiveCellRow())).append("\n");
209: buffer.append(" .activecellcol = ").append(
210: Integer.toHexString(getActiveCellCol())).append("\n");
211: buffer.append(" .activecellref = ").append(
212: Integer.toHexString(getActiveCellRef())).append("\n");
213: buffer.append(" .numrefs = ").append(
214: 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: LittleEndian.putShort(data, 0 + offset, sid);
222: LittleEndian.putShort(data, 2 + offset, (short) 15);
223: data[4 + offset] = getPane();
224: //LittleEndian.putShort(data, 5 + offset, getActiveCellRow());
225: LittleEndian.putShort(data, 5 + offset,
226: (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,
231: (short) getActiveCellRow());
232: LittleEndian.putShort(data, 15 + offset,
233: (short) getActiveCellRow());
234: data[17 + offset] = (byte) getActiveCellCol();
235: data[18 + offset] = (byte) getActiveCellCol();
236: return getRecordSize();
237: }
238:
239: public int getRecordSize() {
240: return 19;
241: }
242:
243: public short getSid() {
244: return sid;
245: }
246:
247: public Object 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: }
|