001/*
002 * $Id: MarkedSection.java 4862 2011-05-11 15:57:42Z redlab_b $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text;
045
046import java.util.Collection;
047import java.util.Iterator;
048
049import com.itextpdf.text.api.Indentable;
050
051/**
052 * Wrapper that allows to add properties to a Chapter/Section object.
053 * Before iText 1.5 every 'basic building block' implemented the MarkupAttributes interface.
054 * By setting attributes, you could add markup to the corresponding XML and/or HTML tag.
055 * This functionality was hardly used by anyone, so it was removed, and replaced by
056 * the MarkedObject functionality.
057 */
058
059public class MarkedSection extends MarkedObject implements Indentable {
060
061        /** This is the title of this section. */
062        protected MarkedObject title = null;
063
064        /**
065         * Creates a MarkedObject with a Section or Chapter object.
066         * @param section       the marked section
067         */
068        public MarkedSection(Section section) {
069                super();
070                if (section.title != null) {
071                        title = new MarkedObject(section.title);
072                        section.setTitle(null);
073                }
074                this.element = section;
075        }
076
077        /**
078         * Adds a <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>
079         * to this <CODE>Section</CODE>.
080         *
081         * @param       index   index at which the specified element is to be inserted
082         * @param       o       an object of type <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>=
083         * @throws      ClassCastException if the object is not a <CODE>Paragraph</CODE>, <CODE>List</CODE> or <CODE>Table</CODE>
084         * @since 5.0.1 (signature changed to use Element)
085         */
086        public void add(int index, Element o) {
087                ((Section)element).add(index, o);
088        }
089
090        /**
091         * Adds a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or another <CODE>Section</CODE>
092         * to this <CODE>Section</CODE>.
093         *
094         * @param       o       an object of type <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or another <CODE>Section</CODE>
095         * @return      a boolean
096         * @throws      ClassCastException if the object is not a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE> or <CODE>Section</CODE>
097         * @since 5.0.1 (signature changed to use Element)
098         */
099        public boolean add(Element o) {
100                return ((Section)element).add(o);
101        }
102
103    /**
104     * Processes the element by adding it (or the different parts) to an
105     * <CODE>ElementListener</CODE>.
106     *
107     * @param       listener        an <CODE>ElementListener</CODE>
108     * @return <CODE>true</CODE> if the element was processed successfully
109     */
110    @Override
111    public boolean process(ElementListener listener) {
112        try {
113                Element element;
114            for (Iterator<Element> i = ((Section)this.element).iterator(); i.hasNext(); ) {
115                element = i.next();
116                listener.add(element);
117            }
118            return true;
119        }
120        catch(DocumentException de) {
121            return false;
122        }
123    }
124
125        /**
126         * Adds a collection of <CODE>Element</CODE>s
127         * to this <CODE>Section</CODE>.
128         *
129         * @param       collection      a collection of <CODE>Paragraph</CODE>s, <CODE>List</CODE>s and/or <CODE>Table</CODE>s
130         * @return      <CODE>true</CODE> if the action succeeded, <CODE>false</CODE> if not.
131         * @throws      ClassCastException if one of the objects isn't a <CODE>Paragraph</CODE>, <CODE>List</CODE>, <CODE>Table</CODE>
132         */
133        public boolean addAll(Collection<? extends Element> collection) {
134                return ((Section)element).addAll(collection);
135        }
136
137        /**
138         * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
139         *
140         * @param       indentation     the indentation of the new section
141         * @param       numberDepth     the numberDepth of the section
142         * @return  a new Section object
143         */
144        public MarkedSection addSection(float indentation, int numberDepth) {
145                MarkedSection section = ((Section)element).addMarkedSection();
146                section.setIndentation(indentation);
147                section.setNumberDepth(numberDepth);
148                return section;
149        }
150
151        /**
152         * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
153         *
154         * @param       indentation     the indentation of the new section
155         * @return  a new Section object
156         */
157        public MarkedSection addSection(float indentation) {
158                MarkedSection section = ((Section)element).addMarkedSection();
159                section.setIndentation(indentation);
160                return section;
161        }
162
163        /**
164         * Creates a <CODE>Section</CODE>, add it to this <CODE>Section</CODE> and returns it.
165         *
166         * @param       numberDepth     the numberDepth of the section
167         * @return  a new Section object
168         */
169        public MarkedSection addSection(int numberDepth) {
170                MarkedSection section = ((Section)element).addMarkedSection();
171                section.setNumberDepth(numberDepth);
172                return section;
173        }
174
175        /**
176         * Creates a <CODE>Section</CODE>, adds it to this <CODE>Section</CODE> and returns it.
177         *
178         * @return  a new Section object
179         */
180        public MarkedSection addSection() {
181                return ((Section)element).addMarkedSection();
182        }
183
184        // public methods
185
186        /**
187         * Sets the title of this section.
188         *
189         * @param       title   the new title
190         */
191        public void setTitle(MarkedObject title) {
192                if (title.element instanceof Paragraph)
193                        this.title = title;
194        }
195
196        /**
197         * Gets the title of this MarkedSection.
198         * @return      a MarkObject with a Paragraph containing the title of a Section
199         * @since       iText 2.0.8
200         */
201    public MarkedObject getTitle() {
202        Paragraph result = Section.constructTitle((Paragraph)title.element, ((Section)element).numbers, ((Section)element).numberDepth, ((Section)element).numberStyle);
203        MarkedObject mo = new MarkedObject(result);
204        mo.markupAttributes = title.markupAttributes;
205        return mo;
206    }
207
208        /**
209         * Sets the depth of the sectionnumbers that will be shown preceding the title.
210         * <P>
211         * If the numberdepth is 0, the sections will not be numbered. If the numberdepth
212         * is 1, the section will be numbered with their own number. If the numberdepth is
213         * higher (for instance x > 1), the numbers of x - 1 parents will be shown.
214         *
215         * @param       numberDepth             the new numberDepth
216         */
217        public void setNumberDepth(int numberDepth) {
218                ((Section)element).setNumberDepth(numberDepth);
219        }
220
221        /**
222         * Sets the indentation of this <CODE>Section</CODE> on the left side.
223         *
224         * @param       indentation             the indentation
225         */
226        public void setIndentationLeft(float indentation) {
227                ((Section)element).setIndentationLeft(indentation);
228        }
229
230        /**
231         * Sets the indentation of this <CODE>Section</CODE> on the right side.
232         *
233         * @param       indentation             the indentation
234         */
235        public void setIndentationRight(float indentation) {
236                ((Section)element).setIndentationRight(indentation);
237        }
238
239        /**
240         * Sets the indentation of the content of this <CODE>Section</CODE>.
241         *
242         * @param       indentation             the indentation
243         */
244        public void setIndentation(float indentation) {
245                ((Section)element).setIndentation(indentation);
246        }
247
248        /** Setter for property bookmarkOpen.
249         * @param bookmarkOpen false if the bookmark children are not
250         * visible.
251         */
252        public void setBookmarkOpen(boolean bookmarkOpen) {
253                ((Section)element).setBookmarkOpen(bookmarkOpen);
254        }
255
256        /**
257         * Setter for property triggerNewPage.
258         * @param triggerNewPage true if a new page has to be triggered.
259         */
260        public void setTriggerNewPage(boolean triggerNewPage) {
261                ((Section)element).setTriggerNewPage(triggerNewPage);
262        }
263
264        /**
265         * Sets the bookmark title. The bookmark title is the same as the section title but
266         * can be changed with this method.
267         * @param bookmarkTitle the bookmark title
268         */
269        public void setBookmarkTitle(String bookmarkTitle) {
270                ((Section)element).setBookmarkTitle(bookmarkTitle);
271        }
272
273        /**
274         * Adds a new page to the section.
275         * @since       2.1.1
276         */
277        public void newPage() {
278                ((Section)element).newPage();
279        }
280
281        /* (non-Javadoc)
282         * @see com.itextpdf.text.api.Indentable#getIndentationLeft()
283         */
284        public float getIndentationLeft() {
285                return ((Section)element).getIndentationLeft();
286        }
287
288        /* (non-Javadoc)
289         * @see com.itextpdf.text.api.Indentable#getIndentationRight()
290         */
291        public float getIndentationRight() {
292                return ((Section)element).getIndentationRight();
293        }
294}