KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import common.Assert;
26 import common.Logger;
27
28 import jxl.biff.RecordData;
29 import jxl.biff.StringHelper;
30 import jxl.biff.IntegerHelper;
31 import jxl.biff.CellReferenceHelper;
32
33 /**
34  * A nested class to hold range information
35  */

36 class Area extends Operand implements ParsedThing
37 {
38   /**
39    * The logger
40    */

41   private static Logger logger = Logger.getLogger(Area.class);
42
43   private int columnFirst;
44   private int rowFirst;
45   private int columnLast;
46   private int rowLast;
47   private boolean columnFirstRelative;
48   private boolean rowFirstRelative;
49   private boolean columnLastRelative;
50   private boolean rowLastRelative;
51
52   /**
53    * Constructor
54    */

55   Area()
56   {
57   }
58
59   /**
60    * Constructor invoked when parsing a string formula
61    *
62    * @param s the string to parse
63    */

64   Area(String JavaDoc s)
65   {
66     int seppos = s.indexOf(":");
67     Assert.verify(seppos != -1);
68     String JavaDoc startcell = s.substring(0, seppos);
69     String JavaDoc endcell = s.substring(seppos+1);
70
71     columnFirst = CellReferenceHelper.getColumn(startcell);
72     rowFirst = CellReferenceHelper.getRow(startcell);
73     columnLast = CellReferenceHelper.getColumn(endcell);
74     rowLast = CellReferenceHelper.getRow(endcell);
75
76     columnFirstRelative = CellReferenceHelper.isColumnRelative(startcell);
77     rowFirstRelative = CellReferenceHelper.isRowRelative(startcell);
78     columnLastRelative = CellReferenceHelper.isColumnRelative(endcell);
79     rowLastRelative = CellReferenceHelper.isRowRelative(endcell);
80   }
81     
82   int getFirstColumn()
83   {
84     return columnFirst;
85   }
86
87   int getFirstRow()
88   {
89     return rowFirst;
90   }
91
92   int getLastColumn()
93   {
94     return columnLast;
95   }
96
97   int getLastRow()
98   {
99     return rowLast;
100   }
101
102   /**
103    * Reads the ptg data from the array starting at the specified position
104    *
105    * @param data the RPN array
106    * @param pos the current position in the array, excluding the ptg identifier
107    * @return the number of bytes read
108    */

109   public int read(byte[] data, int pos)
110   {
111     rowFirst = IntegerHelper.getInt(data[pos], data[pos+1]);
112     rowLast = IntegerHelper.getInt(data[pos+2], data[pos+3]);
113     int columnMask = IntegerHelper.getInt(data[pos+4], data[pos+5]);
114     columnFirst = columnMask & 0x00ff;
115     columnFirstRelative = ((columnMask & 0x4000) != 0);
116     rowFirstRelative = ((columnMask & 0x8000) != 0);
117     columnMask = IntegerHelper.getInt(data[pos+6], data[pos+7]);
118     columnLast = columnMask & 0x00ff;
119     columnLastRelative = ((columnMask & 0x4000) != 0);
120     rowLastRelative = ((columnMask & 0x8000) != 0);
121
122     return 8;
123   }
124
125   /**
126    * Gets the string representation of this item
127    *
128    * @param buf
129    */

130   public void getString(StringBuffer JavaDoc buf)
131   {
132     CellReferenceHelper.getCellReference(columnFirst, rowFirst, buf);
133     buf.append(':');
134     CellReferenceHelper.getCellReference(columnLast, rowLast, buf);
135   }
136
137   /**
138    * Gets the token representation of this item in RPN
139    *
140    * @return the bytes applicable to this formula
141    */

142   byte[] getBytes()
143   {
144     byte[] data = new byte[9];
145     data[0] = !useAlternateCode() ? Token.AREA.getCode() :
146                                     Token.AREA.getCode2();
147
148     IntegerHelper.getTwoBytes(rowFirst, data, 1);
149     IntegerHelper.getTwoBytes(rowLast, data, 3);
150
151     int grcol = columnFirst;
152
153     // Set the row/column relative bits if applicable
154
if (rowFirstRelative)
155     {
156       grcol |= 0x8000;
157     }
158
159     if (columnFirstRelative)
160     {
161       grcol |= 0x4000;
162     }
163
164     IntegerHelper.getTwoBytes(grcol, data, 5);
165
166     grcol = columnLast;
167
168     // Set the row/column relative bits if applicable
169
if (rowLastRelative)
170     {
171       grcol |= 0x8000;
172     }
173
174     if (columnLastRelative)
175     {
176       grcol |= 0x4000;
177     }
178
179     IntegerHelper.getTwoBytes(grcol, data, 7);
180
181     return data;
182   }
183
184   /**
185    * Adjusts all the relative cell references in this formula by the
186    * amount specified. Used when copying formulas
187    *
188    * @param colAdjust the amount to add on to each relative cell reference
189    * @param rowAdjust the amount to add on to each relative row reference
190    */

191   public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
192   {
193     if (columnFirstRelative)
194     {
195       columnFirst += colAdjust;
196     }
197
198     if (columnLastRelative)
199     {
200       columnLast += colAdjust;
201     }
202     
203     if (rowFirstRelative)
204     {
205       rowFirst += rowAdjust;
206     }
207
208     if (rowLastRelative)
209     {
210       rowLast += rowAdjust;
211     }
212   }
213
214   /**
215    * Called when a column is inserted on the specified sheet. Tells
216    * the formula parser to update all of its cell references beyond this
217    * column
218    *
219    * @param sheetIndex the sheet on which the column was inserted
220    * @param col the column number which was inserted
221    * @param currentSheet TRUE if this formula is on the sheet in which the
222    * column was inserted, FALSE otherwise
223    */

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

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

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

308   void rowRemoved(int sheetIndex, int row, boolean currentSheet)
309   {
310     if (!currentSheet)
311     {
312       return;
313     }
314
315     if (row < rowFirst)
316     {
317       rowFirst--;
318     }
319
320     if (row <= rowLast)
321     {
322       rowLast--;
323     }
324   }
325 }
326
327
328
329
330
331
332
333
334
Popular Tags