Need to convert array values if they are convertible fx
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 14 Apr 2013 08:24:05 +0200
branchfx
changeset 9766629bfa8f973
parent 975 094cd25a16d9
child 977 f5070beb03cf
Need to convert array values if they are convertible
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java	Sat Apr 13 09:14:54 2013 +0200
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java	Sun Apr 14 08:24:05 2013 +0200
     1.3 @@ -17,9 +17,13 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.htmlpage;
     1.6  
     1.7 +import java.lang.reflect.InvocationTargetException;
     1.8 +import java.lang.reflect.Method;
     1.9  import java.util.ArrayList;
    1.10  import java.util.Collection;
    1.11  import java.util.Iterator;
    1.12 +import java.util.logging.Level;
    1.13 +import java.util.logging.Logger;
    1.14  import org.apidesign.bck2brwsr.core.JavaScriptOnly;
    1.15  
    1.16  /**
    1.17 @@ -144,8 +148,32 @@
    1.18      private static native int koArray();
    1.19      
    1.20      public Object koData() {
    1.21 -        return toArray();
    1.22 +        Object[] arr = toArray();
    1.23 +        Method toKO = null;
    1.24 +        for (int i = 0; i < arr.length; i++) {
    1.25 +            if (arr[i] == null) {
    1.26 +                continue;
    1.27 +            }
    1.28 +            if (toKO == null || toKO.getDeclaringClass() != arr[i].getClass()) {
    1.29 +                try {
    1.30 +                    toKO = arr[i].getClass().getDeclaredMethod("koData");
    1.31 +                    toKO.setAccessible(true);
    1.32 +                } catch (NoSuchMethodException ex) {
    1.33 +                    LOG.log(Level.FINE, "No koData conversion for " + arr[i], ex);
    1.34 +                    toKO = null;
    1.35 +                }
    1.36 +            }
    1.37 +            if (toKO != null) {
    1.38 +                try {
    1.39 +                    arr[i] = toKO.invoke(arr[i]);
    1.40 +                } catch (IllegalAccessException | InvocationTargetException ex) {
    1.41 +                    LOG.log(Level.SEVERE, "Problems invoking koData on " + arr[i], ex);
    1.42 +                }
    1.43 +            }
    1.44 +        }
    1.45 +        return arr;
    1.46      }
    1.47 +    private static final Logger LOG = Logger.getLogger(KOList.class.getName());
    1.48  
    1.49      private void notifyChange() {
    1.50          Knockout m = model;