KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > List


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

50
51 package com.lowagie.text;
52
53 import java.util.ArrayList JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 import com.lowagie.text.factories.RomanAlphabetFactory;
57
58 /**
59  * A <CODE>List</CODE> contains several <CODE>ListItem</CODE>s.
60  * <P>
61  * <B>Example 1:</B>
62  * <BLOCKQUOTE><PRE>
63  * <STRONG>List list = new List(true, 20);</STRONG>
64  * <STRONG>list.add(new ListItem("First line"));</STRONG>
65  * <STRONG>list.add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));</STRONG>
66  * <STRONG>list.add(new ListItem("Third line"));</STRONG>
67  * </PRE></BLOCKQUOTE>
68  *
69  * The result of this code looks like this:
70  * <OL>
71  * <LI>
72  * First line
73  * </LI>
74  * <LI>
75  * The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?
76  * </LI>
77  * <LI>
78  * Third line
79  * </LI>
80  * </OL>
81  *
82  * <B>Example 2:</B>
83  * <BLOCKQUOTE><PRE>
84  * <STRONG>List overview = new List(false, 10);</STRONG>
85  * <STRONG>overview.add(new ListItem("This is an item"));</STRONG>
86  * <STRONG>overview.add("This is another item");</STRONG>
87  * </PRE></BLOCKQUOTE>
88  *
89  * The result of this code looks like this:
90  * <UL>
91  * <LI>
92  * This is an item
93  * </LI>
94  * <LI>
95  * This is another item
96  * </LI>
97  * </UL>
98  *
99  * @see Element
100  * @see ListItem
101  */

102
103 public class List implements TextElementArray {
104     
105     // constants
106

107     /** a possible value for the numbered parameter */
108     public static final boolean ORDERED = true;
109     /** a possible value for the numbered parameter */
110     public static final boolean UNORDERED = false;
111     /** a possible value for the lettered parameter */
112     public static final boolean NUMERICAL = false;
113     /** a possible value for the lettered parameter */
114     public static final boolean ALPHABETICAL = true;
115     /** a possible value for the lettered parameter */
116     public static final boolean UPPERCASE = false;
117     /** a possible value for the lettered parameter */
118     public static final boolean LOWERCASE = true;
119     
120     // member variables
121

122     /** This is the <CODE>ArrayList</CODE> containing the different <CODE>ListItem</CODE>s. */
123     protected ArrayList JavaDoc list = new ArrayList JavaDoc();
124     
125     /** Indicates if the list has to be numbered. */
126     protected boolean numbered = false;
127     /** Indicates if the listsymbols are numerical or alphabetical. */
128     protected boolean lettered = false;
129     /** Indicates if the listsymbols are lowercase or uppercase. */
130     protected boolean lowercase = false;
131     /** Indicates if the indentation has to be set automatically. */
132     protected boolean autoindent = false;
133     /** Indicates if the indentation of all the items has to be aligned. */
134     protected boolean alignindent = false;
135     
136     /** This variable indicates the first number of a numbered list. */
137     protected int first = 1;
138     /** This is the listsymbol of a list that is not numbered. */
139     protected Chunk symbol = new Chunk("- ");
140     
141     /** The indentation of this list on the left side. */
142     protected float indentationLeft = 0;
143     /** The indentation of this list on the right side. */
144     protected float indentationRight = 0;
145     /** The indentation of the listitems. */
146     protected float symbolIndent = 0;
147     
148     // constructors
149

150     /** Constructs a <CODE>List</CODE>. */
151     public List() {
152         this(false, false);
153     }
154     
155     /**
156      * Constructs a <CODE>List</CODE>.
157      * @param numbered a boolean
158      */

159     public List(boolean numbered) {
160         this(numbered, false);
161     }
162         
163     /**
164      * Constructs a <CODE>List</CODE>.
165      * @param numbered a boolean
166      * @param lettered has the list to be 'numbered' with letters
167      */

168     public List(boolean numbered, boolean lettered) {
169         this.numbered = numbered;
170         this.lettered = lettered;
171         this.autoindent = true;
172         this.alignindent = true;
173     }
174     
175     /**
176      * Constructs a <CODE>List</CODE>.
177      * <P>
178      * Remark: the parameter <VAR>symbolIndent</VAR> is important for instance when
179      * generating PDF-documents; it indicates the indentation of the listsymbol.
180      * It is not important for HTML-documents.
181      *
182      * @param numbered a boolean
183      * @param symbolIndent the indentation that has to be used for the listsymbol
184      */

185     public List(boolean numbered, float symbolIndent) {
186         this(numbered, false, symbolIndent);
187     }
188     
189     /**
190      * Creates a list
191      * @param numbered has the list to be numbered?
192      * @param lettered has the list to be 'numbered' with letters
193      * @param symbolIndent the indentation of the symbol
194      */

195     public List(boolean numbered, boolean lettered, float symbolIndent) {
196         this.numbered = numbered;
197         this.lettered = lettered;
198         this.symbolIndent = symbolIndent;
199     }
200     
201     // implementation of the Element-methods
202

203     /**
204      * Processes the element by adding it (or the different parts) to an
205      * <CODE>ElementListener</CODE>.
206      *
207      * @param listener an <CODE>ElementListener</CODE>
208      * @return <CODE>true</CODE> if the element was processed successfully
209      */

210     public boolean process(ElementListener listener) {
211         try {
212             for (Iterator JavaDoc i = list.iterator(); i.hasNext(); ) {
213                 listener.add((Element) i.next());
214             }
215             return true;
216         }
217         catch(DocumentException de) {
218             return false;
219         }
220     }
221     
222     /**
223      * Gets the type of the text element.
224      *
225      * @return a type
226      */

227     public int type() {
228         return Element.LIST;
229     }
230     
231     /**
232      * Gets all the chunks in this element.
233      *
234      * @return an <CODE>ArrayList</CODE>
235      */

236     public ArrayList JavaDoc getChunks() {
237         ArrayList JavaDoc tmp = new ArrayList JavaDoc();
238         for (Iterator JavaDoc i = list.iterator(); i.hasNext(); ) {
239             tmp.addAll(((Element) i.next()).getChunks());
240         }
241         return tmp;
242     }
243     
244     // methods to set the membervariables
245

246     /**
247      * Adds an <CODE>Object</CODE> to the <CODE>List</CODE>.
248      *
249      * @param o the object to add.
250      * @return true if adding the object succeeded
251      */

252     public boolean add(Object JavaDoc o) {
253         if (o instanceof ListItem) {
254             ListItem item = (ListItem) o;
255             if (numbered || lettered) {
256                 Chunk chunk;
257                 int index = first + list.size();
258                 if ( lettered )
259                     chunk = new Chunk(RomanAlphabetFactory.getString(index, lowercase), symbol.getFont());
260                 else
261                     chunk = new Chunk(String.valueOf(index), symbol.getFont());
262                 chunk.append(". ");
263                 item.setListSymbol(chunk);
264             }
265             else {
266                 item.setListSymbol(symbol);
267             }
268             item.setIndentationLeft(symbolIndent, autoindent);
269             item.setIndentationRight(0);
270             list.add(item);
271         }
272         else if (o instanceof List) {
273             List nested = (List) o;
274             nested.setIndentationLeft(nested.getIndentationLeft() + symbolIndent);
275             first--;
276             return list.add(nested);
277         }
278         else if (o instanceof String JavaDoc) {
279             return this.add(new ListItem((String JavaDoc) o));
280         }
281         return false;
282     }
283     
284     // extra methods
285

286     /** Makes sure all the items in the list have the same indentation. */
287     public void normalizeIndentation() {
288         float max = 0;
289         Element o;
290         for (Iterator JavaDoc i = list.iterator(); i.hasNext(); ) {
291             o = (Element)i.next();
292             if (o instanceof ListItem) {
293                 max = Math.max(max, ((ListItem)o).getIndentationLeft());
294             }
295         }
296         for (Iterator JavaDoc i = list.iterator(); i.hasNext(); ) {
297             o = (Element)i.next();
298             if (o instanceof ListItem) {
299                 ((ListItem)o).setIndentationLeft(max);
300             }
301         }
302     }
303     
304     // setters
305

306     /**
307      * @param numbered the numbered to set
308      */

309     public void setNumbered(boolean numbered) {
310         this.numbered = numbered;
311     }
312
313     /**
314      * @param lettered the lettered to set
315      */

316     public void setLettered(boolean lettered) {
317         this.lettered = lettered;
318     }
319
320     /**
321      * @param uppercase the uppercase to set
322      */

323     public void setLowercase(boolean uppercase) {
324         this.lowercase = uppercase;
325     }
326
327     /**
328      * @param autoindent the autoindent to set
329      */

330     public void setAutoindent(boolean autoindent) {
331         this.autoindent = autoindent;
332     }
333     /**
334      * @param alignindent the alignindent to set
335      */

336     public void setAlignindent(boolean alignindent) {
337         this.alignindent = alignindent;
338     }
339     
340     /**
341      * Sets the number that has to come first in the list.
342      *
343      * @param first a number
344      */

345     public void setFirst(int first) {
346         this.first = first;
347     }
348     
349     /**
350      * Sets the listsymbol.
351      *
352      * @param symbol a <CODE>Chunk</CODE>
353      */

354     public void setListSymbol(Chunk symbol) {
355         this.symbol = symbol;
356     }
357     
358     /**
359      * Sets the listsymbol.
360      * <P>
361      * This is a shortcut for <CODE>setListSymbol(Chunk symbol)</CODE>.
362      *
363      * @param symbol a <CODE>String</CODE>
364      */

365     public void setListSymbol(String JavaDoc symbol) {
366         this.symbol = new Chunk(symbol);
367     }
368     
369     /**
370      * Sets the indentation of this paragraph on the left side.
371      *
372      * @param indentation the new indentation
373      */

374     public void setIndentationLeft(float indentation) {
375         this.indentationLeft = indentation;
376     }
377     
378     /**
379      * Sets the indentation of this paragraph on the right side.
380      *
381      * @param indentation the new indentation
382      */

383     public void setIndentationRight(float indentation) {
384         this.indentationRight = indentation;
385     }
386
387     /**
388      * @param symbolIndent the symbolIndent to set
389      */

390     public void setSymbolIndent(float symbolIndent) {
391         this.symbolIndent = symbolIndent;
392     }
393     
394     // methods to retrieve information
395

396     /**
397      * Gets all the items in the list.
398      *
399      * @return an <CODE>ArrayList</CODE> containing <CODE>ListItem</CODE>s.
400      */

401     public ArrayList JavaDoc getItems() {
402         return list;
403     }
404     
405     /**
406      * Gets the size of the list.
407      *
408      * @return a <CODE>size</CODE>
409      */

410     public int size() {
411         return list.size();
412     }
413
414     /**
415      * Returns <CODE>true</CODE> if the list is empty.
416      *
417      * @return <CODE>true</CODE> if the list is empty
418      */

419     public boolean isEmpty() {
420         return list.isEmpty();
421     }
422
423     /**
424      * Gets the leading of the first listitem.
425      *
426      * @return a <CODE>leading</CODE>
427      */

428     public float getTotalLeading() {
429         if (list.size() < 1) {
430             return -1;
431         }
432         ListItem item = (ListItem) list.get(0);
433         return item.getTotalLeading();
434     }
435     
436     // getters
437

438     /**
439      * Checks if the list is numbered.
440      * @return <CODE>true</CODE> if the list is numbered, <CODE>false</CODE> otherwise.
441      */

442     
443     public boolean isNumbered() {
444         return numbered;
445     }
446
447     /**
448      * Checks if the list is lettered.
449      * @return <CODE>true</CODE> if the list is lettered, <CODE>false</CODE> otherwise.
450      */

451     public boolean isLettered() {
452         return lettered;
453     }
454
455     /**
456      * Checks if the list lettering is lowercase.
457      * @return <CODE>true</CODE> if it is lowercase, <CODE>false</CODE> otherwise.
458      */

459     public boolean isLowercase() {
460         return lowercase;
461     }
462     
463     /**
464      * Checks if the indentation of list items is done automatically.
465      * @return the autoindent
466      */

467     public boolean isAutoindent() {
468         return autoindent;
469     }
470     
471     /**
472      * Checks if all the listitems should be aligned.
473      * @return the alignindent
474      */

475     public boolean isAlignindent() {
476         return alignindent;
477     }
478
479     /**
480      * Gets the first number .
481      * @return a number
482      */

483     public int getFirst() {
484         return first;
485     }
486
487     /**
488      * Gets the Chunk containing the symbol.
489      * @return a Chunk with a symbol
490      */

491     public Chunk getSymbol() {
492         return symbol;
493     }
494
495     /**
496      * Gets the indentation of this paragraph on the left side.
497      * @return the indentation
498      */

499     public float getIndentationLeft() {
500         return indentationLeft;
501     }
502
503     /**
504      * Gets the indentation of this paragraph on the right side.
505      * @return the indentation
506      */

507     public float getIndentationRight() {
508         return indentationRight;
509     }
510
511     /**
512      * Gets the symbol indentation.
513      * @return the symbol indentation
514      */

515     public float getSymbolIndent() {
516         return symbolIndent;
517     }
518     
519     // deprecated constructor and methods
520
/**
521      * Returns a <CODE>List</CODE> that has been constructed taking in account
522      * the value of some <VAR>attributes</VAR>.
523      *
524      * @param attributes Some attributes
525      * @deprecated use ElementFactory.getList(attributes);
526      */

527
528     public List(java.util.Properties JavaDoc attributes) {
529         this();
530         List l = com.lowagie.text.factories.ElementFactory.getList(attributes);
531         this.list = l.list;
532         this.numbered = l.numbered;
533         this.lettered = l.lettered;
534         this.lowercase = l.lowercase;
535         this.autoindent = l.autoindent;
536         this.alignindent = l.alignindent;
537         this.first = l.first;
538         this.symbol = l.symbol;
539         this.indentationLeft = l.indentationLeft;
540         this.indentationRight = l.indentationRight;
541         this.symbolIndent = l.symbolIndent;
542     }
543     
544     /**
545      * Checks if the list lettering is lowercase.
546      * @return <CODE>true</CODE> if it is lowercase, <CODE>false</CODE> otherwise.
547      * @deprecated use isLowercase();
548      */

549     public boolean isLowerCase() {
550         return isLowercase();
551     }
552     
553     /**
554      * Gets the first number .
555      * @return a number
556      * @deprecated use getFirst();
557      */

558     public int first() {
559         return getFirst();
560     }
561     
562     /**
563      * Gets the Chunk containing the symbol.
564      * @return a Chunk with a symbol
565      * @deprecated use getSymbol();
566      */

567     public Chunk symbol() {
568         return getSymbol();
569     }
570     
571     /**
572      * Gets the indentation of this paragraph on the left side.
573      * @return the indentation
574      * @deprecated use getIndentationLeft();
575      */

576     
577     public float indentationLeft() {
578         return indentationLeft;
579     }
580     
581     /**
582      * Gets the indentation of this paragraph on the right side.
583      * @return the indentation
584      * @deprecated use getIndentationRight();
585      */

586     
587     public float indentationRight() {
588         return getIndentationRight();
589     }
590
591     /**
592      * Gets the symbol indentation.
593      * @return the symbol indentation
594      * @deprecated use getSymbolIndent();
595      */

596     public float symbolIndent() {
597         return getSymbolIndent();
598     }
599 }
Popular Tags