Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER Copyright 2011 IBM. All rights reserved. Use is subject to license terms. Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at http://odftoolkit.org/docs/license.txt Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package org.odftoolkit.simple.text.list;
 
 
This class represents a list. It can contain list header, followed by list items.

Since:
0.4
 
 public class List {
 
 	private ListDecorator decorator;

Constructor ListItem, AbstractListContainer and List use only.

Parameters:
element the ODF element
 
 	List(TextListElement element) {
 		 = element;
 		// only getListType and addItem --> item.setParagraphDecorator() need
 		// decorator. We have fixed the problems when decorator is null in these
 		// two conditions. So decorator null is OK for a exist list. But this
 		// constructor should not be used on a new created TextListElement, as
 		// its style name may have not set.
 		 = null;
 	}

Constructor with ListContainer only. A bullet list with default style will be created.

Parameters:
container the container in where this list will be appended.
 
 	public List(ListContainer container) {
 		this(containernullnull);
 	}

Constructor with ListContainer and ListDecorator.

Parameters:
container the container in where this list will be appended.
decorator the ListDecorator of this list.
 
 	public List(ListContainer containerListDecorator decorator) {
 		this(containernulldecorator);
 	}

Constructor with ListContainer, ListDecorator and header.

Parameters:
container the container in where this list will be appended.
decorator the ListDecorator of this list.
header the header of this list.
	public List(ListContainer containerString headerListDecorator decorator) {
		OdfElement containerElement = container.getListContainerElement();
		OdfFileDom ownerDocument = (OdfFileDomcontainerElement.getOwnerDocument();
		 = ownerDocument.newOdfElement(TextListElement.class);
		containerElement.appendChild();
		setHeader(header);
		if (decorator == null) {
			Document doc = (DocumentownerDocument.getDocument();
			decorator = new BulletDecorator(doc);
		}
		this. = decorator;
		decorator.decorateList(this);
	}

Constructor with ListContainer, ListDecorator, header and numbering setting.

Parameters:
container the container in where this list will be appended.
decorator the ListDecorator of this list.
isContinueNumbering If isContinueNumbering is true, the numbering of this list is continuing, otherwise the numbering of this list starts from the beginning.
header the header of this list.
	public List(ListContainer containerString headerboolean isContinueNumberingListDecorator decorator) {
		this(containerheaderdecorator);
		setContinueNumbering(isContinueNumbering);
	}

Constructor with ListContainer, ListDecorator, header and continued list

Parameters:
container the container in where this list will be appended.
decorator the ListDecorator of this list.
continueList the continued list of this list.
header the header of this list.
	public List(ListContainer containerString headerList continueListListDecorator decorator) {
		this(containerheaderdecorator);
		setContinueList(continueList);
	}

Get the type of this list. The list type can be BULLET, NUMBER and IMAGE.

Returns:
the list type.
	public ListType getType() {
		if ( != null) {
else {
			try {
				OdfContentDom contentDocument = doc.getContentDom();
				OdfOfficeAutomaticStyles styles = contentDocument.getAutomaticStyles();
				OdfOfficeStyles documentStyles = doc.getDocumentStyles();
				OdfTextListStyle listStyle = styles.getListStyle(textStyleName);
				if (listStyle == null) {
					listStyle = documentStyles.getListStyle(textStyleName);
				}
				if (listStyle != null) {
					TextListLevelStyleElementBase listLevelStyle = listStyle.getLevel(1);
					if (listLevelStyle instanceof TextListLevelStyleBulletElement) {
else if (listLevelStyle instanceof TextListLevelStyleNumberElement) {
else if (listLevelStyle instanceof TextListLevelStyleImageElement) {
						return .;
					}
				}
catch (Exception e) {
				Logger.getLogger(List.class.getName()).log(.nulle);
			}
			return null;
		}
	}

Get the header of this list.

Returns:
the header of this list.
	public String getHeader() {
		String header = "";
		Node headerNode = .getFirstChild();
		if (headerNode instanceof TextListHeaderElement) {
			Node pNode = headerNode.getFirstChild();
			String splitString = "";
			while (pNode != null) {
				if (pNode instanceof TextPElement) {
					String content = pNode.getTextContent();
					if ((content != null) && (content.length() > 0)) {
						header = header + splitString + content;
						splitString = "\n";
					}
				}
				pNode = pNode.getNextSibling();
			}
		}
		if ("".equals(header)) {
			return null;
else {
			return header;
		}
	}

Set the header of this list. The exist header will be replaced.

Parameters:
header the header to be set.
	public void setHeader(String header) {
		if (header != null) {
			String[] headerContents = header.split("\n");
			TextListHeaderElement listHeaderElement = null;
			Node firstNode = .getFirstChild();
			if (firstNode instanceof TextListHeaderElement) {
				listHeaderElement = (TextListHeaderElementfirstNode;
				Node pElement = listHeaderElement.getFirstChild();
				while (pElement != null) {
					firstNode.removeChild(pElement);
					pElement = pElement.getNextSibling();
				}
else {
				listHeaderElement = ((OdfFileDom.getOwnerDocument())
				.insertBefore(listHeaderElementfirstNode);
			}
			for (String headerContent : headerContents) {
				TextPElement pElement = listHeaderElement.newTextPElement();
				pElement.setTextContent(headerContent);
			}
		}
	}

Set the ListDecorator of this list. The current ListDecorator will be replaced.

This is a useful method which can change the list type and style, even though it has been created.

Parameters:
decorator the ListDecorator to be used.
	public void setDecorator(ListDecorator decorator) {
		if (decorator != null) {
			this. = decorator;
			decorator.decorateList(this);
		}
	}

Add a list item by specifying a string value.

Parameters:
itemContent the list item content to be added.
Returns:
the added ListItem.
	public ListItem addItem(String itemContent) {
		ListItem item = new ListItem(listItemElement);
		item.setTextContent(itemContent);
		return item;
	}

Insert the specified ListItem at the specified location. The ListItem is inserted before the ListItem at the specified location.

Parameters:
location the index to insert. The start number is 0.
itemContent the list item content to be added.
Returns:
the added ListItem.
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public ListItem addItem(int locationString itemContent) {
		TextListItemElement listItemElement = ownerDocument.newOdfElement(TextListItemElement.class);
		Node refNode = getItemByLocation(location);
		.insertBefore(listItemElementrefNode);
		ListItem item = new ListItem(listItemElement);
		item.setTextContent(itemContent);
		return item;
	}

Add the specified list item in ListItem object.

Parameters:
item the list item to be added.
Returns:
the added ListItem.
	public ListItem addItem(ListItem item) {
		ListItem newItem = new ListItem(itemElement);
		return newItem;
	}

Insert a ListItem at the specified location. The ListItem is inserted before the ListItem at the specified location.

Parameters:
location the index to insert.
item the list item to add.
Returns:
the added ListItem.
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public ListItem addItem(int locationListItem item) {
		Node refNode = getItemByLocation(location);
		.insertBefore(itemElementrefNode);
		ListItem newItem = new ListItem(itemElement);
		return newItem;
	}

Add list items by specifying an array of string values.

Parameters:
items the list items to be added.
Returns:
the added items.
	public java.util.List<ListItemaddItems(String[] items) {
		java.util.List<ListItemitemList = new java.util.ArrayList<ListItem>();
		for (String itemString : items) {
			ListItem item = new ListItem(listItemElement);
			item.setTextContent(itemString);
			itemList.add(item);
		}
		return itemList;
	}

Insert the list items at the specified location in this List by giving an array of string values.

Parameters:
location the index to insert.
items the collection of list item contents.
Returns:
the added items.
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public java.util.List<ListItemaddItems(int locationString[] items) {
		java.util.List<ListItemlistCollection = new ArrayList<ListItem>();
		Node refNode = getItemByLocation(location);
		for (int i = items.length - 1; i >= 0; i--) {
			TextListItemElement listItemElement = ownerDocument.newOdfElement(TextListItemElement.class);
			.insertBefore(listItemElementrefNode);
			ListItem item = new ListItem(listItemElement);
			item.setTextContent(items[i]);
			refNode = listItemElement;
			listCollection.add(item);
		}
		return listCollection;
	}

Add list items by specifying an array of ListItem.

Parameters:
items the list items to be added.
	public java.util.List<ListItemaddItems(ListItem[] items) {
		java.util.List<ListItemitemList = new java.util.ArrayList<ListItem>();
		for (ListItem itemClone : items) {
			TextListItemElement itemElement = (TextListItemElement) (itemClone.getOdfElement().cloneNode(true));
			itemList.add(new ListItem(itemElement));
		}
		return itemList;
	}

Insert the list items at the certain location by specifying an array of ListItem.

Parameters:
location the index to insert.
items the collection of items.
Returns:
the added items
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public java.util.List<ListItemaddItems(int locationListItem[] items) {
		java.util.List<ListItemlistCollection = new ArrayList<ListItem>();
		Node refNode = getItemByLocation(location);
		for (int i = items.length - 1; i >= 0; i--) {
			TextListItemElement itemElement = (TextListItemElement) (items[i].getOdfElement().cloneNode(true));
			.insertBefore(itemElementrefNode);
			ListItem item = new ListItem(itemElement);
			refNode = itemElement;
			listCollection.add(item);
		}
		return listCollection;
	}

Return the item at the specified location in this List.

Parameters:
location the index of the element to be returned.
Returns:
the element at the specified location.
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public ListItem getItem(int location) {
		return new ListItem(getItemByLocation(location));
	}

Get all of the list items.

Returns:
all of list items.
		java.util.List<ListItemitemList = new java.util.ArrayList<ListItem>();
		Node firstNode = .getFirstChild();
		while (firstNode != null) {
			if (firstNode instanceof TextListItemElement) {
				itemList.add(new ListItem((TextListItemElementfirstNode));
			}
			firstNode = firstNode.getNextSibling();
		}
		return itemList;
	}

Return the number of direct child items in this List.

Returns:
the number of direct child items in this List.
	public int size() {
		int size = 0;
		Node firstNode = .getFirstChild();
		while (firstNode != null) {
			if (firstNode instanceof TextListItemElement) {
				size++;
			}
			firstNode = firstNode.getNextSibling();
		}
		return size;
	}

Replace the item at the specified location in this List with the specified item.

Parameters:
location the index to put the specified item.
item the new item to be added.
Returns:
the previous element at the index.
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public ListItem set(int locationListItem item) {
		Node oldNode = getItemByLocation(location);
		.replaceChild(itemElementoldNode);
		ListItem newItem = new ListItem(itemElement);
		return newItem;
	}

Replace the item at the specified location in this List with the specified item content.

Parameters:
location the index to insert. The start number is 0.
itemContent the list item content to be added.
Returns:
the previous element at the index.
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public ListItem set(int locationString itemContent) {
		TextListItemElement listItemElement = ownerDocument.newOdfElement(TextListItemElement.class);
		Node oldNode = getItemByLocation(location);
		.replaceChild(listItemElementoldNode);
		ListItem item = new ListItem(listItemElement);
		item.setTextContent(itemContent);
		return item;
	}

Remove the item at the specified location from this List.

Parameters:
location the index of the item to be removed.
Returns:
true if this List is modified, false otherwise.
Throws:
java.lang.IndexOutOfBoundsException when the location is out of the List range.
	public boolean removeItem(int location) {
		TextListItemElement itemElement = getItemByLocation(location);
		if (itemElement == null) {
			return false;
else {
			Document doc = (DocumentownerDocument.getDocument();
			return true;
		}
	}

Remove the specified item from this List.

Parameters:
item the item to be removed.
Returns:
true if this List is modified, false otherwise.
	public boolean removeItem(ListItem item) {
		TextListItemElement itemElement = item.getOdfElement();
		Document doc = (DocumentownerDocument.getDocument();
		Node removedNode = .removeChild(itemElement);
		if (removedNode == null) {
			return false;
else {
			return true;
		}
	}

Remove all the items in the collection from this list

Parameters:
items the collection of items to be removed.
Returns:
true if this List is modified, false otherwise.
	public boolean removeItems(java.util.List<ListItemitems) {
		boolean listChanged = false;
		for (ListItem item : items) {
			TextListItemElement itemElement = item.getOdfElement();
			Document doc = (DocumentownerDocument.getDocument();
			Node removedNode = .removeChild(itemElement);
			if (removedNode != null) {
				listChanged = true;
			}
		}
		return listChanged;
	}

Remove all items from this List.
	public void clear() {
		Node firstChild = .getFirstChild();
		while (firstChild != null) {
			if (firstChild instanceof TextListItemElement) {
				Node removedNode = firstChild;
				firstChild = firstChild.getNextSibling();
else {
				firstChild = firstChild.getNextSibling();
			}
		}
	}

Remove this list from its container.
	public void remove() {
		Node parentElement = .getParentNode();
		Document doc = (DocumentownerDocument.getDocument();
		parentElement.removeChild();
	}

Return whether the numbering of this list is continuing, or whether the numbering of the preceding list is continued or not.

Returns:
true if the numbering of this list is continuing, false if not.
	public boolean isContinueNumbering() {
		if (isContinueNumbering == null) {
			if (continueList != null) {
				return true;
else {
				return false;
			}
else {
			return isContinueNumbering;
		}
	}

Set whether the numbering of the preceding list is continued or not. This method will set the attribute "text:continue-numbering" of list element. As ODF specification describes, this attribute is ignored, if attribute <text:continue-list> is present, the user can call setContinueList(List) to set this attribute. This method is a easy way to set a list continue numbering, while setContinueList(List) is an advance way to set a list numbering. For example, there are three lists ListA, ListB and ListC in order. If the user call set ListC.setContinueNumbering, the first list item in ListC is the number of the last item in ListB incremented by one. It easy, no need to get the reference of ListB. While if the user need the first list item in ListC is the number of the last item in ListA incremented by one, he must use ListC.setContinueList(ListA).

Parameters:
isContinueNumbering If isContinueNumbering is true, and text:continue-list attribute is not present and the numbering style of the preceding list is the same as the current list, the number of the first list item in the current list is the number of the last item in the preceding list incremented by one.the list is continue numbering, otherwise if the text:continue-list attribute is not present, the numbering of the preceding list is not continued.
See also:
setContinueList(org.odftoolkit.simple.text.list.List)
	public void setContinueNumbering(boolean isContinueNumbering) {
		if (getType() == .) {
		}
	}

Get the preceding list whose numbering is continued by this list.

Now only support to get the continued list reference in the same ListContainer and the same Level.

Returns:
the continued list of this list. If the list has no continued list, it will return null.
	public List getContinueList() {
		List continueList = null;
			TextListElement continueListElement = null;
			if (continueListID != null) {
				Node parentElement = .getParentNode();
				Node firstNode = parentElement.getFirstChild();
				while (firstNode != null) {
					if (firstNode instanceof TextListElement) {
						TextListElement listElement = (TextListElementfirstNode;
						String xmlID = listElement.getXmlIdAttribute();
						if (continueListID.equals(xmlID)) {
							continueListElement = (TextListElementfirstNode;
							break;
						}
					}
					firstNode = firstNode.getNextSibling();
				}
else {
				while (preNode != null) {
					if (preNode instanceof TextListElement) {
						continueListElement = (TextListElementpreNode;
						break;
					}
					preNode = preNode.getPreviousSibling();
				}
			}
			continueList = new List(continueListElement);
		}
		return continueList;
	}

Set the list whose numbering is continued by this list. This is an advance way to set a list numbering. For example, there are three lists ListA, ListB and ListC in order. If the user needs the first list item in ListC is the number of the last item in ListA incremented by one, he must use ListC.setContinueList(ListA).

Parameters:
continueList the continued list of this list.
See also:
setContinueNumbering(boolean)
	public void setContinueList(List continueList) {
		if (getType() == .) {
			String xmlId = continueList.listElement.getXmlIdAttribute();
			if (xmlId != null) {
else {
				xmlId = getUniqueXMLID();
				continueList.listElement.setXmlIdAttribute(xmlId);
			}
		}
	}

Get the level of this list.

Every list has a list level. If a list is not contained in another list, its list level is 1. If a list is contained within another list, the list level of the contained list is the list level of the list in which it is contained incremented by one. If a list is contained in a table cell or text box, its list level returns to 1, even though the table or text box may be nested in another list. Every list with a list level of 1 defines a list and the counter domain for its list items and any sub list of that list. Each sub list starts a counter for its list items and any sub list it may contain.

Returns:
list level.
	public int getLevel() {
		int level = 1;
		Node parentNode = .getParentNode();
		while (parentNode != null) {
			if (parentNode instanceof TextListElement) {
				level++;
			}
			if (parentNode instanceof TableTableCellElementBase) {
				break;
			}
			parentNode = parentNode.getParentNode();
		}
		return level;
	}

Get the instance of TextListElement which represents this list.

Returns:
the instance of TextListElement.
		return ;
	}
	public String toString() {
		StringBuilder strBuilder = new StringBuilder();
		int level = getLevel();
		String levelPrefix = "";
		int i = 1;
		while (i < level) {
			levelPrefix += " ";
			i++;
		}
		String splitStr = "";
		String header = getHeader();
		if (header != null) {
			strBuilder.append(levelPrefix);
			strBuilder.append(header);
			splitStr = "\n";
		}
		String itemPrefix = "• ";
		int j = 0;
		if (getType() == .) {
			itemPrefix = ". ";
			j = 1;
		}
		for (ListItem item : getItems()) {
			strBuilder.append(splitStr);
			strBuilder.append(levelPrefix);
			if (j > 0) {
				strBuilder.append(j++);
			}
			strBuilder.append(itemPrefix);
			strBuilder.append(item.toString());
			splitStr = "\n";
		}
		return strBuilder.toString();
	}
	// find the insert node.
	private TextListItemElement getItemByLocation(int location) {
		if (location < 0) {
			throw new IndexOutOfBoundsException("the location " + location + " is is out of the List range.");
		}
		Node firstNode = .getFirstChild();
		TextListItemElement positionNode = null;
		int i = 0;
		while (firstNode != null) {
			if (firstNode instanceof TextListItemElement) {
				if (i == location) {
					break;
				}
				i++;
			}
			firstNode = firstNode.getNextSibling();
		}
		if ((i == location) && (firstNode instanceof TextListItemElement)) {
			positionNode = (TextListItemElementfirstNode;
		}
		if ((location != 0) && (i < location)) {
			throw new IndexOutOfBoundsException("the location " + location + " is is out of the List range.");
		}
		return positionNode;
	}
	private String getUniqueXMLID() {
		return "list" + Math.round(Math.random() * 100000000);
	}

Answers an Iterator on the items of this List. The items are iterated in the same order that they occur in the List.

Returns:
an Iterator on the items of this List
See also:
java.util.Iterator
	/*
	 * public Iterator<ListItem> iterator() { throw new
	 * UnsupportedOperationException("todo"); }
	 */
New to GrepCode? Check out our FAQ X