KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > formula > CellReference3d


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.biff.formula;
21
22 import common.Logger;
23
24 import jxl.Cell;
25 import jxl.biff.IntegerHelper;
26 import jxl.biff.CellReferenceHelper;
27
28 /**
29  * A 3d cell reference in a formula
30  */

31 class CellReference3d extends Operand implements ParsedThing
32 {
33   /**
34    * The logger
35    */

36   private static Logger logger = Logger.getLogger(CellReference3d.class);
37
38   /**
39    * Indicates whether the column reference is relative or absolute
40    */

41   private boolean columnRelative;
42
43   /**
44    * Indicates whether the row reference is relative or absolute
45    */

46   private boolean rowRelative;
47
48   /**
49    * The column reference
50    */

51   private int column;
52
53   /**
54    * The row reference
55    */

56   private int row;
57
58   /**
59    * The cell containing the formula. Stored in order to determine
60    * relative cell values
61    */

62   private Cell relativeTo;
63
64   /**
65    * The sheet which the reference is present on
66    */

67   private int sheet;
68   
69   /**
70    * A handle to the container of the external sheets ie. the workbook
71    */

72   private ExternalSheet workbook;
73
74   /**
75    * Constructor
76    *
77    * @param the cell containing the formula
78    * @param the list of external sheets
79    */

80   public CellReference3d(Cell rt, ExternalSheet w)
81   {
82     relativeTo = rt;
83     workbook = w;
84   }
85
86   /**
87    * Constructs this object from a string
88    *
89    * @param s the string
90    * @param w the external sheet
91    * @exception FormulaException
92    */

93   public CellReference3d(String JavaDoc s, ExternalSheet w) throws FormulaException
94   {
95     workbook = w;
96     columnRelative = true;
97     rowRelative = true;
98
99     // Get the cell details
100
int sep = s.indexOf('!');
101     String JavaDoc cellString = s.substring(sep+1);
102     column = CellReferenceHelper.getColumn(cellString);
103     row = CellReferenceHelper.getRow(cellString);
104
105     // Get the sheet index
106
String JavaDoc sheetName = s.substring(0, sep);
107
108     // Remove single quotes, if they exist
109
if (sheetName.charAt(0) == '\'' &&
110         sheetName.charAt(sheetName.length()-1) == '\'')
111     {
112       sheetName = sheetName.substring(1, sheetName.length() - 1);
113     }
114     sheet = w.getExternalSheetIndex(sheetName);
115
116     if (sheet < 0)
117     {
118       throw new FormulaException(FormulaException.sheetRefNotFound, sheetName);
119     }
120   }
121
122   /**
123    * Reads the ptg data from the array starting at the specified position
124    *
125    * @param data the RPN array
126    * @param pos the current position in the array, excluding the ptg identifier
127    * @return the number of bytes read
128    */

129   public int read(byte[] data, int pos)
130   {
131     sheet = IntegerHelper.getInt(data[pos], data[pos+1]);
132     row = IntegerHelper.getInt(data[pos+2], data[pos+3]);
133     int columnMask = IntegerHelper.getInt(data[pos+4], data[pos+5]);
134     column = columnMask & 0x00ff;
135     columnRelative = ((columnMask & 0x4000) != 0);
136     rowRelative = ((columnMask & 0x8000) != 0);
137
138     return 6;
139   }
140
141   /**
142    * Accessor for the column
143    *
144    * @return the column number
145    */

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

156   public int getRow()
157   {
158     return row;
159   }
160
161   /**
162    * Gets the string version of this cell reference
163    *
164    * @param buf the buffer to append to
165    */

166   public void getString(StringBuffer JavaDoc buf)
167   {
168     CellReferenceHelper.getCellReference(sheet, column, !columnRelative,
169                                          row, !rowRelative,
170                                          workbook, buf);
171   }
172
173   /**
174    * Gets the token representation of this item in RPN
175    *
176    * @return the bytes applicable to this formula
177    */

178   byte[] getBytes()
179   {
180     byte[] data = new byte[7];
181     data[0] = Token.REF3D.getCode();
182
183     IntegerHelper.getTwoBytes(sheet, data, 1);
184     IntegerHelper.getTwoBytes(row, data, 3);
185
186     int grcol = column;
187
188     // Set the row/column relative bits if applicable
189
if (rowRelative)
190     {
191       grcol |= 0x8000;
192     }
193
194     if (columnRelative)
195     {
196       grcol |= 0x4000;
197     }
198
199     IntegerHelper.getTwoBytes(grcol, data, 5);
200
201     return data;
202   }
203
204   /**
205    * Adjusts all the relative cell references in this formula by the
206    * amount specified. Used when copying formulas
207    *
208    * @param colAdjust the amount to add on to each relative cell reference
209    * @param rowAdjust the amount to add on to each relative row reference
210    */

211   public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
212   {
213     if (columnRelative)
214     {
215       column += colAdjust;
216     }
217     
218     if (rowRelative)
219     {
220       row += rowAdjust;
221     }
222   }
223
224   /**
225    * Called when a column is inserted on the specified sheet. Tells
226    * the formula parser to update all of its cell references beyond this
227    * column
228    *
229    * @param sheetIndex the sheet on which the column was inserted
230    * @param col the column number which was inserted
231    * @param currentSheet TRUE if this formula is on the sheet in which the
232    * column was inserted, FALSE otherwise
233    */

234   public void columnInserted(int sheetIndex, int col, boolean currentSheet)
235   {
236     if (sheetIndex != sheet)
237     {
238       return;
239     }
240
241     if (column >= col)
242     {
243       column++;
244     }
245   }
246
247
248   /**
249    * Called when a column is inserted on the specified sheet. Tells
250    * the formula parser to update all of its cell references beyond this
251    * column
252    *
253    * @param sheetIndex the sheet on which the column was removed
254    * @param col the column number which was removed
255    * @param currentSheet TRUE if this formula is on the sheet in which the
256    * column was inserted, FALSE otherwise
257    */

258   void columnRemoved(int sheetIndex, int col, boolean currentSheet)
259   {
260     if (sheetIndex != sheet)
261     {
262       return;
263     }
264
265     if (column >= col)
266     {
267       column--;
268     }
269   }
270
271   /**
272    * Called when a column is inserted on the specified sheet. Tells
273    * the formula parser to update all of its cell references beyond this
274    * column
275    *
276    * @param sheetIndex the sheet on which the row was inserted
277    * @param row the row number which was inserted
278    * @param currentSheet TRUE if this formula is on the sheet in which the
279    * column was inserted, FALSE otherwise
280    */

281   void rowInserted(int sheetIndex, int r, boolean currentSheet)
282   {
283     if (sheetIndex != sheet)
284     {
285       return;
286     }
287
288     if (row >= r)
289     {
290       row++;
291     }
292   }
293
294   /**
295    * Called when a column is inserted on the specified sheet. Tells
296    * the formula parser to update all of its cell references beyond this
297    * column
298    *
299    * @param sheetIndex the sheet on which the row was removed
300    * @param row the row number which was removed
301    * @param currentSheet TRUE if this formula is on the sheet in which the
302    * column was inserted, FALSE otherwise
303    */

304   void rowRemoved(int sheetIndex, int r, boolean currentSheet)
305   {
306     if (sheetIndex != sheet)
307     {
308       return;
309     }
310
311     if (row >= r)
312     {
313       row--;
314     }
315   }
316 }
317
318
319
320
Popular Tags