KickJava   Java API By Example, From Geeks To Geeks.

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


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 Area3d extends Operand implements ParsedThing
37 {
38   /**
39    * The logger
40    */

41   private static Logger logger = Logger.getLogger(Area3d.class);
42
43   private int sheet;
44   private int columnFirst;
45   private int rowFirst;
46   private int columnLast;
47   private int rowLast;
48   private boolean columnFirstRelative;
49   private boolean rowFirstRelative;
50   private boolean columnLastRelative;
51   private boolean rowLastRelative;
52   
53   /**
54    * A handle to the workbook
55    */

56   private ExternalSheet workbook;
57
58   Area3d(ExternalSheet es)
59   {
60     workbook = es;
61   }
62
63   /**
64    * Constructor invoked when parsing a string formula
65    *
66    * @param s the string to parse
67    */

68   Area3d(String JavaDoc s, ExternalSheet es) throws FormulaException
69   {
70     workbook = es;
71     int seppos = s.lastIndexOf(":");
72     Assert.verify(seppos != -1);
73     String JavaDoc startcell = s.substring(0, seppos);
74     String JavaDoc endcell = s.substring(seppos+1);
75
76     // Get the the start cell details
77
int sep = s.indexOf('!');
78     String JavaDoc cellString = s.substring(sep+1, seppos);
79     columnFirst = CellReferenceHelper.getColumn(cellString);
80     rowFirst = CellReferenceHelper.getRow(cellString);
81
82     // Get the sheet index
83
String JavaDoc sheetName = s.substring(0, sep);
84     int sheetNamePos = sheetName.lastIndexOf(']');
85
86     // Remove single quotes, if they exist
87
if (sheetName.charAt(0) == '\'' &&
88         sheetName.charAt(sheetName.length()-1) == '\'')
89     {
90       sheetName = sheetName.substring(1, sheetName.length() - 1);
91     }
92
93     sheet = es.getExternalSheetIndex(sheetName);
94
95     if (sheet < 0)
96     {
97       throw new FormulaException(FormulaException.sheetRefNotFound, sheetName);
98     }
99
100     // Get the last cell index
101
columnLast = CellReferenceHelper.getColumn(endcell);
102     rowLast = CellReferenceHelper.getRow(endcell);
103
104     columnFirstRelative = true;
105     rowFirstRelative = true;
106     columnLastRelative = true;
107     rowLastRelative = true;
108   }
109
110   int getFirstColumn() {return columnFirst;}
111   int getFirstRow() {return rowFirst;}
112   int getLastColumn() {return columnLast;}
113   int getLastRow() {return rowLast;}
114
115   /**
116    * Reads the ptg data from the array starting at the specified position
117    *
118    * @param data the RPN array
119    * @param pos the current position in the array, excluding the ptg identifier
120    * @return the number of bytes read
121    */

122   public int read(byte[] data, int pos)
123   {
124     sheet = IntegerHelper.getInt(data[pos], data[pos+1]);
125     rowFirst = IntegerHelper.getInt(data[pos+2], data[pos+3]);
126     rowLast = IntegerHelper.getInt(data[pos+4], data[pos+5]);
127     int columnMask = IntegerHelper.getInt(data[pos+6], data[pos+7]);
128     columnFirst = columnMask & 0x00ff;
129     columnFirstRelative = ((columnMask & 0x4000) != 0);
130     rowFirstRelative = ((columnMask & 0x8000) != 0);
131     columnMask = IntegerHelper.getInt(data[pos+8], data[pos+9]);
132     columnLast = columnMask & 0x00ff;
133     columnLastRelative = ((columnMask & 0x4000) != 0);
134     rowLastRelative = ((columnMask & 0x8000) != 0);
135
136     return 10;
137   }
138
139   public void getString(StringBuffer JavaDoc buf)
140   {
141     CellReferenceHelper.getCellReference
142       (sheet, columnFirst, rowFirst, workbook, buf);
143     buf.append(':');
144     CellReferenceHelper.getCellReference(columnLast, rowLast, buf);
145   }
146
147   /**
148    * Gets the token representation of this item in RPN
149    *
150    * @return the bytes applicable to this formula
151    */

152   byte[] getBytes()
153   {
154     byte[] data = new byte[11];
155     data[0] = Token.AREA3D.getCode();
156
157     IntegerHelper.getTwoBytes(sheet, data, 1);
158
159     IntegerHelper.getTwoBytes(rowFirst, data, 3);
160     IntegerHelper.getTwoBytes(rowLast, data, 5);
161
162     int grcol = columnFirst;
163
164     // Set the row/column relative bits if applicable
165
if (rowFirstRelative)
166     {
167       grcol |= 0x8000;
168     }
169
170     if (columnFirstRelative)
171     {
172       grcol |= 0x4000;
173     }
174
175     IntegerHelper.getTwoBytes(grcol, data, 7);
176
177     grcol = columnLast;
178
179     // Set the row/column relative bits if applicable
180
if (rowLastRelative)
181     {
182       grcol |= 0x8000;
183     }
184
185     if (columnLastRelative)
186     {
187       grcol |= 0x4000;
188     }
189
190     IntegerHelper.getTwoBytes(grcol, data, 9);
191
192     return data;
193   }
194
195   /**
196    * Adjusts all the relative cell references in this formula by the
197    * amount specified. Used when copying formulas
198    *
199    * @param colAdjust the amount to add on to each relative cell reference
200    * @param rowAdjust the amount to add on to each relative row reference
201    */

202   public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
203   {
204     if (columnFirstRelative)
205     {
206       columnFirst += colAdjust;
207     }
208
209     if (columnLastRelative)
210     {
211       columnLast += colAdjust;
212     }
213     
214     if (rowFirstRelative)
215     {
216       rowFirst += rowAdjust;
217     }
218
219     if (rowLastRelative)
220     {
221       rowLast += rowAdjust;
222     }
223   }
224
225   /**
226    * Called when a column is inserted on the specified sheet. Tells
227    * the formula parser to update all of its cell references beyond this
228    * column
229    *
230    * @param sheetIndex the sheet on which the column was inserted
231    * @param col the column number which was inserted
232    * @param currentSheet TRUE if this formula is on the sheet in which the
233    * column was inserted, FALSE otherwise
234    */

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

263   void columnRemoved(int sheetIndex, int col, boolean currentSheet)
264   {
265     if (sheetIndex != sheet)
266     {
267       return;
268     }
269
270     if (col < columnFirst)
271     {
272       columnFirst--;
273     }
274
275     if (col <= columnLast)
276     {
277       columnLast--;
278     }
279   }
280
281   /**
282    * Called when a column is inserted on the specified sheet. Tells
283    * the formula parser to update all of its cell references beyond this
284    * column
285    *
286    * @param sheetIndex the sheet on which the row was inserted
287    * @param row the row number which was inserted
288    * @param currentSheet TRUE if this formula is on the sheet in which the
289    * column was inserted, FALSE otherwise
290    */

291   void rowInserted(int sheetIndex, int row, boolean currentSheet)
292   {
293     if (sheetIndex != sheet)
294     {
295       return;
296     }
297
298     if (row <= rowFirst)
299     {
300       rowFirst++;
301     }
302
303     if (row <= rowLast)
304     {
305       rowLast++;
306     }
307   }
308
309   /**
310    * Called when a column is inserted on the specified sheet. Tells
311    * the formula parser to update all of its cell references beyond this
312    * column
313    *
314    * @param sheetIndex the sheet on which the row was removed
315    * @param row the row number which was removed
316    * @param currentSheet TRUE if this formula is on the sheet in which the
317    * column was inserted, FALSE otherwise
318    */

319   void rowRemoved(int sheetIndex, int row, boolean currentSheet)
320   {
321     if (sheetIndex != sheet)
322     {
323       return;
324     }
325     if (row < rowFirst)
326     {
327       rowFirst--;
328     }
329
330     if (row <= rowLast)
331     {
332       rowLast--;
333     }
334   }
335
336
337 }
338
339
340
341
342
343
344
345
346
Popular Tags