KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > AbstractRtfField


1 /**
2  * $Id: AbstractRtfField.java 2698 2007-04-19 12:03:08Z blowagie $
3  *
4  * Copyright 2002 by
5  * <a HREF="http://www.smb-tec.com">SMB</a>
6  * Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
7  *
8  * The contents of this file are subject to the Mozilla Public License Version 1.1
9  * (the "License"); you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the License.
15  *
16  * The Original Code is 'iText, a free JAVA-PDF library'.
17  *
18  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
19  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
20  * All Rights Reserved.
21  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
22  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
23  *
24  * Contributor(s): all the names of the contributors are added in the source code
25  * where applicable.
26  *
27  * Alternatively, the contents of this file may be used under the terms of the
28  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
29  * provisions of LGPL are applicable instead of those above. If you wish to
30  * allow use of your version of this file only under the terms of the LGPL
31  * License and not to allow others to use your version of this file under
32  * the MPL, indicate your decision by deleting the provisions above and
33  * replace them with the notice and other provisions required by the LGPL.
34  * If you do not delete the provisions above, a recipient may use your version
35  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
36
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the MPL as stated above or under the terms of the GNU
40  * Library General Public License as published by the Free Software Foundation;
41  * either version 2 of the License, or any later version.
42  *
43  * This library is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
45  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46  * details.
47  *
48  * If you didn't download this code from the following link, you should check if
49  * you aren't using an obsolete version:
50  * http://www.lowagie.com/iText/
51  */

52
53 package com.lowagie.text.rtf;
54
55 import java.io.IOException JavaDoc;
56 import java.io.OutputStream JavaDoc;
57
58 import com.lowagie.text.Chunk;
59 import com.lowagie.text.Font;
60
61
62 /**
63  * This class implements an abstract RtfField.
64  *
65  * This class is based on the RtfWriter-package from Mark Hall.
66  *
67  * ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
68  *
69  * @author Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
70  * @version $Id: AbstractRtfField.java 2698 2007-04-19 12:03:08Z blowagie $
71  * @since Mon Aug 19 14:50:39 2002
72  * @deprecated Please move to the RtfWriter2 and associated classes.
73  */

74 abstract class AbstractRtfField extends Chunk implements RtfField {
75     private static final byte[] fldDirty = "\\flddirty".getBytes();
76     private static final byte[] fldPriv = "\\fldpriv".getBytes();
77     private static final byte[] fldLock = "\\fldlock".getBytes();
78     private static final byte[] fldEdit = "\\fldedit".getBytes();
79     private static final byte[] fldAlt = "\\fldalt".getBytes();
80
81     /**
82      * public constructor
83      * @param content the content of the field
84      * @param font the font of the field
85      */

86     public AbstractRtfField(String JavaDoc content, Font font) {
87         super(content, font);
88     }
89
90     /**
91      * Determines whether this RtfField is locked, i.e. it cannot be
92      * updated. Defaults to <tt>false</tt>.
93      */

94     private boolean rtfFieldIsLocked = false;
95
96     /**
97      * Determines whether a formatting change has been made since the
98      * field was last updated. Defaults to <tt>false</tt>.
99      */

100     private boolean rtfFieldIsDirty = false;
101
102     /**
103      * Determines whether text has been added, removed from thre field
104      * result since the field was last updated. Defaults to
105      * <tt>false</tt>.
106      */

107     private boolean rtfFieldWasEdited = false;
108
109     /**
110      * Determines whether the field is in suitable form for
111      * display. Defaults to <tt>false</tt>.
112      */

113     private boolean rtfFieldIsPrivate = false;
114
115     /**
116      * Determines whether this RtfField shall refer to an end note.
117      */

118     private boolean rtfFieldIsAlt = false;
119
120     /**
121      * Determines whtether the field is locked, i.e. it cannot be
122      * updated.
123      *
124      * @return <tt>true</tt> iff the field cannot be updated,
125      * <tt>false</tt> otherwise.
126      */

127     public final boolean isLocked() {
128         return this.rtfFieldIsLocked;
129     }
130
131     /**
132      * Set whether the field can be updated.
133      *
134      * @param rtfFieldIsLocked <tt>true</tt> if the field cannot be
135      * updated, <tt>false</tt> otherwise.
136      */

137     public final void setLocked(final boolean rtfFieldIsLocked) {
138         this.rtfFieldIsLocked = rtfFieldIsLocked;
139     }
140
141     /**
142      * Set whether a formatting change has been made since the field
143      * was last updated
144      * @param rtfFieldIsDirty <tt>true</tt> if the field was
145      * changed since the field was last updated, <tt>false</tt>
146      * otherwise.
147      */

148     public final void setDirty(final boolean rtfFieldIsDirty) {
149         this.rtfFieldIsDirty = rtfFieldIsDirty;
150     }
151
152     /**
153      * Determines whether the field was changed since the field was
154      * last updated
155      * @return <tt>true</tt> if the field was changed since the field
156      * was last updated, <tt>false</tt> otherwise.
157      */

158     public final boolean isDirty() {
159         return this.rtfFieldIsDirty;
160     }
161
162     /**
163      * Set whether text has been added, removed from thre field result
164      * since the field was last updated.
165      * @param rtfFieldWasEdited Determines whether text has been
166      * added, removed from the field result since the field was last
167      * updated (<tt>true</tt>, <tt>false</tt> otherwise..
168      */

169     public final void setEdited(final boolean rtfFieldWasEdited) {
170         this.rtfFieldWasEdited = rtfFieldWasEdited;
171     }
172
173     /**
174      * Determines whether text has been added, removed from the field
175      * result since the field was last updated.
176      * @return rtfFieldWasEdited <tt>true</tt> if text has been added,
177      * removed from the field result since the field was last updated,
178      * <tt>false</tt> otherwise.
179      */

180     public final boolean wasEdited() {
181         return this.rtfFieldWasEdited;
182     }
183
184     /**
185      * Set whether the field is in suitable form for
186      * display. I.e. it's not a field with a picture as field result
187      * @param rtfFieldIsPrivate Determines whether the field is in
188      * suitable form for display: <tt>true</tt> it can be displayed,
189      * <tt>false</tt> it cannot be displayed.
190      */

191     public final void setPrivate(final boolean rtfFieldIsPrivate) {
192         this.rtfFieldIsPrivate = rtfFieldIsPrivate;
193     }
194
195     /**
196      * Determines whether the field is in suitable form for display.
197      * @return whether the field is in suitable form for display:
198      * <tt>true</tt> yes, <tt>false</tt> no it cannot be displayed.
199      */

200     public final boolean isPrivate() {
201         return this.rtfFieldIsPrivate;
202     }
203
204     /**
205      * Abstract method for writing custom stuff to the Field
206      * Initialization Stuff part of an RtfField.
207      * @param out
208      * @throws IOException
209      */

210     public abstract void writeRtfFieldInitializationStuff(OutputStream JavaDoc out) throws IOException JavaDoc;
211
212     /**
213      * Abstract method for writing custom stuff to the Field Result
214      * part of an RtfField.
215      * @param out
216      * @throws IOException
217      */

218     public abstract void writeRtfFieldResultStuff(OutputStream JavaDoc out) throws IOException JavaDoc;
219
220     /**
221      * Determines whether this RtfField shall refer to an end note.
222      * @param rtfFieldIsAlt <tt>true</tt> if this RtfField shall refer
223      * to an end note, <tt>false</tt> otherwise
224      */

225     public final void setAlt(final boolean rtfFieldIsAlt) {
226         this.rtfFieldIsAlt = rtfFieldIsAlt;
227     }
228
229     /**
230      * Determines whether this RtfField shall refer to an end
231      * note.
232      * @return <tt>true</tt> if this RtfField shall refer to an end
233      * note, <tt>false</tt> otherwise.
234      */

235     public final boolean isAlt() {
236         return this.rtfFieldIsAlt;
237     }
238
239     /**
240      * empty implementation for Chunk.
241      * @return an empty string
242      * @deprecated Use {@link #getContent()} instead
243      */

244     public final String JavaDoc content() {
245         return getContent();
246     }
247
248     /**
249      * empty implementation for Chunk.
250      * @return an empty string
251      */

252     public final String JavaDoc getContent() {
253         return "";
254     }
255
256     /**
257      * For Interface RtfField.
258      * @param writer
259      * @param out
260      * @throws IOException
261      */

262     public void write( RtfWriter writer, OutputStream JavaDoc out ) throws IOException JavaDoc {
263         writeRtfFieldBegin(out);
264         writeRtfFieldModifiers(out);
265         writeRtfFieldInstBegin(out);
266         writer.writeInitialFontSignature( out, this );
267         writeRtfFieldInitializationStuff(out);
268         writeRtfFieldInstEnd(out);
269         writeRtfFieldResultBegin(out);
270         writer.writeInitialFontSignature( out, this );
271         writeRtfFieldResultStuff(out);
272         writeRtfFieldResultEnd(out);
273         writeRtfFieldEnd(out);
274     }
275
276     /**
277      * Write the beginning of an RtfField to the OutputStream.
278      * @param out
279      * @throws IOException
280      */

281     protected final void writeRtfFieldBegin(OutputStream JavaDoc out) throws IOException JavaDoc {
282         out.write(RtfWriter.openGroup);
283         out.write(RtfWriter.escape);
284         out.write(RtfWriter.field);
285     }
286
287     /**
288      * Write the modifiers defined for a RtfField to the OutputStream.
289      * @param out
290      * @throws IOException
291      */

292     protected final void writeRtfFieldModifiers(OutputStream JavaDoc out) throws IOException JavaDoc {
293         if (isDirty()) {
294             out.write(fldDirty);
295         }
296
297         if (wasEdited()) {
298             out.write(fldEdit);
299         }
300
301         if (isLocked()) {
302             out.write(fldLock);
303         }
304
305         if (isPrivate()) {
306             out.write(fldPriv);
307         }
308     }
309
310     /**
311      * Write RtfField Initialization Stuff to OutputStream.
312      * @param out
313      * @throws IOException
314      */

315     protected final void writeRtfFieldInstBegin(OutputStream JavaDoc out) throws IOException JavaDoc {
316         out.write( RtfWriter.openGroup );
317         out.write( RtfWriter.escape );
318         out.write( RtfWriter.fieldContent );
319         out.write( RtfWriter.delimiter );
320     }
321
322     /**
323      * Write end of RtfField Initialization Stuff to OutputStream.
324      * @param out
325      * @throws IOException
326      */

327     protected final void writeRtfFieldInstEnd(OutputStream JavaDoc out) throws IOException JavaDoc {
328         if (isAlt()) {
329             out.write( fldAlt );
330             out.write( RtfWriter.delimiter );
331         }
332
333         out.write( RtfWriter.closeGroup );
334     }
335
336     /**
337      * Write beginning of RtfField Result to OutputStream.
338      * @param out
339      * @throws IOException
340      */

341     protected final void writeRtfFieldResultBegin(OutputStream JavaDoc out) throws IOException JavaDoc {
342         out.write( RtfWriter.openGroup );
343         out.write( RtfWriter.escape );
344         out.write( RtfWriter.fieldDisplay );
345         out.write( RtfWriter.delimiter );
346     }
347
348     /**
349      * Write end of RtfField Result to OutputStream.
350      * @param out
351      * @throws IOException
352      */

353     protected final void writeRtfFieldResultEnd(OutputStream JavaDoc out) throws IOException JavaDoc {
354         out.write( RtfWriter.delimiter );
355         out.write( RtfWriter.closeGroup );
356     }
357
358     /**
359      * Close the RtfField.
360      * @param out
361      * @throws IOException
362      */

363     protected final void writeRtfFieldEnd(OutputStream JavaDoc out) throws IOException JavaDoc {
364         out.write( RtfWriter.closeGroup );
365     }
366 }
367
Popular Tags