All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.odftoolkit.simple.draw.Textbox Maven / Gradle / Ivy

The newest version!
/* 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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

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.draw;

import java.util.Iterator;

import org.odftoolkit.odfdom.dom.element.draw.DrawFrameElement;
import org.odftoolkit.odfdom.dom.element.draw.DrawTextBoxElement;
import org.odftoolkit.odfdom.pkg.OdfElement;
import org.odftoolkit.odfdom.pkg.OdfFileDom;
import org.odftoolkit.simple.Component;
import org.odftoolkit.simple.common.TextExtractor;
import org.odftoolkit.simple.style.StyleTypeDefinitions.CellBordersType;
import org.odftoolkit.simple.style.StyleTypeDefinitions.OdfDrawStroke;
import org.odftoolkit.simple.text.AbstractParagraphContainer;
import org.odftoolkit.simple.text.Paragraph;
import org.odftoolkit.simple.text.ParagraphContainer;
import org.odftoolkit.simple.text.list.AbstractListContainer;
import org.odftoolkit.simple.text.list.List;
import org.odftoolkit.simple.text.list.ListContainer;
import org.odftoolkit.simple.text.list.ListDecorator;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This class provides method to set the content, properties and style of text
 * box.
 * 
 * @since 0.5
 */
public class Textbox extends Frame implements ListContainer, ParagraphContainer {

	/*
	 * The  element is usable within the following elements:
	 * Supported:  10.2.4,  10.4.3,  3.4,
	 *  16.17,  10.2.1,
	 *  16.9,  9.1.5,
	 *  9.1.4,  5.1.3,  5.4 Not
	 * supported:  10.4.12,  10.3.15,  3.9,
	 *  9.2.8,  6.1.8,  5.5.4, 
	 * 5.1.2,  8.2.2,  8.2.3, 
	 * 6.1.9,  7.5.19,  6.3.4, 
	 * 6.4.2,and  6.1.7.
	 * 
	 * The  element is usable within the following element:
	 *  10.4.2.
	 * 
	 *  element has following children:  10.5.2,
	 *  10.4.12,  10.3.11,  10.3.8,
	 *  10.3.10,  10.3.13, 
	 * 10.6.1,  10.3.9,  10.4.2,  10.3.15,
	 *  10.3.3,  10.3.12, 
	 * 10.3.14,  10.3.7,  10.3.5, 
	 * 10.3.4,  10.3.2,  10.3.6, 
	 * 9.1.2,  8.8,  8.9,
	 *  5.5.7.4,  5.5.7.3, 
	 * 5.5.7.2,  5.1.2,  8.4, 
	 * 5.3.1,  5.3.6,  8.6, 
	 * 5.1.3,  5.4,  5.6, 
	 * 8.5,  8.3 and  8.7.
	 */
	DrawTextBoxElement mTextboxElement;
	private ParagraphContainerImpl paragraphContainerImpl;
	private ListContainerImpl listContainerImpl;

	private Textbox(DrawTextBoxElement textbox) {
		super((DrawFrameElement) textbox.getParentNode());
		mTextboxElement = textbox;

	}

	/**
	 * Get a text box instance by an instance of DrawTextBoxElement
	 * .
	 * 
	 * @param element
	 *            - the instance of DrawTextBoxElement
	 * @return an instance of text box
	 */
	public static Textbox getInstanceof(DrawTextBoxElement element) {
		if (element == null)
			return null;

		Textbox textbox = null;
		textbox = (Textbox) Component.getComponentByElement(element);
		if (textbox != null)
			return textbox;

		textbox = new Textbox(element);
		Component.registerComponent(textbox, element);
		return textbox;
	}

	/**
	 * Create an instance of frame
	 * 

* The frame will be added at the end of this container. * * @param container * - the frame container that contains this frame. */ public static Textbox newTextbox(TextboxContainer container) { Textbox textbox = null; OdfElement parent = container.getFrameContainerElement(); OdfFileDom ownerDom = (OdfFileDom) parent.getOwnerDocument(); DrawFrameElement fElement = ownerDom.newOdfElement(DrawFrameElement.class); parent.appendChild(fElement); DrawTextBoxElement boxElement = fElement.newDrawTextBoxElement(); textbox = new Textbox(boxElement); textbox.mFrameContainer = container; Component.registerComponent(textbox, boxElement); // set text box default style textbox.getStyleHandler().setBorders(null, CellBordersType.NONE); textbox.getStyleHandler().setStroke(OdfDrawStroke.NONE, null, null, null); textbox.getStyleHandler().setBackgroundColor(null); // set style:run-through="foreground" textbox.getStyleHandler().setBackgroundFrame(false); return textbox; } /** * Set the text content of this text box. *

* This method will clear the content at first, and then create a paragraph * with the given content. * * @param content * - the text content * @see #clearContent() * @see #addParagraph(String textContent) */ public void setTextContent(String content) { clearContent(); addParagraph(content); } /** * Remove all the content of this text box. */ public void clearContent() { NodeList nodeList = mTextboxElement.getChildNodes(); int i; for (i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == node.TEXT_NODE) mTextboxElement.removeChild(nodeList.item(i)); } } /** * Get the text content of this text box. * * @return - the text content * @see TextExtractor#getText(OdfElement) */ public String getTextContent() { return TextExtractor.getText(mTextboxElement); } /** * Get the instance of DrawTextBoxElement which represents this * frame. * * @return the instance of DrawTextBoxElement */ @Override public DrawTextBoxElement getOdfElement() { return mTextboxElement; } // ***************List support*************************// public List addList() { return getListContainerImpl().addList(); } public List addList(ListDecorator decorator) { return getListContainerImpl().addList(decorator); } public void clearList() { getListContainerImpl().clearList(); } public OdfElement getListContainerElement() { return getListContainerImpl().getListContainerElement(); } public Iterator getListIterator() { return getListContainerImpl().getListIterator(); } public boolean removeList(List list) { return getListContainerImpl().removeList(list); } private ListContainerImpl getListContainerImpl() { if (listContainerImpl == null) { listContainerImpl = new ListContainerImpl(); } return listContainerImpl; } private class ListContainerImpl extends AbstractListContainer { public OdfElement getListContainerElement() { return mTextboxElement; } } // ****************Paragraph support******************// public Paragraph addParagraph(String textContent) { return getParagraphContainerImpl().addParagraph(textContent); } public OdfElement getParagraphContainerElement() { return getParagraphContainerImpl().getParagraphContainerElement(); } public boolean removeParagraph(Paragraph para) { return getParagraphContainerImpl().removeParagraph(para); } private class ParagraphContainerImpl extends AbstractParagraphContainer { public OdfElement getParagraphContainerElement() { return mTextboxElement; } } private ParagraphContainerImpl getParagraphContainerImpl() { if (paragraphContainerImpl == null) paragraphContainerImpl = new ParagraphContainerImpl(); return paragraphContainerImpl; } public Paragraph getParagraphByIndex(int index, boolean isEmptyParagraphSkipped) { return getParagraphContainerImpl().getParagraphByIndex(index, isEmptyParagraphSkipped); } public Paragraph getParagraphByReverseIndex(int reverseIndex, boolean isEmptyParagraphSkipped) { return getParagraphContainerImpl().getParagraphByReverseIndex(reverseIndex, isEmptyParagraphSkipped); } public Iterator getParagraphIterator() { return getParagraphContainerImpl().getParagraphIterator(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy