1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSONList.java Mon Dec 16 16:59:43 2013 +0100
1.3 @@ -0,0 +1,236 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.json.impl;
1.47 +
1.48 +import java.lang.reflect.Array;
1.49 +import java.util.ArrayList;
1.50 +import java.util.Arrays;
1.51 +import java.util.Collection;
1.52 +import java.util.Iterator;
1.53 +import java.util.List;
1.54 +import net.java.html.BrwsrCtx;
1.55 +
1.56 +/**
1.57 + *
1.58 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.59 + */
1.60 +public final class JSONList<T> extends ArrayList<T> {
1.61 + private final String name;
1.62 + private final String[] deps;
1.63 + private Bindings[] model;
1.64 + private Runnable onchange;
1.65 +
1.66 + public JSONList(Bindings[] model, String name, String... deps) {
1.67 + assert model.length == 1;
1.68 + this.model = model;
1.69 + this.name = name;
1.70 + this.deps = deps;
1.71 + }
1.72 +
1.73 + public void init(T... values) {
1.74 + if (values == null || values.length == 0) {
1.75 + return;
1.76 + }
1.77 + if (this.model[0] != null || !isEmpty()) {
1.78 + throw new IllegalStateException();
1.79 + }
1.80 + super.addAll(Arrays.asList(values));
1.81 + }
1.82 +
1.83 + public void init(Object values) {
1.84 + int len;
1.85 + if (values == null || (len = Array.getLength(values)) == 0) {
1.86 + return;
1.87 + }
1.88 + if (this.model[0] != null || !isEmpty()) {
1.89 + throw new IllegalStateException();
1.90 + }
1.91 + for (int i = 0; i < len; i++) {
1.92 + Object data = Array.get(values, i);
1.93 + super.add((T)data);
1.94 + }
1.95 + }
1.96 +
1.97 + public JSONList<T> onChange(Runnable r) {
1.98 + if (this.onchange != null) {
1.99 + throw new IllegalStateException();
1.100 + }
1.101 + this.onchange = r;
1.102 + return this;
1.103 + }
1.104 +
1.105 + @Override
1.106 + public boolean add(T e) {
1.107 + boolean ret = super.add(e);
1.108 + notifyChange();
1.109 + return ret;
1.110 + }
1.111 +
1.112 + @Override
1.113 + public boolean addAll(Collection<? extends T> c) {
1.114 + boolean ret = super.addAll(c);
1.115 + notifyChange();
1.116 + return ret;
1.117 + }
1.118 +
1.119 + @Override
1.120 + public boolean addAll(int index, Collection<? extends T> c) {
1.121 + boolean ret = super.addAll(index, c);
1.122 + notifyChange();
1.123 + return ret;
1.124 + }
1.125 +
1.126 + @Override
1.127 + public boolean remove(Object o) {
1.128 + boolean ret = super.remove(o);
1.129 + notifyChange();
1.130 + return ret;
1.131 + }
1.132 +
1.133 + @Override
1.134 + public void clear() {
1.135 + super.clear();
1.136 + notifyChange();
1.137 + }
1.138 +
1.139 + @Override
1.140 + public boolean removeAll(Collection<?> c) {
1.141 + boolean ret = super.removeAll(c);
1.142 + notifyChange();
1.143 + return ret;
1.144 + }
1.145 +
1.146 + @Override
1.147 + public boolean retainAll(Collection<?> c) {
1.148 + boolean ret = super.retainAll(c);
1.149 + notifyChange();
1.150 + return ret;
1.151 + }
1.152 +
1.153 + @Override
1.154 + public T set(int index, T element) {
1.155 + T ret = super.set(index, element);
1.156 + notifyChange();
1.157 + return ret;
1.158 + }
1.159 +
1.160 + @Override
1.161 + public void add(int index, T element) {
1.162 + super.add(index, element);
1.163 + notifyChange();
1.164 + }
1.165 +
1.166 + @Override
1.167 + public T remove(int index) {
1.168 + T ret = super.remove(index);
1.169 + notifyChange();
1.170 + return ret;
1.171 + }
1.172 +
1.173 + @Override
1.174 + public String toString() {
1.175 + Iterator<T> it = iterator();
1.176 + if (!it.hasNext()) {
1.177 + return "[]";
1.178 + }
1.179 + String sep = "";
1.180 + StringBuilder sb = new StringBuilder();
1.181 + sb.append('[');
1.182 + while (it.hasNext()) {
1.183 + T t = it.next();
1.184 + sb.append(sep);
1.185 + sb.append(JSON.toJSON(t));
1.186 + sep = ",";
1.187 + }
1.188 + sb.append(']');
1.189 + return sb.toString();
1.190 + }
1.191 +
1.192 + private void notifyChange() {
1.193 + Bindings m = model[0];
1.194 + if (m != null) {
1.195 + m.valueHasMutated(name);
1.196 + for (String dependant : deps) {
1.197 + m.valueHasMutated(dependant);
1.198 + }
1.199 + Runnable r = onchange;
1.200 + if (r != null) {
1.201 + r.run();
1.202 + }
1.203 + }
1.204 + }
1.205 +
1.206 + public void cloneAll(BrwsrCtx c, Collection<T> other) {
1.207 + Boolean isModel = null;
1.208 + for (T t : other) {
1.209 + if (isModel == null) {
1.210 + isModel = JSON.isModel(t.getClass());
1.211 + }
1.212 + if (isModel) {
1.213 + add(JSON.bindTo(t, c));
1.214 + } else {
1.215 + add(t);
1.216 + }
1.217 + }
1.218 + }
1.219 +
1.220 + @Override
1.221 + public JSONList clone() {
1.222 + throw new UnsupportedOperationException();
1.223 + }
1.224 +
1.225 + static final Object koData(Collection<?> c, Bindings m) {
1.226 + Object[] arr = c.toArray(new Object[c.size()]);
1.227 + for (int i = 0; i < arr.length; i++) {
1.228 + Object r = WrapperObject.find(arr[i], m);
1.229 + if (r != null) {
1.230 + arr[i] = r;
1.231 + }
1.232 + }
1.233 + return m.wrapArray(arr);
1.234 + }
1.235 +
1.236 + final Object koData() {
1.237 + return koData(this, model[0]);
1.238 + }
1.239 +}