KickJava   Java API By Example, From Geeks To Geeks.

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


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.hssf.model.Workbook;
21
22 import org.apache.poi.util.LittleEndian;
23 import org.apache.poi.util.BitField;
24
25 import java.util.List JavaDoc;
26
27 /**
28  * "Special Attributes"
29  * This seems to be a Misc Stuff and Junk record. One function it serves is
30  * in SUM functions (i.e. SUM(A1:A3) causes an area PTG then an ATTR with the SUM option set)
31  * @author andy
32  * @author Jason Height (jheight at chariot dot net dot au)
33  */

34
35 public class AttrPtg
36     extends OperationPtg
37 {
38     public final static byte sid = 0x19;
39     private final static int SIZE = 4;
40     private byte field_1_options;
41     private short field_2_data;
42     private BitField semiVolatile = new BitField(0x01);
43     private BitField optiIf = new BitField(0x02);
44     private BitField optiChoose = new BitField(0x04);
45     private BitField optGoto = new BitField(0x08);
46     private BitField sum = new BitField(0x10);
47     private BitField baxcel = new BitField(0x20);
48     private BitField space = new BitField(0x40);
49
50     public AttrPtg() {
51     }
52     
53     public AttrPtg(byte [] data, int offset)
54     {
55         offset++; // adjust past id
56
field_1_options = data[ offset + 0 ];
57         field_2_data = LittleEndian.getShort(data, offset + 1);
58     }
59
60     public void setOptions(byte options)
61     {
62         field_1_options = options;
63     }
64
65     public byte getOptions()
66     {
67         return field_1_options;
68     }
69
70     public boolean isSemiVolatile()
71     {
72         return semiVolatile.isSet(getOptions());
73     }
74
75     public boolean isOptimizedIf()
76     {
77         return optiIf.isSet(getOptions());
78     }
79
80     public boolean isOptimizedChoose()
81     {
82         return optiChoose.isSet(getOptions());
83     }
84
85     // lets hope no one uses this anymore
86
public boolean isGoto()
87     {
88         return optGoto.isSet(getOptions());
89     }
90
91     public boolean isSum()
92     {
93         return sum.isSet(getOptions());
94     }
95     
96     public void setSum(boolean bsum) {
97         field_1_options=sum.setByteBoolean(field_1_options,bsum);
98     }
99
100     public void setOptimizedIf(boolean bif) {
101         field_1_options=optiIf.setByteBoolean(field_1_options,bif);
102     }
103
104     /**
105      * Flags this ptg as a goto/jump
106      * @param isGoto
107      */

108     public void setGoto(boolean isGoto) {
109         field_1_options=optGoto.setByteBoolean(field_1_options, isGoto);
110     }
111     
112     // lets hope no one uses this anymore
113
public boolean isBaxcel()
114     {
115         return baxcel.isSet(getOptions());
116     }
117
118     // biff3&4 only shouldn't happen anymore
119
public boolean isSpace()
120     {
121         return space.isSet(getOptions());
122     }
123
124     public void setData(short data)
125     {
126         field_2_data = data;
127     }
128
129     public short getData()
130     {
131         return field_2_data;
132     }
133
134     public String JavaDoc toString()
135     {
136         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
137
138         buffer.append("AttrPtg\n");
139         buffer.append("options=").append(field_1_options).append("\n");
140         buffer.append("data =").append(field_2_data).append("\n");
141         buffer.append("semi =").append(isSemiVolatile()).append("\n");
142         buffer.append("optimif=").append(isOptimizedIf()).append("\n");
143         buffer.append("optchos=").append(isOptimizedChoose()).append("\n");
144         buffer.append("isGoto =").append(isGoto()).append("\n");
145         buffer.append("isSum =").append(isSum()).append("\n");
146         buffer.append("isBaxce=").append(isBaxcel()).append("\n");
147         buffer.append("isSpace=").append(isSpace()).append("\n");
148         return buffer.toString();
149     }
150
151     public void writeBytes(byte [] array, int offset)
152     {
153         array[offset]=sid;
154         array[offset+1]=field_1_options;
155         LittleEndian.putShort(array,offset+2,field_2_data);
156     }
157
158     public int getSize()
159     {
160         return SIZE;
161     }
162
163     public String JavaDoc toFormulaString(String JavaDoc[] operands) {
164         if(space.isSet(field_1_options)) {
165             return operands[ 0 ];
166         } else if (optiIf.isSet(field_1_options)) {
167             return toFormulaString((Workbook)null) + "(" + operands[ 0 ] +")";
168         } else if (optGoto.isSet(field_1_options)) {
169             return toFormulaString((Workbook)null) + operands[0]; //goto isn't a real formula element should not show up
170
} else {
171             return toFormulaString((Workbook)null) + "(" + operands[ 0 ] + ")";
172         }
173     }
174   
175
176     public int getNumberOfOperands()
177     {
178         return 1;
179     }
180
181     public int getType()
182     {
183         return -1;
184     }
185         
186    public String JavaDoc toFormulaString(Workbook book) {
187       if(semiVolatile.isSet(field_1_options)) {
188         return "ATTR(semiVolatile)";
189       }
190       if(optiIf.isSet(field_1_options)) {
191         return "IF";
192       }
193       if( optiChoose.isSet(field_1_options)) {
194         return "CHOOSE";
195       }
196       if(optGoto.isSet(field_1_options)) {
197         return "";
198       }
199       if(sum.isSet(field_1_options)) {
200         return "SUM";
201       }
202       if(baxcel.isSet(field_1_options)) {
203         return "ATTR(baxcel)";
204       }
205       if(space.isSet(field_1_options)) {
206         return "";
207       }
208       return "UNKNOWN ATTRIBUTE";
209      }
210     
211     
212  
213     public byte getDefaultOperandClass() {return Ptg.CLASS_VALUE;}
214
215     public Object JavaDoc clone() {
216       AttrPtg ptg = new AttrPtg();
217       ptg.field_1_options = field_1_options;
218       ptg.field_2_data = field_2_data;
219       return ptg;
220     }
221 }
222
Popular Tags