pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1688677 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
Date Wed, 01 Jul 2015 16:07:01 GMT
Author: msahyoun
Date: Wed Jul  1 16:07:00 2015
New Revision: 1688677

URL: http://svn.apache.org/r1688677
Log:
PDFBOX-2849: change get/setValue oriented methods to allow for arbitary On values

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java

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=1688677&r1=1688676&r2=1688677&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
Wed Jul  1 16:07:00 2015
@@ -67,7 +67,7 @@ public final class PDCheckbox extends PD
      */
     public boolean isChecked()
     {
-        return getValue();
+        return getValue().compareTo(getOnValue()) == 0;
     }
 
     /**
@@ -75,7 +75,7 @@ public final class PDCheckbox extends PD
      */
     public void check() throws IOException
     {
-        setValue(true);
+        setValue(getOnValue());
     }
 
     /**
@@ -83,54 +83,58 @@ public final class PDCheckbox extends PD
      */
     public void unCheck() throws IOException
     {
-        setValue(false);
+        setValue(COSName.Off.getName());
     }
 
     /**
-     * Returns true if this field is checked.
+     * Returns the fields value entry.
      * 
-     * @return True if checked
+     * @return the fields value entry.
      */
-    public boolean getValue()
+    public String getValue()
     {
-        COSBase value = getInheritableAttribute(COSName.V);
-        return value instanceof COSName && value.equals(COSName.YES);
+        COSName value = (COSName) getInheritableAttribute(COSName.V);
+        return value == null ? "" : value.getName();
     }
 
     /**
      * Returns the default value, if any.
      *
-     * @return True if checked, false if not checked, null if missing.
+     * @return the fields default value.
      */
-    public Boolean getDefaultValue()
+    public String getDefaultValue()
     {
-        COSBase value = getInheritableAttribute(COSName.DV);
-        if (value == null)
-        {
-            return null;
-        }
-        return value instanceof COSName && value.equals(COSName.YES);
+        COSName value = (COSName) getInheritableAttribute(COSName.DV);
+        return value == null ? "" : value.getName();
     }
 
     @Override
     public String getValueAsString()
     {
-        return getValue() ? "Yes" : "Off";
+        return getValue();
     }
 
     /**
      * Sets the checked value of this field.
      *
-     * @param value True if checked
+     * @param value matching the On or Off state of the checkbox.
      * @throws IOException if the value could not be set
      */
-    public void setValue(boolean value) throws IOException
+    public void setValue(String value) throws IOException
     {
-        COSName name = value ? COSName.YES : COSName.Off;
-        dictionary.setItem(COSName.V, name);
         
-        // update the appearance state (AS)
-        dictionary.setItem(COSName.AS, name);
+        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);
+            dictionary.setItem(COSName.V, name);
+            dictionary.setItem(COSName.AS, name);
+        }
         
         applyChange();
     }
@@ -139,12 +143,17 @@ public final class PDCheckbox extends PD
      * Sets the default value.
      *
      * @param value True if checked
-     * @throws IOException if the value could not be set
      */
-    public void setDefaultValue(boolean value) throws IOException
+    public void setDefaultValue(String value)
     {
-        COSName name = value ? COSName.YES : COSName.Off;
-        dictionary.setItem(COSName.DV, name);
+        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
+        {
+            dictionary.setItem(COSName.DV, COSName.getPDFName(value));
+        }
     }
 
     /**

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java?rev=1688677&r1=1688676&r2=1688677&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
Wed Jul  1 16:07:00 2015
@@ -89,7 +89,7 @@ public class PDButtonTest
         assertTrue(buttonField.isRadioButton());
         assertFalse(buttonField.isPushButton());
     }
-    
+        
     @Test
     public void retrieveAcrobatCheckBoxProperties() throws IOException
     {
@@ -99,6 +99,34 @@ public class PDButtonTest
         assertEquals(checkbox.getOnValues().get(0), "Yes");
     }
     
+    @Test
+    public void testAcrobatCheckBoxProperties() throws IOException
+    {
+        PDCheckbox checkbox = (PDCheckbox) acrobatAcroForm.getField("Checkbox");
+        assertEquals(checkbox.getValue(), "");
+        assertEquals(checkbox.isChecked(), false);
+
+        checkbox.check();
+        assertEquals(checkbox.getValue(), checkbox.getOnValue());
+        assertEquals(checkbox.isChecked(), true);
+
+        checkbox.setValue("Yes");
+        assertEquals(checkbox.getValue(), checkbox.getOnValue());
+        assertEquals(checkbox.isChecked(), true);
+        assertEquals(checkbox.getCOSObject().getDictionaryObject(COSName.AS), COSName.YES);
+
+        checkbox.setValue("Off");
+        assertEquals(checkbox.getValue(), COSName.Off.getName());
+        assertEquals(checkbox.isChecked(), false);
+        assertEquals(checkbox.getCOSObject().getDictionaryObject(COSName.AS), COSName.Off);
+
+        checkbox = (PDCheckbox) acrobatAcroForm.getField("Checkbox-DefaultValue");
+        assertEquals(checkbox.getDefaultValue(), checkbox.getOnValue());
+        
+        checkbox.setDefaultValue("Off");
+        assertEquals(checkbox.getDefaultValue(), COSName.Off.getName());
+    
+    }
     
     @After
     public void tearDown() throws IOException



Mime
View raw message