jaroslav@37
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@37
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@37
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@37
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@37
|
42 |
*/
|
jaroslav@442
|
43 |
package org.netbeans.html.ko4j;
|
jaroslav@37
|
44 |
|
jtulach@886
|
45 |
import org.netbeans.html.context.spi.Contexts;
|
jtulach@838
|
46 |
import org.netbeans.html.json.spi.FunctionBinding;
|
jtulach@838
|
47 |
import org.netbeans.html.json.spi.PropertyBinding;
|
jtulach@838
|
48 |
import org.netbeans.html.json.spi.Technology;
|
jtulach@886
|
49 |
import static org.netbeans.html.ko4j.KO4J.LOG;
|
jaroslav@37
|
50 |
|
jaroslav@54
|
51 |
/** This is an implementation package - just
|
jaroslav@54
|
52 |
* include its JAR on classpath and use official {@link Context} API
|
jaroslav@54
|
53 |
* to access the functionality.
|
jaroslav@54
|
54 |
* <p>
|
jaroslav@37
|
55 |
*
|
jtulach@790
|
56 |
* @author Jaroslav Tulach
|
jaroslav@37
|
57 |
*/
|
jtulach@886
|
58 |
@Contexts.Id("ko4j")
|
jtulach@886
|
59 |
final class KOTech
|
jtulach@1028
|
60 |
implements Technology.BatchCopy<Object>, Technology.ValueMutated<Object>, Technology.ApplyId<Object> {
|
jaroslav@574
|
61 |
private Object[] jsObjects;
|
jaroslav@574
|
62 |
private int jsIndex;
|
jaroslav@288
|
63 |
|
jtulach@886
|
64 |
public KOTech() {
|
jaroslav@288
|
65 |
}
|
jaroslav@150
|
66 |
|
jaroslav@275
|
67 |
@Override
|
jtulach@1028
|
68 |
public Object wrapModel(Object model, Object copyFrom, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
|
jtulach@1028
|
69 |
return createKO(model, copyFrom, propArr, funcArr, null);
|
jtulach@1007
|
70 |
}
|
jtulach@1007
|
71 |
|
jtulach@1028
|
72 |
final Object createKO(Object model, Object copyFrom, PropertyBinding[] propArr, FunctionBinding[] funcArr, Knockout[] ko) {
|
jaroslav@275
|
73 |
String[] propNames = new String[propArr.length];
|
jtulach@1027
|
74 |
Boolean[] propReadOnly = new Boolean[propArr.length];
|
jtulach@1055
|
75 |
Boolean[] propConstant = new Boolean[propArr.length];
|
jaroslav@279
|
76 |
Object[] propValues = new Object[propArr.length];
|
jaroslav@275
|
77 |
for (int i = 0; i < propNames.length; i++) {
|
jaroslav@275
|
78 |
propNames[i] = propArr[i].getPropertyName();
|
jaroslav@275
|
79 |
propReadOnly[i] = propArr[i].isReadOnly();
|
jtulach@1055
|
80 |
propConstant[i] = propArr[i].isConstant();
|
jtulach@984
|
81 |
Object value = propArr[i].getValue();
|
jtulach@984
|
82 |
if (value instanceof Enum) {
|
jtulach@984
|
83 |
value = value.toString();
|
jtulach@984
|
84 |
}
|
jtulach@984
|
85 |
propValues[i] = value;
|
jaroslav@275
|
86 |
}
|
jaroslav@275
|
87 |
String[] funcNames = new String[funcArr.length];
|
jaroslav@275
|
88 |
for (int i = 0; i < funcNames.length; i++) {
|
jaroslav@275
|
89 |
funcNames[i] = funcArr[i].getFunctionName();
|
jaroslav@275
|
90 |
}
|
jaroslav@574
|
91 |
Object ret = getJSObject();
|
jtulach@1007
|
92 |
Knockout newKO = new Knockout(model, ret, propArr, funcArr);
|
jtulach@1007
|
93 |
if (ko != null) {
|
jtulach@1007
|
94 |
ko[0] = newKO;
|
jtulach@1007
|
95 |
}
|
jtulach@1007
|
96 |
newKO.wrapModel(
|
jtulach@1028
|
97 |
ret, copyFrom,
|
jtulach@1055
|
98 |
propNames, propReadOnly, propConstant, propValues,
|
jtulach@868
|
99 |
funcNames
|
jaroslav@279
|
100 |
);
|
jaroslav@430
|
101 |
return ret;
|
jaroslav@275
|
102 |
}
|
jaroslav@275
|
103 |
|
jaroslav@574
|
104 |
private Object getJSObject() {
|
jaroslav@574
|
105 |
int len = 64;
|
jaroslav@574
|
106 |
if (jsObjects != null && jsIndex < (len = jsObjects.length)) {
|
jtulach@877
|
107 |
Object ret = jsObjects[jsIndex];
|
jtulach@877
|
108 |
jsObjects[jsIndex] = null;
|
jtulach@877
|
109 |
jsIndex++;
|
jtulach@877
|
110 |
return ret;
|
jaroslav@574
|
111 |
}
|
jaroslav@574
|
112 |
jsObjects = Knockout.allocJS(len * 2);
|
jaroslav@574
|
113 |
jsIndex = 1;
|
jtulach@877
|
114 |
Object ret = jsObjects[0];
|
jtulach@877
|
115 |
jsObjects[0] = null;
|
jtulach@877
|
116 |
return ret;
|
jaroslav@574
|
117 |
}
|
jaroslav@574
|
118 |
|
jaroslav@37
|
119 |
@Override
|
jaroslav@430
|
120 |
public Object wrapModel(Object model) {
|
jaroslav@276
|
121 |
throw new UnsupportedOperationException();
|
jaroslav@37
|
122 |
}
|
jaroslav@37
|
123 |
|
jaroslav@37
|
124 |
@Override
|
jaroslav@430
|
125 |
public void bind(PropertyBinding b, Object model, Object data) {
|
jaroslav@276
|
126 |
throw new UnsupportedOperationException();
|
jaroslav@37
|
127 |
}
|
jaroslav@37
|
128 |
|
jaroslav@37
|
129 |
@Override
|
jaroslav@430
|
130 |
public void valueHasMutated(Object data, String propertyName) {
|
jtulach@877
|
131 |
Knockout.cleanUp();
|
jaroslav@567
|
132 |
Knockout.valueHasMutated(data, propertyName, null, null);
|
jaroslav@567
|
133 |
}
|
jaroslav@567
|
134 |
|
jaroslav@567
|
135 |
@Override
|
jaroslav@567
|
136 |
public void valueHasMutated(Object data, String propertyName, Object oldValue, Object newValue) {
|
jtulach@877
|
137 |
Knockout.cleanUp();
|
jtulach@984
|
138 |
if (newValue instanceof Enum) {
|
jtulach@984
|
139 |
newValue = newValue.toString();
|
jtulach@984
|
140 |
}
|
jaroslav@567
|
141 |
Knockout.valueHasMutated(data, propertyName, oldValue, newValue);
|
jaroslav@37
|
142 |
}
|
jaroslav@37
|
143 |
|
jaroslav@37
|
144 |
@Override
|
jaroslav@430
|
145 |
public void expose(FunctionBinding fb, Object model, Object d) {
|
jaroslav@276
|
146 |
throw new UnsupportedOperationException();
|
jaroslav@37
|
147 |
}
|
jaroslav@37
|
148 |
|
jaroslav@37
|
149 |
@Override
|
jaroslav@430
|
150 |
public void applyBindings(Object data) {
|
jtulach@908
|
151 |
applyBindings(null, data);
|
jtulach@908
|
152 |
}
|
jtulach@908
|
153 |
@Override
|
jtulach@908
|
154 |
public void applyBindings(String id, Object data) {
|
jtulach@908
|
155 |
Object ko = Knockout.applyBindings(id, data);
|
jtulach@871
|
156 |
if (ko instanceof Knockout) {
|
jtulach@871
|
157 |
((Knockout)ko).hold();
|
jtulach@871
|
158 |
}
|
jaroslav@37
|
159 |
}
|
jaroslav@37
|
160 |
|
jaroslav@37
|
161 |
@Override
|
jaroslav@37
|
162 |
public Object wrapArray(Object[] arr) {
|
jaroslav@430
|
163 |
return arr;
|
jaroslav@37
|
164 |
}
|
jtulach@886
|
165 |
|
jaroslav@37
|
166 |
@Override
|
jtulach@886
|
167 |
public void runSafe(final Runnable r) {
|
jtulach@886
|
168 |
LOG.warning("Technology.runSafe has been deprecated. Use BrwsrCtx.execute!");
|
jtulach@886
|
169 |
r.run();
|
jtulach@886
|
170 |
}
|
jaroslav@38
|
171 |
|
jaroslav@38
|
172 |
@Override
|
jaroslav@38
|
173 |
public <M> M toModel(Class<M> modelClass, Object data) {
|
jaroslav@430
|
174 |
return modelClass.cast(Knockout.toModel(data));
|
jaroslav@38
|
175 |
}
|
jaroslav@37
|
176 |
}
|