2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.htmlpage;
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;
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
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;
39 public KOList(String name, String... deps) {
44 public void assign(Knockout model) {
45 if (this.model != model) {
51 public KOList<T> onChange(Runnable r) {
52 if (this.onchange != null) {
53 throw new IllegalStateException();
60 public boolean add(T e) {
61 boolean ret = super.add(e);
67 public boolean addAll(Collection<? extends T> c) {
68 boolean ret = super.addAll(c);
74 public boolean addAll(int index, Collection<? extends T> c) {
75 boolean ret = super.addAll(index, c);
81 public boolean remove(Object o) {
82 boolean ret = super.remove(o);
94 public boolean removeAll(Collection<?> c) {
95 boolean ret = super.removeAll(c);
101 public boolean retainAll(Collection<?> c) {
102 boolean ret = super.retainAll(c);
108 public T set(int index, T element) {
109 T ret = super.set(index, element);
115 public void add(int index, T element) {
116 super.add(index, element);
121 public T remove(int index) {
122 T ret = super.remove(index);
128 public String toString() {
129 Iterator<T> it = iterator();
134 StringBuilder sb = new StringBuilder();
136 while (it.hasNext()) {
139 sb.append(ConvertTypes.toJSON(t));
143 return sb.toString();
147 @JavaScriptOnly(name = "koArray", value = "function() { return this.toArray___3Ljava_lang_Object_2(); }")
148 private static native int koArray();
150 public Object koData() {
151 Object[] arr = toArray();
153 for (int i = 0; i < arr.length; i++) {
154 if (arr[i] == null) {
157 if (toKO == null || toKO.getDeclaringClass() != arr[i].getClass()) {
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);
168 Object prev = arr[i];
169 arr[i] = toKO.invoke(arr[i]);
170 LOG.log(Level.INFO, "{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);
178 private static final Logger LOG = Logger.getLogger(KOList.class.getName());
180 private void notifyChange() {
183 m.valueHasMutated(name);
184 for (String dependant : deps) {
185 m.valueHasMutated(dependant);
188 Runnable r = onchange;
195 public KOList clone() {
196 KOList ko = (KOList)super.clone();