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   /*
57    * AreaPtg.java
58    *
59    * Created on November 17, 2001, 9:30 PM
60    */
61   package org.apache.poi.hssf.record.formula;
62   
63   import org.apache.poi.util.LittleEndian;
64   
65   /**
66    *
67    * @author  andy
68    */
69   
70   public class AreaPtg
71       extends Ptg
72   {
73       public final static short sid  = 0x25;
74       private final static int  SIZE = 9;
75       private short             field_1_first_row;
76       private short             field_2_last_row;
77       private short             field_3_first_column;
78       private short             field_4_last_column;
79   
80       /** Creates new AreaPtg */
81   
82       public AreaPtg()
83       {
84       }
85   
86       public AreaPtg(byte [] data, int offset)
87       {
88           offset++;
89           field_1_first_row    = LittleEndian.getShort(data, 0 + offset);
90           field_2_last_row     = LittleEndian.getShort(data, 2 + offset);
91           field_3_first_column = LittleEndian.getShort(data, 4 + offset);
92           field_4_last_column  = LittleEndian.getShort(data, 6 + offset);
93           System.out.println(toString());
94       }
95   
96       public String toString()
97       {
98           StringBuffer buffer = new StringBuffer();
99   
100          buffer.append("AreaPtg\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      }
117  
118      public int getSize()
119      {
120          return SIZE;
121      }
122  
123      public short getFirstRow()
124      {
125          return field_1_first_row;
126      }
127  
128      public void setFirstRow(short row)
129      {
130          field_1_first_row = row;
131      }
132  
133      public short getLastRow()
134      {
135          return field_2_last_row;
136      }
137  
138      public void setLastRow(short row)
139      {
140          field_2_last_row = row;
141      }
142  
143      public short getFirstColumn()
144      {
145          return ( short ) (field_3_first_column & 0x3FFF);
146      }
147  
148      public short getFirstColumnRaw()
149      {
150          return field_3_first_column;
151      }
152  
153      public boolean isFirstColRowRelative()
154      {
155          return (((getFirstColumnRaw()) & 0x8000) == 0x8000);
156      }
157  
158      public boolean isFirstColRelative()
159      {
160          return (((getFirstColumnRaw()) & 0x4000) == 0x4000);
161      }
162  
163      public void setFirstColumn(short column)
164      {
165          field_3_first_column = column;   // fixme
166      }
167  
168      public void setFirstColumnRaw(short column)
169      {
170          field_3_first_column = column;
171      }
172  
173      public short getLastColumn()
174      {
175          return ( short ) (field_4_last_column & 0x3FFF);   // fixme
176      }
177  
178      public short getLastColumnRaw()
179      {
180          return field_4_last_column;
181      }
182  
183      public boolean isLastColRowRelative()
184      {
185          return (((getLastColumnRaw()) & 0x8000) == 1);
186      }
187  
188      public boolean isLastColRelative()
189      {
190          return (((getFirstColumnRaw()) & 0x4000) == 1);
191      }
192  
193      public void setLastColumn(short column)
194      {
195          field_4_last_column = column;   // fixme
196      }
197  
198      public void setLastColumnRaw(short column)
199      {
200          field_4_last_column = column;
201      }
202  
203      public String toFormulaString()
204      {
205          String firstrow = "" + (getFirstRow() + 1);
206          String lastrow  = null;
207  
208          if (isLastColRowRelative())
209          {
210              lastrow = "" + (getFirstRow() + getLastRow());
211          }
212          else
213          {
214              lastrow = "" + (getLastRow() + 1);
215          }
216  
217          // String firstcol = ""+
218          // String lastcol
219          return colNumToLetter(getFirstColumn()) + firstrow + ":"
220                 + colNumToLetter(getLastColumn()) + lastrow;
221      }
222  
223      public String colNumToLetter(int col)
224      {
225          byte[] b =
226          {
227              0x41
228          };
229  
230          b[ 0 ] += ( byte ) col;
231          String retval = null;
232  
233          try
234          {
235              retval = new String(b, "UTF-8");
236          }
237          catch (java.io.UnsupportedEncodingException e)
238          {
239              throw new RuntimeException(
240                  "NON JDK 1.3 COMPLIANT JVM -- YOUR JVM MUST SUPPORT UTF-8 encoding as per docs!");
241          }
242          return retval;
243      }
244  }
245