KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.poi.hssf.record.formula;
18
19 import org.apache.poi.util.LittleEndian;
20 import org.apache.poi.util.BitField;
21
22 import org.apache.poi.hssf.util.CellReference;
23 import org.apache.poi.hssf.model.Workbook;
24
25 /**
26  * ReferencePtg - handles references (such as A1, A2, IA4)
27  * @author Andrew C. Oliver (acoliver@apache.org)
28  * @author Jason Height (jheight at chariot dot net dot au)
29  */

30
31 public class ReferencePtg extends Ptg
32 {
33     private final static int SIZE = 5;
34     public final static byte sid = 0x24;
35     //public final static byte sid = 0x44;
36
private short field_1_row;
37     private short field_2_col;
38     private BitField rowRelative = new BitField(0x8000);
39     private BitField colRelative = new BitField(0x4000);
40
41     private ReferencePtg() {
42       //Required for clone methods
43
}
44     
45     /**
46      * Takes in a String represnetation of a cell reference and fills out the
47      * numeric fields.
48      */

49     public ReferencePtg(String JavaDoc cellref) {
50         CellReference c= new CellReference(cellref);
51         setRow((short) c.getRow());
52         setColumn((short) c.getCol());
53         setColRelative(!c.isColAbsolute());
54         setRowRelative(!c.isRowAbsolute());
55     }
56
57     /** Creates new ValueReferencePtg */
58
59     public ReferencePtg(byte[] data, int offset)
60     {
61         offset++; // adjust for ptg
62
field_1_row = LittleEndian.getShort(data, offset + 0);
63         field_2_col = LittleEndian.getShort(data, offset + 2);
64
65     }
66
67     public String JavaDoc toString()
68     {
69         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("[ValueReferencePtg]\n");
70
71         buffer.append("row = ").append(getRow()).append("\n");
72         buffer.append("col = ").append(getColumnRaw()).append("\n");
73         buffer.append("rowrelative = ").append(isRowRelative()).append("\n");
74         buffer.append("colrelative = ").append(isColRelative()).append("\n");
75         return buffer.toString();
76     }
77
78     public void writeBytes(byte [] array, int offset)
79     {
80         array[offset] = (byte) (sid + ptgClass);
81         LittleEndian.putShort(array,offset+1,field_1_row);
82         LittleEndian.putShort(array,offset+3,field_2_col);
83     }
84
85     public void setRow(short row)
86     {
87         field_1_row = row;
88     }
89
90     public short getRow()
91     {
92         return field_1_row;
93     }
94
95     public boolean isRowRelative()
96     {
97         return rowRelative.isSet(field_2_col);
98     }
99     
100     public void setRowRelative(boolean rel) {
101         field_2_col=rowRelative.setShortBoolean(field_2_col,rel);
102     }
103     
104     public boolean isColRelative()
105     {
106         return colRelative.isSet(field_2_col);
107     }
108     
109     public void setColRelative(boolean rel) {
110         field_2_col=colRelative.setShortBoolean(field_2_col,rel);
111     }
112
113     public void setColumnRaw(short col)
114     {
115         field_2_col = col;
116     }
117
118     public short getColumnRaw()
119     {
120         return field_2_col;
121     }
122
123     public void setColumn(short col)
124     {
125         field_2_col = col; // fix this
126
}
127
128     public short getColumn()
129     {
130         return rowRelative.setShortBoolean(colRelative.setShortBoolean(field_2_col,false),false);
131     }
132
133     public int getSize()
134     {
135         return SIZE;
136     }
137
138     public String JavaDoc toFormulaString(Workbook book)
139     {
140         //TODO -- should we store a cellreference instance in this ptg?? but .. memory is an issue, i believe!
141
return (new CellReference(getRow(),getColumn(),!isRowRelative(),!isColRelative())).toString();
142     }
143     
144     public byte getDefaultOperandClass() {
145         return Ptg.CLASS_REF;
146     }
147     
148     public Object JavaDoc clone() {
149       ReferencePtg ptg = new ReferencePtg();
150       ptg.field_1_row = field_1_row;
151       ptg.field_2_col = field_2_col;
152       ptg.setClass(ptgClass);
153       return ptg;
154     }
155 }
156
Popular Tags