ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/KOList.java
changeset 1189 9f8b07dcbe79
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/KOList.java	Mon Apr 29 12:50:19 2013 +0200
     1.3 @@ -0,0 +1,170 @@
     1.4 +/**
     1.5 + * HTML via Java(tm) Language Bindings
     1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details. apidesign.org
    1.16 + * designates this particular file as subject to the
    1.17 + * "Classpath" exception as provided by apidesign.org
    1.18 + * in the License file that accompanied this code.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License
    1.21 + * along with this program. Look for COPYING file in the top folder.
    1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    1.23 + */
    1.24 +package org.apidesign.html.ko2brwsr;
    1.25 +
    1.26 +import java.util.ArrayList;
    1.27 +import java.util.Collection;
    1.28 +import java.util.Iterator;
    1.29 +import org.apidesign.bck2brwsr.core.JavaScriptOnly;
    1.30 +
    1.31 +/**
    1.32 + *
    1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.34 + */
    1.35 +public final class KOList<T> extends ArrayList<T> {
    1.36 +    private final String name;
    1.37 +    private final String[] deps;
    1.38 +    private Knockout model;
    1.39 +    private Runnable onchange;
    1.40 +
    1.41 +    public KOList(String name, String... deps) {
    1.42 +        this.name = name;
    1.43 +        this.deps = deps;
    1.44 +    }
    1.45 +    
    1.46 +    public void assign(Knockout model) {
    1.47 +        if (this.model != model) {
    1.48 +            this.model = model;
    1.49 +            notifyChange();
    1.50 +        }
    1.51 +    }
    1.52 +    
    1.53 +    public KOList<T> onChange(Runnable r) {
    1.54 +        if (this.onchange != null) {
    1.55 +            throw new IllegalStateException();
    1.56 +        }
    1.57 +        this.onchange = r;
    1.58 +        return this;
    1.59 +    }
    1.60 +
    1.61 +    @Override
    1.62 +    public boolean add(T e) {
    1.63 +        boolean ret = super.add(e);
    1.64 +        notifyChange();
    1.65 +        return ret;
    1.66 +    }
    1.67 +
    1.68 +    @Override
    1.69 +    public boolean addAll(Collection<? extends T> c) {
    1.70 +        boolean ret = super.addAll(c);
    1.71 +        notifyChange();
    1.72 +        return ret;
    1.73 +    }
    1.74 +
    1.75 +    @Override
    1.76 +    public boolean addAll(int index, Collection<? extends T> c) {
    1.77 +        boolean ret = super.addAll(index, c);
    1.78 +        notifyChange();
    1.79 +        return ret;
    1.80 +    }
    1.81 +
    1.82 +    @Override
    1.83 +    public boolean remove(Object o) {
    1.84 +        boolean ret = super.remove(o);
    1.85 +        notifyChange();
    1.86 +        return ret;
    1.87 +    }
    1.88 +
    1.89 +    @Override
    1.90 +    public void clear() {
    1.91 +        super.clear();
    1.92 +        notifyChange();
    1.93 +    }
    1.94 +
    1.95 +    @Override
    1.96 +    public boolean removeAll(Collection<?> c) {
    1.97 +        boolean ret = super.removeAll(c);
    1.98 +        notifyChange();
    1.99 +        return ret;
   1.100 +    }
   1.101 +
   1.102 +    @Override
   1.103 +    public boolean retainAll(Collection<?> c) {
   1.104 +        boolean ret = super.retainAll(c);
   1.105 +        notifyChange();
   1.106 +        return ret;
   1.107 +    }
   1.108 +
   1.109 +    @Override
   1.110 +    public T set(int index, T element) {
   1.111 +        T ret = super.set(index, element);
   1.112 +        notifyChange();
   1.113 +        return ret;
   1.114 +    }
   1.115 +
   1.116 +    @Override
   1.117 +    public void add(int index, T element) {
   1.118 +        super.add(index, element);
   1.119 +        notifyChange();
   1.120 +    }
   1.121 +
   1.122 +    @Override
   1.123 +    public T remove(int index) {
   1.124 +        T ret = super.remove(index);
   1.125 +        notifyChange();
   1.126 +        return ret;
   1.127 +    }
   1.128 +
   1.129 +    @Override
   1.130 +    public String toString() {
   1.131 +        Iterator<T> it = iterator();
   1.132 +        if (!it.hasNext()) {
   1.133 +            return "[]";
   1.134 +        }
   1.135 +        String sep = "";
   1.136 +        StringBuilder sb = new StringBuilder();
   1.137 +        sb.append('[');
   1.138 +        while (it.hasNext()) {
   1.139 +            T t = it.next();
   1.140 +            sb.append(sep);
   1.141 +            sb.append(ConvertTypes.toJSON(t));
   1.142 +            sep = ",";
   1.143 +        }
   1.144 +        sb.append(']');
   1.145 +        return sb.toString();
   1.146 +    }
   1.147 +    
   1.148 +    
   1.149 +    @JavaScriptOnly(name = "koArray", value = "function() { return this.toArray___3Ljava_lang_Object_2(); }")
   1.150 +    private static native int koArray();
   1.151 +
   1.152 +    private void notifyChange() {
   1.153 +        Knockout m = model;
   1.154 +        if (m != null) {
   1.155 +            m.valueHasMutated(name);
   1.156 +            for (String dependant : deps) {
   1.157 +                m.valueHasMutated(dependant);
   1.158 +            }
   1.159 +        }
   1.160 +        Runnable r = onchange;
   1.161 +        if (r != null) {
   1.162 +            r.run();
   1.163 +        }
   1.164 +    }
   1.165 +
   1.166 +    @Override
   1.167 +    public KOList clone() {
   1.168 +        KOList ko = (KOList)super.clone();
   1.169 +        ko.model = null;
   1.170 +        return ko;
   1.171 +    }
   1.172 +    
   1.173 +}