pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1652274 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java
Date Thu, 15 Jan 2015 21:29:35 GMT
Author: msahyoun
Date: Thu Jan 15 21:29:34 2015
New Revision: 1652274

URL: http://svn.apache.org/r1652274
Log:
PDFBOX-2516 setting 'V' key sets/resets 'I' key

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java?rev=1652274&r1=1652273&r2=1652274&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
Thu Jan 15 21:29:34 2015
@@ -459,15 +459,18 @@ public abstract class PDChoice extends P
      */    
     public void setValue(List<String> values)
     {
-        if (values != null)
+        if (values != null && !values.isEmpty())
         {
             if (!isMultiSelect())
             {
-                throw new IllegalArgumentException("The list box does not allow multiple
selection.");
+                throw new IllegalArgumentException("The list box does not allow multiple
selections.");
+            }
+            if (!getOptions().containsAll(values))
+            {
+                throw new IllegalArgumentException("The values are not contained in the selectable
options.");
             }
-            // TODO set the 'I' key
-            // TODO check if the values are contained in the options
             getDictionary().setItem(COSName.V, COSArrayList.convertStringListToCOSStringCOSArray(values));
+            updateSelectedOptionsIndex(values);
         }
         else
         {
@@ -497,4 +500,22 @@ public abstract class PDChoice extends P
         }
         return Collections.<String>emptyList();
     }
+    
+    /**
+     * Update the 'I' key based on values set.
+     */
+    private void updateSelectedOptionsIndex(List<String> values)
+    {
+        List<String> options = getOptions();
+        List<Integer> indices = new ArrayList<Integer>();
+
+        for (String value : values)
+        {
+            indices.add(options.indexOf(value));
+        }
+        
+        // Indices have to be "... array of integers, sorted in ascending order ..."
+        Collections.sort(indices);
+        setSelectedOptionsIndex(indices);
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java?rev=1652274&r1=1652273&r2=1652274&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestListBox.java
Thu Jan 15 21:29:34 2015
@@ -90,10 +90,12 @@ public class TestListBox extends TestCas
             List<String> exportValues = new ArrayList<String>();
             exportValues.add("export01");
             exportValues.add("export02");
+            exportValues.add("export03");
 
             List<String> displayValues = new ArrayList<String>();
             displayValues.add("display02");
             displayValues.add("display01");
+            displayValues.add("display03");
             
             // test with exportValue being set
 
@@ -104,15 +106,53 @@ public class TestListBox extends TestCas
 
             COSArray optItem = (COSArray) choice.getDictionary().getItem(COSName.OPT);
             
-            // assert that the values have been correctly set
+            // assert that the option values have been correctly set
             assertNotNull(choice.getDictionary().getItem(COSName.OPT));
-            assertEquals(optItem.size(),2);
+            assertEquals(optItem.size(),exportValues.size());
             assertEquals(exportValues.get(0), optItem.getString(0));
             
-            // assert that the values can be retrieved correctly
+            // assert that the option values can be retrieved correctly
             List<String> retrievedOptions = choice.getOptions();
-            assertEquals(retrievedOptions.size(),2);
+            assertEquals(retrievedOptions.size(),exportValues.size());
             assertEquals(retrievedOptions, exportValues);
+            
+            // assert that the field value can be set
+            choice.setValue("export01");
+            
+            // ensure that the choice field doesn't allow multiple selections
+            choice.setMultiSelect(false);
+            
+            // wo multiselect setting multiple items shall fail
+            try
+            {
+                choice.setValue(exportValues);
+                fail( "Missing IllegalArgumentException" );
+            }
+            catch( IllegalArgumentException e )
+            {
+                assertEquals("The list box does not allow multiple selections.",e.getMessage()
);
+            }            
+            
+            // ensure that the choice field does allow multiple selections
+            choice.setMultiSelect(true);
+            // now this call must suceed
+            choice.setValue(exportValues);
+            
+            // assert that the option values have been correctly set
+            COSArray valueItems = (COSArray) choice.getDictionary().getItem(COSName.V);
+            assertNotNull(valueItems);
+            assertEquals(valueItems.size(),exportValues.size());
+            assertEquals(exportValues.get(0), valueItems.getString(0));
+            
+            // assert that the index values have been correctly set
+            COSArray indexItems = (COSArray) choice.getDictionary().getItem(COSName.I);
+            assertNotNull(indexItems);
+            assertEquals(indexItems.size(),exportValues.size());
+            
+            // setting a single value shall remove the indices
+            choice.setValue("export01");
+            indexItems = (COSArray) choice.getDictionary().getItem(COSName.I);
+            assertNull(indexItems);
 
             // assert that the Opt entry is removed
             choice.setOptions(null);



Mime
View raw message