Don't swap AreaPtg references from relative to absolute, by correctly processing...
[poi.git] / src / java / org / apache / poi / hssf / record / formula / AreaPtg.java
blob908c8d5e3cce9bbcaf1619f98519cd79dee3b0d9
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================== */
19 package org.apache.poi.hssf.record.formula;
21 import org.apache.poi.util.LittleEndian;
22 import org.apache.poi.util.BitField;
23 import org.apache.poi.util.BitFieldFactory;
25 import org.apache.poi.hssf.util.AreaReference;
26 import org.apache.poi.hssf.util.CellReference;
27 import org.apache.poi.hssf.model.Workbook;
28 import org.apache.poi.hssf.record.RecordInputStream;
30 /**
31 * Specifies a rectangular area of cells A1:A4 for instance.
32 * @author andy
33 * @author Jason Height (jheight at chariot dot net dot au)
36 public class AreaPtg
37 extends Ptg
39 public final static short sid = 0x25;
40 private final static int SIZE = 9;
41 private short field_1_first_row;
42 private short field_2_last_row;
43 private short field_3_first_column;
44 private short field_4_last_column;
46 private final static BitField rowRelative = BitFieldFactory.getInstance(0x8000);
47 private final static BitField colRelative = BitFieldFactory.getInstance(0x4000);
48 private final static BitField columnMask = BitFieldFactory.getInstance(0x3FFF);
50 protected AreaPtg() {
51 //Required for clone methods
54 public AreaPtg(String arearef) {
55 AreaReference ar = new AreaReference(arearef);
56 setFirstRow((short)ar.getCells()[0].getRow());
57 setFirstColumn((short)ar.getCells()[0].getCol());
58 setLastRow((short)ar.getCells()[1].getRow());
59 setLastColumn((short)ar.getCells()[1].getCol());
60 setFirstColRelative(!ar.getCells()[0].isColAbsolute());
61 setLastColRelative(!ar.getCells()[1].isColAbsolute());
62 setFirstRowRelative(!ar.getCells()[0].isRowAbsolute());
63 setLastRowRelative(!ar.getCells()[1].isRowAbsolute());
66 public AreaPtg(short firstRow, short lastRow, short firstColumn, short lastColumn, boolean firstRowRelative, boolean lastRowRelative, boolean firstColRelative, boolean lastColRelative) {
67 setFirstRow(firstRow);
68 setLastRow(lastRow);
69 setFirstColumn(firstColumn);
70 setLastColumn(lastColumn);
71 setFirstRowRelative(firstRowRelative);
72 setLastRowRelative(lastRowRelative);
73 setFirstColRelative(firstColRelative);
74 setLastColRelative(lastColRelative);
77 public AreaPtg(RecordInputStream in)
79 field_1_first_row = in.readShort();
80 field_2_last_row = in.readShort();
81 field_3_first_column = in.readShort();
82 field_4_last_column = in.readShort();
83 //System.out.println(toString());
86 public String getAreaPtgName() {
87 return "AreaPtg";
90 public String toString()
92 StringBuffer buffer = new StringBuffer();
94 buffer.append(getAreaPtgName());
95 buffer.append("\n");
96 buffer.append("firstRow = " + getFirstRow()).append("\n");
97 buffer.append("lastRow = " + getLastRow()).append("\n");
98 buffer.append("firstCol = " + getFirstColumn()).append("\n");
99 buffer.append("lastCol = " + getLastColumn()).append("\n");
100 buffer.append("firstColRowRel= "
101 + isFirstRowRelative()).append("\n");
102 buffer.append("lastColRowRel = "
103 + isLastRowRelative()).append("\n");
104 buffer.append("firstColRel = " + isFirstColRelative()).append("\n");
105 buffer.append("lastColRel = " + isLastColRelative()).append("\n");
106 return buffer.toString();
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);
117 public int getSize()
119 return SIZE;
123 * @return the first row in the area
125 public short getFirstRow()
127 return field_1_first_row;
131 * sets the first row
132 * @param row number (0-based)
134 public void setFirstRow(short row)
136 field_1_first_row = row;
140 * @return last row in the range (x2 in x1,y1-x2,y2)
142 public short getLastRow()
144 return field_2_last_row;
148 * @param row last row number in the area
150 public void setLastRow(short row)
152 field_2_last_row = row;
156 * @return the first column number in the area.
158 public short getFirstColumn()
160 return columnMask.getShortValue(field_3_first_column);
164 * @return the first column number + the options bit settings unstripped
166 public short getFirstColumnRaw()
168 return field_3_first_column;
172 * @return whether or not the first row is a relative reference or not.
174 public boolean isFirstRowRelative()
176 return rowRelative.isSet(field_3_first_column);
180 * sets the first row to relative or not
181 * @param rel is relative or not.
183 public void setFirstRowRelative(boolean rel) {
184 field_3_first_column=rowRelative.setShortBoolean(field_3_first_column,rel);
188 * @return isrelative first column to relative or not
190 public boolean isFirstColRelative()
192 return colRelative.isSet(field_3_first_column);
196 * set whether the first column is relative
198 public void setFirstColRelative(boolean rel) {
199 field_3_first_column=colRelative.setShortBoolean(field_3_first_column,rel);
203 * set the first column in the area
205 public void setFirstColumn(short column)
207 field_3_first_column=columnMask.setShortValue(field_3_first_column, column);
211 * set the first column irespective of the bitmasks
213 public void setFirstColumnRaw(short column)
215 field_3_first_column = column;
219 * @return lastcolumn in the area
221 public short getLastColumn()
223 return columnMask.getShortValue(field_4_last_column);
227 * @return last column and bitmask (the raw field)
229 public short getLastColumnRaw()
231 return field_4_last_column;
235 * @return last row relative or not
237 public boolean isLastRowRelative()
239 return rowRelative.isSet(field_4_last_column);
243 * set whether the last row is relative or not
244 * @param rel <code>true</code> if the last row relative, else
245 * <code>false</code>
247 public void setLastRowRelative(boolean rel) {
248 field_4_last_column=rowRelative.setShortBoolean(field_4_last_column,rel);
252 * @return lastcol relative or not
254 public boolean isLastColRelative()
256 return colRelative.isSet(field_4_last_column);
260 * set whether the last column should be relative or not
262 public void setLastColRelative(boolean rel) {
263 field_4_last_column=colRelative.setShortBoolean(field_4_last_column,rel);
268 * set the last column in the area
270 public void setLastColumn(short column)
272 field_4_last_column=columnMask.setShortValue(field_4_last_column, column);
276 * set the last column irrespective of the bitmasks
278 public void setLastColumnRaw(short column)
280 field_4_last_column = column;
283 public String toFormulaString(Workbook book)
285 return (new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString() + ":" +
286 (new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative())).toString();
289 public byte getDefaultOperandClass() {
290 return Ptg.CLASS_REF;
293 public Object clone() {
294 AreaPtg ptg = new AreaPtg();
295 ptg.field_1_first_row = field_1_first_row;
296 ptg.field_2_last_row = field_2_last_row;
297 ptg.field_3_first_column = field_3_first_column;
298 ptg.field_4_last_column = field_4_last_column;
299 ptg.setClass(ptgClass);
300 return ptg;