1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.hssf.record.formula;
57   
58   import org.apache.poi.util.LittleEndian;
59   import org.apache.poi.hssf.util.RangeAddress;
60   
61   /**
62    * Title:        Area 3D Ptg - 3D referecnce (Sheet + Area)<P>
63    * Description:  Defined a area in Extern Sheet. <P>
64    * REFERENCE:  <P>
65    * @author Libin Roman (Vista Portal LDT. Developer)
66    * @version 1.0-pre
67    */
68   
69   public class Area3DPtg extends Ptg
70   {
71       public final static byte sid  = 0x3b;
72       private final static int  SIZE = 11; // 10 + 1 for Ptg
73       private short             field_1_index_extern_sheet;
74       private short             field_2_first_row;
75       private short             field_3_last_row;
76       private short             field_4_first_column;
77       private short             field_5_last_column;
78   
79       /** Creates new AreaPtg */
80   
81       public Area3DPtg()
82       {
83       }
84   
85       public Area3DPtg(byte[] data, int offset)
86       {
87           offset++;
88           field_1_index_extern_sheet = LittleEndian.getShort(data, 0 + offset);
89           field_2_first_row          = LittleEndian.getShort(data, 2 + offset);
90           field_3_last_row           = LittleEndian.getShort(data, 4 + offset);
91           field_4_first_column       = LittleEndian.getShort(data, 6 + offset);
92           field_5_last_column        = LittleEndian.getShort(data, 8 + offset);
93       }
94   
95       public String toString()
96       {
97           StringBuffer buffer = new StringBuffer();
98   
99           buffer.append("AreaPtg\n");
100          buffer.append("Index to Extern Sheet = " + getExternSheetIndex()).append("\n");
101          buffer.append("firstRow = " + getFirstRow()).append("\n");
102          buffer.append("lastRow  = " + getLastRow()).append("\n");
103          buffer.append("firstCol = " + getFirstColumn()).append("\n");
104          buffer.append("lastCol  = " + getLastColumn()).append("\n");
105          buffer.append("firstColRowRel= "
106                        + isFirstColRowRelative()).append("\n");
107          buffer.append("lastColRowRel = "
108                        + isLastColRowRelative()).append("\n");
109          buffer.append("firstColRel   = " + isFirstColRelative()).append("\n");
110          buffer.append("lastColRel    = " + isLastColRelative()).append("\n");
111          return buffer.toString();
112      }
113  
114      public void writeBytes(byte [] array, int offset)
115      {
116          array[ 0 + offset ] = sid;
117          LittleEndian.putShort(array, 1 + offset , getExternSheetIndex());
118          LittleEndian.putShort(array, 3 + offset , getFirstRow());
119          LittleEndian.putShort(array, 5 + offset , getLastRow());
120          LittleEndian.putShort(array, 7 + offset , getFirstColumnRaw());
121          LittleEndian.putShort(array, 9 + offset , getLastColumnRaw());
122      }
123  
124      public int getSize()
125      {
126          return SIZE;
127      }
128  
129      public short getExternSheetIndex(){
130          return field_1_index_extern_sheet;
131      }
132  
133      public void setExternSheetIndex(short index){
134          field_1_index_extern_sheet = index;
135      }
136  
137      public short getFirstRow()
138      {
139          return field_2_first_row;
140      }
141  
142      public void setFirstRow(short row)
143      {
144          field_2_first_row = row;
145      }
146  
147      public short getLastRow()
148      {
149          return field_3_last_row;
150      }
151  
152      public void setLastRow(short row)
153      {
154          field_3_last_row = row;
155      }
156  
157      public short getFirstColumn()
158      {
159          return ( short ) (field_4_first_column & 0xFF);
160      }
161  
162      public short getFirstColumnRaw()
163      {
164          return field_4_first_column;
165      }
166  
167      public boolean isFirstColRowRelative()
168      {
169          return (((getFirstColumnRaw()) & 0x8000) == 0x8000);
170      }
171  
172      public boolean isFirstColRelative()
173      {
174          return (((getFirstColumnRaw()) & 0x4000) == 0x4000);
175      }
176  
177      public void setFirstColumn(short column)
178      {
179          field_4_first_column &= 0xFF00;
180          field_4_first_column |= column & 0xFF;
181      }
182  
183      public void setFirstColumnRaw(short column)
184      {
185          field_4_first_column = column;
186      }
187  
188      public short getLastColumn()
189      {
190          return ( short ) (field_5_last_column & 0xFF);
191      }
192  
193      public short getLastColumnRaw()
194      {
195          return field_5_last_column;
196      }
197  
198      public boolean isLastColRowRelative()
199      {
200          return (((getLastColumnRaw()) & 0x8000) == 1);
201      }
202  
203      public boolean isLastColRelative()
204      {
205          return (((getFirstColumnRaw()) & 0x4000) == 1);
206      }
207  
208      public void setLastColumn(short column)
209      {
210          field_5_last_column &= 0xFF00;
211          field_5_last_column |= column & 0xFF;
212      }
213  
214      public void setLastColumnRaw(short column)
215      {
216          field_5_last_column = column;
217      }
218  
219      public String getArea(){
220          RangeAddress ra = new RangeAddress( getFirstColumn(),getFirstRow() + 1, getLastColumn(), getLastRow() + 1);
221          String result = ra.getAddress();
222  
223          return result;
224      }
225  
226      public void setArea(String ref){
227          RangeAddress ra = new RangeAddress(ref);
228  
229          String from = ra.getFromCell();
230          String to   = ra.getToCell();
231  
232          setFirstColumn((short) (ra.getXPosition(from) -1));
233          setFirstRow((short) (ra.getYPosition(from) -1));
234          setLastColumn((short) (ra.getXPosition(to) -1));
235          setLastRow((short) (ra.getYPosition(to) -1));
236  
237      }
238  
239      public String toFormulaString()
240      {
241          String result = getArea();
242  
243          return result;
244      }
245  
246     public byte getDefaultOperandClass() {
247         return Ptg.CLASS_VALUE;
248     }
249  
250  }
251