You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2015/09/20 19:37:26 UTC

svn commit: r1704166 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form: PDButton.java PDCheckbox.java PDRadioButton.java

Author: msahyoun
Date: Sun Sep 20 17:37:24 2015
New Revision: 1704166

URL: http://svn.apache.org/viewvc?rev=1704166&view=rev
Log:
PDFBOX-2978: add support for grouped checkboxes

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java?rev=1704166&r1=1704165&r2=1704166&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java Sun Sep 20 17:37:24 2015
@@ -26,9 +26,13 @@ import org.apache.pdfbox.pdmodel.common.
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry;
 
 /**
  * A button field represents an interactive control on the screen
@@ -137,6 +141,34 @@ public abstract class PDButton extends P
     }
 
     /**
+     * Sets the selected option given its name.
+     * 
+     * @param value Name of option to select
+     * @throws IOException if the value could not be set
+     * @throws IllegalArgumentException if the value is not a valid option.
+     */
+    public void setValue(String value) throws IOException
+    {
+        checkValue(value);        
+        getCOSObject().setName(COSName.V, value);
+        // update the appearance state (AS)
+        for (PDAnnotationWidget widget : getWidgets())
+        {
+            PDAppearanceEntry appearanceEntry = widget.getAppearance().getNormalAppearance();
+            if (((COSDictionary) appearanceEntry.getCOSObject()).containsKey(value))
+            {
+                widget.getCOSObject().setName(COSName.AS, value);
+            }
+            else
+            {
+                widget.getCOSObject().setItem(COSName.AS, COSName.Off);
+            }
+        }
+        applyChange();
+    }
+    
+    
+    /**
      * Returns the default value, if any.
      *
      * @return A non-null string.
@@ -154,6 +186,18 @@ public abstract class PDButton extends P
         }
     }
 
+    /**
+     * Sets the default value.
+     *
+     * @param value Name of option to select
+     * @throws IllegalArgumentException if the value is not a valid option.
+     */
+    public void setDefaultValue(String value)
+    {
+        checkValue(value);        
+        getCOSObject().setName(COSName.DV, value);
+    }    
+    
     @Override
     public String getValueAsString()
     {
@@ -214,7 +258,7 @@ public abstract class PDButton extends P
             getCOSObject().removeItem(COSName.OPT);
         }
     }
-    
+
     @Override
     void constructAppearances() throws IOException
     {
@@ -229,5 +273,58 @@ public abstract class PDButton extends P
         }
     }  
 
+    /**
+     * Get the values to set individual buttons within a group to the on state.
+     * 
+     * <p>The On value could be an arbitrary string as long as it is within the limitations of
+     * a PDF name object. The Off value shall always be 'Off'. If not set or not part of the normal
+     * appearance keys 'Off' is the default</p>
+     *
+     * @return the potential values setting the check box to the On state. 
+     *         If an empty Set is returned there is no appearance definition.
+     */
+    public Set<String> getOnValues()
+    {
+        // we need a set as the field can appear multiple times
+        Set<String> onValues = new HashSet<String>();
+        
+        List<PDAnnotationWidget> widgets = this.getWidgets();
+        for (PDAnnotationWidget widget : widgets)
+        {
+            PDAppearanceDictionary apDictionary = widget.getAppearance();
+            if (apDictionary != null) 
+            {
+                PDAppearanceEntry normalAppearance = apDictionary.getNormalAppearance();
+                if (normalAppearance != null)
+                {
+                    Set<COSName> entries = normalAppearance.getSubDictionary().keySet();
+                    for (COSName entry : entries)
+                    {
+                        if (COSName.Off.compareTo(entry) != 0)
+                        {
+                            onValues.add(entry.getName());
+                        }
+                    }
+                }
+            }
+        }        
+        return onValues;
+    }
     
+    /**
+     * Checks value.
+     *
+     * @param value Name of radio button to select
+     * @throws IllegalArgumentException if the value is not a valid option.
+     */
+    void checkValue(String value) throws IllegalArgumentException
+    {
+        Set<String> onValues = getOnValues();
+        if (COSName.Off.getName().compareTo(value) != 0 && !onValues.contains(value))
+        {
+            throw new IllegalArgumentException("value '" + value
+                    + "' is not a valid option for the field " + getFullyQualifiedName()
+                    + ", valid values are: " + onValues + " and " + COSName.Off.getName());
+        }
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java?rev=1704166&r1=1704165&r2=1704166&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java Sun Sep 20 17:37:24 2015
@@ -17,8 +17,6 @@
 package org.apache.pdfbox.pdmodel.interactive.form;
 
 import java.io.IOException;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.pdfbox.cos.COSDictionary;
@@ -89,54 +87,6 @@ public final class PDCheckbox extends PD
     }
 
     /**
-     * Sets the checked value of this field.
-     * 
-     * <p>To retrieve the potential On value use {@link #getOnValue()} or
-     * {@link #getOnValues()}. The Off value shall always be 'Off'.</p>
-     *
-     * @param value matching the On or Off state of the checkbox.
-     * @throws IOException if the appearance couldn't be generated.
-     * @throws IllegalArgumentException if the value is not a valid option for the checkbox.
-     */
-    public void setValue(String value) throws IOException
-    {
-        if (value.compareTo(getOnValue()) != 0 && value.compareTo(COSName.Off.getName()) != 0)
-        {
-            throw new IllegalArgumentException(value + " is not a valid option for the checkbox " + getFullyQualifiedName());
-        }
-        else
-        {
-            // Update the field value and the appearance state.
-            // Both are necessary to work properly with different viewers.
-            COSName name = COSName.getPDFName(value);
-            getCOSObject().setItem(COSName.V, name);
-            for (PDAnnotationWidget widget : getWidgets())
-            {
-                widget.setAppearanceState(value);
-            }
-        }
-        applyChange();
-    }
-
-    /**
-     * Sets the default value.
-     *
-     * @see #setValue(String)
-     * @param value matching the On or Off state of the checkbox.
-     */
-    public void setDefaultValue(String value)
-    {
-        if (value.compareTo(getOnValue()) != 0 && value.compareTo(COSName.Off.getName()) != 0)
-        {
-            throw new IllegalArgumentException(value + " is not a valid option for the checkbox " + getFullyQualifiedName());
-        }
-        else
-        {
-            getCOSObject().setName(COSName.DV, value);
-        }
-    }
-
-    /**
      * Get the value which sets the check box to the On state.
      * 
      * <p>The On value should be 'Yes' but other values are possible
@@ -170,31 +120,4 @@ public final class PDCheckbox extends PD
         }
         return onValue;
     }
-    
-    /**
-     * Get the values which sets the check box to the On state.
-     * 
-     * <p>This is a convenience function to provide a similar method to 
-     * {@link PDRadioButton} </p>
-     *
-     * @see #getOnValue()
-     * @return the value setting the check box to the On state. 
-     *          If an empty List is returned there is no appearance definition.
-     */
-    public Set<String> getOnValues()
-    {
-        String onValue = getOnValue();
-        
-        if (onValue.isEmpty())
-        {
-            return Collections.emptySet();
-        }
-        else
-        {
-            Set<String> onValues = new HashSet<String>();
-            onValues.add(onValue);
-            return onValues;
-        }
-    }
-    
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java?rev=1704166&r1=1704165&r2=1704166&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java Sun Sep 20 17:37:24 2015
@@ -18,15 +18,11 @@ package org.apache.pdfbox.pdmodel.intera
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry;
 
 /**
  * Radio button fields contain a set of related buttons that can each be on or off.
@@ -102,7 +98,7 @@ public final class PDRadioButton extends
      */
     public List<String> getSelectedExportValues() throws IOException
     {
-        List<String> onValues = getSelectableOnValues();
+        Set<String> onValues = getOnValues();
         List<String> exportValues = getExportValues();
         List<String> selectedExportValues = new ArrayList<String>();
         if (exportValues.isEmpty())
@@ -124,112 +120,4 @@ public final class PDRadioButton extends
             return selectedExportValues;
         }
     }
-
-    /**
-     * Sets the selected radio button, given its name.
-     * 
-     * @param value Name of radio button to select
-     * @throws IOException if the value could not be set
-     * @throws IllegalArgumentException if the value is not a valid option.
-     */
-    public void setValue(String value) throws IOException
-    {
-        checkValue(value);        
-        getCOSObject().setName(COSName.V, value);
-        // update the appearance state (AS)
-        for (PDAnnotationWidget widget : getWidgets())
-        {
-            PDAppearanceEntry appearanceEntry = widget.getAppearance().getNormalAppearance();
-            if (((COSDictionary) appearanceEntry.getCOSObject()).containsKey(value))
-            {
-                widget.getCOSObject().setName(COSName.AS, value);
-            }
-            else
-            {
-                widget.getCOSObject().setItem(COSName.AS, COSName.Off);
-            }
-        }
-        applyChange();
-    }
-    
-    /**
-     * Sets the default value.
-     *
-     * @param value Name of radio button to select
-     * @throws IllegalArgumentException if the value is not a valid option.
-     */
-    public void setDefaultValue(String value)
-    {
-        checkValue(value);        
-        getCOSObject().setName(COSName.DV, value);
-    }    
-
-    /**
-     * Get the values to set individual radio buttons to the on state.
-     * 
-     * <p>The On value could be an arbitrary string as long as it is within the limitations of
-     * a PDF name object. The Off value shall always be 'Off'. If not set or not part of the normal
-     * appearance keys 'Off' is the default</p>
-     *
-     * @return the value setting the check box to the On state. 
-     *          If an empty string is returned there is no appearance definition.
-     */
-    public Set<String> getOnValues()
-    {
-        // we need a set as the radio buttons can appear multiple times
-        Set<String> onValues = new HashSet<String>();
-        onValues.addAll(getSelectableOnValues());
-        return onValues;
-    }
-    
-    /**
-     * Checks value.
-     *
-     * @param value Name of radio button to select
-     * @throws IllegalArgumentException if the value is not a valid option.
-     */
-    private void checkValue(String value) throws IllegalArgumentException
-    {
-        Set<String> onValues = getOnValues();
-        if (COSName.Off.getName().compareTo(value) != 0 && !onValues.contains(value))
-        {
-            throw new IllegalArgumentException("value '" + value
-                    + "' is not a valid option for the radio button " + getFullyQualifiedName()
-                    + ", valid values are: " + onValues + " and " + COSName.Off.getName());
-        }
-    }
-    
-    /**
-     * Get all potential ON values.
-     * 
-     * @return the ON values.
-     */
-    private List<String> getSelectableOnValues()
-    {
-        List<PDAnnotationWidget> widgets = this.getWidgets();
-        // we need a set as the radio buttons can appear multiple times
-        List<String> onValues = new ArrayList<String>();
-        
-        for (PDAnnotationWidget widget : widgets)
-        {
-            PDAppearanceDictionary apDictionary = widget.getAppearance();
-            
-            if (apDictionary != null) 
-            {
-                PDAppearanceEntry normalAppearance = apDictionary.getNormalAppearance();
-                if (normalAppearance != null)
-                {
-                    Set<COSName> entries = normalAppearance.getSubDictionary().keySet();
-                    for (COSName entry : entries)
-                    {
-                        if (COSName.Off.compareTo(entry) != 0)
-                        {
-                            onValues.add(entry.getName());
-                        }
-                    }
-                }
-            }
-        }        
-        return onValues;
-    }    
 }