KickJava   Java API By Example, From Geeks To Geeks.

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


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.WritableRecordData;
27 import jxl.biff.IntegerHelper;
28
29 /**
30  * An external sheet record, used to maintain integrity when formulas
31  * are copied from read databases
32  */

33 class ExternalSheetRecord extends WritableRecordData
34 {
35   /**
36    * The underlying external sheet data
37    */

38   private byte[] data;
39
40   /**
41    * The list of XTI structures
42    */

43   private ArrayList JavaDoc xtis;
44
45   /**
46    * An XTI structure
47    */

48   private static class XTI
49   {
50     int supbookIndex;
51     int firstTab;
52     int lastTab;
53
54     XTI(int s, int f, int l)
55     {
56       supbookIndex = s;
57       firstTab = f;
58       lastTab = l;
59     }
60
61     void sheetInserted(int index)
62     {
63       if (firstTab >= index)
64       {
65         firstTab++;
66       }
67
68       if (lastTab >= index)
69       {
70         lastTab++;
71       }
72     }
73
74     void sheetRemoved(int index)
75     {
76       if (firstTab == index)
77       {
78         firstTab = 0;
79       }
80
81       if (lastTab == index)
82       {
83         lastTab = 0;
84       }
85
86       if (firstTab > index)
87       {
88         firstTab--;
89       }
90
91       if (lastTab > index)
92       {
93         lastTab--;
94       }
95     }
96   }
97
98   /**
99    * Constructor
100    *
101    * @param esf the external sheet record to copy
102    */

103   public ExternalSheetRecord(jxl.read.biff.ExternalSheetRecord esf)
104   {
105     super(Type.EXTERNSHEET);
106
107     xtis = new ArrayList JavaDoc(esf.getNumRecords());
108     XTI xti = null;
109     for (int i = 0 ; i < esf.getNumRecords(); i++)
110     {
111       xti = new XTI(esf.getSupbookIndex(i),
112                     esf.getFirstTabIndex(i),
113                     esf.getLastTabIndex(i));
114       xtis.add(xti);
115     }
116   }
117
118   /**
119    * Constructor used for writable workbooks
120    */

121   public ExternalSheetRecord()
122   {
123     super(Type.EXTERNSHEET);
124     xtis = new ArrayList JavaDoc();
125   }
126
127   /**
128    * Gets the extern sheet index for the specified parameters, creating
129    * a new xti record if necessary
130    * @param supbookind the internal supbook reference
131    * @param sheetind the sheet index
132    */

133   int getIndex(int supbookind, int sheetind)
134   {
135     Iterator JavaDoc i = xtis.iterator();
136     XTI xti = null;
137     boolean found = false;
138     int pos = 0;
139     while (i.hasNext() && !found)
140     {
141       xti = (XTI) i.next();
142
143       if (xti.supbookIndex == supbookind &&
144           xti.firstTab == sheetind)
145       {
146         found = true;
147       }
148       else
149       {
150         pos++;
151       }
152     }
153
154     if (!found)
155     {
156       xti = new XTI(supbookind, sheetind, sheetind);
157       xtis.add(xti);
158       pos = xtis.size() - 1;
159     }
160
161     return pos;
162   }
163
164   /**
165    * Gets the binary data for output to file
166    *
167    * @return the binary data
168    */

169   public byte[] getData()
170   {
171     byte[] data = new byte[2 + xtis.size() * 6];
172
173     int pos = 0;
174     IntegerHelper.getTwoBytes(xtis.size(), data, 0);
175     pos += 2;
176
177     Iterator JavaDoc i = xtis.iterator();
178     XTI xti = null;
179     while (i.hasNext())
180     {
181       xti = (XTI) i.next();
182       IntegerHelper.getTwoBytes(xti.supbookIndex, data, pos);
183       IntegerHelper.getTwoBytes(xti.firstTab, data, pos+2);
184       IntegerHelper.getTwoBytes(xti.lastTab, data, pos+4);
185       pos +=6 ;
186     }
187   
188     return data;
189   }
190
191   /**
192    * Gets the supbook index for the specified external sheet
193    *
194    * @param the index of the supbook record
195    * @return the supbook index
196    */

197   public int getSupbookIndex(int index)
198   {
199     return ((XTI) xtis.get(index)).supbookIndex;
200   }
201
202   /**
203    * Gets the first tab index for the specified external sheet
204    *
205    * @param the index of the supbook record
206    * @return the first tab index
207    */

208   public int getFirstTabIndex(int index)
209   {
210     return ((XTI) xtis.get(index)).firstTab;
211   }
212
213   /**
214    * Gets the last tab index for the specified external sheet
215    *
216    * @param the index of the supbook record
217    * @return the last tab index
218    */

219   public int getLastTabIndex(int index)
220   {
221     return ((XTI) xtis.get(index)).lastTab;
222   }
223
224   /**
225    * Called when a sheet has been inserted via the API
226    * @param the position of the insertion
227    */

228   void sheetInserted(int index)
229   {
230     XTI xti = null;
231     for (Iterator JavaDoc i = xtis.iterator(); i.hasNext() ; )
232     {
233       xti = (XTI) i.next();
234       xti.sheetInserted(index);
235     }
236   }
237
238   /**
239    * Called when a sheet has been removed via the API
240    * @param the position of the insertion
241    */

242   void sheetRemoved(int index)
243   {
244     XTI xti = null;
245     for (Iterator JavaDoc i = xtis.iterator(); i.hasNext() ; )
246     {
247       xti = (XTI) i.next();
248       xti.sheetRemoved(index);
249     }
250   }
251
252 }
253
Popular Tags