javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 15 Apr 2013 22:03:59 +0200
branchfx
changeset 993 08e7b312664f
parent 987 5d8eea9d2831
permissions -rw-r--r--
Disable logging - Twttr Client works, so why continue to log?
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    20 import java.lang.reflect.InvocationTargetException;
    21 import java.lang.reflect.Method;
    22 import java.util.ArrayList;
    23 import java.util.Collection;
    24 import java.util.Iterator;
    25 import java.util.logging.Level;
    26 import java.util.logging.Logger;
    27 import org.apidesign.bck2brwsr.core.JavaScriptOnly;
    28 
    29 /**
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 public final class KOList<T> extends ArrayList<T> {
    34     private final String name;
    35     private final String[] deps;
    36     private Knockout model;
    37     private Runnable onchange;
    38 
    39     public KOList(String name, String... deps) {
    40         this.name = name;
    41         this.deps = deps;
    42     }
    43     
    44     public void assign(Knockout model) {
    45         if (this.model != model) {
    46             this.model = model;
    47             notifyChange();
    48         }
    49     }
    50     
    51     public KOList<T> onChange(Runnable r) {
    52         if (this.onchange != null) {
    53             throw new IllegalStateException();
    54         }
    55         this.onchange = r;
    56         return this;
    57     }
    58 
    59     @Override
    60     public boolean add(T e) {
    61         boolean ret = super.add(e);
    62         notifyChange();
    63         return ret;
    64     }
    65 
    66     @Override
    67     public boolean addAll(Collection<? extends T> c) {
    68         boolean ret = super.addAll(c);
    69         notifyChange();
    70         return ret;
    71     }
    72 
    73     @Override
    74     public boolean addAll(int index, Collection<? extends T> c) {
    75         boolean ret = super.addAll(index, c);
    76         notifyChange();
    77         return ret;
    78     }
    79 
    80     @Override
    81     public boolean remove(Object o) {
    82         boolean ret = super.remove(o);
    83         notifyChange();
    84         return ret;
    85     }
    86 
    87     @Override
    88     public void clear() {
    89         super.clear();
    90         notifyChange();
    91     }
    92 
    93     @Override
    94     public boolean removeAll(Collection<?> c) {
    95         boolean ret = super.removeAll(c);
    96         notifyChange();
    97         return ret;
    98     }
    99 
   100     @Override
   101     public boolean retainAll(Collection<?> c) {
   102         boolean ret = super.retainAll(c);
   103         notifyChange();
   104         return ret;
   105     }
   106 
   107     @Override
   108     public T set(int index, T element) {
   109         T ret = super.set(index, element);
   110         notifyChange();
   111         return ret;
   112     }
   113 
   114     @Override
   115     public void add(int index, T element) {
   116         super.add(index, element);
   117         notifyChange();
   118     }
   119 
   120     @Override
   121     public T remove(int index) {
   122         T ret = super.remove(index);
   123         notifyChange();
   124         return ret;
   125     }
   126 
   127     @Override
   128     public String toString() {
   129         Iterator<T> it = iterator();
   130         if (!it.hasNext()) {
   131             return "[]";
   132         }
   133         String sep = "";
   134         StringBuilder sb = new StringBuilder();
   135         sb.append('[');
   136         while (it.hasNext()) {
   137             T t = it.next();
   138             sb.append(sep);
   139             sb.append(ConvertTypes.toJSON(t));
   140             sep = ",";
   141         }
   142         sb.append(']');
   143         return sb.toString();
   144     }
   145     
   146     
   147     @JavaScriptOnly(name = "koArray", value = "function() { return this.toArray___3Ljava_lang_Object_2(); }")
   148     private static native int koArray();
   149     
   150     public Object koData() {
   151         Object[] arr = toArray();
   152         Method toKO = null;
   153         for (int i = 0; i < arr.length; i++) {
   154             if (arr[i] == null) {
   155                 continue;
   156             }
   157             if (toKO == null || toKO.getDeclaringClass() != arr[i].getClass()) {
   158                 try {
   159                     toKO = arr[i].getClass().getDeclaredMethod("koData");
   160                     toKO.setAccessible(true);
   161                 } catch (NoSuchMethodException ex) {
   162                     LOG.log(Level.FINE, "No koData conversion for " + arr[i], ex);
   163                     toKO = null;
   164                 }
   165             }
   166             if (toKO != null) {
   167                 try {
   168                     Object prev = arr[i];
   169                     arr[i] = toKO.invoke(arr[i]);
   170                     LOG.log(Level.FINER, "{0}th element {1} replaced by {2}", new Object[]{i, prev, arr[i]});
   171                 } catch (IllegalAccessException | InvocationTargetException ex) {
   172                     LOG.log(Level.SEVERE, "Problems invoking koData on " + arr[i], ex);
   173                 }
   174             }
   175         }
   176         return Knockout.toArray(arr);
   177     }
   178     private static final Logger LOG = Logger.getLogger(KOList.class.getName());
   179 
   180     private void notifyChange() {
   181         Knockout m = model;
   182         if (m != null) {
   183             m.valueHasMutated(name);
   184             for (String dependant : deps) {
   185                 m.valueHasMutated(dependant);
   186             }
   187         }
   188         Runnable r = onchange;
   189         if (r != null) {
   190             r.run();
   191         }
   192     }
   193 
   194     @Override
   195     public KOList clone() {
   196         KOList ko = (KOList)super.clone();
   197         ko.model = null;
   198         return ko;
   199     }
   200     
   201 }