KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > formula > AreaPtg


1 /* ====================================================================
2    Copyright 2003-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18 package org.apache.poi.hssf.record.formula;
19
20 import org.apache.poi.util.LittleEndian;
21 import org.apache.poi.util.BitField;
22
23 import org.apache.poi.hssf.util.AreaReference;
24 import org.apache.poi.hssf.util.CellReference;
25 import org.apache.poi.hssf.model.Workbook;
26
27 /**
28  * Specifies a rectangular area of cells A1:A4 for instance.
29  * @author andy
30  * @author Jason Height (jheight at chariot dot net dot au)
31  */

32
33 public class AreaPtg
34     extends Ptg
35 {
36     public final static short sid = 0x25;
37     private final static int SIZE = 9;
38     private short field_1_first_row;
39     private short field_2_last_row;
40     private short field_3_first_column;
41     private short field_4_last_column;
42     
43     private BitField rowRelative = new BitField(0x8000);
44     private BitField colRelative = new BitField(0x4000);
45     private BitField column = new BitField(0x3FFF);
46
47     private AreaPtg() {
48       //Required for clone methods
49
}
50    
51     public AreaPtg(String JavaDoc arearef) {
52         AreaReference ar = new AreaReference(arearef);
53         setFirstRow((short)ar.getCells()[0].getRow());
54         setFirstColumn((short)ar.getCells()[0].getCol());
55         setLastRow((short)ar.getCells()[1].getRow());
56         setLastColumn((short)ar.getCells()[1].getCol());
57         setFirstColRelative(!ar.getCells()[0].isColAbsolute());
58         setLastColRelative(!ar.getCells()[1].isColAbsolute());
59         setFirstRowRelative(!ar.getCells()[0].isRowAbsolute());
60         setLastRowRelative(!ar.getCells()[1].isRowAbsolute());
61         
62     }
63
64     public AreaPtg(byte [] data, int offset)
65     {
66         offset++;
67         field_1_first_row = LittleEndian.getShort(data, 0 + offset);
68         field_2_last_row = LittleEndian.getShort(data, 2 + offset);
69         field_3_first_column = LittleEndian.getShort(data, 4 + offset);
70         field_4_last_column = LittleEndian.getShort(data, 6 + offset);
71         //System.out.println(toString());
72
}
73
74     public String JavaDoc toString()
75     {
76         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
77
78         buffer.append("AreaPtg\n");
79         buffer.append("firstRow = " + getFirstRow()).append("\n");
80         buffer.append("lastRow = " + getLastRow()).append("\n");
81         buffer.append("firstCol = " + getFirstColumn()).append("\n");
82         buffer.append("lastCol = " + getLastColumn()).append("\n");
83         buffer.append("firstColRowRel= "
84                       + isFirstRowRelative()).append("\n");
85         buffer.append("lastColRowRel = "
86                       + isLastRowRelative()).append("\n");
87         buffer.append("firstColRel = " + isFirstColRelative()).append("\n");
88         buffer.append("lastColRel = " + isLastColRelative()).append("\n");
89         return buffer.toString();
90     }
91
92     public void writeBytes(byte [] array, int offset) {
93         array[offset] = (byte) (sid + ptgClass);
94         LittleEndian.putShort(array,offset+1,field_1_first_row);
95         LittleEndian.putShort(array,offset+3,field_2_last_row);
96         LittleEndian.putShort(array,offset+5,field_3_first_column);
97         LittleEndian.putShort(array,offset+7,field_4_last_column);
98     }
99
100     public int getSize()
101     {
102         return SIZE;
103     }
104
105     /**
106      * @return the first row in the area
107      */

108     public short getFirstRow()
109     {
110         return field_1_first_row;
111     }
112
113     /**
114      * sets the first row
115      * @param row number (0-based)
116      */

117     public void setFirstRow(short row)
118     {
119         field_1_first_row = row;
120     }
121
122     /**
123      * @return last row in the range (x2 in x1,y1-x2,y2)
124      */

125     public short getLastRow()
126     {
127         return field_2_last_row;
128     }
129
130     /**
131      * @param row last row number in the area
132      */

133     public void setLastRow(short row)
134     {
135         field_2_last_row = row;
136     }
137
138     /**
139      * @return the first column number in the area.
140      */

141     public short getFirstColumn()
142     {
143         return column.getShortValue(field_3_first_column);
144     }
145
146     /**
147      * @return the first column number + the options bit settings unstripped
148      */

149     public short getFirstColumnRaw()
150     {
151         return field_3_first_column;
152     }
153
154     /**
155      * @return whether or not the first row is a relative reference or not.
156      */

157     public boolean isFirstRowRelative()
158     {
159         return rowRelative.isSet(field_3_first_column);
160     }
161     
162     /**
163      * sets the first row to relative or not
164      * @param rel is relative or not.
165      */

166     public void setFirstRowRelative(boolean rel) {
167         field_3_first_column=rowRelative.setShortBoolean(field_3_first_column,rel);
168     }
169
170     /**
171      * @return isrelative first column to relative or not
172      */

173     public boolean isFirstColRelative()
174     {
175         return colRelative.isSet(field_3_first_column);
176     }
177     
178     /**
179      * set whether the first column is relative
180      */

181     public void setFirstColRelative(boolean rel) {
182         field_3_first_column=colRelative.setShortBoolean(field_3_first_column,rel);
183     }
184
185     /**
186      * set the first column in the area
187      */

188     public void setFirstColumn(short column)
189     {
190         field_3_first_column = column; // fixme
191
}
192
193     /**
194      * set the first column irespective of the bitmasks
195      */

196     public void setFirstColumnRaw(short column)
197     {
198         field_3_first_column = column;
199     }
200
201     /**
202      * @return lastcolumn in the area
203      */

204     public short getLastColumn()
205     {
206         return column.getShortValue(field_4_last_column);
207     }
208
209     /**
210      * @return last column and bitmask (the raw field)
211      */

212     public short getLastColumnRaw()
213     {
214         return field_4_last_column;
215     }
216
217     /**
218      * @return last row relative or not
219      */

220     public boolean isLastRowRelative()
221     {
222         return rowRelative.isSet(field_4_last_column);
223     }
224     
225     /**
226      * set whether the last row is relative or not
227      * @param rel <code>true</code> if the last row relative, else
228      * <code>false</code>
229      */

230     public void setLastRowRelative(boolean rel) {
231         field_4_last_column=rowRelative.setShortBoolean(field_4_last_column,rel);
232     }
233
234     /**
235      * @return lastcol relative or not
236      */

237     public boolean isLastColRelative()
238     {
239         return colRelative.isSet(field_4_last_column);
240     }
241     
242     /**
243      * set whether the last column should be relative or not
244      */

245     public void setLastColRelative(boolean rel) {
246         field_4_last_column=colRelative.setShortBoolean(field_4_last_column,rel);
247     }
248     
249
250     /**
251      * set the last column in the area
252      */

253     public void setLastColumn(short column)
254     {
255         field_4_last_column = column; // fixme
256
}
257
258     /**
259      * set the last column irrespective of the bitmasks
260      */

261     public void setLastColumnRaw(short column)
262     {
263         field_4_last_column = column;
264     }
265
266     public String JavaDoc toFormulaString(Workbook book)
267     {
268          return (new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString() + ":" +
269                 (new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative())).toString();
270     }
271
272     public byte getDefaultOperandClass() {
273         return Ptg.CLASS_REF;
274     }
275     
276     public Object JavaDoc clone() {
277       AreaPtg ptg = new AreaPtg();
278       ptg.field_1_first_row = field_1_first_row;
279       ptg.field_2_last_row = field_2_last_row;
280       ptg.field_3_first_column = field_3_first_column;
281       ptg.field_4_last_column = field_4_last_column;
282       ptg.setClass(ptgClass);
283       return ptg;
284     }
285
286 }
287
Popular Tags