KickJava   Java API By Example, From Geeks To Geeks.

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


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.Cell;
26 import jxl.Sheet;
27 import jxl.CellFeatures;
28 import jxl.biff.Type;
29 import jxl.biff.IntegerHelper;
30 import jxl.biff.WritableRecordData;
31 import jxl.biff.XFRecord;
32 import jxl.biff.FormattingRecords;
33 import jxl.biff.NumFormatRecordsException;
34 import jxl.biff.drawing.Comment;
35 import jxl.write.WritableCell;
36 import jxl.write.WritableWorkbook;
37 import jxl.write.WritableCellFeatures;
38 import jxl.write.WritableCellFormat;
39 import jxl.format.CellFormat;
40 import jxl.format.Alignment;
41 import jxl.format.Border;
42 import jxl.format.BorderLineStyle;
43 import jxl.format.Font;
44 import jxl.format.VerticalAlignment;
45
46 /**
47  * Abstract class which stores the common data used for cells, such
48  * as row, column and formatting information.
49  * Any record which directly represents the contents of a cell, such
50  * as labels and numbers, are derived from this class
51  * data store
52  */

53 public abstract class CellValue extends WritableRecordData
54   implements WritableCell
55 {
56   /**
57    * The logger
58    */

59   private static Logger logger = Logger.getLogger(CellValue.class);
60   
61   /**
62    * The row in the worksheet at which this cell is located
63    */

64   private int row;
65   /**
66    * The column in the worksheet at which this cell is located
67    */

68   private int column;
69   /**
70    * The format applied to this cell
71    */

72   private XFRecord format;
73   
74   /**
75    * A handle to the formatting records, used in case we want
76    * to change the format of the cell once it has been added
77    * to the spreadsheet
78    */

79   private FormattingRecords formattingRecords;
80
81   /**
82    * A flag to indicate that this record is already referenced within
83    * a worksheet
84    */

85   private boolean referenced;
86
87   /**
88    * A handle to the sheet
89    */

90   private WritableSheetImpl sheet;
91
92   /**
93    * The cell features
94    */

95   private WritableCellFeatures features;
96
97   /**
98    * Internal copied flag, to prevent cell features being added multiple
99    * times to the drawing array
100    */

101   private boolean copied;
102
103   /**
104    * Constructor used when building writable cells from the Java API
105    *
106    * @param c the column
107    * @param t the type indicator
108    * @param r the row
109    */

110   protected CellValue(Type t, int c, int r)
111   {
112     this(t, c, r, WritableWorkbookImpl.NORMAL_STYLE);
113     copied = false;
114   }
115
116   /**
117    * Constructor used when creating a writable cell from a read-only cell
118    * (when copying a workbook)
119    *
120    * @param c the cell to clone
121    * @param t the type of this cell
122    */

123   protected CellValue(Type t, Cell c)
124   {
125     this(t, c.getColumn(), c.getRow());
126     copied = true;
127
128     format = (XFRecord) c.getCellFormat();
129     
130     if (c.getCellFeatures() != null)
131     {
132       features = new WritableCellFeatures(c.getCellFeatures());
133     }
134   }
135
136   /**
137    * Overloaded constructor used when building writable cells from the
138    * Java API which also takes a format
139    *
140    * @param c the column
141    * @param t the cell type
142    * @param r the row
143    * @param st the format to apply to this cell
144    */

145   protected CellValue(Type t, int c, int r, CellFormat st)
146   {
147     super(t);
148     row = r;
149     column = c;
150     format = (XFRecord) st;
151     referenced = false;
152     copied = false;
153   }
154
155   /**
156    * Copy constructor
157    *
158    * @param c the column
159    * @param t the cell type
160    * @param r the row
161    * @param cv the value to copy
162    */

163   protected CellValue(Type t, int c, int r, CellValue cv)
164   {
165     super(t);
166     row = r;
167     column = c;
168     format = cv.format;
169     referenced = false;
170     copied = false; // used during a deep copy, so we want to add cell
171
// features again
172

173     if (cv.features != null)
174     {
175       features = new WritableCellFeatures(cv.features);
176     }
177   }
178
179   /**
180    * An API function which sets the format to apply to this cell
181    *
182    * @param cf the format to apply to this cell
183    */

184   public void setCellFormat(CellFormat cf)
185   {
186     format = (XFRecord) cf;
187
188     // If the referenced flag has not been set, this cell has not
189
// been added to the spreadsheet, so we don't need to perform
190
// any further logic
191
if (!referenced)
192     {
193       return;
194     }
195
196     // The cell has already been added to the spreadsheet, so the
197
// formattingRecords reference must be initialized
198
Assert.verify(formattingRecords != null);
199
200     addCellFormat();
201   }
202
203   /**
204    * Returns the row number of this cell
205    *
206    * @return the row number of this cell
207    */

208   public int getRow()
209   {
210     return row;
211   }
212
213   /**
214    * Returns the column number of this cell
215    *
216    * @return the column number of this cell
217    */

218   public int getColumn()
219   {
220     return column;
221   }
222
223   /**
224    * Indicates whether or not this cell is hidden, by virtue of either
225    * the entire row or column being collapsed
226    *
227    * @return TRUE if this cell is hidden, FALSE otherwise
228    */

229   public boolean isHidden()
230   {
231     ColumnInfoRecord cir = sheet.getColumnInfo(column);
232     
233     if (cir != null && cir.getWidth() == 0)
234     {
235       return true;
236     }
237
238     RowRecord rr = sheet.getRowInfo(row);
239
240     if (rr != null && (rr.getRowHeight() == 0 || rr.isCollapsed()))
241     {
242       return true;
243     }
244
245     return false;
246   }
247
248   /**
249    * Gets the data to write to the output file
250    *
251    * @return the binary data
252    */

253   public byte[] getData()
254   {
255     byte[] mydata = new byte[6];
256     IntegerHelper.getTwoBytes(row, mydata, 0);
257     IntegerHelper.getTwoBytes(column, mydata, 2);
258     IntegerHelper.getTwoBytes(format.getXFIndex(), mydata, 4);
259     return mydata;
260   }
261
262   /**
263    * Called when the cell is added to the worksheet in order to indicate
264    * that this object is already added to the worksheet
265    * This method also verifies that the associated formats and formats
266    * have been initialized correctly
267    *
268    * @param fr the formatting records
269    * @param ss the shared strings used within the workbook
270    * @param s the sheet this is being added to
271    */

272   void setCellDetails(FormattingRecords fr, SharedStrings ss,
273                       WritableSheetImpl s)
274   {
275     referenced = true;
276     sheet = s;
277     formattingRecords = fr;
278
279     addCellFormat();
280     addCellFeatures();
281   }
282
283   /**
284    * Internal method to see if this cell is referenced within the workbook.
285    * Once this has been placed in the workbook, it becomes immutable
286    *
287    * @return TRUE if this cell has been added to a sheet, FALSE otherwise
288    */

289   final boolean isReferenced()
290   {
291     return referenced;
292   }
293
294   /**
295    * Gets the internal index of the formatting record
296    *
297    * @return the index of the format record
298    */

299   final int getXFIndex()
300   {
301     return format.getXFIndex();
302   }
303
304   /**
305    * API method which gets the format applied to this cell
306    *
307    * @return the format for this cell
308    */

309   public CellFormat getCellFormat()
310   {
311     return format;
312   }
313
314   /**
315    * Increments the row of this cell by one. Invoked by the sheet when
316    * inserting rows
317    */

318   void incrementRow()
319   {
320     row++;
321
322     if (features != null)
323     {
324       Comment c = features.getCommentDrawing();
325       c.setX(column);
326       c.setY(row);
327     }
328   }
329
330   /**
331    * Decrements the row of this cell by one. Invoked by the sheet when
332    * removing rows
333    */

334   void decrementRow()
335   {
336     row--;
337
338     if (features != null)
339     {
340       Comment c = features.getCommentDrawing();
341       c.setX(column);
342       c.setY(row);
343     }
344   }
345
346   /**
347    * Increments the column of this cell by one. Invoked by the sheet when
348    * inserting columns
349    */

350   void incrementColumn()
351   {
352     column++;
353
354     if (features != null)
355     {
356       Comment c = features.getCommentDrawing();
357       c.setX(column);
358       c.setY(row);
359     }
360
361   }
362
363   /**
364    * Decrements the column of this cell by one. Invoked by the sheet when
365    * removing columns
366    */

367   void decrementColumn()
368   {
369     column--;
370
371     if (features != null)
372     {
373       Comment c = features.getCommentDrawing();
374       c.setX(column);
375       c.setY(row);
376     }
377
378   }
379
380   /**
381    * Called when a column is inserted on the specified sheet. Notifies all
382    * RCIR cells of this change. The default implementation here does nothing
383    *
384    * @param s the sheet on which the column was inserted
385    * @param sheetIndex the sheet index on which the column was inserted
386    * @param col the column number which was inserted
387    */

388   void columnInserted(Sheet s, int sheetIndex, int col)
389   {
390   }
391
392   /**
393    * Called when a column is removed on the specified sheet. Notifies all
394    * RCIR cells of this change. The default implementation here does nothing
395    *
396    * @param s the sheet on which the column was inserted
397    * @param sheetIndex the sheet index on which the column was inserted
398    * @param col the column number which was inserted
399    */

400   void columnRemoved(Sheet s, int sheetIndex, int col)
401   {
402   }
403
404   /**
405    * Called when a row is inserted on the specified sheet. Notifies all
406    * RCIR cells of this change. The default implementation here does nothing
407    *
408    * @param s the sheet on which the column was inserted
409    * @param sheetIndex the sheet index on which the column was inserted
410    * @param row the column number which was inserted
411    */

412   void rowInserted(Sheet s, int sheetIndex, int row)
413   {
414   }
415
416   /**
417    * Called when a row is inserted on the specified sheet. Notifies all
418    * RCIR cells of this change. The default implementation here does nothing
419    *
420    * @param s the sheet on which the row was removed
421    * @param sheetIndex the sheet index on which the column was removed
422    * @param row the column number which was removed
423    */

424   void rowRemoved(Sheet s, int sheetIndex, int row)
425   {
426   }
427
428   /**
429    * Accessor for the sheet containing this cell
430    *
431    * @return the sheet containing this cell
432    */

433   protected WritableSheetImpl getSheet()
434   {
435     return sheet;
436   }
437
438   /**
439    * Adds the format information to the shared records. Performs the necessary
440    * checks (and clones) to ensure that the formats are not shared.
441    * Called from setCellDetails and setCellFormat
442    */

443   private void addCellFormat()
444   {
445     // Check to see if the format is one of the shared Workbook defaults. If
446
// so, then get hold of the Workbook's specific instance
447
Styles styles = sheet.getWorkbook().getStyles();
448     format = styles.getFormat(format);
449
450     try
451     {
452       if (!format.isInitialized())
453       {
454         formattingRecords.addStyle(format);
455       }
456     }
457     catch (NumFormatRecordsException e)
458     {
459       logger.warn("Maximum number of format records exceeded. Using " +
460                   "default format.");
461       format = styles.getNormalStyle();
462     }
463   }
464
465   /**
466    * Accessor for the cell features
467    *
468    * @return the cell features or NULL if this cell doesn't have any
469    */

470   public CellFeatures getCellFeatures()
471   {
472     return features;
473   }
474
475   /**
476    * Accessor for the cell features
477    *
478    * @return the cell features or NULL if this cell doesn't have any
479    */

480   public WritableCellFeatures getWritableCellFeatures()
481   {
482     return features;
483   }
484
485   /**
486    * Sets the cell features
487    *
488    * @param cf the cell features
489    */

490   public void setCellFeatures(WritableCellFeatures cf)
491   {
492     if (features != null)
493     {
494       logger.warn("current cell features not null - overwriting");
495     }
496
497     features = cf;
498     cf.setWritableCell(this);
499
500     // If the cell is already on the worksheet, then add the cell features
501
// to the workbook
502
if (referenced)
503     {
504       addCellFeatures();
505     }
506   }
507
508   /**
509    * Handles any addition cell features, such as comments or data
510    * validation. Called internally from this class when a cell is
511    * added to the workbook, and also externally from BaseCellFeatures
512    * following a call to setComment
513    */

514   public final void addCellFeatures()
515   {
516     if (features == null)
517     {
518       return;
519     }
520
521     if (copied == true)
522     {
523       copied = false; // make sure we go the whole nine yards next time
524
return;
525     }
526
527     if (features.getComment() != null)
528     {
529       Comment comment = new Comment(features.getComment(),
530                                     column, row);
531       comment.setWidth(features.getCommentWidth());
532       comment.setHeight(features.getCommentHeight());
533       sheet.addDrawing(comment);
534       sheet.getWorkbook().addDrawing(comment);
535       features.setCommentDrawing(comment);
536     }
537   }
538 }
539
Popular Tags