001: /*
002: * $Id: RtfBorderGroup.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf.table;
052:
053: import java.awt.Color;
054: import java.io.ByteArrayOutputStream;
055: import java.io.IOException;
056: import java.io.OutputStream;
057: import java.util.Enumeration;
058: import java.util.Hashtable;
059:
060: import com.lowagie.text.Rectangle;
061: import com.lowagie.text.rtf.RtfElement;
062: import com.lowagie.text.rtf.document.RtfDocument;
063:
064: /**
065: * The RtfBorderGroup represents a collection of RtfBorders to use in a RtfCell
066: * or RtfTable.
067: *
068: * @version $Id: RtfBorderGroup.java 2776 2007-05-23 20:01:40Z hallm $
069: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
070: * @author Thomas Bickel (tmb99@inode.at)
071: */
072: public class RtfBorderGroup extends RtfElement {
073: /**
074: * The type of borders this RtfBorderGroup contains.
075: * RtfBorder.ROW_BORDER or RtfBorder.CELL_BORDER
076: */
077: private int borderType = RtfBorder.ROW_BORDER;
078: /**
079: * The borders in this RtfBorderGroup
080: */
081: private Hashtable borders = null;
082:
083: /**
084: * Constructs an empty RtfBorderGroup.
085: */
086: public RtfBorderGroup() {
087: super (null);
088: this .borders = new Hashtable();
089: }
090:
091: /**
092: * Constructs a RtfBorderGroup with on border style for multiple borders.
093: *
094: * @param bordersToAdd The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX)
095: * @param borderStyle The style of border to add (from RtfBorder)
096: * @param borderWidth The border width to use
097: * @param borderColor The border color to use
098: */
099: public RtfBorderGroup(int bordersToAdd, int borderStyle,
100: float borderWidth, Color borderColor) {
101: super (null);
102: this .borders = new Hashtable();
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,
114: RtfBorderGroup borderGroup) {
115: super (doc);
116: this .borders = new Hashtable();
117: this .borderType = borderType;
118: if (borderGroup != null) {
119: Enumeration borderEnum = borderGroup.getBorders().keys();
120: while (borderEnum.hasMoreElements()) {
121: Integer borderPos = (Integer) borderEnum.nextElement();
122: RtfBorder border = (RtfBorder) borderGroup.getBorders()
123: .get(borderPos);
124: this .borders.put(borderPos, new RtfBorder(
125: this .document, this .borderType, border));
126: }
127: }
128: }
129:
130: /**
131: * Constructs a RtfBorderGroup with certain borders
132: *
133: * @param doc The RtfDocument this RtfBorderGroup belongs to
134: * @param borderType The type of borders this RtfBorderGroup contains
135: * @param bordersToUse The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX)
136: * @param borderWidth The border width to use
137: * @param borderColor The border color to use
138: */
139: protected RtfBorderGroup(RtfDocument doc, int borderType,
140: int bordersToUse, float borderWidth, Color borderColor) {
141: super (doc);
142: this .borderType = borderType;
143: this .borders = new Hashtable();
144: addBorder(bordersToUse, RtfBorder.BORDER_SINGLE, borderWidth,
145: borderColor);
146: }
147:
148: /**
149: * Sets a border in the Hashtable of borders
150: *
151: * @param borderPosition The position of this RtfBorder
152: * @param borderStyle The type of borders this RtfBorderGroup contains
153: * @param borderWidth The border width to use
154: * @param borderColor The border color to use
155: */
156: private void setBorder(int borderPosition, int borderStyle,
157: float borderWidth, Color borderColor) {
158: RtfBorder border = new RtfBorder(this .document,
159: this .borderType, borderPosition, borderStyle,
160: borderWidth, borderColor);
161: this .borders.put(new Integer(borderPosition), border);
162: }
163:
164: /**
165: * Adds borders to the RtfBorderGroup
166: *
167: * @param bordersToAdd The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX)
168: * @param borderStyle The style of border to add (from RtfBorder)
169: * @param borderWidth The border width to use
170: * @param borderColor The border color to use
171: */
172: public void addBorder(int bordersToAdd, int borderStyle,
173: float borderWidth, Color borderColor) {
174: if ((bordersToAdd & Rectangle.LEFT) == Rectangle.LEFT) {
175: setBorder(RtfBorder.LEFT_BORDER, borderStyle, borderWidth,
176: borderColor);
177: }
178: if ((bordersToAdd & Rectangle.TOP) == Rectangle.TOP) {
179: setBorder(RtfBorder.TOP_BORDER, borderStyle, borderWidth,
180: borderColor);
181: }
182: if ((bordersToAdd & Rectangle.RIGHT) == Rectangle.RIGHT) {
183: setBorder(RtfBorder.RIGHT_BORDER, borderStyle, borderWidth,
184: borderColor);
185: }
186: if ((bordersToAdd & Rectangle.BOTTOM) == Rectangle.BOTTOM) {
187: setBorder(RtfBorder.BOTTOM_BORDER, borderStyle,
188: borderWidth, borderColor);
189: }
190: if ((bordersToAdd & Rectangle.BOX) == Rectangle.BOX
191: && this .borderType == RtfBorder.ROW_BORDER) {
192: setBorder(RtfBorder.VERTICAL_BORDER, borderStyle,
193: borderWidth, borderColor);
194: setBorder(RtfBorder.HORIZONTAL_BORDER, borderStyle,
195: borderWidth, borderColor);
196: }
197: }
198:
199: /**
200: * Removes borders from the list of borders
201: *
202: * @param bordersToRemove The borders to remove (from Rectangle)
203: */
204: public void removeBorder(int bordersToRemove) {
205: if ((bordersToRemove & Rectangle.LEFT) == Rectangle.LEFT) {
206: this .borders.remove(new Integer(RtfBorder.LEFT_BORDER));
207: }
208: if ((bordersToRemove & Rectangle.TOP) == Rectangle.TOP) {
209: this .borders.remove(new Integer(RtfBorder.TOP_BORDER));
210: }
211: if ((bordersToRemove & Rectangle.RIGHT) == Rectangle.RIGHT) {
212: this .borders.remove(new Integer(RtfBorder.RIGHT_BORDER));
213: }
214: if ((bordersToRemove & Rectangle.BOTTOM) == Rectangle.BOTTOM) {
215: this .borders.remove(new Integer(RtfBorder.BOTTOM_BORDER));
216: }
217: if ((bordersToRemove & Rectangle.BOX) == Rectangle.BOX
218: && this .borderType == RtfBorder.ROW_BORDER) {
219: this .borders.remove(new Integer(RtfBorder.VERTICAL_BORDER));
220: this .borders
221: .remove(new Integer(RtfBorder.HORIZONTAL_BORDER));
222: }
223: }
224:
225: /**
226: * Writes the borders of this RtfBorderGroup
227: *
228: * @return A byte array with the borders of this RtfBorderGroup
229: * @deprecated replaced by {@link #writeContent(OutputStream)}
230: */
231: public byte[] write() {
232: ByteArrayOutputStream result = new ByteArrayOutputStream();
233: try {
234: writeContent(result);
235: } catch (IOException ioe) {
236: ioe.printStackTrace();
237: }
238: return result.toByteArray();
239: }
240:
241: /**
242: * Writes the borders of this RtfBorderGroup
243: */
244: public void writeContent(final OutputStream result)
245: throws IOException {
246: Enumeration borderEnum = this .borders.keys();
247: while (borderEnum.hasMoreElements()) {
248: RtfBorder rb = (RtfBorder) this .borders.get(borderEnum
249: .nextElement());
250: //.result.write(rb.write());
251: rb.writeContent(result);
252: }
253: }
254:
255: /**
256: * Gets the RtfBorders of this RtfBorderGroup
257: *
258: * @return The RtfBorders of this RtfBorderGroup
259: */
260: protected Hashtable getBorders() {
261: return this.borders;
262: }
263: }
|