KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.pdfbox.cos.COSBase;
34 import org.pdfbox.cos.COSDictionary;
35 import org.pdfbox.cos.COSName;
36
37 import java.io.IOException JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * A class for handling the PDF field as a checkbox.
43  *
44  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
45  * @author sug
46  * @version $Revision: 1.11 $
47  */

48 public class PDCheckbox extends PDChoiceButton
49 {
50     private static final COSName KEY = COSName.getPDFName("AS");
51     private static final COSName OFF_VALUE = COSName.getPDFName("Off");
52
53     private COSName value;
54
55     /**
56      * @see PDField#PDField(PDAcroForm,COSDictionary)
57      *
58      * @param theAcroForm The acroForm for this field.
59      * @param field The checkbox field dictionary
60      */

61     public PDCheckbox( PDAcroForm theAcroForm, COSDictionary field)
62     {
63         super( theAcroForm, field);
64         COSDictionary ap = (COSDictionary) field.getDictionaryObject(COSName.getPDFName("AP"));
65         if( ap != null )
66         {
67             COSBase n = ap.getDictionaryObject(COSName.getPDFName("N"));
68     
69             if( n instanceof COSDictionary )
70             {
71                 List JavaDoc li = ((COSDictionary)n).keyList();
72                 for( int i=0; i<li.size(); i++ )
73                 {
74                     COSName name = (COSName)li.get( i );
75                     if( !name.equals( OFF_VALUE ))
76                     {
77                         value = name;
78                     }
79                 }
80                 
81             }
82         }
83         else
84         {
85             value = (COSName)getDictionary().getDictionaryObject( "V" );
86         }
87     }
88     
89     /**
90      * This will tell if this radio button is currently checked or not.
91      *
92      * @return true If the radio button is checked.
93      */

94     public boolean isChecked()
95     {
96         boolean retval = false;
97         String JavaDoc onValue = getOnValue();
98         COSName radioValue = (COSName)getDictionary().getDictionaryObject( KEY );
99         if( radioValue != null && value != null && radioValue.getName().equals( onValue ) )
100         {
101             retval = true;
102         }
103         
104         return retval;
105     }
106
107     /**
108      * Checks the radiobutton.
109      */

110     public void check()
111     {
112         getDictionary().setItem(KEY, value);
113     }
114
115     /**
116      * Unchecks the radiobutton.
117      */

118     public void unCheck()
119     {
120         getDictionary().setItem(KEY, OFF_VALUE);
121     }
122
123     /**
124      * {@inheritDoc}
125      */

126     public void setValue(String JavaDoc newValue)
127     {
128         getDictionary().setName( "V", newValue );
129         if( newValue == null )
130         {
131             getDictionary().setItem( KEY, OFF_VALUE );
132         }
133         else
134         {
135             getDictionary().setName( KEY, newValue );
136         }
137     }
138     
139     /**
140      * This will get the value of the radio button.
141      *
142      * @return The value of the radio button.
143      */

144     public String JavaDoc getOffValue()
145     {
146         return OFF_VALUE.getName();
147     }
148     
149     /**
150      * This will get the value of the radio button.
151      *
152      * @return The value of the radio button.
153      */

154     public String JavaDoc getOnValue()
155     {
156         String JavaDoc retval = null;
157         COSDictionary ap = (COSDictionary) getDictionary().getDictionaryObject(COSName.getPDFName("AP"));
158         COSBase n = ap.getDictionaryObject(COSName.getPDFName("N"));
159
160         //N can be a COSDictionary or a COSStream
161
if( n instanceof COSDictionary )
162         {
163             Iterator JavaDoc li = ((COSDictionary)n).keyList().iterator();
164             while( li.hasNext() )
165             {
166                 Object JavaDoc key = li.next();
167                 if( !key.equals( OFF_VALUE) )
168                 {
169                     retval = ((COSName)key).getName();
170                 }
171             }
172         }
173         return retval;
174     }
175
176     /**
177      * getValue gets the fields value to as a string.
178      *
179      * @return The string value of this field.
180      *
181      * @throws IOException If there is an error getting the value.
182      */

183     public String JavaDoc getValue() throws IOException JavaDoc
184     {
185         return getDictionary().getNameAsString( "V" );
186     }
187
188 }
Popular Tags