KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > BoolErrRecord


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

17         
18
19 /*
20  * BoolErrRecord.java
21  *
22  * Created on January 19, 2002, 9:30 AM
23  */

24 package org.apache.poi.hssf.record;
25
26 import org.apache.poi.util.LittleEndian;
27
28 /**
29  * Creates new BoolErrRecord. <P>
30  * REFERENCE: PG ??? Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
31  * @author Michael P. Harhen
32  * @author Jason Height (jheight at chariot dot net dot au)
33  * @version 2.0-pre
34  */

35
36 public class BoolErrRecord
37     extends Record
38     implements CellValueRecordInterface, Comparable JavaDoc
39 {
40     public final static short sid = 0x205;
41     //private short field_1_row;
42
private int field_1_row;
43     private short field_2_column;
44     private short field_3_xf_index;
45     private byte field_4_bBoolErr;
46     private byte field_5_fError;
47
48     /** Creates new BoolErrRecord */
49
50     public BoolErrRecord()
51     {
52     }
53
54     /**
55      * Constructs a BoolErr record and sets its fields appropriately.
56      *
57      * @param id id must be 0x205 or an exception will be throw upon validation
58      * @param size the size of the data area of the record
59      * @param data data of the record (should not contain sid/len)
60      */

61
62     public BoolErrRecord(short id, short size, byte [] data)
63     {
64         super(id, size, data);
65     }
66
67     /**
68      * Constructs a BoolErr record and sets its fields appropriately.
69      *
70      * @param id id must be 0x205 or an exception will be throw upon validation
71      * @param size the size of the data area of the record
72      * @param data data of the record (should not contain sid/len)
73      * @param offset of the record
74      */

75
76     public BoolErrRecord(short id, short size, byte [] data, int offset)
77     {
78         super(id, size, data, offset);
79     }
80
81     /**
82      * called by the constructor, should set class level fields. Should throw
83      * runtime exception for bad/icomplete data.
84      *
85      * @param data raw data
86      * @param size size of data
87      */

88
89     protected void fillFields(byte [] data, short size, int offset)
90     {
91         //field_1_row = LittleEndian.getShort(data, 0 + offset);
92
field_1_row = LittleEndian.getUShort(data, 0 + offset);
93         field_2_column = LittleEndian.getShort(data, 2 + offset);
94         field_3_xf_index = LittleEndian.getShort(data, 4 + offset);
95         field_4_bBoolErr = data[ 6 + offset ];
96         field_5_fError = data[ 7 + offset ];
97     }
98
99     //public void setRow(short row)
100
public void setRow(int row)
101     {
102         field_1_row = row;
103     }
104
105     public void setColumn(short col)
106     {
107         field_2_column = col;
108     }
109
110     /**
111      * set the index to the ExtendedFormat
112      * @see org.apache.poi.hssf.record.ExtendedFormatRecord
113      * @param xf index to the XF record
114      */

115
116     public void setXFIndex(short xf)
117     {
118         field_3_xf_index = xf;
119     }
120
121     /**
122      * set the boolean value for the cell
123      *
124      * @param value representing the boolean value
125      */

126
127     public void setValue(boolean value)
128     {
129         field_4_bBoolErr = value ? ( byte ) 1
130                                  : ( byte ) 0;
131         field_5_fError = ( byte ) 0;
132     }
133
134     /**
135      * set the error value for the cell
136      *
137      * @param value error representing the error value
138      * this value can only be 0,7,15,23,29,36 or 42
139      * see bugzilla bug 16560 for an explanation
140      */

141
142     public void setValue(byte value)
143     {
144         if ( (value==0)||(value==7)||(value==15)||(value==23)||(value==29)||(value==36)||(value==42)) {
145             field_4_bBoolErr = value;
146             field_5_fError = ( byte ) 1;
147         } else {
148             throw new RuntimeException JavaDoc("Error Value can only be 0,7,15,23,29,36 or 42. It cannot be "+value);
149         }
150     }
151
152     //public short getRow()
153
public int getRow()
154     {
155         return field_1_row;
156     }
157
158     public short getColumn()
159     {
160         return field_2_column;
161     }
162
163     /**
164      * get the index to the ExtendedFormat
165      * @see org.apache.poi.hssf.record.ExtendedFormatRecord
166      * @return index to the XF record
167      */

168
169     public short getXFIndex()
170     {
171         return field_3_xf_index;
172     }
173
174     /**
175      * get the value for the cell
176      *
177      * @return boolean representing the boolean value
178      */

179
180     public boolean getBooleanValue()
181     {
182         return (field_4_bBoolErr != 0);
183     }
184
185     /**
186      * get the error value for the cell
187      *
188      * @return byte representing the error value
189      */

190
191     public byte getErrorValue()
192     {
193         return field_4_bBoolErr;
194     }
195
196     /**
197      * Indicates whether the call holds a boolean value
198      *
199      * @return boolean true if the cell holds a boolean value
200      */

201
202     public boolean isBoolean()
203     {
204         return (field_5_fError == ( byte ) 0);
205     }
206
207     /**
208      * manually indicate this is an error rather than a boolean
209      */

210     public void setError(boolean val) {
211         field_5_fError = (byte) (val == false ? 0 : 1);
212     }
213
214     /**
215      * Indicates whether the call holds an error value
216      *
217      * @return boolean true if the cell holds an error value
218      */

219
220     public boolean isError()
221     {
222         return (field_5_fError != ( byte ) 0);
223     }
224
225     public String JavaDoc toString()
226     {
227         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
228
229         buffer.append("[BOOLERR]\n");
230         buffer.append(" .row = ")
231             .append(Integer.toHexString(getRow())).append("\n");
232         buffer.append(" .col = ")
233             .append(Integer.toHexString(getColumn())).append("\n");
234         buffer.append(" .xfindex = ")
235             .append(Integer.toHexString(getXFIndex())).append("\n");
236         if (isBoolean())
237         {
238             buffer.append(" .booleanValue = ").append(getBooleanValue())
239                 .append("\n");
240         }
241         else
242         {
243             buffer.append(" .errorValue = ").append(getErrorValue())
244                 .append("\n");
245         }
246         buffer.append("[/BOOLERR]\n");
247         return buffer.toString();
248     }
249
250     /**
251      * called by the class that is responsible for writing this sucker.
252      * Subclasses should implement this so that their data is passed back in a
253      * byte array.
254      *
255      * @return byte array containing instance data
256      */

257
258     public int serialize(int offset, byte [] data)
259     {
260         LittleEndian.putShort(data, 0 + offset, sid);
261         LittleEndian.putShort(data, 2 + offset, ( short ) 8);
262         //LittleEndian.putShort(data, 4 + offset, getRow());
263
LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
264         LittleEndian.putShort(data, 6 + offset, getColumn());
265         LittleEndian.putShort(data, 8 + offset, getXFIndex());
266         data[ 10 + offset ] = field_4_bBoolErr;
267         data[ 11 + offset ] = field_5_fError;
268         return getRecordSize();
269     }
270
271     public int getRecordSize()
272     {
273         return 12;
274     }
275
276     /**
277      * called by constructor, should throw runtime exception in the event of a
278      * record passed with a differing ID.
279      *
280      * @param id alleged id for this record
281      */

282
283     protected void validateSid(short id)
284     {
285         if (id != this.sid)
286         {
287             throw new RecordFormatException("Not a valid BoolErrRecord");
288         }
289     }
290
291     public short getSid()
292     {
293         return this.sid;
294     }
295
296     public boolean isBefore(CellValueRecordInterface i)
297     {
298         if (this.getRow() > i.getRow())
299         {
300             return false;
301         }
302         if ((this.getRow() == i.getRow())
303                 && (this.getColumn() > i.getColumn()))
304         {
305             return false;
306         }
307         if ((this.getRow() == i.getRow())
308                 && (this.getColumn() == i.getColumn()))
309         {
310             return false;
311         }
312         return true;
313     }
314
315     public boolean isAfter(CellValueRecordInterface i)
316     {
317         if (this.getRow() < i.getRow())
318         {
319             return false;
320         }
321         if ((this.getRow() == i.getRow())
322                 && (this.getColumn() < i.getColumn()))
323         {
324             return false;
325         }
326         if ((this.getRow() == i.getRow())
327                 && (this.getColumn() == i.getColumn()))
328         {
329             return false;
330         }
331         return true;
332     }
333
334     public boolean isEqual(CellValueRecordInterface i)
335     {
336         return ((this.getRow() == i.getRow())
337                 && (this.getColumn() == i.getColumn()));
338     }
339
340     public boolean isInValueSection()
341     {
342         return true;
343     }
344
345     public boolean isValue()
346     {
347         return true;
348     }
349
350     public int compareTo(Object JavaDoc obj)
351     {
352         CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
353
354         if ((this.getRow() == loc.getRow())
355                 && (this.getColumn() == loc.getColumn()))
356         {
357             return 0;
358         }
359         if (this.getRow() < loc.getRow())
360         {
361             return -1;
362         }
363         if (this.getRow() > loc.getRow())
364         {
365             return 1;
366         }
367         if (this.getColumn() < loc.getColumn())
368         {
369             return -1;
370         }
371         if (this.getColumn() > loc.getColumn())
372         {
373             return 1;
374         }
375         return -1;
376     }
377
378     public boolean equals(Object JavaDoc obj)
379     {
380         if (!(obj instanceof CellValueRecordInterface))
381         {
382             return false;
383         }
384         CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
385
386         if ((this.getRow() == loc.getRow())
387                 && (this.getColumn() == loc.getColumn()))
388         {
389             return true;
390         }
391         return false;
392     }
393
394     public Object JavaDoc clone() {
395       BoolErrRecord rec = new BoolErrRecord();
396       rec.field_1_row = field_1_row;
397       rec.field_2_column = field_2_column;
398       rec.field_3_xf_index = field_3_xf_index;
399       rec.field_4_bBoolErr = field_4_bBoolErr;
400       rec.field_5_fError = field_5_fError;
401       return rec;
402     }
403 }
404
Popular Tags