KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import org.apache.poi.hssf.util.RangeAddress;
23 import org.apache.poi.hssf.util.CellReference;
24 import org.apache.poi.hssf.util.SheetReferences;
25 import org.apache.poi.hssf.model.Workbook;
26 import org.apache.poi.util.BitField;
27 import org.apache.poi.hssf.model.Workbook;
28
29 /**
30  * Title: Reference 3D Ptg <P>
31  * Description: Defined a cell in extern sheet. <P>
32  * REFERENCE: <P>
33  * @author Libin Roman (Vista Portal LDT. Developer)
34  * @author Jason Height (jheight at chariot dot net dot au)
35  * @version 1.0-pre
36  */

37
38 public class Ref3DPtg extends Ptg {
39     public final static byte sid = 0x3a;
40     private final static int SIZE = 7; // 6 + 1 for Ptg
41
private short field_1_index_extern_sheet;
42     private short field_2_row;
43     private short field_3_column;
44     private BitField rowRelative = new BitField(0x8000);
45     private BitField colRelative = new BitField(0x4000);
46
47     /** Creates new AreaPtg */
48     public Ref3DPtg() {}
49
50     public Ref3DPtg(byte[] data, int offset) {
51         offset++;
52         field_1_index_extern_sheet = LittleEndian.getShort(data, 0 + offset);
53         field_2_row = LittleEndian.getShort(data, 2 + offset);
54         field_3_column = LittleEndian.getShort(data, 4 + offset);
55     }
56     
57     public Ref3DPtg(String JavaDoc cellref, short externIdx ) {
58         CellReference c= new CellReference(cellref);
59         setRow((short) c.getRow());
60         setColumn((short) c.getCol());
61         setColRelative(!c.isColAbsolute());
62         setRowRelative(!c.isRowAbsolute());
63         setExternSheetIndex(externIdx);
64     }
65
66     public String JavaDoc toString() {
67         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
68
69         buffer.append("Ref3dPtg\n");
70         buffer.append("Index to Extern Sheet = " + getExternSheetIndex()).append("\n");
71         buffer.append("Row = " + getRow()).append("\n");
72         buffer.append("Col = " + getColumn()).append("\n");
73         buffer.append("ColRowRel= "
74         + isRowRelative()).append("\n");
75         buffer.append("ColRel = " + isColRelative()).append("\n");
76         return buffer.toString();
77     }
78
79     public void writeBytes(byte [] array, int offset) {
80         array[ 0 + offset ] = (byte) (sid + ptgClass);
81         LittleEndian.putShort(array, 1 + offset , getExternSheetIndex());
82         LittleEndian.putShort(array, 3 + offset , getRow());
83         LittleEndian.putShort(array, 5 + offset , getColumnRaw());
84     }
85
86     public int getSize() {
87         return SIZE;
88     }
89
90     public short getExternSheetIndex(){
91         return field_1_index_extern_sheet;
92     }
93
94     public void setExternSheetIndex(short index){
95         field_1_index_extern_sheet = index;
96     }
97
98     public short getRow() {
99         return field_2_row;
100     }
101
102     public void setRow(short row) {
103         field_2_row = row;
104     }
105
106     public short getColumn() {
107         return ( short ) (field_3_column & 0xFF);
108     }
109
110     public short getColumnRaw() {
111         return field_3_column;
112     }
113
114      public boolean isRowRelative()
115     {
116         return rowRelative.isSet(field_3_column);
117     }
118     
119     public void setRowRelative(boolean rel) {
120         field_3_column=rowRelative.setShortBoolean(field_3_column,rel);
121     }
122     
123     public boolean isColRelative()
124     {
125         return colRelative.isSet(field_3_column);
126     }
127     
128     public void setColRelative(boolean rel) {
129         field_3_column=colRelative.setShortBoolean(field_3_column,rel);
130     }
131     public void setColumn(short column) {
132         field_3_column &= 0xFF00;
133         field_3_column |= column & 0xFF;
134     }
135
136     public void setColumnRaw(short column) {
137         field_3_column = column;
138     }
139
140    /* public String getArea(){
141         RangeAddress ra = new RangeAddress("");
142
143         String result = (ra.numTo26Sys(getColumn()) + (getRow() + 1));
144
145         return result;
146     }*/

147
148     public void setArea(String JavaDoc ref){
149         RangeAddress ra = new RangeAddress(ref);
150
151         String JavaDoc from = ra.getFromCell();
152
153         setColumn((short) (ra.getXPosition(from) -1));
154         setRow((short) (ra.getYPosition(from) -1));
155
156     }
157
158     public String JavaDoc toFormulaString(Workbook book) {
159         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
160         SheetReferences refs = book == null ? null : book.getSheetReferences();
161         if (refs != null) {
162             retval.append(refs.getSheetName((int)this.field_1_index_extern_sheet));
163             retval.append('!');
164         }
165         retval.append((new CellReference(getRow(),getColumn(),!isRowRelative(),!isColRelative())).toString());
166         return retval.toString();
167     }
168
169    public byte getDefaultOperandClass() {return Ptg.CLASS_REF;}
170
171    public Object JavaDoc clone() {
172      Ref3DPtg ptg = new Ref3DPtg();
173      ptg.field_1_index_extern_sheet = field_1_index_extern_sheet;
174      ptg.field_2_row = field_2_row;
175      ptg.field_3_column = field_3_column;
176      ptg.setClass(ptgClass);
177      return ptg;
178    }
179
180 }
181
Popular Tags