001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.interactive.form;
031:
032: import org.pdfbox.cos.COSBase;
033: import org.pdfbox.cos.COSDictionary;
034: import org.pdfbox.cos.COSName;
035:
036: import java.io.IOException;
037: import java.util.Iterator;
038: import java.util.List;
039:
040: /**
041: * A class for handling the PDF field as a checkbox.
042: *
043: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
044: * @author sug
045: * @version $Revision: 1.11 $
046: */
047: public class PDCheckbox extends PDChoiceButton {
048: private static final COSName KEY = COSName.getPDFName("AS");
049: private static final COSName OFF_VALUE = COSName.getPDFName("Off");
050:
051: private COSName value;
052:
053: /**
054: * @see PDField#PDField(PDAcroForm,COSDictionary)
055: *
056: * @param theAcroForm The acroForm for this field.
057: * @param field The checkbox field dictionary
058: */
059: public PDCheckbox(PDAcroForm theAcroForm, COSDictionary field) {
060: super (theAcroForm, field);
061: COSDictionary ap = (COSDictionary) field
062: .getDictionaryObject(COSName.getPDFName("AP"));
063: if (ap != null) {
064: COSBase n = ap.getDictionaryObject(COSName.getPDFName("N"));
065:
066: if (n instanceof COSDictionary) {
067: List li = ((COSDictionary) n).keyList();
068: for (int i = 0; i < li.size(); i++) {
069: COSName name = (COSName) li.get(i);
070: if (!name.equals(OFF_VALUE)) {
071: value = name;
072: }
073: }
074:
075: }
076: } else {
077: value = (COSName) getDictionary().getDictionaryObject("V");
078: }
079: }
080:
081: /**
082: * This will tell if this radio button is currently checked or not.
083: *
084: * @return true If the radio button is checked.
085: */
086: public boolean isChecked() {
087: boolean retval = false;
088: String onValue = getOnValue();
089: COSName radioValue = (COSName) getDictionary()
090: .getDictionaryObject(KEY);
091: if (radioValue != null && value != null
092: && radioValue.getName().equals(onValue)) {
093: retval = true;
094: }
095:
096: return retval;
097: }
098:
099: /**
100: * Checks the radiobutton.
101: */
102: public void check() {
103: getDictionary().setItem(KEY, value);
104: }
105:
106: /**
107: * Unchecks the radiobutton.
108: */
109: public void unCheck() {
110: getDictionary().setItem(KEY, OFF_VALUE);
111: }
112:
113: /**
114: * {@inheritDoc}
115: */
116: public void setValue(String newValue) {
117: getDictionary().setName("V", newValue);
118: if (newValue == null) {
119: getDictionary().setItem(KEY, OFF_VALUE);
120: } else {
121: getDictionary().setName(KEY, newValue);
122: }
123: }
124:
125: /**
126: * This will get the value of the radio button.
127: *
128: * @return The value of the radio button.
129: */
130: public String getOffValue() {
131: return OFF_VALUE.getName();
132: }
133:
134: /**
135: * This will get the value of the radio button.
136: *
137: * @return The value of the radio button.
138: */
139: public String getOnValue() {
140: String retval = null;
141: COSDictionary ap = (COSDictionary) getDictionary()
142: .getDictionaryObject(COSName.getPDFName("AP"));
143: COSBase n = ap.getDictionaryObject(COSName.getPDFName("N"));
144:
145: //N can be a COSDictionary or a COSStream
146: if (n instanceof COSDictionary) {
147: Iterator li = ((COSDictionary) n).keyList().iterator();
148: while (li.hasNext()) {
149: Object key = li.next();
150: if (!key.equals(OFF_VALUE)) {
151: retval = ((COSName) key).getName();
152: }
153: }
154: }
155: return retval;
156: }
157:
158: /**
159: * getValue gets the fields value to as a string.
160: *
161: * @return The string value of this field.
162: *
163: * @throws IOException If there is an error getting the value.
164: */
165: public String getValue() throws IOException {
166: return getDictionary().getNameAsString("V");
167: }
168:
169: }
|