KickJava   Java API By Example, From Geeks To Geeks.

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


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 import jxl.Cell;
24 import jxl.biff.IntegerHelper;
25 import jxl.biff.CellReferenceHelper;
26
27 /**
28  * A cell reference in a formula
29  */

30 class CellReference extends Operand implements ParsedThing
31 {
32   /**
33    * The logger
34    */

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

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

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

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

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

61   private Cell relativeTo;
62
63   /**
64    * Constructor
65    *
66    * @param rt the cell containing the formula
67    */

68   public CellReference(Cell rt)
69   {
70     relativeTo = rt;
71   }
72
73   /**
74    * Constructor
75    */

76   public CellReference()
77   {
78   }
79
80   /**
81    * Constructor invoked when parsing a text string
82    *
83    * @param s the string being parsed
84    */

85   public CellReference(String JavaDoc s)
86   {
87     column = CellReferenceHelper.getColumn(s);
88     row = CellReferenceHelper.getRow(s);
89     columnRelative = CellReferenceHelper.isColumnRelative(s);
90     rowRelative = CellReferenceHelper.isRowRelative(s);
91   }
92
93   /**
94    * Reads the ptg data from the array starting at the specified position
95    *
96    * @param data the RPN array
97    * @param pos the current position in the array, excluding the ptg identifier
98    * @return the number of bytes read
99    */

100   public int read(byte[] data, int pos)
101   {
102     row = IntegerHelper.getInt(data[pos], data[pos+1]);
103     int columnMask = IntegerHelper.getInt(data[pos+2], data[pos+3]);
104     column = columnMask & 0x00ff;
105     columnRelative = ((columnMask & 0x4000) != 0);
106     rowRelative = ((columnMask & 0x8000) != 0);
107
108     return 4;
109   }
110
111   /**
112    * Accessor for the column
113    *
114    * @return the column
115    */

116   public int getColumn()
117   {
118     return column;
119   }
120
121   /**
122    * Accessor for the row
123    *
124    * @return the row
125    */

126   public int getRow()
127   {
128     return row;
129   }
130
131   /**
132    * Gets the cell reference as a string for this item
133    *
134    * @param buf the string buffer to populate
135    */

136   public void getString(StringBuffer JavaDoc buf)
137   {
138     CellReferenceHelper.getCellReference(column, !columnRelative,
139                                          row, !rowRelative,
140                                          buf);
141   }
142
143   /**
144    * Gets the token representation of this item in RPN
145    *
146    * @return the bytes applicable to this formula
147    */

148   byte[] getBytes()
149   {
150     byte[] data = new byte[5];
151     data[0] = !useAlternateCode() ? Token.REF.getCode() :
152                                     Token.REF.getCode2();
153
154     IntegerHelper.getTwoBytes(row, data, 1);
155
156     int grcol = column;
157
158     // Set the row/column relative bits if applicable
159
if (rowRelative)
160     {
161       grcol |= 0x8000;
162     }
163
164     if (columnRelative)
165     {
166       grcol |= 0x4000;
167     }
168
169     IntegerHelper.getTwoBytes(grcol, data, 3);
170
171     return data;
172   }
173
174   /**
175    * Adjusts all the relative cell references in this formula by the
176    * amount specified. Used when copying formulas
177    *
178    * @param colAdjust the amount to add on to each relative cell reference
179    * @param rowAdjust the amount to add on to each relative row reference
180    */

181   public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
182   {
183     if (columnRelative)
184     {
185       column += colAdjust;
186     }
187     
188     if (rowRelative)
189     {
190       row += rowAdjust;
191     }
192   }
193
194   /**
195    * Called when a column is inserted on the specified sheet. Tells
196    * the formula parser to update all of its cell references beyond this
197    * column
198    *
199    * @param sheetIndex the sheet on which the column was inserted
200    * @param col the column number which was inserted
201    * @param currentSheet TRUE if this formula is on the sheet in which the
202    * column was inserted, FALSE otherwise
203    */

204   public void columnInserted(int sheetIndex, int col, boolean currentSheet)
205   {
206     if (!currentSheet)
207     {
208       return;
209     }
210
211     if (column >= col)
212     {
213       column++;
214     }
215   }
216
217   /**
218    * Called when a column is inserted on the specified sheet. Tells
219    * the formula parser to update all of its cell references beyond this
220    * column
221    *
222    * @param sheetIndex the sheet on which the column was removed
223    * @param col the column number which was removed
224    * @param currentSheet TRUE if this formula is on the sheet in which the
225    * column was inserted, FALSE otherwise
226    */

227   void columnRemoved(int sheetIndex, int col, boolean currentSheet)
228   {
229     if (!currentSheet)
230     {
231       return;
232     }
233
234     if (column >= col)
235     {
236       column--;
237     }
238   }
239
240   /**
241    * Called when a column is inserted on the specified sheet. Tells
242    * the formula parser to update all of its cell references beyond this
243    * column
244    *
245    * @param sheetIndex the sheet on which the row was inserted
246    * @param row the row number which was inserted
247    * @param currentSheet TRUE if this formula is on the sheet in which the
248    * column was inserted, FALSE otherwise
249    */

250   void rowInserted(int sheetIndex, int r, boolean currentSheet)
251   {
252     if (!currentSheet)
253     {
254       return;
255     }
256
257     if (row >= r)
258     {
259       row++;
260     }
261   }
262
263   /**
264    * Called when a column is inserted on the specified sheet. Tells
265    * the formula parser to update all of its cell references beyond this
266    * column
267    *
268    * @param sheetIndex the sheet on which the row was removed
269    * @param row the row number which was removed
270    * @param currentSheet TRUE if this formula is on the sheet in which the
271    * column was inserted, FALSE otherwise
272    */

273   void rowRemoved(int sheetIndex, int r, boolean currentSheet)
274   {
275     if (!currentSheet)
276     {
277       return;
278     }
279
280     if (row >= r)
281     {
282       row--;
283     }
284   }
285 }
286
Popular Tags