Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.interactive.form » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.pdmodel.interactive.form;
   18   
   19   import java.io.IOException;
   20   import java.util.ArrayList;
   21   import java.util.List;
   22   
   23   import org.apache.pdfbox.cos.COSArray;
   24   import org.apache.pdfbox.cos.COSDictionary;
   25   import org.apache.pdfbox.cos.COSName;
   26   
   27   import org.apache.pdfbox.pdmodel.common.COSArrayList;
   28   import org.apache.pdfbox.util.BitFlagHelper;
   29   
   30   /**
   31    * A class for handling the PDF field as a Radio Collection.
   32    * This class automatically keeps track of the child radio buttons
   33    * in the collection.
   34    *
   35    * @see PDCheckbox
   36    * @author sug
   37    * @version $Revision: 1.13 $
   38    */
   39   public class PDRadioCollection extends PDChoiceButton
   40   {
   41       /**
   42        * A Ff flag.
   43        */
   44       public static final int FLAG_RADIOS_IN_UNISON = 1 << 25;
   45   
   46       /**
   47        * @param theAcroForm The acroForm for this field.
   48        * @param field The field that makes up the radio collection.
   49        *
   50        * {@inheritDoc}
   51        */
   52       public PDRadioCollection( PDAcroForm theAcroForm, COSDictionary field)
   53       {
   54           super(theAcroForm,field);
   55       }
   56   
   57       /**
   58        * From the PDF Spec <br/>
   59        * If set, a group of radio buttons within a radio button field that
   60        * use the same value for the on state will turn on and off in unison; that is if
   61        * one is checked, they are all checked. If clear, the buttons are mutually exclusive
   62        * (the same behavior as HTML radio buttons).
   63        *
   64        * @param radiosInUnison The new flag for radiosInUnison.
   65        */
   66       public void setRadiosInUnison(boolean radiosInUnison)
   67       {
   68           BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_RADIOS_IN_UNISON, radiosInUnison );
   69       }
   70   
   71       /**
   72        *
   73        * @return true If the flag is set for radios in unison.
   74        */
   75       public boolean isRadiosInUnison()
   76       {
   77           return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_RADIOS_IN_UNISON );
   78       }
   79   
   80       /**
   81        * This setValue method iterates the collection of radiobuttons
   82        * and checks or unchecks each radiobutton according to the
   83        * given value.
   84        * If the value is not represented by any of the radiobuttons,
   85        * then none will be checked.
   86        *
   87        * {@inheritDoc}
   88        */
   89       public void setValue(String value) throws IOException
   90       {
   91           getDictionary().setString( "V", value );
   92           List kids = getKids();
   93           for (int i = 0; i < kids.size(); i++)
   94           {
   95               PDField field = (PDField)kids.get(i);
   96               if ( field instanceof PDCheckbox )
   97               {
   98                   PDCheckbox btn = (PDCheckbox)field;
   99                   if( btn.getOnValue().equals(value) )
  100                   {
  101                       btn.check();
  102                   }
  103                   else
  104                   {
  105                       btn.unCheck();
  106                   }
  107               }
  108           }
  109       }
  110   
  111       /**
  112        * getValue gets the fields value to as a string.
  113        *
  114        * @return The string value of this field.
  115        *
  116        * @throws IOException If there is an error getting the value.
  117        */
  118       public String getValue()throws IOException
  119       {
  120           String retval = null;
  121           List kids = getKids();
  122           for (int i = 0; i < kids.size(); i++)
  123           {
  124               PDField kid = (PDField)kids.get(i);
  125               if ( kid instanceof PDCheckbox )
  126               {
  127                   PDCheckbox btn = (PDCheckbox)kid;
  128                   if( btn.isChecked() )
  129                   {
  130                       retval = btn.getOnValue();
  131                   }
  132               }
  133           }
  134           if( retval == null )
  135           {
  136               retval = getDictionary().getNameAsString( "V" );
  137           }
  138           return retval;
  139       }
  140   
  141   
  142       /**
  143        * This will return a list of PDField objects that are part of this radio collection.
  144        *
  145        * @see PDField#getWidget()
  146        * @return A list of PDWidget objects.
  147        * @throws IOException if there is an error while creating the children objects.
  148        */
  149       @SuppressWarnings("unchecked")
  150       public List getKids() throws IOException
  151       {
  152           List retval = null;
  153           COSArray kids = (COSArray)getDictionary().getDictionaryObject(COSName.KIDS);
  154           if( kids != null )
  155           {
  156               List kidsList = new ArrayList();
  157               for (int i = 0; i < kids.size(); i++)
  158               {
  159                   kidsList.add( PDFieldFactory.createField( getAcroForm(), (COSDictionary)kids.getObject(i) ) );
  160               }
  161               retval = new COSArrayList( kidsList, kids );
  162           }
  163           return retval;
  164       }
  165   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.interactive.form » [javadoc | source]