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.formula;
019:
020: import org.apache.poi.util.LittleEndian;
021: import org.apache.poi.util.BitField;
022: import org.apache.poi.util.BitFieldFactory;
023:
024: import org.apache.poi.hssf.util.CellReference;
025: import org.apache.poi.hssf.model.Workbook;
026: import org.apache.poi.hssf.record.RecordInputStream;
027:
028: /**
029: * ReferencePtg - handles references (such as A1, A2, IA4)
030: * @author Andrew C. Oliver (acoliver@apache.org)
031: * @author Jason Height (jheight at chariot dot net dot au)
032: */
033:
034: public class ReferencePtg extends Ptg {
035: private final static int SIZE = 5;
036: public final static byte sid = 0x24;
037: private final static int MAX_ROW_NUMBER = 65536;
038: //public final static byte sid = 0x44;
039:
040: /**
041: * The row number, between 0 and 65535, but stored as a signed
042: * short between -32767 and 32768.
043: * Take care about which version you fetch back!
044: */
045: private short field_1_row;
046: /**
047: * The column number, between 0 and ??
048: */
049: private short field_2_col;
050: private BitField rowRelative = BitFieldFactory.getInstance(0x8000);
051: private BitField colRelative = BitFieldFactory.getInstance(0x4000);
052: private BitField column = BitFieldFactory.getInstance(0x3FFF);
053:
054: protected ReferencePtg() {
055: //Required for clone methods
056: }
057:
058: /**
059: * Takes in a String represnetation of a cell reference and fills out the
060: * numeric fields.
061: */
062: public ReferencePtg(String cellref) {
063: CellReference c = new CellReference(cellref);
064: setRow((short) c.getRow());
065: setColumn((short) c.getCol());
066: setColRelative(!c.isColAbsolute());
067: setRowRelative(!c.isRowAbsolute());
068: }
069:
070: public ReferencePtg(short row, short column, boolean isRowRelative,
071: boolean isColumnRelative) {
072: setRow(row);
073: setColumn(column);
074: setRowRelative(isRowRelative);
075: setColRelative(isColumnRelative);
076: }
077:
078: /** Creates new ValueReferencePtg */
079:
080: public ReferencePtg(RecordInputStream in) {
081: field_1_row = in.readShort();
082: field_2_col = in.readShort();
083: }
084:
085: public String getRefPtgName() {
086: return "ReferencePtg";
087: }
088:
089: public String toString() {
090: StringBuffer buffer = new StringBuffer("[");
091: buffer.append(getRefPtgName());
092: buffer.append("]\n");
093:
094: buffer.append("row = ").append(getRow()).append("\n");
095: buffer.append("col = ").append(getColumn()).append("\n");
096: buffer.append("rowrelative = ").append(isRowRelative()).append(
097: "\n");
098: buffer.append("colrelative = ").append(isColRelative()).append(
099: "\n");
100: return buffer.toString();
101: }
102:
103: public void writeBytes(byte[] array, int offset) {
104: array[offset] = (byte) (sid + ptgClass);
105:
106: LittleEndian.putShort(array, offset + 1, field_1_row);
107: LittleEndian.putShort(array, offset + 3, field_2_col);
108: }
109:
110: public void setRow(short row) {
111: field_1_row = row;
112: }
113:
114: public void setRow(int row) {
115: if (row < 0 || row >= MAX_ROW_NUMBER) {
116: throw new IllegalArgumentException(
117: "The row number, when specified as an integer, must be between 0 and "
118: + MAX_ROW_NUMBER);
119: }
120:
121: // Save, wrapping as needed
122: if (row > Short.MAX_VALUE) {
123: field_1_row = (short) (row - MAX_ROW_NUMBER);
124: } else {
125: field_1_row = (short) row;
126: }
127: }
128:
129: /**
130: * Returns the row number as a short, which will be
131: * wrapped (negative) for values between 32769 and 65535
132: */
133: public short getRow() {
134: return field_1_row;
135: }
136:
137: /**
138: * Returns the row number as an int, between 0 and 65535
139: */
140: public int getRowAsInt() {
141: if (field_1_row < 0) {
142: return field_1_row + MAX_ROW_NUMBER;
143: }
144: return field_1_row;
145: }
146:
147: public boolean isRowRelative() {
148: return rowRelative.isSet(field_2_col);
149: }
150:
151: public void setRowRelative(boolean rel) {
152: field_2_col = rowRelative.setShortBoolean(field_2_col, rel);
153: }
154:
155: public boolean isColRelative() {
156: return colRelative.isSet(field_2_col);
157: }
158:
159: public void setColRelative(boolean rel) {
160: field_2_col = colRelative.setShortBoolean(field_2_col, rel);
161: }
162:
163: public void setColumnRaw(short col) {
164: field_2_col = col;
165: }
166:
167: public short getColumnRaw() {
168: return field_2_col;
169: }
170:
171: public void setColumn(short col) {
172: field_2_col = column.setShortValue(field_2_col, col);
173: }
174:
175: public short getColumn() {
176: return column.getShortValue(field_2_col);
177: }
178:
179: public int getSize() {
180: return SIZE;
181: }
182:
183: public String toFormulaString(Workbook book) {
184: //TODO -- should we store a cellreference instance in this ptg?? but .. memory is an issue, i believe!
185: return (new CellReference(getRowAsInt(), getColumn(),
186: !isRowRelative(), !isColRelative())).toString();
187: }
188:
189: public byte getDefaultOperandClass() {
190: return Ptg.CLASS_REF;
191: }
192:
193: public Object clone() {
194: ReferencePtg ptg = new ReferencePtg();
195: ptg.field_1_row = field_1_row;
196: ptg.field_2_col = field_2_col;
197: ptg.setClass(ptgClass);
198: return ptg;
199: }
200: }
|