001: package com.lowagie.text.pdf.collection;
002:
003: import java.util.Calendar;
004:
005: import com.lowagie.text.pdf.PdfDate;
006: import com.lowagie.text.pdf.PdfDictionary;
007: import com.lowagie.text.pdf.PdfName;
008: import com.lowagie.text.pdf.PdfNumber;
009: import com.lowagie.text.pdf.PdfObject;
010: import com.lowagie.text.pdf.PdfString;
011:
012: public class PdfCollectionItem extends PdfDictionary {
013:
014: /** The PdfCollectionSchema with the names and types of the items. */
015: PdfCollectionSchema schema;
016:
017: /**
018: * Constructs a Collection Item that can be added to a PdfFileSpecification.
019: */
020: public PdfCollectionItem(PdfCollectionSchema schema) {
021: super (PdfName.COLLECTIONITEM);
022: this .schema = schema;
023: }
024:
025: /**
026: * Sets the value of the collection item.
027: * @param value
028: */
029: public void addItem(String key, String value) {
030: PdfName fieldname = new PdfName(key);
031: PdfCollectionField field = (PdfCollectionField) schema
032: .get(fieldname);
033: put(fieldname, field.getValue(value));
034: }
035:
036: /**
037: * Sets the value of the collection item.
038: * @param value
039: */
040: public void addItem(String key, PdfString value) {
041: PdfName fieldname = new PdfName(key);
042: PdfCollectionField field = (PdfCollectionField) schema
043: .get(fieldname);
044: if (field.type == PdfCollectionField.TEXT) {
045: put(fieldname, value);
046: }
047: }
048:
049: /**
050: * Sets the value of the collection item.
051: * @param d
052: */
053: public void addItem(String key, PdfDate d) {
054: PdfName fieldname = new PdfName(key);
055: PdfCollectionField field = (PdfCollectionField) schema
056: .get(fieldname);
057: if (field.type == PdfCollectionField.DATE) {
058: put(fieldname, d);
059: }
060: }
061:
062: /**
063: * Sets the value of the collection item.
064: * @param n
065: */
066: public void addItem(String key, PdfNumber n) {
067: PdfName fieldname = new PdfName(key);
068: PdfCollectionField field = (PdfCollectionField) schema
069: .get(fieldname);
070: if (field.type == PdfCollectionField.NUMBER) {
071: put(fieldname, n);
072: }
073: }
074:
075: /**
076: * Sets the value of the collection item.
077: * @param c
078: */
079: public void addItem(String key, Calendar c) {
080: addItem(key, new PdfDate(c));
081: }
082:
083: /**
084: * Sets the value of the collection item.
085: * @param i
086: */
087: public void addItem(String key, int i) {
088: addItem(key, new PdfNumber(i));
089: }
090:
091: /**
092: * Sets the value of the collection item.
093: * @param f
094: */
095: public void addItem(String key, float f) {
096: addItem(key, new PdfNumber(f));
097: }
098:
099: /**
100: * Sets the value of the collection item.
101: * @param d
102: */
103: public void addItem(String key, double d) {
104: addItem(key, new PdfNumber(d));
105: }
106:
107: /**
108: * Adds a prefix for the Collection item.
109: * You can only use this method after you have set the value of the item.
110: * @param prefix a prefix
111: */
112: public void setPrefix(String key, String prefix) {
113: PdfName fieldname = new PdfName(key);
114: PdfObject o = get(fieldname);
115: if (o == null)
116: throw new IllegalArgumentException(
117: "You must set a value before adding a prefix.");
118: PdfDictionary dict = new PdfDictionary(
119: PdfName.COLLECTIONSUBITEM);
120: dict.put(PdfName.D, o);
121: dict.put(PdfName.P, new PdfString(prefix,
122: PdfObject.TEXT_UNICODE));
123: put(fieldname, dict);
124: }
125: }
|