KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > read > biff > NumberValue


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.read.biff;
21
22 import java.text.NumberFormat JavaDoc;
23 import java.text.DecimalFormat JavaDoc;
24
25 import jxl.NumberCell;
26 import jxl.CellType;
27 import jxl.CellFeatures;
28 import jxl.format.CellFormat;
29 import jxl.biff.FormattingRecords;
30
31 /**
32  * A numerical cell value, initialized indirectly from a multiple biff record
33  * rather than directly from the binary data
34  */

35 class NumberValue implements NumberCell, CellFeaturesAccessor
36 {
37   /**
38    * The row containing this number
39    */

40   private int row;
41   /**
42    * The column containing this number
43    */

44   private int column;
45   /**
46    * The value of this number
47    */

48   private double value;
49
50   /**
51    * The cell format
52    */

53   private NumberFormat JavaDoc format;
54
55   /**
56    * The raw cell format
57    */

58   private CellFormat cellFormat;
59
60   /**
61    * The cell features
62    */

63   private CellFeatures features;
64
65   /**
66    * The index to the XF Record
67    */

68   private int xfIndex;
69
70   /**
71    * A handle to the formatting records
72    */

73   private FormattingRecords formattingRecords;
74
75   /**
76    * A flag to indicate whether this object's formatting things have
77    * been initialized
78    */

79   private boolean initialized;
80
81   /**
82    * A handle to the sheet
83    */

84   private SheetImpl sheet;
85
86   /**
87    * The format in which to return this number as a string
88    */

89   private static DecimalFormat JavaDoc defaultFormat = new DecimalFormat JavaDoc("#.###");
90
91   /**
92    * Constructs this number
93    *
94    * @param r the zero based row
95    * @param c the zero base column
96    * @param val the value
97    * @param xfi the xf index
98    * @param fr the formatting records
99    * @param si the sheet
100    */

101   public NumberValue(int r, int c, double val,
102                      int xfi,
103                      FormattingRecords fr,
104                      SheetImpl si)
105   {
106     row = r;
107     column = c;
108     value = val;
109     format = defaultFormat;
110     xfIndex = xfi;
111     formattingRecords = fr;
112     sheet = si;
113     initialized = false;
114   }
115
116   /**
117    * Sets the format for the number based on the Excel spreadsheets' format.
118    * This is called from SheetImpl when it has been definitely established
119    * that this cell is a number and not a date
120    *
121    * @param f the format
122    */

123   final void setNumberFormat(NumberFormat JavaDoc f)
124   {
125     if (f != null)
126     {
127       format = f;
128     }
129   }
130
131   /**
132    * Accessor for the row
133    *
134    * @return the zero based row
135    */

136   public final int getRow()
137   {
138     return row;
139   }
140
141   /**
142    * Accessor for the column
143    *
144    * @return the zero based column
145    */

146   public final int getColumn()
147   {
148     return column;
149   }
150
151   /**
152    * Accessor for the value
153    *
154    * @return the value
155    */

156   public double getValue()
157   {
158     return value;
159   }
160
161   /**
162    * Accessor for the contents as a string
163    *
164    * @return the value as a string
165    */

166   public String JavaDoc getContents()
167   {
168     return format.format(value);
169   }
170
171   /**
172    * Accessor for the cell type
173    *
174    * @return the cell type
175    */

176   public CellType getType()
177   {
178     return CellType.NUMBER;
179   }
180
181   /**
182    * Gets the cell format
183    *
184    * @return the cell format
185    */

186   public CellFormat getCellFormat()
187   {
188     if (!initialized)
189     {
190       cellFormat = formattingRecords.getXFRecord(xfIndex);
191       initialized = true;
192     }
193
194     return cellFormat;
195   }
196
197   /**
198    * Determines whether or not this cell has been hidden
199    *
200    * @return TRUE if this cell has been hidden, FALSE otherwise
201    */

202   public boolean isHidden()
203   {
204     ColumnInfoRecord cir = sheet.getColumnInfo(column);
205
206     if (cir != null && cir.getWidth() == 0)
207     {
208       return true;
209     }
210
211     RowRecord rr = sheet.getRowInfo(row);
212
213     if (rr != null && (rr.getRowHeight() == 0 || rr.isCollapsed()))
214     {
215       return true;
216     }
217
218     return false;
219   }
220
221   /**
222    * Gets the NumberFormat used to format this cell. This is the java
223    * equivalent of the Excel format
224    *
225    * @return the NumberFormat used to format the cell
226    */

227   public NumberFormat JavaDoc getNumberFormat()
228   {
229     return format;
230   }
231
232   /**
233    * Accessor for the cell features
234    *
235    * @return the cell features or NULL if this cell doesn't have any
236    */

237   public CellFeatures getCellFeatures()
238   {
239     return features;
240   }
241
242   /**
243    * Sets the cell features
244    *
245    * @param cf the cell features
246    */

247   public void setCellFeatures(CellFeatures cf)
248   {
249     features = cf;
250   }
251
252 }
253
254
255
256
Popular Tags