1.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSONList.java Fri Apr 19 11:11:07 2013 +0200
1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSONList.java Fri Apr 19 11:29:49 2013 +0200
1.3 @@ -21,26 +21,148 @@
1.4 package org.apidesign.html.json.impl;
1.5
1.6 import java.util.ArrayList;
1.7 +import java.util.Collection;
1.8 +import java.util.Iterator;
1.9
1.10 /**
1.11 *
1.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.13 */
1.14 public final class JSONList<T> extends ArrayList<T> {
1.15 + private final String name;
1.16 + private final String[] deps;
1.17 + private Bindings model;
1.18 + private Runnable onchange;
1.19
1.20 - public JSONList(String prop) {
1.21 + public JSONList(String name, String... deps) {
1.22 + this.name = name;
1.23 + this.deps = deps;
1.24 }
1.25 -
1.26 +
1.27 + public void assign(Bindings model) {
1.28 + if (this.model != model) {
1.29 + this.model = model;
1.30 + notifyChange();
1.31 + }
1.32 + }
1.33 +
1.34 public JSONList<T> onChange(Runnable r) {
1.35 + if (this.onchange != null) {
1.36 + throw new IllegalStateException();
1.37 + }
1.38 + this.onchange = r;
1.39 return this;
1.40 }
1.41
1.42 - public JSONList<T> assign(Bindings ko) {
1.43 - return this;
1.44 + @Override
1.45 + public boolean add(T e) {
1.46 + boolean ret = super.add(e);
1.47 + notifyChange();
1.48 + return ret;
1.49 }
1.50 -
1.51 +
1.52 @Override
1.53 - public JSONList<T> clone() {
1.54 - return this;
1.55 + public boolean addAll(Collection<? extends T> c) {
1.56 + boolean ret = super.addAll(c);
1.57 + notifyChange();
1.58 + return ret;
1.59 + }
1.60 +
1.61 + @Override
1.62 + public boolean addAll(int index, Collection<? extends T> c) {
1.63 + boolean ret = super.addAll(index, c);
1.64 + notifyChange();
1.65 + return ret;
1.66 + }
1.67 +
1.68 + @Override
1.69 + public boolean remove(Object o) {
1.70 + boolean ret = super.remove(o);
1.71 + notifyChange();
1.72 + return ret;
1.73 + }
1.74 +
1.75 + @Override
1.76 + public void clear() {
1.77 + super.clear();
1.78 + notifyChange();
1.79 + }
1.80 +
1.81 + @Override
1.82 + public boolean removeAll(Collection<?> c) {
1.83 + boolean ret = super.removeAll(c);
1.84 + notifyChange();
1.85 + return ret;
1.86 + }
1.87 +
1.88 + @Override
1.89 + public boolean retainAll(Collection<?> c) {
1.90 + boolean ret = super.retainAll(c);
1.91 + notifyChange();
1.92 + return ret;
1.93 + }
1.94 +
1.95 + @Override
1.96 + public T set(int index, T element) {
1.97 + T ret = super.set(index, element);
1.98 + notifyChange();
1.99 + return ret;
1.100 + }
1.101 +
1.102 + @Override
1.103 + public void add(int index, T element) {
1.104 + super.add(index, element);
1.105 + notifyChange();
1.106 + }
1.107 +
1.108 + @Override
1.109 + public T remove(int index) {
1.110 + T ret = super.remove(index);
1.111 + notifyChange();
1.112 + return ret;
1.113 + }
1.114 +
1.115 + @Override
1.116 + public String toString() {
1.117 + Iterator<T> it = iterator();
1.118 + if (!it.hasNext()) {
1.119 + return "[]";
1.120 + }
1.121 + String sep = "";
1.122 + StringBuilder sb = new StringBuilder();
1.123 + sb.append('[');
1.124 + while (it.hasNext()) {
1.125 + T t = it.next();
1.126 + sb.append(sep);
1.127 + sb.append(JSON.toJSON(t));
1.128 + sep = ",";
1.129 + }
1.130 + sb.append(']');
1.131 + return sb.toString();
1.132 + }
1.133 +
1.134 + public Object koData() {
1.135 + return toArray();
1.136 + }
1.137 +
1.138 + private void notifyChange() {
1.139 + Bindings m = model;
1.140 + if (m != null) {
1.141 + m.valueHasMutated(name);
1.142 + for (String dependant : deps) {
1.143 + m.valueHasMutated(dependant);
1.144 + }
1.145 + }
1.146 + Runnable r = onchange;
1.147 + if (r != null) {
1.148 + r.run();
1.149 + }
1.150 + }
1.151 +
1.152 + @Override
1.153 + public JSONList clone() {
1.154 + JSONList ko = (JSONList) super.clone();
1.155 + ko.model = null;
1.156 + return ko;
1.157 }
1.158 }