KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.lowagie.text.pdf.collection;
2
3 import com.lowagie.text.pdf.PdfBoolean;
4 import com.lowagie.text.pdf.PdfDate;
5 import com.lowagie.text.pdf.PdfDictionary;
6 import com.lowagie.text.pdf.PdfName;
7 import com.lowagie.text.pdf.PdfNumber;
8 import com.lowagie.text.pdf.PdfObject;
9 import com.lowagie.text.pdf.PdfString;
10
11 /**
12  * @author blowagie
13  *
14  */

15 public class PdfCollectionField extends PdfDictionary {
16     /** A possible type of collection field. */
17     public static final int TEXT = 0;
18     /** A possible type of collection field. */
19     public static final int DATE = 1;
20     /** A possible type of collection field. */
21     public static final int NUMBER = 2;
22     /** A possible type of collection field. */
23     public static final int FILENAME = 3;
24     /** A possible type of collection field. */
25     public static final int DESC = 4;
26     /** A possible type of collection field. */
27     public static final int MODDATE = 5;
28     /** A possible type of collection field. */
29     public static final int CREATIONDATE = 6;
30     /** A possible type of collection field. */
31     public static final int SIZE = 7;
32     
33     /** The type of the PDF collection field. */
34     protected int type;
35
36     /**
37      * Creates a PdfCollectionField.
38      * @param name the field name
39      * @param type the field type
40      */

41     public PdfCollectionField(String JavaDoc name, int type) {
42         super(PdfName.COLLECTIONFIELD);
43         put(PdfName.N, new PdfString(name, PdfObject.TEXT_UNICODE));
44         this.type = type;
45         switch(type) {
46         default:
47             put(PdfName.SUBTYPE, PdfName.S);
48             break;
49         case DATE:
50             put(PdfName.SUBTYPE, PdfName.D);
51             break;
52         case NUMBER:
53             put(PdfName.SUBTYPE, PdfName.N);
54             break;
55         case FILENAME:
56             put(PdfName.SUBTYPE, PdfName.F);
57             break;
58         case DESC:
59             put(PdfName.SUBTYPE, PdfName.DESC);
60             break;
61         case MODDATE:
62             put(PdfName.SUBTYPE, PdfName.MODDATE);
63             break;
64         case CREATIONDATE:
65             put(PdfName.SUBTYPE, PdfName.CREATIONDATE);
66             break;
67         case SIZE:
68             put(PdfName.SUBTYPE, PdfName.SIZE);
69             break;
70         }
71     }
72     
73     /**
74      * The relative order of the field name. Fields are sorted in ascending order.
75      * @param i a number indicating the order of the field
76      */

77     public void setOrder(int i) {
78         put(PdfName.O, new PdfNumber(i));
79     }
80     
81     /**
82      * Sets the initial visibility of the field.
83      * @param visible the default is true (visible)
84      */

85     public void setVisible(boolean visible) {
86         put(PdfName.V, new PdfBoolean(visible));
87     }
88     
89     /**
90      * Indication if the field value should be editable in the viewer.
91      * @param editable the default is false (not editable)
92      */

93     public void setEditable(boolean editable) {
94         put(PdfName.E, new PdfBoolean(editable));
95     }
96
97     /**
98      * Checks if the type of the field is suitable for a Collection Item.
99      */

100     public boolean isCollectionItem() {
101         switch(type) {
102         case TEXT:
103         case DATE:
104         case NUMBER:
105             return true;
106         default:
107             return false;
108         }
109     }
110     
111     /**
112      * Returns a PdfObject that can be used as the value of a Collection Item.
113      * @param v value the value that has to be changed into a PdfObject (PdfString, PdfDate or PdfNumber)
114      */

115     public PdfObject getValue(String JavaDoc v) {
116         switch(type) {
117         case TEXT:
118             return new PdfString(v, PdfObject.TEXT_UNICODE);
119         case DATE:
120             return new PdfDate(PdfDate.decode(v));
121         case NUMBER:
122             return new PdfNumber(v);
123         }
124         throw new IllegalArgumentException JavaDoc(v + " is not an acceptable value for the field " + get(PdfName.N).toString());
125     }
126 }
Popular Tags