KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.poi.hssf.record;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.poi.util.LittleEndian;
28
29 /**
30  * <p>Record that contains the functionality page breaks (horizontal and vertical)</p>
31  *
32  * <p>The other two classes just specifically set the SIDS for record creation.</p>
33  *
34  * <p>REFERENCE: Microsoft Excel SDK page 322 and 420</p>
35  *
36  * @see HorizontalPageBreakRecord
37  * @see VerticalPageBreakRecord
38  *
39  * @author Danny Mui (dmui at apache dot org)
40  */

41 public class PageBreakRecord extends Record {
42    public static final short HORIZONTAL_SID = (short)0x1B;
43    public static final short VERTICAL_SID = (short)0x1A;
44    public short sid;
45    private short numBreaks;
46    private List JavaDoc breaks;
47    private Map JavaDoc BreakMap;
48       
49     /**
50      * Since both records store 2byte integers (short), no point in
51      * differentiating it in the records.
52      * <p>
53      * The subs (rows or columns, don't seem to be able to set but excel sets
54      * them automatically)
55      */

56     public class Break
57     {
58
59         public short main;
60         public short subFrom;
61         public short subTo;
62
63         public Break(short main, short subFrom, short subTo)
64         {
65             this.main = main;
66             this.subFrom = subFrom;
67             this.subTo = subTo;
68         }
69     }
70
71     public PageBreakRecord()
72     {
73
74     }
75
76     /**
77      *
78      * @param sid
79      */

80     public PageBreakRecord(short sid) {
81        super();
82        this.sid = sid;
83     }
84
85     public PageBreakRecord(short id, short size, byte data[])
86     {
87         super(id, size, data);
88         this.sid = id;
89     }
90
91     public PageBreakRecord(short id, short size, byte data[], int offset)
92     {
93         super(id, size, data, offset);
94         this.sid = id;
95     }
96
97     protected void fillFields(byte data[], short size, int offset)
98     {
99           short loadedBreaks = LittleEndian.getShort(data, 0 + offset);
100         setNumBreaks(loadedBreaks);
101         int pos = 2;
102         for(int k = 0; k < loadedBreaks; k++)
103         {
104             addBreak((short)(LittleEndian.getShort(data, pos + offset) - 1), LittleEndian.getShort(data, pos + 2 + offset), LittleEndian.getShort(data, pos + 4 + offset));
105             pos += 6;
106         }
107
108     }
109
110     public short getSid()
111     {
112         return sid;
113     }
114
115     public int serialize(int offset, byte data[])
116     {
117         int recordsize = getRecordSize();
118         int pos = 6;
119         LittleEndian.putShort(data, offset + 0, getSid());
120         LittleEndian.putShort(data, offset + 2, (short)(recordsize - 4));
121         LittleEndian.putShort(data, offset + 4, getNumBreaks());
122         for(Iterator JavaDoc iterator = getBreaksIterator(); iterator.hasNext();)
123         {
124             Break Break = (Break)iterator.next();
125             LittleEndian.putShort(data, offset + pos, (short)(Break.main + 1));
126             pos += 2;
127             LittleEndian.putShort(data, offset + pos, Break.subFrom);
128             pos += 2;
129             LittleEndian.putShort(data, offset + pos, Break.subTo);
130             pos += 2;
131         }
132
133         return recordsize;
134     }
135
136     protected void validateSid(short id)
137     {
138         if(id != HORIZONTAL_SID && id != VERTICAL_SID)
139             throw new RecordFormatException("NOT A HorizontalPageBreak or VerticalPageBreak RECORD!! " + id);
140         else
141             return;
142     }
143
144     public short getNumBreaks()
145     {
146         return breaks != null ? (short)breaks.size() : numBreaks;
147     }
148
149     public void setNumBreaks(short numBreaks)
150     {
151         this.numBreaks = numBreaks;
152     }
153
154     public Iterator JavaDoc getBreaksIterator()
155     {
156         if(breaks == null)
157             return Collections.EMPTY_LIST.iterator();
158         else
159             return breaks.iterator();
160     }
161
162     public String JavaDoc toString()
163     {
164         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
165         
166         if (getSid() != HORIZONTAL_SID && getSid()!= VERTICAL_SID)
167             return "[INVALIDPAGEBREAK]\n .sid ="+getSid()+"[INVALIDPAGEBREAK]";
168         
169         String JavaDoc label;
170         String JavaDoc mainLabel;
171         String JavaDoc subLabel;
172         
173         if (getSid() == HORIZONTAL_SID) {
174            label = "HORIZONTALPAGEBREAK";
175            mainLabel = "row";
176            subLabel = "col";
177         } else {
178            label = "VERTICALPAGEBREAK";
179            mainLabel = "column";
180            subLabel = "row";
181         }
182         
183         retval.append("["+label+"]").append("\n");
184         retval.append(" .sid =").append(getSid()).append("\n");
185         retval.append(" .numbreaks =").append(getNumBreaks()).append("\n");
186         Iterator JavaDoc iterator = getBreaksIterator();
187         for(int k = 0; k < getNumBreaks(); k++)
188         {
189             Break region = (Break)iterator.next();
190             
191             retval.append(" .").append(mainLabel).append(" (zero-based) =").append(region.main).append("\n");
192             retval.append(" .").append(subLabel).append("From =").append(region.subFrom).append("\n");
193             retval.append(" .").append(subLabel).append("To =").append(region.subTo).append("\n");
194         }
195
196         retval.append("["+label+"]").append("\n");
197         return retval.toString();
198     }
199
200    /**
201     * Adds the page break at the specified parameters
202     * @param main Depending on sid, will determine row or column to put page break (zero-based)
203     * @param subFrom No user-interface to set (defaults to minumum, 0)
204     * @param subTo No user-interface to set
205     */

206     public void addBreak(short main, short subFrom, short subTo)
207     {
208         if(breaks == null)
209         {
210             breaks = new ArrayList JavaDoc(getNumBreaks() + 10);
211             BreakMap = new HashMap JavaDoc();
212         }
213         Integer JavaDoc key = new Integer JavaDoc(main);
214         Break region = (Break)BreakMap.get(key);
215         if(region != null)
216         {
217             region.main = main;
218             region.subFrom = subFrom;
219             region.subTo = subTo;
220         } else
221         {
222             region = new Break(main, subFrom, subTo);
223             breaks.add(region);
224         }
225         BreakMap.put(key, region);
226     }
227
228     /**
229      * Removes the break indicated by the parameter
230      * @param main (zero-based)
231      */

232     public void removeBreak(short main)
233     {
234         Integer JavaDoc rowKey = new Integer JavaDoc(main);
235         Break region = (Break)BreakMap.get(rowKey);
236         breaks.remove(region);
237         BreakMap.remove(rowKey);
238     }
239
240     public int getRecordSize()
241     {
242         return 6 + getNumBreaks() * 6;
243     }
244
245     /**
246      * Retrieves the region at the row/column indicated
247      * @param main FIXME: Document this!
248      * @return FIXME: Document this!
249      */

250     public Break getBreak(short main)
251     {
252         Integer JavaDoc rowKey = new Integer JavaDoc(main);
253         return (Break)BreakMap.get(rowKey);
254     }
255
256    /* Clones the page break record
257     * @see java.lang.Object#clone()
258     */

259    public Object JavaDoc clone() {
260       PageBreakRecord record = new PageBreakRecord(getSid());
261       Iterator JavaDoc iterator = getBreaksIterator();
262       while (iterator.hasNext()) {
263          Break original = (Break)iterator.next();
264          record.addBreak(original.main, original.subFrom, original.subTo);
265       }
266       return record;
267    }
268
269     
270 }
271
Popular Tags