KickJava   Java API By Example, From Geeks To Geeks.

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


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

29 class SharedFormulaArea extends Operand implements ParsedThing
30 {
31   private int columnFirst;
32   private int rowFirst;
33   private int columnLast;
34   private int rowLast;
35
36   private boolean columnFirstRelative;
37   private boolean rowFirstRelative;
38   private boolean columnLastRelative;
39   private boolean rowLastRelative;
40
41   /**
42    * The cell containing the formula. Stored in order to determine
43    * relative cell values
44    */

45   private Cell relativeTo;
46
47   /**
48    * Constructor
49    *
50    * @param the cell the formula is relative to
51    */

52   public SharedFormulaArea(Cell rt)
53   {
54     relativeTo = rt;
55   }
56
57   int getFirstColumn()
58   {
59     return columnFirst;
60   }
61   
62   int getFirstRow()
63   {
64     return rowFirst;
65   }
66
67   int getLastColumn()
68   {
69     return columnLast;
70   }
71
72   int getLastRow()
73   {
74     return rowLast;
75   }
76
77   /**
78    * Reads the ptg data from the array starting at the specified position
79    *
80    * @param data the RPN array
81    * @param pos the current position in the array, excluding the ptg identifier
82    * @return the number of bytes read
83    */

84   public int read(byte[] data, int pos)
85   {
86     // Preserve signage on column and row values, because they will
87
// probably be relative
88

89     rowFirst = IntegerHelper.getShort(data[pos], data[pos+1]);
90     rowLast = IntegerHelper.getShort(data[pos+2], data[pos+3]);
91
92     int columnMask = IntegerHelper.getInt(data[pos+4], data[pos+5]);
93     columnFirst = columnMask & 0x00ff;
94     columnFirstRelative = ((columnMask & 0x4000) != 0);
95     rowFirstRelative = ((columnMask & 0x8000) != 0);
96
97     if (columnFirstRelative)
98     {
99       columnFirst = relativeTo.getColumn() + columnFirst;
100     }
101
102     if (rowFirstRelative)
103     {
104       rowFirst = relativeTo.getRow() + rowFirst;
105     }
106
107     columnMask = IntegerHelper.getInt(data[pos+6], data[pos+7]);
108     columnLast = columnMask & 0x00ff;
109
110     columnLastRelative = ((columnMask & 0x4000) != 0);
111     rowLastRelative = ((columnMask & 0x8000) != 0);
112
113     if (columnLastRelative)
114     {
115       columnLast = relativeTo.getColumn() + columnLast;
116     }
117
118     if (rowLastRelative)
119     {
120       rowLast = relativeTo.getRow() + rowLast;
121     }
122
123
124     return 8;
125   }
126
127   public void getString(StringBuffer JavaDoc buf)
128   {
129     CellReferenceHelper.getCellReference(columnFirst, rowFirst, buf);
130     buf.append(':');
131     CellReferenceHelper.getCellReference(columnLast, rowLast, buf);
132   }
133
134   /**
135    * Gets the token representation of this item in RPN
136    *
137    * @return the bytes applicable to this formula
138    */

139   byte[] getBytes()
140   {
141     byte[] data = new byte[9];
142     data[0] = Token.AREA.getCode();
143
144     // Use absolute references for columns, so don't bother about
145
// the col relative/row relative bits
146
IntegerHelper.getTwoBytes(rowFirst, data, 1);
147     IntegerHelper.getTwoBytes(rowLast, data, 3);
148     IntegerHelper.getTwoBytes(columnFirst, data, 5);
149     IntegerHelper.getTwoBytes(columnLast, data, 7);
150
151     return data;
152   }
153 }
154
155
156
157
Popular Tags