001/*
002 * $Id: PdfCollectionField.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf.collection;
045
046import com.itextpdf.text.pdf.PdfBoolean;
047import com.itextpdf.text.pdf.PdfDate;
048import com.itextpdf.text.pdf.PdfDictionary;
049import com.itextpdf.text.pdf.PdfName;
050import com.itextpdf.text.pdf.PdfNumber;
051import com.itextpdf.text.pdf.PdfObject;
052import com.itextpdf.text.pdf.PdfString;
053import com.itextpdf.text.error_messages.MessageLocalization;
054
055/**
056 * @author blowagie
057 *
058 */
059public class PdfCollectionField extends PdfDictionary {
060        /** A possible type of collection field. */
061        public static final int TEXT = 0;
062        /** A possible type of collection field. */
063        public static final int DATE = 1;
064        /** A possible type of collection field. */
065        public static final int NUMBER = 2;
066        /** A possible type of collection field. */
067        public static final int FILENAME = 3;
068        /** A possible type of collection field. */
069        public static final int DESC = 4;
070        /** A possible type of collection field. */
071        public static final int MODDATE = 5;
072        /** A possible type of collection field. */
073        public static final int CREATIONDATE = 6;
074        /** A possible type of collection field. */
075        public static final int SIZE = 7;
076        
077        /**
078         * The type of the PDF collection field.
079         * @since 2.1.2 (was called <code>type</code> previously)
080         */
081        protected int fieldType;
082
083        /**
084         * Creates a PdfCollectionField.
085         * @param name          the field name
086         * @param type          the field type
087         */
088        public PdfCollectionField(String name, int type) {
089                super(PdfName.COLLECTIONFIELD);
090                put(PdfName.N, new PdfString(name, PdfObject.TEXT_UNICODE));
091                this.fieldType = type;
092                switch(type) {
093                default:
094                        put(PdfName.SUBTYPE, PdfName.S);
095                        break;
096                case DATE:
097                        put(PdfName.SUBTYPE, PdfName.D);
098                        break;
099                case NUMBER:
100                        put(PdfName.SUBTYPE, PdfName.N);
101                        break;
102                case FILENAME:
103                        put(PdfName.SUBTYPE, PdfName.F);
104                        break;
105                case DESC:
106                        put(PdfName.SUBTYPE, PdfName.DESC);
107                        break;
108                case MODDATE:
109                        put(PdfName.SUBTYPE, PdfName.MODDATE);
110                        break;
111                case CREATIONDATE:
112                        put(PdfName.SUBTYPE, PdfName.CREATIONDATE);
113                        break;
114                case SIZE:
115                        put(PdfName.SUBTYPE, PdfName.SIZE);
116                        break;
117                }
118        }
119        
120        /**
121         * The relative order of the field name. Fields are sorted in ascending order.
122         * @param i     a number indicating the order of the field
123         */
124        public void setOrder(int i) {
125                put(PdfName.O, new PdfNumber(i));
126        }
127        
128        /**
129         * Sets the initial visibility of the field.
130         * @param visible       the default is true (visible)
131         */
132        public void setVisible(boolean visible) {
133                put(PdfName.V, new PdfBoolean(visible));
134        }
135        
136        /**
137         * Indication if the field value should be editable in the viewer.
138         * @param editable      the default is false (not editable)
139         */
140        public void setEditable(boolean editable) {
141                put(PdfName.E, new PdfBoolean(editable));
142        }
143
144        /**
145         * Checks if the type of the field is suitable for a Collection Item.
146         */
147        public boolean isCollectionItem() {
148                switch(fieldType) {
149                case TEXT:
150                case DATE:
151                case NUMBER:
152                        return true;
153                default:
154                        return false;
155                }
156        }
157        
158        /**
159         * Returns a PdfObject that can be used as the value of a Collection Item.
160         * @param v     value   the value that has to be changed into a PdfObject (PdfString, PdfDate or PdfNumber)     
161         */
162        public PdfObject getValue(String v) {
163                switch(fieldType) {
164                case TEXT:
165                        return new PdfString(v, PdfObject.TEXT_UNICODE);
166                case DATE:
167                        return new PdfDate(PdfDate.decode(v));
168                case NUMBER:
169                        return new PdfNumber(v);
170                }
171                throw new IllegalArgumentException(MessageLocalization.getComposedMessage("1.is.not.an.acceptable.value.for.the.field.2", v, get(PdfName.N).toString()));
172        }
173}