2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
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.
43 package org.netbeans.html.json.impl;
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;
55 * @author Jaroslav Tulach <jtulach@netbeans.org>
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;
63 public JSONList(Bindings[] model, String name, String... deps) {
64 assert model.length == 1;
70 public void init(T... values) {
71 if (values == null || values.length == 0) {
74 if (this.model[0] != null || !isEmpty()) {
75 throw new IllegalStateException();
77 super.addAll(Arrays.asList(values));
80 public void init(Object values) {
82 if (values == null || (len = Array.getLength(values)) == 0) {
85 if (this.model[0] != null || !isEmpty()) {
86 throw new IllegalStateException();
88 for (int i = 0; i < len; i++) {
89 Object data = Array.get(values, i);
94 public JSONList<T> onChange(Runnable r) {
95 if (this.onchange != null) {
96 throw new IllegalStateException();
103 public boolean add(T e) {
104 boolean ret = super.add(e);
110 public boolean addAll(Collection<? extends T> c) {
111 boolean ret = super.addAll(c);
117 public boolean addAll(int index, Collection<? extends T> c) {
118 boolean ret = super.addAll(index, c);
124 public boolean remove(Object o) {
125 boolean ret = super.remove(o);
131 public void clear() {
137 public boolean removeAll(Collection<?> c) {
138 boolean ret = super.removeAll(c);
144 public boolean retainAll(Collection<?> c) {
145 boolean ret = super.retainAll(c);
151 public T set(int index, T element) {
152 T ret = super.set(index, element);
158 public void add(int index, T element) {
159 super.add(index, element);
164 public T remove(int index) {
165 T ret = super.remove(index);
171 public String toString() {
172 Iterator<T> it = iterator();
177 StringBuilder sb = new StringBuilder();
179 while (it.hasNext()) {
182 sb.append(JSON.toJSON(t));
186 return sb.toString();
189 private void notifyChange() {
190 Bindings m = model[0];
192 m.valueHasMutated(name);
193 for (String dependant : deps) {
194 m.valueHasMutated(dependant);
196 Runnable r = onchange;
203 public void cloneAll(BrwsrCtx c, Collection<T> other) {
204 Boolean isModel = null;
206 if (isModel == null) {
207 isModel = JSON.isModel(t.getClass());
210 add(JSON.bindTo(t, c));
218 public JSONList clone() {
219 throw new UnsupportedOperationException();
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);
230 return m.wrapArray(arr);
233 final Object koData() {
234 return koData(this, model[0]);