using System;
using System.Collections;
using System.util;
using iTextSharp.text.factories;
/*
* $Id: List.cs,v 1.20 2008/05/13 11:25:11 psoares33 Exp $
*
*
* Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text {
///
/// A List contains several ListItems.
///
///
/// Example 1:
///
/// List list = new List(true, 20);
/// list.Add(new ListItem("First line"));
/// 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?"));
/// list.Add(new ListItem("Third line"));
///
///
/// The result of this code looks like this:
///
/// -
/// First line
///
/// -
/// The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?
///
/// -
/// Third line
///
///
///
/// Example 2:
///
/// List overview = new List(false, 10);
/// overview.Add(new ListItem("This is an item"));
/// overview.Add("This is another item");
///
///
/// The result of this code looks like this:
///
/// -
/// This is an item
///
/// -
/// This is another item
///
///
///
///
///
public class List : ITextElementArray {
// membervariables
/** a possible value for the numbered parameter */
public const bool ORDERED = true;
/** a possible value for the numbered parameter */
public const bool UNORDERED = false;
/** a possible value for the lettered parameter */
public const bool NUMERICAL = false;
/** a possible value for the lettered parameter */
public const bool ALPHABETICAL = true;
/** a possible value for the lettered parameter */
public const bool UPPERCASE = false;
/** a possible value for the lettered parameter */
public const bool LOWERCASE = true;
/// This is the ArrayList containing the different ListItems.
protected ArrayList list = new ArrayList();
/** Indicates if the list has to be numbered. */
protected bool numbered = false;
/** Indicates if the listsymbols are numerical or alphabetical. */
protected bool lettered = false;
/** Indicates if the listsymbols are lowercase or uppercase. */
protected bool lowercase = false;
/** Indicates if the indentation has to be set automatically. */
protected bool autoindent = false;
/** Indicates if the indentation of all the items has to be aligned. */
protected bool alignindent = false;
/// This variable indicates the first number of a numbered list.
protected int first = 1;
/// This is the listsymbol of a list that is not numbered.
protected Chunk symbol = new Chunk("-");
/**
* In case you are using numbered/lettered lists, this String is added before the number/letter.
* @since iText 2.1.1
*/
protected String preSymbol = "";
/**
* In case you are using numbered/lettered lists, this String is added after the number/letter.
* @since iText 2.1.1
*/
protected String postSymbol = ". ";
/// The indentation of this list on the left side.
protected float indentationLeft = 0;
/// The indentation of this list on the right side.
protected float indentationRight = 0;
/// The indentation of the listitems.
protected float symbolIndent = 0;
// constructors
/**
* Constructs a List
.
*/
public List() : this(false, false) {
}
/**
* Constructs a List
with a specific symbol indentation.
* @param symbolIndent the symbol indentation
* @since iText 2.0.8
*/
public List(float symbolIndent) {
this.symbolIndent = symbolIndent;
}
/**
* Constructs a List
.
*
* @param numbered a bool
*/
public List(bool numbered) : this(numbered, false) {
}
/**
* Constructs a List
.
*
* @param numbered a bool
* @param lettered has the list to be 'numbered' with letters
*/
public List(bool numbered, bool lettered) {
this.numbered = numbered;
this.lettered = lettered;
this.autoindent = true;
this.alignindent = true;
}
///
/// Constructs a List.
///
///
/// the parameter symbolIndent is important for instance when
/// generating PDF-documents; it indicates the indentation of the listsymbol.
///
/// a bool
/// the indentation that has to be used for the listsymbol
public List(bool numbered, float symbolIndent) : this(numbered, false, symbolIndent) {
}
///
/// Constructs a List.
///
/// a bool
/// a bool
/// the indentation that has to be used for the listsymbol
public List(bool numbered, bool lettered, float symbolIndent ) {
this.numbered = numbered;
this.lettered = lettered;
this.symbolIndent = symbolIndent;
}
// implementation of the Element-methods
///
/// Processes the element by adding it (or the different parts) to an
/// IElementListener.
///
/// an IElementListener
/// true if the element was processed successfully
public bool Process(IElementListener listener) {
try {
foreach (IElement ele in list) {
listener.Add(ele);
}
return true;
}
catch (DocumentException) {
return false;
}
}
///
/// Gets the type of the text element.
///
/// a type
public int Type {
get {
return Element.LIST;
}
}
///
/// Gets all the chunks in this element.
///
/// an ArrayList
public ArrayList Chunks {
get {
ArrayList tmp = new ArrayList();
foreach (IElement ele in list) {
tmp.AddRange(ele.Chunks);
}
return tmp;
}
}
// methods to set the membervariables
///
/// Adds an Object to the List.
///
/// the object to add
/// true is successful
public virtual bool Add(Object o) {
if (o is ListItem) {
ListItem item = (ListItem) o;
if (numbered || lettered) {
Chunk chunk = new Chunk(preSymbol, symbol.Font);
int index = first + list.Count;
if (lettered)
chunk.Append(RomanAlphabetFactory.GetString(index, lowercase));
else
chunk.Append(index.ToString());
chunk.Append(postSymbol);
item.ListSymbol = chunk;
}
else {
item.ListSymbol = symbol;
}
item.SetIndentationLeft(symbolIndent, autoindent);
item.IndentationRight = 0;
list.Add(item);
return true;
}
else if (o is List) {
List nested = (List) o;
nested.IndentationLeft = nested.IndentationLeft + symbolIndent;
first--;
list.Add(nested);
return true;
}
else if (o is string) {
return this.Add(new ListItem((string) o));
}
return false;
}
// extra methods
/** Makes sure all the items in the list have the same indentation. */
public void NormalizeIndentation() {
float max = 0;
foreach (IElement o in list) {
if (o is ListItem) {
max = Math.Max(max, ((ListItem)o).IndentationLeft);
}
}
foreach (IElement o in list) {
if (o is ListItem) {
((ListItem)o).IndentationLeft = max;
}
}
}
//setters/getters
public bool Numbered {
set {
numbered = value;
}
get {
return numbered;
}
}
public bool Lettered {
set {
lettered = value;
}
get {
return lettered;
}
}
public bool Lowercase {
set {
lowercase = value;
}
get {
return lowercase;
}
}
public bool Autoindent {
set {
autoindent = value;
}
get {
return autoindent;
}
}
public bool Alignindent {
set {
alignindent = value;
}
get {
return alignindent;
}
}
///
/// Get/set the first number
///
/// an int
public int First {
get {
return first;
}
set {
this.first = value;
}
}
///
/// Sets the symbol
///
/// a Chunk
public Chunk ListSymbol {
set {
this.symbol = value;
}
}
///
/// Sets the listsymbol.
///
///
/// This is a shortcut for SetListSymbol(Chunk symbol).
///
/// a string
public void SetListSymbol(string symbol) {
this.symbol = new Chunk(symbol);
}
///
/// Get/set the indentation of this paragraph on the left side.
///
/// the indentation
public float IndentationLeft {
get {
return indentationLeft;
}
set {
this.indentationLeft = value;
}
}
///
/// Get/set the indentation of this paragraph on the right side.
///
/// the indentation
public float IndentationRight {
get {
return indentationRight;
}
set {
this.indentationRight = value;
}
}
///
/// Gets the symbol indentation.
///
/// the symbol indentation
public float SymbolIndent {
set {
symbolIndent = value;
}
get {
return symbolIndent;
}
}
/**
* @see com.lowagie.text.Element#isContent()
* @since iText 2.0.8
*/
public bool IsContent() {
return true;
}
/**
* @see com.lowagie.text.Element#isNestable()
* @since iText 2.0.8
*/
public bool IsNestable() {
return true;
}
// methods to retrieve information
///
/// Gets all the items in the list.
///
/// an ArrayList containing ListItems
public ArrayList Items {
get {
return list;
}
}
///
/// Gets the size of the list.
///
/// a size
public int Size {
get {
return list.Count;
}
}
/**
* Returns true
if the list is empty.
*
* @return true
if the list is empty
*/
public virtual bool IsEmpty() {
return list.Count == 0;
}
///
/// Gets the leading of the first listitem.
///
/// a leading
public float TotalLeading {
get {
if (list.Count < 1) {
return -1;
}
ListItem item = (ListItem)list[0];
return item.TotalLeading;
}
}
///
/// Get/set the symbol indentation.
///
/// a Chunk
public Chunk Symbol {
get {
return symbol;
}
set {
this.symbol = value;
}
}
/**
* Returns the String that is after a number or letter in the list symbol.
* @return the String that is after a number or letter in the list symbol
* @since iText 2.1.1
*/
public String getPostSymbol() {
return postSymbol;
}
/**
* Sets the String that has to be added after a number or letter in the list symbol.
* @since iText 2.1.1
* @param postSymbol the String that has to be added after a number or letter in the list symbol.
*/
public String PostSymbol {
set {
postSymbol = value;
}
get {
return postSymbol;
}
}
/**
* Sets the String that has to be added before a number or letter in the list symbol.
* @since iText 2.1.1
* @param preSymbol the String that has to be added before a number or letter in the list symbol.
*/
public String PreSymbol {
set {
preSymbol = value;
}
get {
return preSymbol;
}
}
}
}