KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > interactive > form > PDChoiceField


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.interactive.form;
32
33 import java.io.IOException JavaDoc;
34
35 import org.pdfbox.cos.COSArray;
36 import org.pdfbox.cos.COSBase;
37 import org.pdfbox.cos.COSDictionary;
38 import org.pdfbox.cos.COSName;
39 import org.pdfbox.cos.COSString;
40 import org.pdfbox.cos.COSInteger;
41
42 /**
43  * A class for handling the PDF field as a choicefield.
44  *
45  * @author sug
46  * @version $Revision: 1.7 $
47  */

48 public class PDChoiceField extends PDVariableText
49 {
50
51     /**
52      * @see org.pdfbox.pdmodel.interactive.form.PDField#PDField(PDAcroForm,COSDictionary)
53      *
54      * @param theAcroForm The acroForm for this field.
55      * @param field The field for this choice field.
56      */

57     public PDChoiceField( PDAcroForm theAcroForm, COSDictionary field)
58     {
59         super(theAcroForm, field);
60     }
61     
62     /**
63      * @see org.pdfbox.pdmodel.interactive.form.PDField#setValue(java.lang.String)
64      *
65      * @param optionValue The new value for this text field.
66      *
67      * @throws IOException If there is an error calculating the appearance stream or the value in not one
68      * of the existing options.
69      */

70     public void setValue(String JavaDoc optionValue) throws IOException JavaDoc
71     {
72         int indexSelected = -1;
73         COSArray options = (COSArray)getDictionary().getDictionaryObject( "Opt" );
74         if( options.size() == 0 )
75         {
76             throw new IOException JavaDoc( "Error: You cannot set a value for a choice field if there are no options." );
77         }
78         else
79         {
80             COSBase option = options.getObject( 0 );
81             if( option instanceof COSArray )
82             {
83                 for( int i=0; i<options.size() && indexSelected == -1; i++ )
84                 {
85                     COSArray keyValuePair = (COSArray)options.get( i );
86                     COSString key = (COSString)keyValuePair.getObject( 0 );
87                     COSString value = (COSString)keyValuePair.getObject( 1 );
88                     if( optionValue.equals( key.getString() ) || optionValue.equals( value.getString() ) )
89                     {
90                         //have the parent draw the appearance stream with the value
91
super.setValue( value.getString() );
92                         //but then use the key as the V entry
93
getDictionary().setItem( COSName.getPDFName( "V" ), key );
94                         indexSelected = i;
95                     }
96                 }
97             }
98             else
99             {
100                 for( int i=0; i<options.size() && indexSelected == -1; i++ )
101                 {
102                     COSString value = (COSString)options.get( i );
103                     if( optionValue.equals( value.getString() ) )
104                     {
105                         super.setValue( optionValue );
106                         indexSelected = i;
107                     }
108                 }
109             }
110         }
111         if( indexSelected == -1 )
112         {
113             throw new IOException JavaDoc( "Error: '" + optionValue + "' was not an available option.");
114         }
115         else
116         {
117             COSArray indexArray = (COSArray)getDictionary().getDictionaryObject( "I" );
118             if( indexArray != null )
119             {
120                 indexArray.clear();
121                 indexArray.add( new COSInteger( indexSelected ) );
122             }
123         }
124     }
125     
126     
127 }
Popular Tags