KickJava   Java API By Example, From Geeks To Geeks.

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


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 SharedFormulaCellReference extends Operand implements ParsedThing
30 {
31   /**
32    * Indicates whether the column reference is relative or absolute
33    */

34   private boolean columnRelative;
35
36   /**
37    * Indicates whether the row reference is relative or absolute
38    */

39   private boolean rowRelative;
40
41   /**
42    * The column reference
43    */

44   private int column;
45
46   /**
47    * The row reference
48    */

49   private int row;
50
51   /**
52    * The cell containing the formula. Stored in order to determine
53    * relative cell values
54    */

55   private Cell relativeTo;
56
57   /**
58    * Constructor
59    *
60    * @param the cell the formula is relative to
61    */

62   public SharedFormulaCellReference(Cell rt)
63   {
64     relativeTo = rt;
65   }
66
67   /**
68    * Reads the ptg data from the array starting at the specified position
69    *
70    * @param data the RPN array
71    * @param pos the current position in the array, excluding the ptg identifier
72    * @return the number of bytes read
73    */

74   public int read(byte[] data, int pos)
75   {
76     // Preserve signage on column and row values, because they will
77
// probably be relative
78
row = IntegerHelper.getShort(data[pos], data[pos+1]);
79
80     int columnMask = IntegerHelper.getInt(data[pos+2], data[pos+3]);
81
82     column = (byte) (columnMask & 0xff);
83     columnRelative = ((columnMask & 0x4000) != 0);
84     rowRelative = ((columnMask & 0x8000) != 0);
85
86     if (columnRelative)
87     {
88       column = relativeTo.getColumn() + column;
89     }
90
91     if (rowRelative)
92     {
93       row = relativeTo.getRow() + row;
94     }
95
96     return 4;
97   }
98
99   public int getColumn()
100   {
101     return column;
102   }
103
104   public int getRow()
105   {
106     return row;
107   }
108
109   public void getString(StringBuffer JavaDoc buf)
110   {
111     CellReferenceHelper.getCellReference(column, row, buf);
112   }
113
114   /**
115    * Gets the token representation of this item in RPN
116    *
117    * @return the bytes applicable to this formula
118    */

119   byte[] getBytes()
120   {
121     byte[] data = new byte[5];
122     data[0] = Token.REF.getCode();
123     
124     IntegerHelper.getTwoBytes(row, data, 1);
125
126     int columnMask = column;
127
128     if (columnRelative)
129     {
130       columnMask |= 0x4000;
131     }
132
133     if (rowRelative)
134     {
135       columnMask |= 0x8000;
136     }
137
138     IntegerHelper.getTwoBytes(columnMask, data, 3);
139
140     return data;
141   }
142 }
143
144
145
146
147
148
149
150
151
152
Popular Tags