javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1568 0db00da6f375
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.util.ArrayList;
    21 import java.util.Collection;
    22 import java.util.Iterator;
    23 import org.apidesign.bck2brwsr.core.JavaScriptOnly;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public final class KOList<T> extends ArrayList<T> {
    30     private final String name;
    31     private final String[] deps;
    32     private Knockout model;
    33     private Runnable onchange;
    34 
    35     public KOList(String name, String... deps) {
    36         this.name = name;
    37         this.deps = deps;
    38     }
    39     
    40     public void assign(Knockout model) {
    41         if (this.model != model) {
    42             this.model = model;
    43             notifyChange();
    44         }
    45     }
    46     
    47     public KOList<T> onChange(Runnable r) {
    48         if (this.onchange != null) {
    49             throw new IllegalStateException();
    50         }
    51         this.onchange = r;
    52         return this;
    53     }
    54 
    55     @Override
    56     public boolean add(T e) {
    57         boolean ret = super.add(e);
    58         notifyChange();
    59         return ret;
    60     }
    61 
    62     @Override
    63     public boolean addAll(Collection<? extends T> c) {
    64         boolean ret = super.addAll(c);
    65         notifyChange();
    66         return ret;
    67     }
    68 
    69     @Override
    70     public boolean addAll(int index, Collection<? extends T> c) {
    71         boolean ret = super.addAll(index, c);
    72         notifyChange();
    73         return ret;
    74     }
    75 
    76     @Override
    77     public boolean remove(Object o) {
    78         boolean ret = super.remove(o);
    79         notifyChange();
    80         return ret;
    81     }
    82 
    83     @Override
    84     public void clear() {
    85         super.clear();
    86         notifyChange();
    87     }
    88 
    89     @Override
    90     public boolean removeAll(Collection<?> c) {
    91         boolean ret = super.removeAll(c);
    92         notifyChange();
    93         return ret;
    94     }
    95 
    96     @Override
    97     public boolean retainAll(Collection<?> c) {
    98         boolean ret = super.retainAll(c);
    99         notifyChange();
   100         return ret;
   101     }
   102 
   103     @Override
   104     public T set(int index, T element) {
   105         T ret = super.set(index, element);
   106         notifyChange();
   107         return ret;
   108     }
   109 
   110     @Override
   111     public void add(int index, T element) {
   112         super.add(index, element);
   113         notifyChange();
   114     }
   115 
   116     @Override
   117     public T remove(int index) {
   118         T ret = super.remove(index);
   119         notifyChange();
   120         return ret;
   121     }
   122 
   123     @Override
   124     public String toString() {
   125         Iterator<T> it = iterator();
   126         if (!it.hasNext()) {
   127             return "[]";
   128         }
   129         String sep = "";
   130         StringBuilder sb = new StringBuilder();
   131         sb.append('[');
   132         while (it.hasNext()) {
   133             T t = it.next();
   134             sb.append(sep);
   135             sb.append(ConvertTypes.toJSON(t));
   136             sep = ",";
   137         }
   138         sb.append(']');
   139         return sb.toString();
   140     }
   141     
   142     
   143     @JavaScriptOnly(name = "koArray", value = "function() { return this['toArray___3Ljava_lang_Object_2'](); }")
   144     private static native int koArray();
   145 
   146     private void notifyChange() {
   147         Knockout m = model;
   148         if (m != null) {
   149             m.valueHasMutated(name);
   150             for (String dependant : deps) {
   151                 m.valueHasMutated(dependant);
   152             }
   153         }
   154         Runnable r = onchange;
   155         if (r != null) {
   156             r.run();
   157         }
   158     }
   159 
   160     @Override
   161     public KOList clone() {
   162         KOList ko = (KOList)super.clone();
   163         ko.model = null;
   164         return ko;
   165     }
   166     
   167 }