KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RtfBorder.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
58 import com.lowagie.text.rtf.RtfElement;
59 import com.lowagie.text.rtf.document.RtfDocument;
60 import com.lowagie.text.rtf.style.RtfColor;
61
62
63 /**
64  * The RtfBorder handle one row or cell border.
65  * INTERNAL USE ONLY
66  *
67  * @version $Id: RtfBorder.java 2776 2007-05-23 20:01:40Z hallm $
68  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
69  * @author Thomas Bickel (tmb99@inode.at)
70  */

71 public class RtfBorder extends RtfElement {
72
73     /**
74      * Constant for the left row border
75      */

76     protected static final byte[] ROW_BORDER_LEFT = "\\trbrdrl".getBytes();
77     /**
78      * Constant for the top row border
79      */

80     protected static final byte[] ROW_BORDER_TOP = "\\trbrdrt".getBytes();
81     /**
82      * Constant for the right row border
83      */

84     protected static final byte[] ROW_BORDER_RIGHT = "\\trbrdrr".getBytes();
85     /**
86      * Constant for the bottom row border
87      */

88     protected static final byte[] ROW_BORDER_BOTTOM = "\\trbrdrb".getBytes();
89     /**
90      * Constant for the horizontal line
91      */

92     protected static final byte[] ROW_BORDER_HORIZONTAL = "\\trbrdrh".getBytes();
93     /**
94      * Constant for the vertical line
95      */

96     protected static final byte[] ROW_BORDER_VERTICAL = "\\trbrdrv".getBytes();
97     /**
98      * Constant for the left cell border
99      */

100     protected static final byte[] CELL_BORDER_LEFT = "\\clbrdrl".getBytes();
101     /**
102      * Constant for the top cell border
103      */

104     protected static final byte[] CELL_BORDER_TOP = "\\clbrdrt".getBytes();
105     /**
106      * Constant for the right cell border
107      */

108     protected static final byte[] CELL_BORDER_RIGHT = "\\clbrdrr".getBytes();
109     /**
110      * Constant for the bottom cell border
111      */

112     protected static final byte[] CELL_BORDER_BOTTOM = "\\clbrdrb".getBytes();
113     /**
114      * Constant for the border width
115      */

116     protected static final byte[] BORDER_WIDTH = "\\brdrw".getBytes();
117     /**
118      * Constant for the border colour number
119      */

120     protected static final byte[] BORDER_COLOR_NUMBER = "\\brdrcf".getBytes();
121     /**
122      * Constant for the single border style
123      */

124     protected static final byte[] BORDER_STYLE_SINGLE = "\\brdrs".getBytes();
125     /**
126      * Constant for the double thick border style
127      */

128     protected static final byte[] BORDER_STYLE_DOUBLE_THICK = "\\brdrth".getBytes();
129     /**
130      * Constant for the shadowed border style
131      */

132     protected static final byte[] BORDER_STYLE_SHADOWED = "\\brdrsh".getBytes();
133     /**
134      * Constant for the dotted border style
135      */

136     protected static final byte[] BORDER_STYLE_DOTTED = "\\brdrdot".getBytes();
137     /**
138      * Constant for the dashed border style
139      */

140     protected static final byte[] BORDER_STYLE_DASHED = "\\brdrdash".getBytes();
141     /**
142      * Constant for the hairline border style
143      */

144     protected static final byte[] BORDER_STYLE_HAIRLINE = "\\brdrhair".getBytes();
145     /**
146      * Constant for the double border style
147      */

148     protected static final byte[] BORDER_STYLE_DOUBLE = "\\brdrdb".getBytes();
149     /**
150      * Constant for the dot dash border style
151      */

152     protected static final byte[] BORDER_STYLE_DOT_DASH = "\\brdrdashd".getBytes();
153     /**
154      * Constant for the dot dot dash border style
155      */

156     protected static final byte[] BORDER_STYLE_DOT_DOT_DASH = "\\brdrdashdd".getBytes();
157     /**
158      * Constant for the triple border style
159      */

160     protected static final byte[] BORDER_STYLE_TRIPLE = "\\brdrtriple".getBytes();
161     /**
162      * Constant for the thick thin border style
163      */

164     protected static final byte[] BORDER_STYLE_THICK_THIN = "\\brdrtnthsg".getBytes();
165     /**
166      * Constant for the thin thick border style
167      */

168     protected static final byte[] BORDER_STYLE_THIN_THICK = "\\brdrthtnsg".getBytes();
169     /**
170      * Constant for the thin thick thin border style
171      */

172     protected static final byte[] BORDER_STYLE_THIN_THICK_THIN = "\\brdrtnthtnsg".getBytes();
173     /**
174      * Constant for the thick thin medium border style
175      */

176     protected static final byte[] BORDER_STYLE_THICK_THIN_MED = "\\brdrtnthmg".getBytes();
177     /**
178      * Constant for the thin thick medium border style
179      */

180     protected static final byte[] BORDER_STYLE_THIN_THICK_MED = "\\brdrthtnmg".getBytes();
181     /**
182      * Constant for the thin thick thin medium border style
183      */

184     protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_MED = "\\brdrtnthtnmg".getBytes();
185     /**
186      * Constant for the thick thin large border style
187      */

188     protected static final byte[] BORDER_STYLE_THICK_THIN_LARGE = "\\brdrtnthlg".getBytes();
189     /**
190      * Constant for the thin thick large border style
191      */

192     protected static final byte[] BORDER_STYLE_THIN_THICK_LARGE = "\\brdrthtnlg".getBytes();
193     /**
194      * Constant for the thin thick thin large border style
195      */

196     protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_LARGE = "\\brdrtnthtnlg".getBytes();
197     /**
198      * Constant for the wavy border style
199      */

200     protected static final byte[] BORDER_STYLE_WAVY = "\\brdrwavy".getBytes();
201     /**
202      * Constant for the double wavy border style
203      */

204     protected static final byte[] BORDER_STYLE_DOUBLE_WAVY = "\\brdrwavydb".getBytes();
205     /**
206      * Constant for the striped border style
207      */

208     protected static final byte[] BORDER_STYLE_STRIPED = "\\brdrdashdotstr".getBytes();
209     /**
210      * Constant for the embossed border style
211      */

212     protected static final byte[] BORDER_STYLE_EMBOSS = "\\brdremboss".getBytes();
213     /**
214      * Constant for the engraved border style
215      */

216     protected static final byte[] BORDER_STYLE_ENGRAVE = "\\brdrengrave".getBytes();
217
218     /**
219      * Constant for a row border
220      */

221     protected static final int ROW_BORDER = 1;
222     /**
223      * Constant for a cell border
224      */

225     protected static final int CELL_BORDER = 2;
226     
227     /**
228      * This border is no border :-)
229      */

230     protected static final int NO_BORDER = 0;
231     /**
232      * Constant for a left border
233      */

234     protected static final int LEFT_BORDER = 1;
235     /**
236      * Constant for a top border
237      */

238     protected static final int TOP_BORDER = 2;
239     /**
240      * Constant for a right border
241      */

242     protected static final int RIGHT_BORDER = 4;
243     /**
244      * Constant for a bottom border
245      */

246     protected static final int BOTTOM_BORDER = 8;
247     /**
248      * Constant for a box (left, top, right, bottom) border
249      */

250     protected static final int BOX_BORDER = 15;
251     /**
252      * Constant for a vertical line
253      */

254     protected static final int VERTICAL_BORDER = 16;
255     /**
256      * Constant for a horizontal line
257      */

258     protected static final int HORIZONTAL_BORDER = 32;
259     
260     /**
261      * Constant for a border with no border
262      */

263     public static final int BORDER_NONE = 0;
264     /**
265      * Constant for a single border
266      */

267     public static final int BORDER_SINGLE = 1;
268     /**
269      * Constant for a double thick border
270      */

271     public static final int BORDER_DOUBLE_THICK = 2;
272     /**
273      * Constant for a shadowed border
274      */

275     public static final int BORDER_SHADOWED = 3;
276     /**
277      * Constant for a dotted border
278      */

279     public static final int BORDER_DOTTED = 4;
280     /**
281      * Constant for a dashed border
282      */

283     public static final int BORDER_DASHED = 5;
284     /**
285      * Constant for a hairline border
286      */

287     public static final int BORDER_HAIRLINE = 6;
288     /**
289      * Constant for a double border
290      */

291     public static final int BORDER_DOUBLE = 7;
292     /**
293      * Constant for a dot dash border
294      */

295     public static final int BORDER_DOT_DASH = 8;
296     /**
297      * Constant for a dot dot dash border
298      */

299     public static final int BORDER_DOT_DOT_DASH = 9;
300     /**
301      * Constant for a triple border
302      */

303     public static final int BORDER_TRIPLE = 10;
304     /**
305      * Constant for a thick thin border
306      */

307     public static final int BORDER_THICK_THIN = 11;
308     /**
309      * Constant for a thin thick border
310      */

311     public static final int BORDER_THIN_THICK = 12;
312     /**
313      * Constant for a thin thick thin border
314      */

315     public static final int BORDER_THIN_THICK_THIN = 13;
316     /**
317      * Constant for a thick thin medium border
318      */

319     public static final int BORDER_THICK_THIN_MED = 14;
320     /**
321      * Constant for a thin thick medium border
322      */

323     public static final int BORDER_THIN_THICK_MED = 15;
324     /**
325      * Constant for a thin thick thin medium border
326      */

327     public static final int BORDER_THIN_THICK_THIN_MED = 16;
328     /**
329      * Constant for a thick thin large border
330      */

331     public static final int BORDER_THICK_THIN_LARGE = 17;
332     /**
333      * Constant for a thin thick large border
334      */

335     public static final int BORDER_THIN_THICK_LARGE = 18;
336     /**
337      * Constant for a thin thick thin large border
338      */

339     public static final int BORDER_THIN_THICK_THIN_LARGE = 19;
340     /**
341      * Constant for a wavy border
342      */

343     public static final int BORDER_WAVY = 20;
344     /**
345      * Constant for a double wavy border
346      */

347     public static final int BORDER_DOUBLE_WAVY = 21;
348     /**
349      * Constant for a striped border
350      */

351     public static final int BORDER_STRIPED = 22;
352     /**
353      * Constant for an embossed border
354      */

355     public static final int BORDER_EMBOSS = 23;
356     /**
357      * Constant for an engraved border
358      */

359     public static final int BORDER_ENGRAVE = 24;
360     
361     /**
362      * The type of this RtfBorder
363      */

364     private int borderType = ROW_BORDER;
365     /**
366      * The position of this RtfBorder
367      */

368     private int borderPosition = NO_BORDER;
369     /**
370      * The style of this RtfBorder
371      */

372     private int borderStyle = BORDER_NONE;
373     /**
374      * The width of this RtfBorder
375      */

376     private int borderWidth = 20;
377     /**
378      * The colour of this RtfBorder
379      */

380     private RtfColor borderColor = null;
381     
382     /**
383      * Makes a copy of the given RtfBorder
384      *
385      * @param doc The RtfDocument this RtfBorder belongs to
386      * @param borderType The border type of this RtfBorder
387      * @param border The RtfBorder to copy
388      */

389     protected RtfBorder(RtfDocument doc, int borderType, RtfBorder border) {
390         super(doc);
391         this.borderType = borderType;
392         this.borderPosition = border.getBorderPosition();
393         this.borderStyle = border.getBorderStyle();
394         this.borderWidth = border.getBorderWidth();
395         this.borderColor = new RtfColor(this.document, border.getBorderColor());
396     }
397     
398     /**
399      * Constructs a RtfBorder
400      *
401      * @param doc The RtfDocument this RtfBorder belongs to
402      * @param borderType The type of border this RtfBorder is
403      * @param borderPosition The position of this RtfBorder
404      * @param borderStyle The style of this RtfBorder
405      * @param borderWidth The width of this RtfBorder
406      * @param borderColor The colour of this RtfBorder
407      */

408     protected RtfBorder(RtfDocument doc, int borderType, int borderPosition, int borderStyle, float borderWidth, Color JavaDoc borderColor) {
409         super(doc);
410         this.borderType = borderType;
411         this.borderPosition = borderPosition;
412         this.borderStyle = borderStyle;
413         if(borderWidth > 2) {
414             borderWidth = 2;
415         }
416         this.borderWidth = (int) (borderWidth * TWIPS_FACTOR);
417         if(this.borderWidth == 0) {
418             this.borderStyle = BORDER_NONE;
419         }
420         if(borderColor == null) {
421             this.borderColor = new RtfColor(this.document, new Color JavaDoc(0, 0, 0));
422         } else {
423             this.borderColor = new RtfColor(this.document, borderColor);
424         }
425     }
426     
427     /**
428      * Writes the RtfBorder settings
429      *
430      * @return A byte array with the RtfBorder settings
431      * @deprecated replaced by {@link #writeContent(OutputStream)}
432      */

433     public byte[] write()
434     {
435         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
436         try {
437             writeContent(result);
438         } catch(IOException JavaDoc ioe) {
439             ioe.printStackTrace();
440         }
441         
442         return result.toByteArray();
443     }
444     /**
445      * Writes the RtfBorder settings
446      */

447     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
448     {
449         if(this.borderStyle == BORDER_NONE || this.borderPosition == NO_BORDER || this.borderWidth == 0) {
450             return;
451         }
452
453         if(this.borderType == ROW_BORDER) {
454             switch(this.borderPosition) {
455                 case LEFT_BORDER:
456                     result.write(ROW_BORDER_LEFT);
457                     break;
458                 case TOP_BORDER:
459                     result.write(ROW_BORDER_TOP);
460                     break;
461                 case RIGHT_BORDER:
462                     result.write(ROW_BORDER_RIGHT);
463                     break;
464                 case BOTTOM_BORDER:
465                     result.write(ROW_BORDER_BOTTOM);
466                     break;
467                 case HORIZONTAL_BORDER:
468                     result.write(ROW_BORDER_HORIZONTAL);
469                     break;
470                 case VERTICAL_BORDER:
471                     result.write(ROW_BORDER_VERTICAL);
472                     break;
473                 default:
474                     return;
475             }
476             result.write(writeBorderStyle());
477             result.write(BORDER_WIDTH);
478             result.write(intToByteArray(this.borderWidth));
479             result.write(BORDER_COLOR_NUMBER);
480             result.write(intToByteArray(this.borderColor.getColorNumber()));
481             result.write('\n');
482         } else if(this.borderType == CELL_BORDER) {
483             switch(this.borderPosition) {
484                 case LEFT_BORDER:
485                     result.write(CELL_BORDER_LEFT);
486                     break;
487                 case TOP_BORDER:
488                     result.write(CELL_BORDER_TOP);
489                     break;
490                 case RIGHT_BORDER:
491                     result.write(CELL_BORDER_RIGHT);
492                     break;
493                 case BOTTOM_BORDER:
494                     result.write(CELL_BORDER_BOTTOM);
495                     break;
496                 default:
497                     return;
498             }
499             result.write(writeBorderStyle());
500             result.write(BORDER_WIDTH);
501             result.write(intToByteArray(this.borderWidth));
502             result.write(BORDER_COLOR_NUMBER);
503             result.write(intToByteArray(this.borderColor.getColorNumber()));
504             result.write('\n');
505         }
506     }
507      
508     /**
509      * Writes the style of this RtfBorder
510      *
511      * @return A byte array containing the style of this RtfBorder
512      */

513     private byte[] writeBorderStyle() {
514         switch(this.borderStyle) {
515             case BORDER_NONE : return new byte[0];
516             case BORDER_SINGLE : return BORDER_STYLE_SINGLE;
517             case BORDER_DOUBLE_THICK : return BORDER_STYLE_DOUBLE_THICK;
518             case BORDER_SHADOWED : return BORDER_STYLE_SHADOWED;
519             case BORDER_DOTTED : return BORDER_STYLE_DOTTED;
520             case BORDER_DASHED : return BORDER_STYLE_DASHED;
521             case BORDER_HAIRLINE : return BORDER_STYLE_HAIRLINE;
522             case BORDER_DOUBLE : return BORDER_STYLE_DOUBLE;
523             case BORDER_DOT_DASH : return BORDER_STYLE_DOT_DASH;
524             case BORDER_DOT_DOT_DASH : return BORDER_STYLE_DOT_DOT_DASH;
525             case BORDER_TRIPLE : return BORDER_STYLE_TRIPLE;
526             case BORDER_THICK_THIN : return BORDER_STYLE_THICK_THIN;
527             case BORDER_THIN_THICK : return BORDER_STYLE_THIN_THICK;
528             case BORDER_THIN_THICK_THIN : return BORDER_STYLE_THIN_THICK_THIN;
529             case BORDER_THICK_THIN_MED : return BORDER_STYLE_THICK_THIN_MED;
530             case BORDER_THIN_THICK_MED : return BORDER_STYLE_THIN_THICK_MED;
531             case BORDER_THIN_THICK_THIN_MED : return BORDER_STYLE_THIN_THICK_THIN_MED;
532             case BORDER_THICK_THIN_LARGE : return BORDER_STYLE_THICK_THIN_LARGE;
533             case BORDER_THIN_THICK_LARGE : return BORDER_STYLE_THIN_THICK_LARGE;
534             case BORDER_THIN_THICK_THIN_LARGE : return BORDER_STYLE_THIN_THICK_THIN_LARGE;
535             case BORDER_WAVY : return BORDER_STYLE_WAVY;
536             case BORDER_DOUBLE_WAVY : return BORDER_STYLE_DOUBLE_WAVY;
537             case BORDER_STRIPED : return BORDER_STYLE_STRIPED;
538             case BORDER_EMBOSS : return BORDER_STYLE_EMBOSS;
539             case BORDER_ENGRAVE : return BORDER_STYLE_ENGRAVE;
540             default : return BORDER_STYLE_SINGLE;
541         }
542     }
543     
544     /**
545      * Gets the colour of this RtfBorder
546      *
547      * @return Returns RtfColor of this RtfBorder
548      */

549     protected RtfColor getBorderColor() {
550         return borderColor;
551     }
552
553     /**
554      * Gets the position of this RtfBorder
555      * @return Returns the position of this RtfBorder
556      */

557     protected int getBorderPosition() {
558         return borderPosition;
559     }
560
561     /**
562      * Gets the style of this RtfBorder
563      *
564      * @return Returns the style of this RtfBorder
565      */

566     protected int getBorderStyle() {
567         return borderStyle;
568     }
569
570     /**
571      * Gets the type of this RtfBorder
572      *
573      * @return Returns the type of this RtfBorder
574      */

575     protected int getBorderType() {
576         return borderType;
577     }
578
579     /**
580      * Gets the width of this RtfBorder
581      *
582      * @return Returns the width of this RtfBorder
583      */

584     protected int getBorderWidth() {
585         return borderWidth;
586     }
587 }
588
Popular Tags