001: package com.lowagie.text.pdf.collection;
002:
003: import com.lowagie.text.pdf.PdfBoolean;
004: import com.lowagie.text.pdf.PdfDate;
005: import com.lowagie.text.pdf.PdfDictionary;
006: import com.lowagie.text.pdf.PdfName;
007: import com.lowagie.text.pdf.PdfNumber;
008: import com.lowagie.text.pdf.PdfObject;
009: import com.lowagie.text.pdf.PdfString;
010:
011: /**
012: * @author blowagie
013: *
014: */
015: public class PdfCollectionField extends PdfDictionary {
016: /** A possible type of collection field. */
017: public static final int TEXT = 0;
018: /** A possible type of collection field. */
019: public static final int DATE = 1;
020: /** A possible type of collection field. */
021: public static final int NUMBER = 2;
022: /** A possible type of collection field. */
023: public static final int FILENAME = 3;
024: /** A possible type of collection field. */
025: public static final int DESC = 4;
026: /** A possible type of collection field. */
027: public static final int MODDATE = 5;
028: /** A possible type of collection field. */
029: public static final int CREATIONDATE = 6;
030: /** A possible type of collection field. */
031: public static final int SIZE = 7;
032:
033: /** The type of the PDF collection field. */
034: protected int type;
035:
036: /**
037: * Creates a PdfCollectionField.
038: * @param name the field name
039: * @param type the field type
040: */
041: public PdfCollectionField(String name, int type) {
042: super (PdfName.COLLECTIONFIELD);
043: put(PdfName.N, new PdfString(name, PdfObject.TEXT_UNICODE));
044: this .type = type;
045: switch (type) {
046: default:
047: put(PdfName.SUBTYPE, PdfName.S);
048: break;
049: case DATE:
050: put(PdfName.SUBTYPE, PdfName.D);
051: break;
052: case NUMBER:
053: put(PdfName.SUBTYPE, PdfName.N);
054: break;
055: case FILENAME:
056: put(PdfName.SUBTYPE, PdfName.F);
057: break;
058: case DESC:
059: put(PdfName.SUBTYPE, PdfName.DESC);
060: break;
061: case MODDATE:
062: put(PdfName.SUBTYPE, PdfName.MODDATE);
063: break;
064: case CREATIONDATE:
065: put(PdfName.SUBTYPE, PdfName.CREATIONDATE);
066: break;
067: case SIZE:
068: put(PdfName.SUBTYPE, PdfName.SIZE);
069: break;
070: }
071: }
072:
073: /**
074: * The relative order of the field name. Fields are sorted in ascending order.
075: * @param i a number indicating the order of the field
076: */
077: public void setOrder(int i) {
078: put(PdfName.O, new PdfNumber(i));
079: }
080:
081: /**
082: * Sets the initial visibility of the field.
083: * @param visible the default is true (visible)
084: */
085: public void setVisible(boolean visible) {
086: put(PdfName.V, new PdfBoolean(visible));
087: }
088:
089: /**
090: * Indication if the field value should be editable in the viewer.
091: * @param editable the default is false (not editable)
092: */
093: public void setEditable(boolean editable) {
094: put(PdfName.E, new PdfBoolean(editable));
095: }
096:
097: /**
098: * Checks if the type of the field is suitable for a Collection Item.
099: */
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 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(v
125: + " is not an acceptable value for the field "
126: + get(PdfName.N).toString());
127: }
128: }
|