KickJava   Java API By Example, From Geeks To Geeks.

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


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.hssf.model.Workbook;
21 import org.apache.poi.hssf.record.NameRecord;
22
23 /**
24  *
25  * @author andy
26  * @author Jason Height (jheight at chariot dot net dot au)
27  */

28
29 public class NamePtg
30     extends Ptg
31 {
32     public final static short sid = 0x23;
33     private final static int SIZE = 5;
34     private short field_1_label_index;
35     private short field_2_zero; // reserved must be 0
36
boolean xtra=false;
37
38
39     private NamePtg() {
40       //Required for clone methods
41
}
42
43     /** Creates new NamePtg */
44
45     public NamePtg(String JavaDoc name, Workbook book)
46     {
47         final short n = (short) (book.getNumNames() + 1);
48         NameRecord rec;
49         for (short i = 1; i < n; i++) {
50             rec = book.getNameRecord(i - 1);
51             if (name.equals(rec.getNameText())) {
52                 field_1_label_index = i;
53                 return;
54             }
55         }
56         rec = new NameRecord();
57         rec.setNameText(name);
58         rec.setNameTextLength((byte) name.length());
59         book.addName(rec);
60         field_1_label_index = n;
61     }
62
63     /** Creates new NamePtg */
64
65     public NamePtg(byte [] data, int offset)
66     {
67         offset++;
68         //field_1_ixti = LittleEndian.getShort(data, offset);
69
field_1_label_index = LittleEndian.getShort(data, offset );
70         field_2_zero = LittleEndian.getShort(data, offset + 2);
71         //if (data[offset+6]==0) xtra=true;
72
}
73
74     public void writeBytes(byte [] array, int offset)
75     {
76         array[offset+0]= (byte) (sid + ptgClass);
77         LittleEndian.putShort(array,offset+1,field_1_label_index);
78         LittleEndian.putShort(array,offset+3, field_2_zero);
79     }
80
81     public int getSize()
82     {
83         return SIZE;
84     }
85
86     public String JavaDoc toFormulaString(Workbook book)
87     {
88         NameRecord rec = book.getNameRecord(field_1_label_index - 1);
89         return rec.getNameText();
90     }
91     
92     public byte getDefaultOperandClass() {return Ptg.CLASS_REF;}
93
94     public Object JavaDoc clone() {
95       NamePtg ptg = new NamePtg();
96       ptg.field_1_label_index = field_1_label_index;
97       ptg.field_2_zero = field_2_zero;
98       return ptg;
99     }
100 }
101
Popular Tags