KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Row.java 2748 2007-05-12 15:11:48Z blowagie $
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
55 /**
56  * A <CODE>Row</CODE> is part of a <CODE>Table</CODE>
57  * and contains some <CODE>Cells</CODE>.
58  * <P>
59  * All <CODE>Row</CODE>s are constructed by a <CODE>Table</CODE>-object.
60  * You don't have to construct any <CODE>Row</CODE> yourself.
61  * In fact you can't construct a <CODE>Row</CODE> outside the package.
62  * <P>
63  * Since a <CODE>Cell</CODE> can span several rows and/or columns
64  * a row can contain reserved space without any content.
65  *
66  * @see Element
67  * @see Cell
68  * @see Table
69  */

70 public class Row implements Element {
71     
72     // constants
73

74     /** id of a null element in a Row*/
75     public static final int NULL = 0;
76     
77     /** id of the Cell element in a Row*/
78     public static final int CELL = 1;
79     
80     /** id of the Table element in a Row*/
81     public static final int TABLE = 2;
82     
83     // member variables
84

85     /** This is the number of columns in the <CODE>Row</CODE>. */
86     protected int columns;
87     
88     /** This is a valid position the <CODE>Row</CODE>. */
89     protected int currentColumn;
90     
91     /** This is the array that keeps track of reserved cells. */
92     protected boolean[] reserved;
93     
94     /** This is the array of Objects (<CODE>Cell</CODE> or <CODE>Table</CODE>). */
95     protected Object JavaDoc[] cells;
96     
97     /** This is the vertical alignment. */
98     protected int horizontalAlignment;
99     
100     // constructors
101

102     /**
103      * Constructs a <CODE>Row</CODE> with a certain number of <VAR>columns</VAR>.
104      *
105      * @param columns a number of columns
106      */

107     protected Row(int columns) {
108         this.columns = columns;
109         reserved = new boolean[columns];
110         cells = new Object JavaDoc[columns];
111         currentColumn = 0;
112     }
113     
114     // implementation of the Element-methods
115

116     /**
117      * Processes the element by adding it (or the different parts) to a
118      * <CODE>ElementListener</CODE>.
119      *
120      * @param listener an <CODE>ElementListener</CODE>
121      * @return <CODE>true</CODE> if the element was processed successfully
122      */

123     public boolean process(ElementListener listener) {
124         try {
125             return listener.add(this);
126         }
127         catch(DocumentException de) {
128             return false;
129         }
130     }
131     
132     /**
133      * Gets the type of the text element.
134      *
135      * @return a type
136      */

137     public int type() {
138         return Element.ROW;
139     }
140     
141     /**
142      * Gets all the chunks in this element.
143      *
144      * @return an <CODE>ArrayList</CODE>
145      */

146     public ArrayList JavaDoc getChunks() {
147         return new ArrayList JavaDoc();
148     }
149     
150     // method to delete a column
151

152     /**
153      * Returns a <CODE>Row</CODE> that is a copy of this <CODE>Row</CODE>
154      * in which a certain column has been deleted.
155      *
156      * @param column the number of the column to delete
157      */

158     void deleteColumn(int column) {
159         if ((column >= columns) || (column < 0)) {
160             throw new IndexOutOfBoundsException JavaDoc("getCell at illegal index : " + column);
161         }
162         columns--;
163         boolean newReserved[] = new boolean[columns];
164         Object JavaDoc newCells[] = new Cell[columns];
165         
166         for (int i = 0; i < column; i++) {
167             newReserved[i] = reserved[i];
168             newCells[i] = cells[i];
169             if (newCells[i] != null && (i + ((Cell) newCells[i]).getColspan() > column)) {
170                 ((Cell) newCells[i]).setColspan(((Cell) cells[i]).getColspan() - 1);
171             }
172         }
173         for (int i = column; i < columns; i++) {
174             newReserved[i] = reserved[i + 1];
175             newCells[i] = cells[i + 1];
176         }
177         if (cells[column] != null && ((Cell) cells[column]).getColspan() > 1) {
178             newCells[column] = cells[column];
179             ((Cell) newCells[column]).setColspan(((Cell) newCells[column]).getColspan() - 1);
180         }
181         reserved = newReserved;
182         cells = newCells;
183     }
184     
185     // methods
186

187     /**
188      * Adds a <CODE>Cell</CODE> to the <CODE>Row</CODE>.
189      *
190      * @param element the element to add (currently only Cells and Tables supported)
191      * @return the column position the <CODE>Cell</CODE> was added,
192      * or <CODE>-1</CODE> if the <CODE>element</CODE> couldn't be added.
193      */

194     int addElement(Object JavaDoc element) {
195         return addElement(element, currentColumn);
196     }
197     
198     /**
199      * Adds an element to the <CODE>Row</CODE> at the position given.
200      *
201      * @param element the element to add. (currently only Cells and Tables supported
202      * @param column the position where to add the cell.
203      * @return the column position the <CODE>Cell</CODE> was added,
204      * or <CODE>-1</CODE> if the <CODE>Cell</CODE> couldn't be added.
205      */

206     int addElement(Object JavaDoc element, int column) {
207         if (element == null) throw new NullPointerException JavaDoc("addCell - null argument");
208         if ((column < 0) || (column > columns)) throw new IndexOutOfBoundsException JavaDoc("addCell - illegal column argument");
209         if ( !((getObjectID(element) == CELL) || (getObjectID(element) == TABLE)) ) throw new IllegalArgumentException JavaDoc("addCell - only Cells or Tables allowed");
210         
211         int lColspan = ( (Cell.class.isInstance(element)) ? ((Cell) element).getColspan() : 1);
212         
213         if (!reserve(column, lColspan)) {
214             return -1;
215         }
216         
217         cells[column] = element;
218         currentColumn += lColspan - 1;
219         
220         return column;
221     }
222     
223     /**
224      * Puts <CODE>Cell</CODE> to the <CODE>Row</CODE> at the position given, doesn't reserve colspan.
225      *
226      * @param aElement the cell to add.
227      * @param column the position where to add the cell.
228      */

229     void setElement(Object JavaDoc aElement, int column) {
230         if (reserved[column]) throw new IllegalArgumentException JavaDoc("setElement - position already taken");
231         
232         cells[column] = aElement;
233         if (aElement != null) {
234             reserved[column] = true;
235         }
236     }
237     
238     /**
239      * Reserves a <CODE>Cell</CODE> in the <CODE>Row</CODE>.
240      *
241      * @param column the column that has to be reserved.
242      * @return <CODE>true</CODE> if the column was reserved, <CODE>false</CODE> if not.
243      */

244     boolean reserve(int column) {
245         return reserve(column, 1);
246     }
247     
248     
249     /**
250      * Reserves a <CODE>Cell</CODE> in the <CODE>Row</CODE>.
251      *
252      * @param column the column that has to be reserved.
253      * @param size the number of columns
254      * @return <CODE>true</CODE> if the column was reserved, <CODE>false</CODE> if not.
255      */

256     boolean reserve(int column, int size) {
257         if ((column < 0) || ((column + size) > columns)) throw new IndexOutOfBoundsException JavaDoc("reserve - incorrect column/size");
258         
259         for(int i=column; i < column + size; i++)
260         {
261             if (reserved[i]) {
262                 // undo reserve
263
for(int j=i; j >= column; j--) {
264                     reserved[j] = false;
265                 }
266                 return false;
267             }
268             reserved[i] = true;
269         }
270         return true;
271     }
272     
273     // methods to retrieve information
274

275     /**
276      * Returns true/false when this position in the <CODE>Row</CODE> has been reserved, either filled or through a colspan of an Element.
277      *
278      * @param column the column.
279      * @return <CODE>true</CODE> if the column was reserved, <CODE>false</CODE> if not.
280      */

281     boolean isReserved(int column) {
282         return reserved[column];
283     }
284     
285     /**
286      * Returns the type-id of the element in a Row.
287      *
288      * @param column the column of which you'd like to know the type
289      * @return the type-id of the element in the row
290      */

291     int getElementID(int column) {
292         if (cells[column] == null) return NULL;
293         else if (Cell.class.isInstance(cells[column])) return CELL;
294         else if (Table.class.isInstance(cells[column])) return TABLE;
295         
296         return -1;
297     }
298     
299     /**
300      * Returns the type-id of an Object.
301      *
302      * @param element the object of which you'd like to know the type-id, -1 if invalid
303      * @return the type-id of an object
304      */

305     int getObjectID(Object JavaDoc element) {
306         if (element == null) return NULL;
307         else if (Cell.class.isInstance(element)) return CELL;
308         else if (Table.class.isInstance(element)) return TABLE;
309         return -1;
310     }
311     
312     /**
313      * Gets a <CODE>Cell</CODE> or <CODE>Table</CODE> from a certain column.
314      *
315      * @param column the column the <CODE>Cell/Table</CODE> is in.
316      * @return the <CODE>Cell</CODE>,<CODE>Table</CODE> or <VAR>Object</VAR> if the column was
317      * reserved or null if empty.
318      */

319     public Object JavaDoc getCell(int column) {
320         if ((column < 0) || (column > columns)) {
321             throw new IndexOutOfBoundsException JavaDoc("getCell at illegal index :" + column + " max is " + columns);
322         }
323         return cells[column];
324     }
325     
326     /**
327      * Checks if the row is empty.
328      *
329      * @return <CODE>true</CODE> if none of the columns is reserved.
330      */

331     public boolean isEmpty() {
332         for (int i = 0; i < columns; i++) {
333             if (cells[i] != null) {
334                 return false;
335             }
336         }
337         return true;
338     }
339     
340     /**
341      * Gets the number of columns.
342      *
343      * @return a value
344      */

345     public int getColumns() {
346         return columns;
347     }
348     
349     /**
350      * Sets the horizontal alignment.
351      *
352      * @param value the new value
353      */

354     public void setHorizontalAlignment(int value) {
355         horizontalAlignment = value;
356     }
357     
358     /**
359      * Gets the horizontal alignment.
360      *
361      * @return a value
362      */

363     public int getHorizontalAlignment() {
364         return horizontalAlignment;
365     }
366 }
367
Popular Tags