KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > MergeCellsRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.record;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.poi.util.LittleEndian;
25
26 /**
27  * Title: Merged Cells Record
28  * <br>
29  * Description: Optional record defining a square area of cells to "merged" into
30  * one cell. <br>
31  * REFERENCE: NONE (UNDOCUMENTED PRESENTLY) <br>
32  * @author Andrew C. Oliver (acoliver at apache dot org)
33  * @version 2.0-pre
34  */

35 public class MergeCellsRecord
36     extends Record
37 {
38     public final static short sid = 0xe5;
39     private ArrayList JavaDoc field_2_regions;
40
41     public MergeCellsRecord()
42     {
43     }
44
45     /**
46      * Constructs a MergedCellsRecord and sets its fields appropriately
47      *
48      * @param sid id must be 0xe5 or an exception will be throw upon validation
49      * @param size the size of the data area of the record
50      * @param data data of the record (should not contain sid/len)
51      */

52
53     public MergeCellsRecord(short sid, short size, byte [] data)
54     {
55         super(sid, size, data);
56     }
57
58     /**
59      * Constructs a MergedCellsRecord and sets its fields appropriately
60      *
61      * @param sid id must be 0xe5 or an exception will be throw upon validation
62      * @param size the size of the data area of the record
63      * @param data data of the record (should not contain sid/len)
64      * @param offset the offset of the record's data
65      */

66
67     public MergeCellsRecord(short sid, short size, byte [] data, int offset)
68     {
69         super(sid, size, data, offset);
70     }
71
72     protected void fillFields(byte [] data, short size, int offset)
73     {
74         short numAreas = LittleEndian.getShort(data, 0 + offset);
75         field_2_regions = new ArrayList JavaDoc(numAreas + 10);
76         int pos = 2;
77
78         for (int k = 0; k < numAreas; k++)
79         {
80             MergedRegion region =
81                 new MergedRegion(LittleEndian
82                     .getShort(data, pos + offset), LittleEndian
83                     .getShort(data, pos + 2 + offset), LittleEndian
84                     .getShort(data, pos + 4 + offset), LittleEndian
85                     .getShort(data, pos + 6 + offset));
86
87             pos += 8;
88             field_2_regions.add(region);
89         }
90     }
91
92     /**
93      * get the number of merged areas. If this drops down to 0 you should just go
94      * ahead and delete the record.
95      * @return number of areas
96      */

97
98     public short getNumAreas()
99     {
100         //if the array size is larger than a short (65536), the record can't hold that many merges anyway
101
if (field_2_regions == null) return 0;
102         return (short)field_2_regions.size();
103     }
104
105     /**
106      * set the number of merged areas. You do not need to call this if you use addArea,
107      * it will be incremented automatically or decremented when an area is removed. If
108      * you are setting this to 0 then you are a terrible person. Just remove the record.
109      * (just kidding about you being a terrible person..hehe)
110      * @deprecated We now link the size to the actual array of merged regions
111      * @see #getNumAreas()
112      * @param numareas number of areas
113      */

114
115     public void setNumAreas(short numareas)
116     {
117         
118     }
119
120     /**
121      * Add an area to consider a merged cell. The index returned is only gauranteed to
122      * be correct provided you do not add ahead of or remove ahead of it (in which case
123      * you should increment or decrement appropriately....in other words its an arrayList)
124      *
125      * @param rowfrom - the upper left hand corner's row
126      * @param colfrom - the upper left hand corner's col
127      * @param rowto - the lower right hand corner's row
128      * @param colto - the lower right hand corner's col
129      * @return new index of said area (don't depend on it if you add/remove)
130      */

131
132     //public int addArea(short rowfrom, short colfrom, short rowto, short colto)
133
public int addArea(int rowfrom, short colfrom, int rowto, short colto)
134     {
135         if (field_2_regions == null)
136         {
137             field_2_regions = new ArrayList JavaDoc(10);
138         }
139         MergedRegion region = new MergedRegion(rowfrom, rowto, colfrom,
140                                                colto);
141
142         field_2_regions.add(region);
143         return field_2_regions.size() - 1;
144     }
145
146     /**
147      * essentially unmerge the cells in the "area" stored at the passed in index
148      * @param area index
149      */

150
151     public void removeAreaAt(int area)
152     {
153         field_2_regions.remove(area);
154     }
155
156     /**
157      * return the MergedRegion at the given index.
158      *
159      * @return MergedRegion representing the area that is Merged (r1,c1 - r2,c2)
160      */

161
162     public MergedRegion getAreaAt(int index)
163     {
164         return ( MergedRegion ) field_2_regions.get(index);
165     }
166
167     public int getRecordSize()
168     {
169         int retValue;
170
171         retValue = 6 + (8 * field_2_regions.size());
172         return retValue;
173     }
174
175     public short getSid()
176     {
177         return sid;
178     }
179
180     public int serialize(int offset, byte [] data)
181     {
182         int recordsize = getRecordSize();
183         int pos = 6;
184
185         LittleEndian.putShort(data, offset + 0, sid);
186         LittleEndian.putShort(data, offset + 2, ( short ) (recordsize - 4));
187         LittleEndian.putShort(data, offset + 4, getNumAreas());
188         for (int k = 0; k < getNumAreas(); k++)
189         {
190             MergedRegion region = getAreaAt(k);
191
192             //LittleEndian.putShort(data, offset + pos, region.row_from);
193
LittleEndian.putShort(data, offset + pos, ( short ) region.row_from);
194             pos += 2;
195             //LittleEndian.putShort(data, offset + pos, region.row_to);
196
LittleEndian.putShort(data, offset + pos, ( short ) region.row_to);
197             pos += 2;
198             LittleEndian.putShort(data, offset + pos, region.col_from);
199             pos += 2;
200             LittleEndian.putShort(data, offset + pos, region.col_to);
201             pos += 2;
202         }
203         return recordsize;
204     }
205
206     public String JavaDoc toString()
207     {
208         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
209
210         retval.append("[MERGEDCELLS]").append("\n");
211         retval.append(" .sid =").append(sid).append("\n");
212         retval.append(" .numregions =").append(getNumAreas())
213             .append("\n");
214         for (int k = 0; k < getNumAreas(); k++)
215         {
216             MergedRegion region = ( MergedRegion ) field_2_regions.get(k);
217
218             retval.append(" .rowfrom =").append(region.row_from)
219                 .append("\n");
220             retval.append(" .colfrom =").append(region.col_from)
221                 .append("\n");
222             retval.append(" .rowto =").append(region.row_to)
223                 .append("\n");
224             retval.append(" .colto =").append(region.col_to)
225                 .append("\n");
226         }
227         retval.append("[MERGEDCELLS]").append("\n");
228         return retval.toString();
229     }
230
231     protected void validateSid(short id)
232     {
233         if (id != sid)
234         {
235             throw new RecordFormatException("NOT A MERGEDCELLS RECORD!! "
236                                             + id);
237         }
238     }
239
240     /**
241      * this is a low level representation of a MergedRegion of cells. It is an
242      * inner class because we do not want it used without reference to this class.
243      *
244      */

245
246     public class MergedRegion
247     {
248
249         /**
250          * create a merged region all in one stroke.
251          */

252
253         //public MergedRegion(short row_from, short row_to, short col_from,
254
public MergedRegion(int row_from, int row_to, short col_from,
255                             short col_to)
256         {
257             this.row_from = row_from;
258             this.row_to = row_to;
259             this.col_from = col_from;
260             this.col_to = col_to;
261         }
262
263         /**
264          * upper lefthand corner row
265          */

266
267         //public short row_from;
268
public int row_from;
269
270         /**
271          * lower right hand corner row
272          */

273
274         //public short row_to;
275
public int row_to;
276
277         /**
278          * upper right hand corner col
279          */

280
281         public short col_from;
282
283         /**
284          * lower right hand corner col
285          */

286
287         public short col_to;
288     }
289
290     public Object JavaDoc clone() {
291         MergeCellsRecord rec = new MergeCellsRecord();
292         rec.field_2_regions = new ArrayList JavaDoc();
293         Iterator JavaDoc iterator = field_2_regions.iterator();
294         while (iterator.hasNext()) {
295            MergedRegion oldRegion = (MergedRegion)iterator.next();
296            rec.addArea(oldRegion.row_from, oldRegion.col_from, oldRegion.row_to, oldRegion.col_to);
297         }
298         
299         return rec;
300     }
301 }
302
Popular Tags