KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > DBCellRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.write.biff;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import jxl.biff.Type;
26 import jxl.biff.IntegerHelper;
27 import jxl.biff.WritableRecordData;
28
29 /**
30  * Indexes the first row record of the block and each individual cell.
31  * This is invoked by the sheets write process
32  */

33 class DBCellRecord extends WritableRecordData
34 {
35   /**
36    * The file position of the first Row record in this block
37    */

38   private int rowPos;
39
40   /**
41    * The position of the start of the next cell after the first row. This
42    * is used as the offset for the cell positions
43    */

44   private int cellOffset;
45
46   /**
47    * The list of all cell positions in this block
48    */

49   private ArrayList JavaDoc cellRowPositions;
50
51   /**
52    * The position of this record in the file. Vital for calculating offsets
53    */

54   private int position;
55
56   /**
57    * Constructor
58    *
59    * @param rp the position of this row
60    */

61   public DBCellRecord(int rp)
62   {
63     super(Type.DBCELL);
64     rowPos = rp;
65     cellRowPositions = new ArrayList JavaDoc(10);
66   }
67
68   /**
69    * Sets the offset of this cell record within the sheet stream
70    *
71    * @param pos the offset
72    */

73   void setCellOffset(int pos)
74   {
75     cellOffset = pos;
76   }
77
78   /**
79    * Adds a cell
80    *
81    * @param pos
82    */

83   void addCellRowPosition(int pos)
84   {
85     cellRowPositions.add(new Integer JavaDoc(pos));
86   }
87
88   /**
89    * Sets the position of this cell within the sheet stream
90    *
91    * @param pos the position
92    */

93   void setPosition(int pos)
94   {
95     position = pos;
96   }
97
98   /**
99    * Gets the binary data for this cell record
100    *
101    * @return the binary data
102    */

103   protected byte[] getData()
104   {
105     byte[] data = new byte[4 + 2 * cellRowPositions.size()];
106
107     // Set the offset to the first row
108
IntegerHelper.getFourBytes(position - rowPos, data, 0);
109
110     // Now add in all the cell offsets
111
int pos = 4;
112     int lastCellPos = cellOffset;
113     Iterator JavaDoc i = cellRowPositions.iterator();
114     while (i.hasNext())
115     {
116       int cellPos = ((Integer JavaDoc) i.next()).intValue();
117       IntegerHelper.getTwoBytes(cellPos - lastCellPos, data, pos);
118       lastCellPos = cellPos;
119       pos += 2;
120     }
121
122     return data;
123   }
124 }
125
Popular Tags