1    
2    
3    /* ====================================================================
4     * The Apache Software License, Version 1.1
5     *
6     * Copyright (c) 2002 The Apache Software Foundation.  All rights
7     * reserved.
8     *
9     * Redistribution and use in source and binary forms, with or without
10    * modification, are permitted provided that the following conditions
11    * are met:
12    *
13    * 1. Redistributions of source code must retain the above copyright
14    *    notice, this list of conditions and the following disclaimer.
15    *
16    * 2. Redistributions in binary form must reproduce the above copyright
17    *    notice, this list of conditions and the following disclaimer in
18    *    the documentation and/or other materials provided with the
19    *    distribution.
20    *
21    * 3. The end-user documentation included with the redistribution,
22    *    if any, must include the following acknowledgment:
23    *       "This product includes software developed by the
24    *        Apache Software Foundation (http://www.apache.org/)."
25    *    Alternately, this acknowledgment may appear in the software itself,
26    *    if and wherever such third-party acknowledgments normally appear.
27    *
28    * 4. The names "Apache" and "Apache Software Foundation" and
29    *    "Apache POI" must not be used to endorse or promote products
30    *    derived from this software without prior written permission. For
31    *    written permission, please contact apache@apache.org.
32    *
33    * 5. Products derived from this software may not be called "Apache",
34    *    "Apache POI", nor may "Apache" appear in their name, without
35    *    prior written permission of the Apache Software Foundation.
36    *
37    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48    * SUCH DAMAGE.
49    * ====================================================================
50    *
51    * This software consists of voluntary contributions made by many
52    * individuals on behalf of the Apache Software Foundation.  For more
53    * information on the Apache Software Foundation, please see
54    * <http://www.apache.org/>.
55    */
56   
57   package org.apache.poi.hssf.record.formula;
58   
59   import org.apache.poi.util.LittleEndian;
60   
61   import org.apache.poi.hssf.util.RangeAddress;
62   import org.apache.poi.hssf.util.CellReference;
63   import org.apache.poi.hssf.util.SheetReferences;
64   import org.apache.poi.util.BitField;
65   import org.apache.poi.hssf.model.Workbook;
66   
67   /**
68    * Title:        Reference 3D Ptg <P>
69    * Description:  Defined a cell in extern sheet. <P>
70    * REFERENCE:  <P>
71    * @author Libin Roman (Vista Portal LDT. Developer)
72    * @author Jason Height (jheight at chariot dot net dot au)
73    * @version 1.0-pre
74    */
75   
76   public class Ref3DPtg extends Ptg {
77       public final static byte sid  = 0x3a;
78       private final static int  SIZE = 7; // 6 + 1 for Ptg
79       private short             field_1_index_extern_sheet;
80       private short             field_2_row;
81       private short             field_3_column;
82       private BitField         rowRelative = new BitField(0x8000);
83       private BitField         colRelative = new BitField(0x4000);
84   
85       /** Creates new AreaPtg */
86       public Ref3DPtg() {}
87   
88       public Ref3DPtg(byte[] data, int offset) {
89           offset++;
90           field_1_index_extern_sheet = LittleEndian.getShort(data, 0 + offset);
91           field_2_row          = LittleEndian.getShort(data, 2 + offset);
92           field_3_column        = LittleEndian.getShort(data, 4 + offset);
93       }
94       
95       public Ref3DPtg(String cellref, short externIdx ) {
96           CellReference c= new CellReference(cellref);
97           setRow((short) c.getRow());
98           setColumn((short) c.getCol());
99           setColRelative(!c.isColAbsolute());
100          setRowRelative(!c.isRowAbsolute());   
101          setExternSheetIndex(externIdx);
102      }
103  
104      public String toString() {
105          StringBuffer buffer = new StringBuffer();
106  
107          buffer.append("Ref3dPrg\n");
108          buffer.append("Index to Extern Sheet = " + getExternSheetIndex()).append("\n");
109          buffer.append("Row = " + getRow()).append("\n");
110          buffer.append("Col  = " + getColumn()).append("\n");
111          buffer.append("ColRowRel= "
112          + isRowRelative()).append("\n");
113          buffer.append("ColRel   = " + isColRelative()).append("\n");
114          return buffer.toString();
115      }
116  
117      public void writeBytes(byte [] array, int offset) {
118          array[ 0 + offset ] = (byte) (sid + ptgClass);
119          LittleEndian.putShort(array, 1 + offset , getExternSheetIndex());
120          LittleEndian.putShort(array, 3 + offset , getRow());
121          LittleEndian.putShort(array, 5 + offset , getColumnRaw());
122      }
123  
124      public int getSize() {
125          return SIZE;
126      }
127  
128      public short getExternSheetIndex(){
129          return field_1_index_extern_sheet;
130      }
131  
132      public void setExternSheetIndex(short index){
133          field_1_index_extern_sheet = index;
134      }
135  
136      public short getRow() {
137          return field_2_row;
138      }
139  
140      public void setRow(short row) {
141          field_2_row = row;
142      }
143  
144      public short getColumn() {
145          return ( short ) (field_3_column & 0xFF);
146      }
147  
148      public short getColumnRaw() {
149          return field_3_column;
150      }
151  
152       public boolean isRowRelative()
153      {
154          return rowRelative.isSet(field_3_column);
155      }
156      
157      public void setRowRelative(boolean rel) {
158          field_3_column=rowRelative.setShortBoolean(field_3_column,rel);
159      }
160      
161      public boolean isColRelative()
162      {
163          return colRelative.isSet(field_3_column);
164      }
165      
166      public void setColRelative(boolean rel) {
167          field_3_column=colRelative.setShortBoolean(field_3_column,rel);
168      }
169      public void setColumn(short column) {
170          field_3_column &= 0xFF00;
171          field_3_column |= column & 0xFF;
172      }
173  
174      public void setColumnRaw(short column) {
175          field_3_column = column;
176      }
177  
178     /* public String getArea(){
179          RangeAddress ra = new RangeAddress("");
180  
181          String result = (ra.numTo26Sys(getColumn()) + (getRow() + 1));
182  
183          return result;
184      }*/
185  
186      public void setArea(String ref){
187          RangeAddress ra = new RangeAddress(ref);
188  
189          String from = ra.getFromCell();
190  
191          setColumn((short) (ra.getXPosition(from) -1));
192          setRow((short) (ra.getYPosition(from) -1));
193  
194      }
195  
196      public String toFormulaString(SheetReferences refs) {
197          StringBuffer retval = new StringBuffer();
198          if (refs != null) {
199              retval.append(refs.getSheetName((int)this.field_1_index_extern_sheet));
200              retval.append('!');
201          }
202          retval.append((new CellReference(getRow(),getColumn(),!isRowRelative(),!isColRelative())).toString()); 
203          return retval.toString();
204      }
205  
206     public byte getDefaultOperandClass() {return Ptg.CLASS_REF;}
207  
208     public Object clone() {
209       Ref3DPtg ptg = new Ref3DPtg();
210       ptg.field_1_index_extern_sheet = field_1_index_extern_sheet;
211       ptg.field_2_row = field_2_row;
212       ptg.field_3_column = field_3_column;
213       return ptg;
214     }
215  
216  }
217