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.AreaReference;
025: import org.apache.poi.hssf.util.CellReference;
026: import org.apache.poi.hssf.model.Workbook;
027: import org.apache.poi.hssf.record.RecordInputStream;
028:
029: /**
030: * Specifies a rectangular area of cells A1:A4 for instance.
031: * @author andy
032: * @author Jason Height (jheight at chariot dot net dot au)
033: */
034:
035: public class AreaPtg extends Ptg {
036: public final static short sid = 0x25;
037: private final static int SIZE = 9;
038: private short field_1_first_row;
039: private short field_2_last_row;
040: private short field_3_first_column;
041: private short field_4_last_column;
042:
043: private BitField rowRelative = BitFieldFactory.getInstance(0x8000);
044: private BitField colRelative = BitFieldFactory.getInstance(0x4000);
045: private BitField column = BitFieldFactory.getInstance(0x3FFF);
046:
047: protected AreaPtg() {
048: //Required for clone methods
049: }
050:
051: public AreaPtg(String arearef) {
052: AreaReference ar = new AreaReference(arearef);
053: setFirstRow((short) ar.getCells()[0].getRow());
054: setFirstColumn((short) ar.getCells()[0].getCol());
055: setLastRow((short) ar.getCells()[1].getRow());
056: setLastColumn((short) ar.getCells()[1].getCol());
057: setFirstColRelative(!ar.getCells()[0].isColAbsolute());
058: setLastColRelative(!ar.getCells()[1].isColAbsolute());
059: setFirstRowRelative(!ar.getCells()[0].isRowAbsolute());
060: setLastRowRelative(!ar.getCells()[1].isRowAbsolute());
061: }
062:
063: public AreaPtg(short firstRow, short lastRow, short firstColumn,
064: short lastColumn, boolean firstRowRelative,
065: boolean lastRowRelative, boolean firstColRelative,
066: boolean lastColRelative) {
067: setFirstRow(firstRow);
068: setLastRow(lastRow);
069: setFirstColumn(firstColumn);
070: setLastColumn(lastColumn);
071: setFirstRowRelative(firstRowRelative);
072: setLastRowRelative(lastRowRelative);
073: setFirstColRelative(firstColRelative);
074: setLastColRelative(lastColRelative);
075: }
076:
077: public AreaPtg(RecordInputStream in) {
078: field_1_first_row = in.readShort();
079: field_2_last_row = in.readShort();
080: field_3_first_column = in.readShort();
081: field_4_last_column = in.readShort();
082: //System.out.println(toString());
083: }
084:
085: public String getAreaPtgName() {
086: return "AreaPtg";
087: }
088:
089: public String toString() {
090: StringBuffer buffer = new StringBuffer();
091:
092: buffer.append(getAreaPtgName());
093: buffer.append("\n");
094: buffer.append("firstRow = " + getFirstRow()).append("\n");
095: buffer.append("lastRow = " + getLastRow()).append("\n");
096: buffer.append("firstCol = " + getFirstColumn()).append("\n");
097: buffer.append("lastCol = " + getLastColumn()).append("\n");
098: buffer.append("firstColRowRel= " + isFirstRowRelative())
099: .append("\n");
100: buffer.append("lastColRowRel = " + isLastRowRelative()).append(
101: "\n");
102: buffer.append("firstColRel = " + isFirstColRelative())
103: .append("\n");
104: buffer.append("lastColRel = " + isLastColRelative()).append(
105: "\n");
106: return buffer.toString();
107: }
108:
109: public void writeBytes(byte[] array, int offset) {
110: array[offset] = (byte) (sid + ptgClass);
111: LittleEndian.putShort(array, offset + 1, field_1_first_row);
112: LittleEndian.putShort(array, offset + 3, field_2_last_row);
113: LittleEndian.putShort(array, offset + 5, field_3_first_column);
114: LittleEndian.putShort(array, offset + 7, field_4_last_column);
115: }
116:
117: public int getSize() {
118: return SIZE;
119: }
120:
121: /**
122: * @return the first row in the area
123: */
124: public short getFirstRow() {
125: return field_1_first_row;
126: }
127:
128: /**
129: * sets the first row
130: * @param row number (0-based)
131: */
132: public void setFirstRow(short row) {
133: field_1_first_row = row;
134: }
135:
136: /**
137: * @return last row in the range (x2 in x1,y1-x2,y2)
138: */
139: public short getLastRow() {
140: return field_2_last_row;
141: }
142:
143: /**
144: * @param row last row number in the area
145: */
146: public void setLastRow(short row) {
147: field_2_last_row = row;
148: }
149:
150: /**
151: * @return the first column number in the area.
152: */
153: public short getFirstColumn() {
154: return column.getShortValue(field_3_first_column);
155: }
156:
157: /**
158: * @return the first column number + the options bit settings unstripped
159: */
160: public short getFirstColumnRaw() {
161: return field_3_first_column;
162: }
163:
164: /**
165: * @return whether or not the first row is a relative reference or not.
166: */
167: public boolean isFirstRowRelative() {
168: return rowRelative.isSet(field_3_first_column);
169: }
170:
171: /**
172: * sets the first row to relative or not
173: * @param rel is relative or not.
174: */
175: public void setFirstRowRelative(boolean rel) {
176: field_3_first_column = rowRelative.setShortBoolean(
177: field_3_first_column, rel);
178: }
179:
180: /**
181: * @return isrelative first column to relative or not
182: */
183: public boolean isFirstColRelative() {
184: return colRelative.isSet(field_3_first_column);
185: }
186:
187: /**
188: * set whether the first column is relative
189: */
190: public void setFirstColRelative(boolean rel) {
191: field_3_first_column = colRelative.setShortBoolean(
192: field_3_first_column, rel);
193: }
194:
195: /**
196: * set the first column in the area
197: */
198: public void setFirstColumn(short column) {
199: field_3_first_column = column; // fixme
200: }
201:
202: /**
203: * set the first column irespective of the bitmasks
204: */
205: public void setFirstColumnRaw(short column) {
206: field_3_first_column = column;
207: }
208:
209: /**
210: * @return lastcolumn in the area
211: */
212: public short getLastColumn() {
213: return column.getShortValue(field_4_last_column);
214: }
215:
216: /**
217: * @return last column and bitmask (the raw field)
218: */
219: public short getLastColumnRaw() {
220: return field_4_last_column;
221: }
222:
223: /**
224: * @return last row relative or not
225: */
226: public boolean isLastRowRelative() {
227: return rowRelative.isSet(field_4_last_column);
228: }
229:
230: /**
231: * set whether the last row is relative or not
232: * @param rel <code>true</code> if the last row relative, else
233: * <code>false</code>
234: */
235: public void setLastRowRelative(boolean rel) {
236: field_4_last_column = rowRelative.setShortBoolean(
237: field_4_last_column, rel);
238: }
239:
240: /**
241: * @return lastcol relative or not
242: */
243: public boolean isLastColRelative() {
244: return colRelative.isSet(field_4_last_column);
245: }
246:
247: /**
248: * set whether the last column should be relative or not
249: */
250: public void setLastColRelative(boolean rel) {
251: field_4_last_column = colRelative.setShortBoolean(
252: field_4_last_column, rel);
253: }
254:
255: /**
256: * set the last column in the area
257: */
258: public void setLastColumn(short column) {
259: field_4_last_column = column; // fixme
260: }
261:
262: /**
263: * set the last column irrespective of the bitmasks
264: */
265: public void setLastColumnRaw(short column) {
266: field_4_last_column = column;
267: }
268:
269: public String toFormulaString(Workbook book) {
270: return (new CellReference(getFirstRow(), getFirstColumn(),
271: !isFirstRowRelative(), !isFirstColRelative()))
272: .toString()
273: + ":"
274: + (new CellReference(getLastRow(), getLastColumn(),
275: !isLastRowRelative(), !isLastColRelative()))
276: .toString();
277: }
278:
279: public byte getDefaultOperandClass() {
280: return Ptg.CLASS_REF;
281: }
282:
283: public Object clone() {
284: AreaPtg ptg = new AreaPtg();
285: ptg.field_1_first_row = field_1_first_row;
286: ptg.field_2_last_row = field_2_last_row;
287: ptg.field_3_first_column = field_3_first_column;
288: ptg.field_4_last_column = field_4_last_column;
289: ptg.setClass(ptgClass);
290: return ptg;
291: }
292:
293: }
|