json/src/main/java/org/netbeans/html/json/impl/JSONList.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 json/src/main/java/org/apidesign/html/json/impl/JSONList.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package org.netbeans.html.json.impl;
    44 
    45 import java.lang.reflect.Array;
    46 import java.util.ArrayList;
    47 import java.util.Arrays;
    48 import java.util.Collection;
    49 import java.util.Iterator;
    50 import java.util.List;
    51 import net.java.html.BrwsrCtx;
    52 
    53 /**
    54  *
    55  * @author Jaroslav Tulach <jtulach@netbeans.org>
    56  */
    57 public final class JSONList<T> extends ArrayList<T> {
    58     private final String name;
    59     private final String[] deps;
    60     private Bindings[] model;
    61     private Runnable onchange;
    62 
    63     public JSONList(Bindings[] model, String name, String... deps) {
    64         assert model.length == 1;
    65         this.model = model;
    66         this.name = name;
    67         this.deps = deps;
    68     }
    69     
    70     public void init(T... values) {
    71         if (values == null || values.length == 0) {
    72             return;
    73         }
    74         if (this.model[0] != null || !isEmpty()) {
    75             throw new IllegalStateException();
    76         }
    77         super.addAll(Arrays.asList(values));
    78     }
    79     
    80     public void init(Object values) {
    81         int len;
    82         if (values == null || (len = Array.getLength(values)) == 0) {
    83             return;
    84         }
    85         if (this.model[0] != null || !isEmpty()) {
    86             throw new IllegalStateException();
    87         }
    88         for (int i = 0; i < len; i++) {
    89             Object data = Array.get(values, i);
    90             super.add((T)data);
    91         }
    92     }
    93     
    94     public JSONList<T> onChange(Runnable r) {
    95         if (this.onchange != null) {
    96             throw new IllegalStateException();
    97         }
    98         this.onchange = r;
    99         return this;
   100     }
   101 
   102     @Override
   103     public boolean add(T e) {
   104         boolean ret = super.add(e);
   105         notifyChange();
   106         return ret;
   107     }
   108 
   109     @Override
   110     public boolean addAll(Collection<? extends T> c) {
   111         boolean ret = super.addAll(c);
   112         notifyChange();
   113         return ret;
   114     }
   115 
   116     @Override
   117     public boolean addAll(int index, Collection<? extends T> c) {
   118         boolean ret = super.addAll(index, c);
   119         notifyChange();
   120         return ret;
   121     }
   122 
   123     @Override
   124     public boolean remove(Object o) {
   125         boolean ret = super.remove(o);
   126         notifyChange();
   127         return ret;
   128     }
   129 
   130     @Override
   131     public void clear() {
   132         super.clear();
   133         notifyChange();
   134     }
   135 
   136     @Override
   137     public boolean removeAll(Collection<?> c) {
   138         boolean ret = super.removeAll(c);
   139         notifyChange();
   140         return ret;
   141     }
   142 
   143     @Override
   144     public boolean retainAll(Collection<?> c) {
   145         boolean ret = super.retainAll(c);
   146         notifyChange();
   147         return ret;
   148     }
   149 
   150     @Override
   151     public T set(int index, T element) {
   152         T ret = super.set(index, element);
   153         notifyChange();
   154         return ret;
   155     }
   156 
   157     @Override
   158     public void add(int index, T element) {
   159         super.add(index, element);
   160         notifyChange();
   161     }
   162 
   163     @Override
   164     public T remove(int index) {
   165         T ret = super.remove(index);
   166         notifyChange();
   167         return ret;
   168     }
   169 
   170     @Override
   171     public String toString() {
   172         Iterator<T> it = iterator();
   173         if (!it.hasNext()) {
   174             return "[]";
   175         }
   176         String sep = "";
   177         StringBuilder sb = new StringBuilder();
   178         sb.append('[');
   179         while (it.hasNext()) {
   180             T t = it.next();
   181             sb.append(sep);
   182             sb.append(JSON.toJSON(t));
   183             sep = ",";
   184         }
   185         sb.append(']');
   186         return sb.toString();
   187     }
   188 
   189     private void notifyChange() {
   190         Bindings m = model[0];
   191         if (m != null) {
   192             m.valueHasMutated(name);
   193             for (String dependant : deps) {
   194                 m.valueHasMutated(dependant);
   195             }
   196             Runnable r = onchange;
   197             if (r != null) {
   198                 r.run();
   199             }
   200         }
   201     }
   202     
   203     public void cloneAll(BrwsrCtx c, Collection<T> other) {
   204         Boolean isModel = null;
   205         for (T t : other) {
   206             if (isModel == null) {
   207                 isModel = JSON.isModel(t.getClass());
   208             }
   209             if (isModel) {
   210                 add(JSON.bindTo(t, c));
   211             } else {
   212                 add(t);
   213             }
   214         }
   215     }
   216 
   217     @Override
   218     public JSONList clone() {
   219         throw new UnsupportedOperationException();
   220     }
   221 
   222     static final Object koData(Collection<?> c, Bindings m) {
   223         Object[] arr = c.toArray(new Object[c.size()]);
   224         for (int i = 0; i < arr.length; i++) {
   225             Object r = WrapperObject.find(arr[i], m);
   226             if (r != null) {
   227                 arr[i] = r;
   228             }
   229         }
   230         return m.wrapArray(arr);
   231     }
   232 
   233     final Object koData() {
   234         return koData(this, model[0]);
   235     }
   236 }