1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java Mon Feb 18 13:03:01 2013 +0100
1.3 @@ -0,0 +1,107 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 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.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.htmlpage;
1.22 +
1.23 +import java.util.ArrayList;
1.24 +import java.util.Collection;
1.25 +
1.26 +/**
1.27 + *
1.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.29 + */
1.30 +public final class KOList<T> extends ArrayList<T> {
1.31 + private final String name;
1.32 + private final String[] deps;
1.33 + private Knockout model;
1.34 +
1.35 + public KOList(String name, String... deps) {
1.36 + this.name = name;
1.37 + this.deps = deps;
1.38 + }
1.39 +
1.40 + public void assign(Knockout model) {
1.41 + this.model = model;
1.42 + }
1.43 +
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 + @Override
1.52 + public boolean remove(Object o) {
1.53 + boolean ret = super.remove(o);
1.54 + notifyChange();
1.55 + return ret;
1.56 + }
1.57 +
1.58 + @Override
1.59 + public void clear() {
1.60 + super.clear();
1.61 + notifyChange();
1.62 + }
1.63 +
1.64 + @Override
1.65 + public boolean removeAll(Collection<?> c) {
1.66 + boolean ret = super.removeAll(c);
1.67 + notifyChange();
1.68 + return ret;
1.69 + }
1.70 +
1.71 + @Override
1.72 + public boolean retainAll(Collection<?> c) {
1.73 + boolean ret = super.retainAll(c);
1.74 + notifyChange();
1.75 + return ret;
1.76 + }
1.77 +
1.78 + @Override
1.79 + public T set(int index, T element) {
1.80 + T ret = super.set(index, element);
1.81 + notifyChange();
1.82 + return ret;
1.83 + }
1.84 +
1.85 + @Override
1.86 + public void add(int index, T element) {
1.87 + super.add(index, element);
1.88 + notifyChange();
1.89 + }
1.90 +
1.91 + @Override
1.92 + public T remove(int index) {
1.93 + T ret = super.remove(index);
1.94 + notifyChange();
1.95 + return ret;
1.96 + }
1.97 +
1.98 + private void notifyChange() {
1.99 + Knockout m = model;
1.100 + if (m == null) {
1.101 + return;
1.102 + }
1.103 + m.valueHasMutated(name);
1.104 + for (String dependant : deps) {
1.105 + m.valueHasMutated(dependant);
1.106 + }
1.107 + }
1.108 +
1.109 +
1.110 +}