KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > events > IndexEvents


1 /*
2  * Copyright 2005 by Michael Niedermair.
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999-2005 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000-2005 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47 package com.lowagie.text.pdf.events;
48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.Comparator JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.TreeMap JavaDoc;
56
57 import com.lowagie.text.Chunk;
58 import com.lowagie.text.Document;
59 import com.lowagie.text.Rectangle;
60 import com.lowagie.text.pdf.PdfPageEventHelper;
61 import com.lowagie.text.pdf.PdfWriter;
62
63 /**
64  * Class for an index.
65  *
66  * @author Michael Niedermair
67  */

68 public class IndexEvents extends PdfPageEventHelper {
69
70     /**
71      * keeps the indextag with the pagenumber
72      */

73     private Map JavaDoc indextag = new TreeMap JavaDoc();
74
75     /**
76      * All the text that is passed to this event, gets registered in the indexentry.
77      *
78      * @see com.lowagie.text.pdf.PdfPageEventHelper#onGenericTag(
79      * com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document,
80      * com.lowagie.text.Rectangle, java.lang.String)
81      */

82     public void onGenericTag(PdfWriter writer, Document document,
83             Rectangle rect, String JavaDoc text) {
84         indextag.put(text, new Integer JavaDoc(writer.getPageNumber()));
85     }
86
87     // --------------------------------------------------------------------
88
/**
89      * indexcounter
90      */

91     private long indexcounter = 0;
92
93     /**
94      * the list for the index entry
95      */

96     private List JavaDoc indexentry = new ArrayList JavaDoc();
97
98     /**
99      * Create an index entry.
100      *
101      * @param text The text for the Chunk.
102      * @param in1 The first level.
103      * @param in2 The second level.
104      * @param in3 The third level.
105      * @return Returns the Chunk.
106      */

107     public Chunk create(final String JavaDoc text, final String JavaDoc in1, final String JavaDoc in2,
108             final String JavaDoc in3) {
109
110         Chunk chunk = new Chunk(text);
111         String JavaDoc tag = "idx_" + (indexcounter++);
112         chunk.setGenericTag(tag);
113         chunk.setLocalDestination(tag);
114         Entry entry = new Entry(in1, in2, in3, tag);
115         indexentry.add(entry);
116         return chunk;
117     }
118
119     /**
120      * Create an index entry.
121      *
122      * @param text The text for the Chunk.
123      * @param in1 The first level.
124      * @return Returns the Chunk.
125      */

126     public Chunk create(final String JavaDoc text, final String JavaDoc in1) {
127         return create(text, in1, "", "");
128     }
129
130     /**
131      * Create an index entry.
132      *
133      * @param text The text for the Chunk.
134      * @param in1 The first level.
135      * @param in2 The second level.
136      * @return Returns the Chunk.
137      */

138     public Chunk create(final String JavaDoc text, final String JavaDoc in1, final String JavaDoc in2) {
139         return create(text, in1, in2, "");
140     }
141
142     /**
143      * Create an index entry.
144      *
145      * @param text The text.
146      * @param in1 The first level.
147      * @param in2 The second level.
148      * @param in3 The third level.
149      */

150     public void create(final Chunk text, final String JavaDoc in1, final String JavaDoc in2,
151             final String JavaDoc in3) {
152
153         String JavaDoc tag = "idx_" + (indexcounter++);
154         text.setGenericTag(tag);
155         text.setLocalDestination(tag);
156         Entry entry = new Entry(in1, in2, in3, tag);
157         indexentry.add(entry);
158     }
159
160     /**
161      * Create an index entry.
162      *
163      * @param text The text.
164      * @param in1 The first level.
165      */

166     public void create(final Chunk text, final String JavaDoc in1) {
167         create(text, in1, "", "");
168     }
169
170     /**
171      * Create an index entry.
172      *
173      * @param text The text.
174      * @param in1 The first level.
175      * @param in2 The second level.
176      */

177     public void create(final Chunk text, final String JavaDoc in1, final String JavaDoc in2) {
178         create(text, in1, in2, "");
179     }
180
181     /**
182      * Comparator for sorting the index
183      */

184     private Comparator JavaDoc comparator = new Comparator JavaDoc() {
185
186         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
187             Entry en1 = (Entry) arg0;
188             Entry en2 = (Entry) arg1;
189
190             int rt = 0;
191             if (en1.getIn1() != null && en2.getIn1() != null) {
192                 if ((rt = en1.getIn1().compareToIgnoreCase(en2.getIn1())) == 0) {
193                     // in1 equals
194
if (en1.getIn2() != null && en2.getIn2() != null) {
195                         if ((rt = en1.getIn2()
196                                 .compareToIgnoreCase(en2.getIn2())) == 0) {
197                             // in2 equals
198
if (en1.getIn3() != null && en2.getIn3() != null) {
199                                 rt = en1.getIn3().compareToIgnoreCase(
200                                         en2.getIn3());
201                             }
202                         }
203                     }
204                 }
205             }
206             return rt;
207         }
208     };
209
210     /**
211      * Set the comparator.
212      * @param aComparator The comparator to set.
213      */

214     public void setComparator(Comparator JavaDoc aComparator) {
215         comparator = aComparator;
216     }
217
218     /**
219      * Returns the sorted list with the entries and the collected page numbers.
220      * @return Returns the sorted list with the entries and teh collected page numbers.
221      */

222     public List JavaDoc getSortedEntries() {
223
224         Map JavaDoc grouped = new HashMap JavaDoc();
225
226         for (int i = 0; i < indexentry.size(); i++) {
227             Entry e = (Entry) indexentry.get(i);
228             String JavaDoc key = e.getKey();
229
230             Entry master = (Entry) grouped.get(key);
231             if (master != null) {
232                 master.addPageNumberAndTag(e.getPageNumber(), e.getTag());
233             } else {
234                 e.addPageNumberAndTag(e.getPageNumber(), e.getTag());
235                 grouped.put(key, e);
236             }
237         }
238
239         // copy to a list and sort it
240
List JavaDoc sorted = new ArrayList JavaDoc(grouped.values());
241         Collections.sort(sorted, comparator);
242         return sorted;
243     }
244
245     // --------------------------------------------------------------------
246
/**
247      * Class for an index entry.
248      * <p>
249      * In the first step, only in1, in2,in3 and tag are used.
250      * After the collections of the index entries, pagenumbers are used.
251      * </p>
252      */

253     public class Entry {
254
255         /**
256          * first level
257          */

258         private String JavaDoc in1;
259
260         /**
261          * second level
262          */

263         private String JavaDoc in2;
264
265         /**
266          * third level
267          */

268         private String JavaDoc in3;
269
270         /**
271          * the tag
272          */

273         private String JavaDoc tag;
274
275         /**
276          * the lsit of all page numbers.
277          */

278         private List JavaDoc pagenumbers = new ArrayList JavaDoc();
279
280         /**
281          * the lsit of all tags.
282          */

283         private List JavaDoc tags = new ArrayList JavaDoc();
284
285         /**
286          * Create a new object.
287          * @param aIn1 The first level.
288          * @param aIn2 The second level.
289          * @param aIn3 The third level.
290          * @param aTag The tag.
291          */

292         public Entry(final String JavaDoc aIn1, final String JavaDoc aIn2, final String JavaDoc aIn3,
293                 final String JavaDoc aTag) {
294             in1 = aIn1;
295             in2 = aIn2;
296             in3 = aIn3;
297             tag = aTag;
298         }
299
300         /**
301          * Returns the in1.
302          * @return Returns the in1.
303          */

304         public String JavaDoc getIn1() {
305             return in1;
306         }
307
308         /**
309          * Returns the in2.
310          * @return Returns the in2.
311          */

312         public String JavaDoc getIn2() {
313             return in2;
314         }
315
316         /**
317          * Returns the in3.
318          * @return Returns the in3.
319          */

320         public String JavaDoc getIn3() {
321             return in3;
322         }
323
324         /**
325          * Returns the tag.
326          * @return Returns the tag.
327          */

328         public String JavaDoc getTag() {
329             return tag;
330         }
331
332         /**
333          * Returns the pagenumer for this entry.
334          * @return Returns the pagenumer for this entry.
335          */

336         public int getPageNumber() {
337             int rt = -1;
338             Integer JavaDoc i = (Integer JavaDoc) indextag.get(tag);
339             if (i != null) {
340                 rt = i.intValue();
341             }
342             return rt;
343         }
344
345         /**
346          * Add a pagenumber.
347          * @param number The page number.
348          * @param tag
349          */

350         public void addPageNumberAndTag(final int number, final String JavaDoc tag) {
351             pagenumbers.add(new Integer JavaDoc(number));
352             tags.add(tag);
353         }
354
355         /**
356          * Returns the key for the map-entry.
357          * @return Returns the key for the map-entry.
358          */

359         public String JavaDoc getKey() {
360             return in1 + "!" + in2 + "!" + in3;
361         }
362
363         /**
364          * Returns the pagenumbers.
365          * @return Returns the pagenumbers.
366          */

367         public List JavaDoc getPagenumbers() {
368             return pagenumbers;
369         }
370
371         /**
372          * Returns the tags.
373          * @return Returns the tags.
374          */

375         public List JavaDoc getTags() {
376             return tags;
377         }
378
379         /**
380          * print the entry (only for test)
381          * @return the toString implementation of the entry
382          */

383         public String JavaDoc toString() {
384             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
385             buf.append(in1).append(' ');
386             buf.append(in2).append(' ');
387             buf.append(in3).append(' ');
388             for (int i = 0; i < pagenumbers.size(); i++) {
389                 buf.append(pagenumbers.get(i)).append(' ');
390             }
391             return buf.toString();
392         }
393     }
394 }
Popular Tags