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 2016/06/24 18:14:59 UTC

svn commit: r1750155 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java

Author: msahyoun
Date: Fri Jun 24 18:14:59 2016
New Revision: 1750155

URL: http://svn.apache.org/viewvc?rev=1750155&view=rev
Log:
improve setting the fields value if an /Opts entry is defined

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java?rev=1750155&r1=1750154&r2=1750155&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java Fri Jun 24 18:14:59 2016
@@ -150,21 +150,20 @@ public abstract class PDButton extends P
     @Override
     public void setValue(String value) throws IOException
     {
-        checkValue(value);        
-        getCOSObject().setName(COSName.V, value);
-        // update the appearance state (AS)
-        for (PDAnnotationWidget widget : getWidgets())
+        checkValue(value);
+        
+        // if there are export values/an Opt entry there is a different 
+        // approach to setting the value
+        boolean hasExportValues = getExportValues().size() > 0;
+
+        if (hasExportValues) {
+            updateByOption(value);
+        }
+        else
         {
-            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);
-            }
+            updateByValue(value);
         }
+        
         applyChange();
     }
     
@@ -227,6 +226,7 @@ public abstract class PDButton extends P
     public List<String> getExportValues()
     {
         COSBase value = getInheritableAttribute(COSName.OPT);
+        
         if (value instanceof COSString)
         {
             List<String> array = new ArrayList<String>();
@@ -263,28 +263,28 @@ public abstract class PDButton extends P
     @Override
     void constructAppearances() throws IOException
     {
-        for (PDAnnotationWidget widget : getWidgets())
+        List<String> exportValues = getExportValues();
+        if (exportValues.size() > 0)
         {
-            PDAppearanceDictionary appearance = widget.getAppearance();
-            if (appearance == null || appearance.getNormalAppearance() == null)
+            // the value is the index value of the option. So we need to get that
+            // and use it to set the value
+            try
             {
-                // TODO: implement appearance generation for radio buttons
-                throw new UnsupportedOperationException("Appearance generation is not implemented yet, see PDFBOX-2849");
-            }
-            else 
-            {
-                PDAppearanceEntry appearanceEntry = widget.getAppearance().getNormalAppearance();
-                String value = getValue();
-                if (((COSDictionary) appearanceEntry.getCOSObject()).containsKey(value))
-                {
-                    widget.getCOSObject().setName(COSName.AS, value);
-                }
-                else
+                int optionsIndex = Integer.parseInt(getValue());
+                if (optionsIndex < exportValues.size())
                 {
-                    widget.getCOSObject().setItem(COSName.AS, COSName.Off);
+                    updateByOption(exportValues.get(optionsIndex));
                 }
+            } catch (NumberFormatException e)
+            {
+                // silently ignore that
+                // and don't update the appearance
             }
         }
+        else
+        {
+            updateByValue(getValue());
+        }
     }  
 
     /**
@@ -302,6 +302,12 @@ public abstract class PDButton extends P
         // we need a set as the field can appear multiple times
         Set<String> onValues = new HashSet<String>();
         
+        if (getExportValues().size() > 0)
+        {
+            onValues.addAll(getExportValues());
+            return onValues;
+        }
+        
         List<PDAnnotationWidget> widgets = this.getWidgets();
         for (PDAnnotationWidget widget : widgets)
         {
@@ -341,4 +347,53 @@ public abstract class PDButton extends P
                     + ", valid values are: " + onValues + " and " + COSName.Off.getName());
         }
     }
+
+    private void updateByValue(String value) throws IOException
+    {
+        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);
+            }
+        }
+    }
+
+    private void updateByOption(String value) throws IOException
+    {
+        List<PDAnnotationWidget> widgets = getWidgets();
+        List<String> options = getExportValues();
+        
+        if (widgets.size() != options.size())
+        {
+            throw new IllegalArgumentException("The number of options doesn't match the number of widgets");
+        }
+        
+        // the value is the index of the matching option
+        int optionsIndex = options.indexOf(value);
+        getCOSObject().setName(COSName.V, String.valueOf(optionsIndex));
+
+        
+        // update the appearance state (AS)
+        for (int i = 0; i < widgets.size(); i++)
+        {
+            PDAnnotationWidget widget = widgets.get(i);
+            if (value.compareTo(options.get(i)) == 0)
+            {
+                widget.getCOSObject().setName(COSName.AS, String.valueOf(i));
+            }
+            else 
+            {
+                widget.getCOSObject().setItem(COSName.AS, COSName.Off);
+            }
+        }
+    }
+    
 }