KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > table > RtfBorderGroup


1 /*
2  * $Id: RtfBorderGroup.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2001, 2002, 2003, 2004 by Mark Hall
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.rtf.table;
52
53 import java.awt.Color JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.OutputStream JavaDoc;
57 import java.util.Enumeration JavaDoc;
58 import java.util.Hashtable JavaDoc;
59
60 import com.lowagie.text.Rectangle;
61 import com.lowagie.text.rtf.RtfElement;
62 import com.lowagie.text.rtf.document.RtfDocument;
63
64
65 /**
66  * The RtfBorderGroup represents a collection of RtfBorders to use in a RtfCell
67  * or RtfTable.
68  *
69  * @version $Id: RtfBorderGroup.java 2776 2007-05-23 20:01:40Z hallm $
70  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
71  * @author Thomas Bickel (tmb99@inode.at)
72  */

73 public class RtfBorderGroup extends RtfElement {
74     /**
75      * The type of borders this RtfBorderGroup contains.
76      * RtfBorder.ROW_BORDER or RtfBorder.CELL_BORDER
77      */

78     private int borderType = RtfBorder.ROW_BORDER;
79     /**
80      * The borders in this RtfBorderGroup
81      */

82     private Hashtable JavaDoc borders = null;
83
84     /**
85      * Constructs an empty RtfBorderGroup.
86      */

87     public RtfBorderGroup() {
88         super(null);
89         this.borders = new Hashtable JavaDoc();
90     }
91     
92     /**
93      * Constructs a RtfBorderGroup with on border style for multiple borders.
94      *
95      * @param bordersToAdd The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX)
96      * @param borderStyle The style of border to add (from RtfBorder)
97      * @param borderWidth The border width to use
98      * @param borderColor The border color to use
99      */

100     public RtfBorderGroup(int bordersToAdd, int borderStyle, float borderWidth, Color JavaDoc borderColor) {
101         super(null);
102         this.borders = new Hashtable JavaDoc();
103         addBorder(bordersToAdd, borderStyle, borderWidth, borderColor);
104     }
105     
106     /**
107      * Constructs a RtfBorderGroup based on another RtfBorderGroup.
108      *
109      * @param doc The RtfDocument this RtfBorderGroup belongs to
110      * @param borderType The type of borders this RtfBorderGroup contains
111      * @param borderGroup The RtfBorderGroup to use as a base
112      */

113     protected RtfBorderGroup(RtfDocument doc, int borderType, RtfBorderGroup borderGroup) {
114         super(doc);
115         this.borders = new Hashtable JavaDoc();
116         this.borderType = borderType;
117         if(borderGroup != null) {
118             Enumeration JavaDoc borderEnum = borderGroup.getBorders().keys();
119             while(borderEnum.hasMoreElements()) {
120                 Integer JavaDoc borderPos = (Integer JavaDoc) borderEnum.nextElement();
121                 RtfBorder border = (RtfBorder) borderGroup.getBorders().get(borderPos);
122                 this.borders.put(borderPos, new RtfBorder(this.document, this.borderType, border));
123             }
124         }
125     }
126     
127     /**
128      * Constructs a RtfBorderGroup with certain borders
129      *
130      * @param doc The RtfDocument this RtfBorderGroup belongs to
131      * @param borderType The type of borders this RtfBorderGroup contains
132      * @param bordersToUse The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX)
133      * @param borderWidth The border width to use
134      * @param borderColor The border color to use
135      */

136     protected RtfBorderGroup(RtfDocument doc, int borderType, int bordersToUse, float borderWidth, Color JavaDoc borderColor) {
137         super(doc);
138         this.borderType = borderType;
139         this.borders = new Hashtable JavaDoc();
140         addBorder(bordersToUse, RtfBorder.BORDER_SINGLE, borderWidth, borderColor);
141     }
142     
143     /**
144      * Sets a border in the Hashtable of borders
145      *
146      * @param borderPosition The position of this RtfBorder
147      * @param borderStyle The type of borders this RtfBorderGroup contains
148      * @param borderWidth The border width to use
149      * @param borderColor The border color to use
150      */

151     private void setBorder(int borderPosition, int borderStyle, float borderWidth, Color JavaDoc borderColor) {
152         RtfBorder border = new RtfBorder(this.document, this.borderType, borderPosition, borderStyle, borderWidth, borderColor);
153         this.borders.put(new Integer JavaDoc(borderPosition), border);
154     }
155     
156     /**
157      * Adds borders to the RtfBorderGroup
158      *
159      * @param bordersToAdd The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX)
160      * @param borderStyle The style of border to add (from RtfBorder)
161      * @param borderWidth The border width to use
162      * @param borderColor The border color to use
163      */

164     public void addBorder(int bordersToAdd, int borderStyle, float borderWidth, Color JavaDoc borderColor) {
165         if((bordersToAdd & Rectangle.LEFT) == Rectangle.LEFT) {
166             setBorder(RtfBorder.LEFT_BORDER, borderStyle, borderWidth, borderColor);
167         }
168         if((bordersToAdd & Rectangle.TOP) == Rectangle.TOP) {
169             setBorder(RtfBorder.TOP_BORDER, borderStyle, borderWidth, borderColor);
170         }
171         if((bordersToAdd & Rectangle.RIGHT) == Rectangle.RIGHT) {
172             setBorder(RtfBorder.RIGHT_BORDER, borderStyle, borderWidth, borderColor);
173         }
174         if((bordersToAdd & Rectangle.BOTTOM) == Rectangle.BOTTOM) {
175             setBorder(RtfBorder.BOTTOM_BORDER, borderStyle, borderWidth, borderColor);
176         }
177         if((bordersToAdd & Rectangle.BOX) == Rectangle.BOX && this.borderType == RtfBorder.ROW_BORDER) {
178             setBorder(RtfBorder.VERTICAL_BORDER, borderStyle, borderWidth, borderColor);
179             setBorder(RtfBorder.HORIZONTAL_BORDER, borderStyle, borderWidth, borderColor);
180         }
181     }
182     
183     /**
184      * Removes borders from the list of borders
185      *
186      * @param bordersToRemove The borders to remove (from Rectangle)
187      */

188     public void removeBorder(int bordersToRemove) {
189         if((bordersToRemove & Rectangle.LEFT) == Rectangle.LEFT) {
190             this.borders.remove(new Integer JavaDoc(RtfBorder.LEFT_BORDER));
191         }
192         if((bordersToRemove & Rectangle.TOP) == Rectangle.TOP) {
193             this.borders.remove(new Integer JavaDoc(RtfBorder.TOP_BORDER));
194         }
195         if((bordersToRemove & Rectangle.RIGHT) == Rectangle.RIGHT) {
196             this.borders.remove(new Integer JavaDoc(RtfBorder.RIGHT_BORDER));
197         }
198         if((bordersToRemove & Rectangle.BOTTOM) == Rectangle.BOTTOM) {
199             this.borders.remove(new Integer JavaDoc(RtfBorder.BOTTOM_BORDER));
200         }
201         if((bordersToRemove & Rectangle.BOX) == Rectangle.BOX && this.borderType == RtfBorder.ROW_BORDER) {
202             this.borders.remove(new Integer JavaDoc(RtfBorder.VERTICAL_BORDER));
203             this.borders.remove(new Integer JavaDoc(RtfBorder.HORIZONTAL_BORDER));
204         }
205     }
206     
207     /**
208      * Writes the borders of this RtfBorderGroup
209      *
210      * @return A byte array with the borders of this RtfBorderGroup
211      * @deprecated replaced by {@link #writeContent(OutputStream)}
212      */

213     public byte[] write()
214     {
215         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
216         try {
217             writeContent(result);
218         } catch(IOException JavaDoc ioe) {
219             ioe.printStackTrace();
220         }
221         return result.toByteArray();
222     }
223     /**
224      * Writes the borders of this RtfBorderGroup
225      */

226     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
227     {
228         Enumeration JavaDoc borderEnum = this.borders.keys();
229         while(borderEnum.hasMoreElements()) {
230             RtfBorder rb = (RtfBorder)this.borders.get(borderEnum.nextElement());
231             //.result.write(rb.write());
232
rb.writeContent(result);
233         }
234     }
235     
236     /**
237      * Gets the RtfBorders of this RtfBorderGroup
238      *
239      * @return The RtfBorders of this RtfBorderGroup
240      */

241     protected Hashtable JavaDoc getBorders() {
242         return this.borders;
243     }
244 }
245
Popular Tags