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: /*
019: * ColumnInfoRecord.java
020: *
021: * Created on December 8, 2001, 8:44 AM
022: */
023: package org.apache.poi.hssf.record;
024:
025: import org.apache.poi.util.LittleEndian;
026: import org.apache.poi.util.BitField;
027: import org.apache.poi.util.BitFieldFactory;
028:
029: /**
030: * Title: ColumnInfo Record<P>
031: * Description: Defines with width and formatting for a range of columns<P>
032: * REFERENCE: PG 293 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
033: * @author Andrew C. Oliver (acoliver at apache dot org)
034: * @version 2.0-pre
035: */
036:
037: public class ColumnInfoRecord extends Record {
038: public static final short sid = 0x7d;
039: private short field_1_first_col;
040: private short field_2_last_col;
041: private short field_3_col_width;
042: private short field_4_xf_index;
043: private short field_5_options;
044: static final private BitField hidden = BitFieldFactory
045: .getInstance(0x01);
046: static final private BitField outlevel = BitFieldFactory
047: .getInstance(0x0700);
048: static final private BitField collapsed = BitFieldFactory
049: .getInstance(0x1000);
050: private short field_6_reserved;
051:
052: public ColumnInfoRecord() {
053: }
054:
055: /**
056: * Constructs a ColumnInfo record and sets its fields appropriately
057: * @param in the RecordInputstream to read the record from
058: */
059:
060: public ColumnInfoRecord(RecordInputStream in) {
061: super (in);
062: }
063:
064: protected void fillFields(RecordInputStream in) {
065: field_1_first_col = in.readShort();
066: field_2_last_col = in.readShort();
067: field_3_col_width = in.readShort();
068: field_4_xf_index = in.readShort();
069: field_5_options = in.readShort();
070: field_6_reserved = in.readShort();
071: }
072:
073: protected void validateSid(short id) {
074: if (id != sid) {
075: throw new RecordFormatException("NOT A COLINFO RECORD!!");
076: }
077: }
078:
079: /**
080: * set the first column this record defines formatting info for
081: * @param fc - the first column index (0-based)
082: */
083:
084: public void setFirstColumn(short fc) {
085: field_1_first_col = fc;
086: }
087:
088: /**
089: * set the last column this record defines formatting info for
090: * @param lc - the last column index (0-based)
091: */
092:
093: public void setLastColumn(short lc) {
094: field_2_last_col = lc;
095: }
096:
097: /**
098: * set the columns' width in 1/256 of a character width
099: * @param cw - column width
100: */
101:
102: public void setColumnWidth(short cw) {
103: field_3_col_width = cw;
104: }
105:
106: /**
107: * set the columns' default format info
108: * @param xfi - the extended format index
109: * @see org.apache.poi.hssf.record.ExtendedFormatRecord
110: */
111:
112: public void setXFIndex(short xfi) {
113: field_4_xf_index = xfi;
114: }
115:
116: /**
117: * set the options bitfield - use the bitsetters instead
118: * @param options - the bitfield raw value
119: */
120:
121: public void setOptions(short options) {
122: field_5_options = options;
123: }
124:
125: // start options bitfield
126:
127: /**
128: * set whether or not these cells are hidden
129: * @param ishidden - whether the cells are hidden.
130: * @see #setOptions(short)
131: */
132:
133: public void setHidden(boolean ishidden) {
134: field_5_options = hidden.setShortBoolean(field_5_options,
135: ishidden);
136: }
137:
138: /**
139: * set the outline level for the cells
140: * @see #setOptions(short)
141: * @param olevel -outline level for the cells
142: */
143:
144: public void setOutlineLevel(short olevel) {
145: field_5_options = outlevel.setShortValue(field_5_options,
146: olevel);
147: }
148:
149: /**
150: * set whether the cells are collapsed
151: * @param iscollapsed - wether the cells are collapsed
152: * @see #setOptions(short)
153: */
154:
155: public void setCollapsed(boolean iscollapsed) {
156: field_5_options = collapsed.setShortBoolean(field_5_options,
157: iscollapsed);
158: }
159:
160: // end options bitfield
161:
162: /**
163: * get the first column this record defines formatting info for
164: * @return the first column index (0-based)
165: */
166:
167: public short getFirstColumn() {
168: return field_1_first_col;
169: }
170:
171: /**
172: * get the last column this record defines formatting info for
173: * @return the last column index (0-based)
174: */
175:
176: public short getLastColumn() {
177: return field_2_last_col;
178: }
179:
180: /**
181: * get the columns' width in 1/256 of a character width
182: * @return column width
183: */
184:
185: public short getColumnWidth() {
186: return field_3_col_width;
187: }
188:
189: /**
190: * get the columns' default format info
191: * @return the extended format index
192: * @see org.apache.poi.hssf.record.ExtendedFormatRecord
193: */
194:
195: public short getXFIndex() {
196: return field_4_xf_index;
197: }
198:
199: /**
200: * get the options bitfield - use the bitsetters instead
201: * @return the bitfield raw value
202: */
203:
204: public short getOptions() {
205: return field_5_options;
206: }
207:
208: // start options bitfield
209:
210: /**
211: * get whether or not these cells are hidden
212: * @return whether the cells are hidden.
213: * @see #setOptions(short)
214: */
215:
216: public boolean getHidden() {
217: return hidden.isSet(field_5_options);
218: }
219:
220: /**
221: * get the outline level for the cells
222: * @see #setOptions(short)
223: * @return outline level for the cells
224: */
225:
226: public short getOutlineLevel() {
227: return outlevel.getShortValue(field_5_options);
228: }
229:
230: /**
231: * get whether the cells are collapsed
232: * @return wether the cells are collapsed
233: * @see #setOptions(short)
234: */
235:
236: public boolean getCollapsed() {
237: return collapsed.isSet(field_5_options);
238: }
239:
240: // end options bitfield
241: public short getSid() {
242: return sid;
243: }
244:
245: public int serialize(int offset, byte[] data) {
246: LittleEndian.putShort(data, 0 + offset, sid);
247: LittleEndian.putShort(data, 2 + offset, (short) 12);
248: LittleEndian.putShort(data, 4 + offset, getFirstColumn());
249: LittleEndian.putShort(data, 6 + offset, getLastColumn());
250: LittleEndian.putShort(data, 8 + offset, getColumnWidth());
251: LittleEndian.putShort(data, 10 + offset, getXFIndex());
252: LittleEndian.putShort(data, 12 + offset, getOptions());
253: LittleEndian.putShort(data, 14 + offset, field_6_reserved);
254: return getRecordSize();
255: }
256:
257: public int getRecordSize() {
258: return 16;
259: }
260:
261: public String toString() {
262: StringBuffer buffer = new StringBuffer();
263:
264: buffer.append("[COLINFO]\n");
265: buffer.append("colfirst = ").append(getFirstColumn())
266: .append("\n");
267: buffer.append("collast = ").append(getLastColumn())
268: .append("\n");
269: buffer.append("colwidth = ").append(getColumnWidth())
270: .append("\n");
271: buffer.append("xfindex = ").append(getXFIndex()).append(
272: "\n");
273: buffer.append("options = ").append(getOptions()).append(
274: "\n");
275: buffer.append(" hidden = ").append(getHidden()).append(
276: "\n");
277: buffer.append(" olevel = ").append(getOutlineLevel())
278: .append("\n");
279: buffer.append(" collapsed = ").append(getCollapsed())
280: .append("\n");
281: buffer.append("[/COLINFO]\n");
282: return buffer.toString();
283: }
284:
285: public Object clone() {
286: ColumnInfoRecord rec = new ColumnInfoRecord();
287: rec.field_1_first_col = field_1_first_col;
288: rec.field_2_last_col = field_2_last_col;
289: rec.field_3_col_width = field_3_col_width;
290: rec.field_4_xf_index = field_4_xf_index;
291: rec.field_5_options = field_5_options;
292: rec.field_6_reserved = field_6_reserved;
293: return rec;
294: }
295: }
|