KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*********************************************************************
2 *
3 * Copyright (C) 2001 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 common.Assert;
23 import common.Logger;
24
25 import jxl.CellType;
26 import jxl.LabelCell;
27 import jxl.format.CellFormat;
28 import jxl.biff.Type;
29 import jxl.biff.StringHelper;
30 import jxl.biff.IntegerHelper;
31 import jxl.biff.FormattingRecords;
32 import jxl.biff.CellReferenceHelper;
33
34 /**
35  * A label record, used for writing out string
36  */

37 public abstract class LabelRecord extends CellValue
38 {
39   /**
40    * The logger
41    */

42   private static Logger logger = Logger.getLogger(LabelRecord.class);
43
44   /**
45    * The string
46    */

47   private String JavaDoc contents;
48
49   /**
50    * A handle to the shared strings used within this workbook
51    */

52   private SharedStrings sharedStrings;
53
54   /**
55    * The index of the string in the shared string table
56    */

57   private int index;
58
59   /**
60    * Constructor used when creating a label from the user API
61    *
62    * @param c the column
63    * @param cont the contents
64    * @param r the row
65    */

66   protected LabelRecord(int c, int r, String JavaDoc cont)
67   {
68     super(Type.LABELSST, c, r);
69     contents = cont;
70     if (contents == null)
71     {
72       contents="";
73     }
74   }
75
76   /**
77    * Constructor used when creating a label from the API. This is
78    * overloaded to allow formatting information to be passed to the record
79    *
80    * @param c the column
81    * @param cont the contents
82    * @param r the row
83    * @param st the format applied to the cell
84    */

85   protected LabelRecord(int c, int r, String JavaDoc cont, CellFormat st)
86   {
87     super(Type.LABELSST, c, r, st);
88     contents = cont;
89
90     if (contents == null)
91     {
92       contents="";
93     }
94   }
95
96
97   /**
98    * Copy constructor
99    *
100    * @param c the column
101    * @param r the row
102    * @param nr the record to copy
103    */

104   protected LabelRecord(int c, int r, LabelRecord lr)
105   {
106     super(Type.LABELSST, c, r, lr);
107     contents = lr.contents;
108   }
109
110   /**
111    * Constructor used when copying a label from a read only
112    * spreadsheet
113    *
114    * @param lc the label to copy
115    */

116   protected LabelRecord(LabelCell lc)
117   {
118     super(Type.LABELSST, lc);
119     contents = lc.getString();
120     if (contents == null)
121     {
122       contents="";
123     }
124   }
125
126   /**
127    * Returns the content type of this cell
128    *
129    * @return the content type for this cell
130    */

131   public CellType getType()
132   {
133     return CellType.LABEL;
134   }
135
136   /**
137    * Gets the binary data for output to file
138    *
139    * @return the binary data
140    */

141   public byte[] getData()
142   {
143     byte[] celldata = super.getData();
144     byte[] data = new byte[celldata.length + 4];
145     System.arraycopy(celldata, 0, data, 0, celldata.length);
146     IntegerHelper.getFourBytes(index, data, celldata.length);
147
148     return data;
149   }
150
151   /**
152    * Quick and dirty function to return the contents of this cell as a string.
153    * For more complex manipulation of the contents, it is necessary to cast
154    * this interface to correct subinterface
155    *
156    * @return the contents of this cell as a string
157    */

158   public String JavaDoc getContents()
159   {
160     return contents;
161   }
162
163   /**
164    * Gets the label for this cell. The value returned will be the same
165    * as for the getContents method in the base class
166    *
167    * @return the cell contents
168    */

169   public String JavaDoc getString()
170   {
171     return contents;
172   }
173
174   /**
175    * Sets the string contents of this cell
176    *
177    * @param s the new string contents
178    */

179   protected void setString(String JavaDoc s)
180   {
181     if (s == null)
182     {
183       s = "";
184     }
185
186     contents = s;
187
188     // Don't bother doing anything if this cell has not been referenced
189
// yet - everything will be set up in due course
190
if (!isReferenced())
191     {
192       return;
193     }
194
195     Assert.verify(sharedStrings != null);
196
197     // Initalize the shared string index
198
index = sharedStrings.getIndex(contents);
199
200     // Use the sharedStrings reference instead of this object's own
201
// handle - this means that the bespoke copy becomes eligible for
202
// garbage collection
203
contents = sharedStrings.get(index);
204   }
205
206   /**
207    * Overrides the method in the base class in order to add the string
208    * content to the shared string table, and to store its shared string
209    * index
210    *
211    * @param fr the formatting records
212    * @param ss the shared strings used within the workbook
213    * @param s
214    */

215   void setCellDetails(FormattingRecords fr, SharedStrings ss,
216                       WritableSheetImpl s)
217   {
218     super.setCellDetails(fr, ss, s);
219
220     sharedStrings = ss;
221
222     index = sharedStrings.getIndex(contents);
223
224     // Use the sharedStrings reference instead of this object's own
225
// handle - this means that the bespoke copy becomes eligible for
226
// garbage collection
227
contents = sharedStrings.get(index);
228   }
229
230 }
231
232
233
234
Popular Tags