KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > collection > PdfCollectionItem


1 package com.lowagie.text.pdf.collection;
2
3 import java.util.Calendar JavaDoc;
4
5 import com.lowagie.text.pdf.PdfDate;
6 import com.lowagie.text.pdf.PdfDictionary;
7 import com.lowagie.text.pdf.PdfName;
8 import com.lowagie.text.pdf.PdfNumber;
9 import com.lowagie.text.pdf.PdfObject;
10 import com.lowagie.text.pdf.PdfString;
11
12 public class PdfCollectionItem extends PdfDictionary {
13     
14     /** The PdfCollectionSchema with the names and types of the items. */
15     PdfCollectionSchema schema;
16     
17     /**
18      * Constructs a Collection Item that can be added to a PdfFileSpecification.
19      */

20     public PdfCollectionItem(PdfCollectionSchema schema) {
21         super(PdfName.COLLECTIONITEM);
22         this.schema = schema;
23     }
24     
25     /**
26      * Sets the value of the collection item.
27      * @param value
28      */

29     public void addItem(String JavaDoc key, String JavaDoc value) {
30         PdfName fieldname = new PdfName(key);
31         PdfCollectionField field = (PdfCollectionField)schema.get(fieldname);
32         put(fieldname, field.getValue(value));
33     }
34     
35     /**
36      * Sets the value of the collection item.
37      * @param value
38      */

39     public void addItem(String JavaDoc key, PdfString value) {
40         PdfName fieldname = new PdfName(key);
41         PdfCollectionField field = (PdfCollectionField)schema.get(fieldname);
42         if (field.type == PdfCollectionField.TEXT) {
43             put(fieldname, value);
44         }
45     }
46     
47     /**
48      * Sets the value of the collection item.
49      * @param d
50      */

51     public void addItem(String JavaDoc key, PdfDate d) {
52         PdfName fieldname = new PdfName(key);
53         PdfCollectionField field = (PdfCollectionField)schema.get(fieldname);
54         if (field.type == PdfCollectionField.DATE) {
55             put(fieldname, d);
56         }
57     }
58     
59     /**
60      * Sets the value of the collection item.
61      * @param n
62      */

63     public void addItem(String JavaDoc key, PdfNumber n) {
64         PdfName fieldname = new PdfName(key);
65         PdfCollectionField field = (PdfCollectionField)schema.get(fieldname);
66         if (field.type == PdfCollectionField.NUMBER) {
67             put(fieldname, n);
68         }
69     }
70     
71     /**
72      * Sets the value of the collection item.
73      * @param c
74      */

75     public void addItem(String JavaDoc key, Calendar JavaDoc c) {
76         addItem(key, new PdfDate(c));
77     }
78     
79     /**
80      * Sets the value of the collection item.
81      * @param i
82      */

83     public void addItem(String JavaDoc key, int i) {
84         addItem(key, new PdfNumber(i));
85     }
86     
87     /**
88      * Sets the value of the collection item.
89      * @param f
90      */

91     public void addItem(String JavaDoc key, float f) {
92         addItem(key, new PdfNumber(f));
93     }
94     
95     /**
96      * Sets the value of the collection item.
97      * @param d
98      */

99     public void addItem(String JavaDoc key, double d) {
100         addItem(key, new PdfNumber(d));
101     }
102     
103     /**
104      * Adds a prefix for the Collection item.
105      * You can only use this method after you have set the value of the item.
106      * @param prefix a prefix
107      */

108     public void setPrefix(String JavaDoc key, String JavaDoc prefix) {
109         PdfName fieldname = new PdfName(key);
110         PdfObject o = get(fieldname);
111         if (o == null)
112             throw new IllegalArgumentException JavaDoc("You must set a value before adding a prefix.");
113         PdfDictionary dict = new PdfDictionary(PdfName.COLLECTIONSUBITEM);
114         dict.put(PdfName.D, o);
115         dict.put(PdfName.P, new PdfString(prefix, PdfObject.TEXT_UNICODE));
116         put(fieldname, dict);
117     }
118 }
Popular Tags