001: /**
002: * $Id: AbstractRtfField.java 2698 2007-04-19 12:03:08Z blowagie $
003: *
004: * Copyright 2002 by
005: * <a href="http://www.smb-tec.com">SMB</a>
006: * Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
007: *
008: * The contents of this file are subject to the Mozilla Public License Version 1.1
009: * (the "License"); you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the License.
015: *
016: * The Original Code is 'iText, a free JAVA-PDF library'.
017: *
018: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
019: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
020: * All Rights Reserved.
021: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
022: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
023: *
024: * Contributor(s): all the names of the contributors are added in the source code
025: * where applicable.
026: *
027: * Alternatively, the contents of this file may be used under the terms of the
028: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
029: * provisions of LGPL are applicable instead of those above. If you wish to
030: * allow use of your version of this file only under the terms of the LGPL
031: * License and not to allow others to use your version of this file under
032: * the MPL, indicate your decision by deleting the provisions above and
033: * replace them with the notice and other provisions required by the LGPL.
034: * If you do not delete the provisions above, a recipient may use your version
035: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
036:
037: *
038: * This library is free software; you can redistribute it and/or modify it
039: * under the terms of the MPL as stated above or under the terms of the GNU
040: * Library General Public License as published by the Free Software Foundation;
041: * either version 2 of the License, or any later version.
042: *
043: * This library is distributed in the hope that it will be useful, but WITHOUT
044: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
045: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
046: * details.
047: *
048: * If you didn't download this code from the following link, you should check if
049: * you aren't using an obsolete version:
050: * http://www.lowagie.com/iText/
051: */package com.lowagie.text.rtf;
052:
053: import java.io.IOException;
054: import java.io.OutputStream;
055:
056: import com.lowagie.text.Chunk;
057: import com.lowagie.text.Font;
058:
059: /**
060: * This class implements an abstract RtfField.
061: *
062: * This class is based on the RtfWriter-package from Mark Hall.
063: *
064: * ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
065: *
066: * @author Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
067: * @version $Id: AbstractRtfField.java 2698 2007-04-19 12:03:08Z blowagie $
068: * @since Mon Aug 19 14:50:39 2002
069: * @deprecated Please move to the RtfWriter2 and associated classes.
070: */
071: abstract class AbstractRtfField extends Chunk implements RtfField {
072: private static final byte[] fldDirty = "\\flddirty".getBytes();
073: private static final byte[] fldPriv = "\\fldpriv".getBytes();
074: private static final byte[] fldLock = "\\fldlock".getBytes();
075: private static final byte[] fldEdit = "\\fldedit".getBytes();
076: private static final byte[] fldAlt = "\\fldalt".getBytes();
077:
078: /**
079: * public constructor
080: * @param content the content of the field
081: * @param font the font of the field
082: */
083: public AbstractRtfField(String content, Font font) {
084: super (content, font);
085: }
086:
087: /**
088: * Determines whether this RtfField is locked, i.e. it cannot be
089: * updated. Defaults to <tt>false</tt>.
090: */
091: private boolean rtfFieldIsLocked = false;
092:
093: /**
094: * Determines whether a formatting change has been made since the
095: * field was last updated. Defaults to <tt>false</tt>.
096: */
097: private boolean rtfFieldIsDirty = false;
098:
099: /**
100: * Determines whether text has been added, removed from thre field
101: * result since the field was last updated. Defaults to
102: * <tt>false</tt>.
103: */
104: private boolean rtfFieldWasEdited = false;
105:
106: /**
107: * Determines whether the field is in suitable form for
108: * display. Defaults to <tt>false</tt>.
109: */
110: private boolean rtfFieldIsPrivate = false;
111:
112: /**
113: * Determines whether this RtfField shall refer to an end note.
114: */
115: private boolean rtfFieldIsAlt = false;
116:
117: /**
118: * Determines whtether the field is locked, i.e. it cannot be
119: * updated.
120: *
121: * @return <tt>true</tt> iff the field cannot be updated,
122: * <tt>false</tt> otherwise.
123: */
124: public final boolean isLocked() {
125: return this .rtfFieldIsLocked;
126: }
127:
128: /**
129: * Set whether the field can be updated.
130: *
131: * @param rtfFieldIsLocked <tt>true</tt> if the field cannot be
132: * updated, <tt>false</tt> otherwise.
133: */
134: public final void setLocked(final boolean rtfFieldIsLocked) {
135: this .rtfFieldIsLocked = rtfFieldIsLocked;
136: }
137:
138: /**
139: * Set whether a formatting change has been made since the field
140: * was last updated
141: * @param rtfFieldIsDirty <tt>true</tt> if the field was
142: * changed since the field was last updated, <tt>false</tt>
143: * otherwise.
144: */
145: public final void setDirty(final boolean rtfFieldIsDirty) {
146: this .rtfFieldIsDirty = rtfFieldIsDirty;
147: }
148:
149: /**
150: * Determines whether the field was changed since the field was
151: * last updated
152: * @return <tt>true</tt> if the field was changed since the field
153: * was last updated, <tt>false</tt> otherwise.
154: */
155: public final boolean isDirty() {
156: return this .rtfFieldIsDirty;
157: }
158:
159: /**
160: * Set whether text has been added, removed from thre field result
161: * since the field was last updated.
162: * @param rtfFieldWasEdited Determines whether text has been
163: * added, removed from the field result since the field was last
164: * updated (<tt>true</tt>, <tt>false</tt> otherwise..
165: */
166: public final void setEdited(final boolean rtfFieldWasEdited) {
167: this .rtfFieldWasEdited = rtfFieldWasEdited;
168: }
169:
170: /**
171: * Determines whether text has been added, removed from the field
172: * result since the field was last updated.
173: * @return rtfFieldWasEdited <tt>true</tt> if text has been added,
174: * removed from the field result since the field was last updated,
175: * <tt>false</tt> otherwise.
176: */
177: public final boolean wasEdited() {
178: return this .rtfFieldWasEdited;
179: }
180:
181: /**
182: * Set whether the field is in suitable form for
183: * display. I.e. it's not a field with a picture as field result
184: * @param rtfFieldIsPrivate Determines whether the field is in
185: * suitable form for display: <tt>true</tt> it can be displayed,
186: * <tt>false</tt> it cannot be displayed.
187: */
188: public final void setPrivate(final boolean rtfFieldIsPrivate) {
189: this .rtfFieldIsPrivate = rtfFieldIsPrivate;
190: }
191:
192: /**
193: * Determines whether the field is in suitable form for display.
194: * @return whether the field is in suitable form for display:
195: * <tt>true</tt> yes, <tt>false</tt> no it cannot be displayed.
196: */
197: public final boolean isPrivate() {
198: return this .rtfFieldIsPrivate;
199: }
200:
201: /**
202: * Abstract method for writing custom stuff to the Field
203: * Initialization Stuff part of an RtfField.
204: * @param out
205: * @throws IOException
206: */
207: public abstract void writeRtfFieldInitializationStuff(
208: OutputStream out) throws IOException;
209:
210: /**
211: * Abstract method for writing custom stuff to the Field Result
212: * part of an RtfField.
213: * @param out
214: * @throws IOException
215: */
216: public abstract void writeRtfFieldResultStuff(OutputStream out)
217: throws IOException;
218:
219: /**
220: * Determines whether this RtfField shall refer to an end note.
221: * @param rtfFieldIsAlt <tt>true</tt> if this RtfField shall refer
222: * to an end note, <tt>false</tt> otherwise
223: */
224: public final void setAlt(final boolean rtfFieldIsAlt) {
225: this .rtfFieldIsAlt = rtfFieldIsAlt;
226: }
227:
228: /**
229: * Determines whether this RtfField shall refer to an end
230: * note.
231: * @return <tt>true</tt> if this RtfField shall refer to an end
232: * note, <tt>false</tt> otherwise.
233: */
234: public final boolean isAlt() {
235: return this .rtfFieldIsAlt;
236: }
237:
238: /**
239: * empty implementation for Chunk.
240: * @return an empty string
241: * @deprecated Use {@link #getContent()} instead
242: */
243: public final String content() {
244: return getContent();
245: }
246:
247: /**
248: * empty implementation for Chunk.
249: * @return an empty string
250: */
251: public final String getContent() {
252: return "";
253: }
254:
255: /**
256: * For Interface RtfField.
257: * @param writer
258: * @param out
259: * @throws IOException
260: */
261: public void write(RtfWriter writer, OutputStream out)
262: throws IOException {
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 out)
282: throws IOException {
283: out.write(RtfWriter.openGroup);
284: out.write(RtfWriter.escape);
285: out.write(RtfWriter.field);
286: }
287:
288: /**
289: * Write the modifiers defined for a RtfField to the OutputStream.
290: * @param out
291: * @throws IOException
292: */
293: protected final void writeRtfFieldModifiers(OutputStream out)
294: throws IOException {
295: if (isDirty()) {
296: out.write(fldDirty);
297: }
298:
299: if (wasEdited()) {
300: out.write(fldEdit);
301: }
302:
303: if (isLocked()) {
304: out.write(fldLock);
305: }
306:
307: if (isPrivate()) {
308: out.write(fldPriv);
309: }
310: }
311:
312: /**
313: * Write RtfField Initialization Stuff to OutputStream.
314: * @param out
315: * @throws IOException
316: */
317: protected final void writeRtfFieldInstBegin(OutputStream out)
318: throws IOException {
319: out.write(RtfWriter.openGroup);
320: out.write(RtfWriter.escape);
321: out.write(RtfWriter.fieldContent);
322: out.write(RtfWriter.delimiter);
323: }
324:
325: /**
326: * Write end of RtfField Initialization Stuff to OutputStream.
327: * @param out
328: * @throws IOException
329: */
330: protected final void writeRtfFieldInstEnd(OutputStream out)
331: throws IOException {
332: if (isAlt()) {
333: out.write(fldAlt);
334: out.write(RtfWriter.delimiter);
335: }
336:
337: out.write(RtfWriter.closeGroup);
338: }
339:
340: /**
341: * Write beginning of RtfField Result to OutputStream.
342: * @param out
343: * @throws IOException
344: */
345: protected final void writeRtfFieldResultBegin(OutputStream out)
346: throws IOException {
347: out.write(RtfWriter.openGroup);
348: out.write(RtfWriter.escape);
349: out.write(RtfWriter.fieldDisplay);
350: out.write(RtfWriter.delimiter);
351: }
352:
353: /**
354: * Write end of RtfField Result to OutputStream.
355: * @param out
356: * @throws IOException
357: */
358: protected final void writeRtfFieldResultEnd(OutputStream out)
359: throws IOException {
360: out.write(RtfWriter.delimiter);
361: out.write(RtfWriter.closeGroup);
362: }
363:
364: /**
365: * Close the RtfField.
366: * @param out
367: * @throws IOException
368: */
369: protected final void writeRtfFieldEnd(OutputStream out)
370: throws IOException {
371: out.write(RtfWriter.closeGroup);
372: }
373: }
|