jaroslav@376
|
1 |
/**
|
jaroslav@373
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@373
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@373
|
5 |
*
|
jaroslav@373
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@373
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@373
|
8 |
*
|
jaroslav@373
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@373
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@373
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@373
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@373
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@373
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@373
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@373
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@373
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@373
|
18 |
* Notice in each file and include the License file at
|
jaroslav@373
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@373
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@373
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@373
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@373
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@373
|
24 |
* your own identifying information:
|
jaroslav@373
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@373
|
26 |
*
|
jaroslav@373
|
27 |
* Contributor(s):
|
jaroslav@373
|
28 |
*
|
jaroslav@376
|
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@373
|
31 |
*
|
jaroslav@373
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@373
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@373
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@373
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@373
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@373
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@373
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@373
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@373
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@373
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@373
|
42 |
*/
|
jtulach@838
|
43 |
package org.netbeans.html.json.spi;
|
jaroslav@373
|
44 |
|
jtulach@970
|
45 |
import java.util.ArrayList;
|
jaroslav@383
|
46 |
import java.util.Collection;
|
jaroslav@383
|
47 |
import java.util.List;
|
jaroslav@373
|
48 |
import net.java.html.BrwsrCtx;
|
jaroslav@408
|
49 |
import net.java.html.json.ComputedProperty;
|
jaroslav@402
|
50 |
import net.java.html.json.Model;
|
jaroslav@374
|
51 |
import org.netbeans.html.json.impl.Bindings;
|
jaroslav@374
|
52 |
import org.netbeans.html.json.impl.JSON;
|
jtulach@940
|
53 |
import org.netbeans.html.json.impl.JSON.WS;
|
jaroslav@383
|
54 |
import org.netbeans.html.json.impl.JSONList;
|
jtulach@940
|
55 |
import org.netbeans.html.json.impl.PropertyBindingAccessor;
|
jaroslav@386
|
56 |
import org.netbeans.html.json.impl.RcvrJSON;
|
jaroslav@386
|
57 |
import org.netbeans.html.json.impl.RcvrJSON.MsgEvnt;
|
jaroslav@373
|
58 |
|
jaroslav@402
|
59 |
/** Object associated with one instance of a model generated by the
|
jaroslav@402
|
60 |
* {@link Model} annotation. Contains methods the generated class can
|
jaroslav@402
|
61 |
* use to communicate with behind the scene associated {@link Technology}.
|
jaroslav@402
|
62 |
* Each {@link Proto} object is associated with <a href="http://wiki.apidesign.org/wiki/Singletonizer">
|
jtulach@940
|
63 |
* singletonizer</a>-like interface {@link Type} which provides the
|
jtulach@940
|
64 |
* associated {@link Technology} the necessary information about the
|
jaroslav@402
|
65 |
* generated {@link Model} class.
|
jaroslav@373
|
66 |
*
|
jtulach@655
|
67 |
* @author Jaroslav Tulach
|
jaroslav@373
|
68 |
* @since 0.7
|
jaroslav@373
|
69 |
*/
|
jaroslav@373
|
70 |
public final class Proto {
|
jaroslav@374
|
71 |
private final Object obj;
|
jaroslav@373
|
72 |
private final Type type;
|
jaroslav@373
|
73 |
private final net.java.html.BrwsrCtx context;
|
jaroslav@374
|
74 |
private org.netbeans.html.json.impl.Bindings ko;
|
jtulach@782
|
75 |
private Observers observers;
|
jaroslav@373
|
76 |
|
jaroslav@374
|
77 |
Proto(Object obj, Type type, BrwsrCtx context) {
|
jaroslav@374
|
78 |
this.obj = obj;
|
jaroslav@373
|
79 |
this.type = type;
|
jaroslav@373
|
80 |
this.context = context;
|
jaroslav@373
|
81 |
}
|
jaroslav@373
|
82 |
|
jaroslav@402
|
83 |
/** Browser context this proto object and its associated model
|
jaroslav@402
|
84 |
* are operating-in.
|
jtulach@940
|
85 |
*
|
jtulach@940
|
86 |
* @return the associated context
|
jaroslav@402
|
87 |
*/
|
jaroslav@373
|
88 |
public BrwsrCtx getContext() {
|
jaroslav@373
|
89 |
return context;
|
jaroslav@373
|
90 |
}
|
jaroslav@402
|
91 |
|
jtulach@786
|
92 |
/** Acquires global lock to compute a {@link ComputedProperty derived property}
|
jtulach@786
|
93 |
* on this proto object. This proto object must not be locked yet. No
|
jtulach@786
|
94 |
* dependency tracking is performed.
|
jtulach@940
|
95 |
*
|
jaroslav@402
|
96 |
* @throws IllegalStateException if already locked
|
jaroslav@402
|
97 |
*/
|
jaroslav@373
|
98 |
public void acquireLock() throws IllegalStateException {
|
jtulach@778
|
99 |
acquireLock(null);
|
jtulach@774
|
100 |
}
|
jtulach@940
|
101 |
|
jtulach@786
|
102 |
/** Acquires global lock to compute a {@link ComputedProperty derived property}
|
jtulach@786
|
103 |
* on this proto object. This proto object must not be locked yet. The
|
jtulach@786
|
104 |
* name of the property is used to track dependencies on own
|
jtulach@786
|
105 |
* properties of other proto objects - when they are changed, this
|
jtulach@786
|
106 |
* {@link #valueHasMutated(java.lang.String) property is changed too}.
|
jtulach@940
|
107 |
*
|
jtulach@786
|
108 |
* @param propName name of property we are about to compute
|
jtulach@786
|
109 |
* @throws IllegalStateException thrown when there is a cyclic
|
jtulach@786
|
110 |
* call is detected
|
jtulach@786
|
111 |
* @since 0.9
|
jtulach@786
|
112 |
*/
|
jtulach@774
|
113 |
public void acquireLock(String propName) throws IllegalStateException {
|
jtulach@783
|
114 |
Observers.beginComputing(this, propName);
|
jtulach@774
|
115 |
}
|
jtulach@940
|
116 |
|
jtulach@786
|
117 |
/** A property on this proto object is about to be accessed. Verifies
|
jtulach@786
|
118 |
* whether this proto object is accessible - e.g. it has not been
|
jtulach@786
|
119 |
* {@link #acquireLock() locked yet}. If everything is OK, the
|
jtulach@786
|
120 |
* <code>propName</code> is recorded in the chain of dependencies
|
jtulach@786
|
121 |
* tracked by {@link #acquireLock(java.lang.String)} and watched by
|
jtulach@786
|
122 |
* {@link #valueHasMutated(java.lang.String)}.
|
jtulach@940
|
123 |
*
|
jtulach@786
|
124 |
* @param propName name of the property that is requested
|
jtulach@786
|
125 |
* @throws IllegalStateException if the model is locked
|
jtulach@786
|
126 |
* @since 0.9
|
jtulach@786
|
127 |
*/
|
jtulach@786
|
128 |
public void accessProperty(String propName) throws IllegalStateException {
|
jtulach@785
|
129 |
Observers.accessingValue(this, propName);
|
jaroslav@373
|
130 |
}
|
jtulach@940
|
131 |
|
jaroslav@402
|
132 |
/** Verifies the model is not locked otherwise throws an exception.
|
jaroslav@402
|
133 |
* @throws IllegalStateException if the model is locked
|
jaroslav@402
|
134 |
*/
|
jaroslav@402
|
135 |
public void verifyUnlocked() throws IllegalStateException {
|
jtulach@783
|
136 |
Observers.verifyUnlocked(this);
|
jaroslav@373
|
137 |
}
|
jtulach@940
|
138 |
|
jtulach@940
|
139 |
/** When modifications are over, the model is switched into
|
jaroslav@402
|
140 |
* unlocked state by calling this method.
|
jaroslav@402
|
141 |
*/
|
jaroslav@373
|
142 |
public void releaseLock() {
|
jtulach@785
|
143 |
Observers.finishComputing(this);
|
jaroslav@373
|
144 |
}
|
jtulach@940
|
145 |
|
jaroslav@403
|
146 |
/** Whenever model changes a property. It should notify the
|
jtulach@940
|
147 |
* associated technology by calling this method.
|
jaroslav@718
|
148 |
* Since 0.8.3: This method may be called by any thread - it reschedules
|
jaroslav@718
|
149 |
* its actual execution into appropriate one by using
|
jaroslav@718
|
150 |
* {@link BrwsrCtx#execute(java.lang.Runnable)}.
|
jtulach@940
|
151 |
*
|
jaroslav@567
|
152 |
* @param propName name of the changed property
|
jaroslav@403
|
153 |
*/
|
jaroslav@717
|
154 |
public void valueHasMutated(final String propName) {
|
jaroslav@717
|
155 |
context.execute(new Runnable() {
|
jaroslav@717
|
156 |
@Override
|
jaroslav@717
|
157 |
public void run() {
|
jaroslav@717
|
158 |
if (ko != null) {
|
jaroslav@717
|
159 |
ko.valueHasMutated(propName, null, null);
|
jaroslav@717
|
160 |
}
|
jtulach@785
|
161 |
Observers.valueHasMutated(Proto.this, propName);
|
jaroslav@717
|
162 |
}
|
jaroslav@717
|
163 |
});
|
jaroslav@567
|
164 |
}
|
jaroslav@567
|
165 |
|
jaroslav@567
|
166 |
/** Whenever model changes a propertyit should notify the
|
jaroslav@567
|
167 |
* associated technology. Either by calling this method
|
jaroslav@567
|
168 |
* (if the new value is known and different to the old one) or
|
jaroslav@567
|
169 |
* via (slightly ineffective) {@link #valueHasMutated(java.lang.String)}
|
jaroslav@567
|
170 |
* method.
|
jaroslav@718
|
171 |
* Since 0.8.3: This method may be called by any thread - it reschedules
|
jaroslav@718
|
172 |
* its actual execution into appropriate one by using
|
jaroslav@718
|
173 |
* {@link BrwsrCtx#execute(java.lang.Runnable)}.
|
jtulach@940
|
174 |
*
|
jaroslav@567
|
175 |
* @param propName name of the changed property
|
jaroslav@567
|
176 |
* @param oldValue provides previous value of the property
|
jaroslav@567
|
177 |
* @param newValue provides new value of the property
|
jaroslav@567
|
178 |
* @since 0.7.6
|
jaroslav@567
|
179 |
*/
|
jaroslav@717
|
180 |
public void valueHasMutated(
|
jaroslav@717
|
181 |
final String propName, final Object oldValue, final Object newValue
|
jaroslav@717
|
182 |
) {
|
jaroslav@717
|
183 |
context.execute(new Runnable() {
|
jaroslav@717
|
184 |
@Override
|
jaroslav@717
|
185 |
public void run() {
|
jaroslav@717
|
186 |
if (ko != null) {
|
jaroslav@717
|
187 |
ko.valueHasMutated(propName, oldValue, newValue);
|
jaroslav@717
|
188 |
}
|
jtulach@785
|
189 |
Observers.valueHasMutated(Proto.this, propName);
|
jaroslav@717
|
190 |
}
|
jaroslav@717
|
191 |
});
|
jaroslav@373
|
192 |
}
|
jtulach@940
|
193 |
|
jaroslav@403
|
194 |
/** Initializes the associated model in the current {@link #getContext() context}.
|
jtulach@940
|
195 |
* In case of <em>knockout.js</em> technology, applies given bindings
|
jaroslav@403
|
196 |
* of the current model to the <em>body</em> element of the page.
|
jaroslav@403
|
197 |
*/
|
jaroslav@373
|
198 |
public void applyBindings() {
|
jtulach@908
|
199 |
initBindings().applyBindings(null);
|
jtulach@908
|
200 |
}
|
jtulach@940
|
201 |
|
jtulach@908
|
202 |
/** Initializes the associated model to the specified element's subtree.
|
jtulach@908
|
203 |
* The technology is taken from the current {@link #getContext() context} and
|
jtulach@940
|
204 |
* in case of <em>knockout.js</em> applies given bindings
|
jtulach@908
|
205 |
* of the current model to the element of the page with 'id' attribute
|
jtulach@908
|
206 |
* set to the specified <code>id</code> value.
|
jtulach@940
|
207 |
*
|
jtulach@908
|
208 |
* @param id the id of element to apply the binding to
|
jtulach@908
|
209 |
* @since 1.1
|
jtulach@908
|
210 |
* @see Technology.ApplyId
|
jtulach@908
|
211 |
*/
|
jtulach@908
|
212 |
public void applyBindings(String id) {
|
jtulach@908
|
213 |
initBindings().applyBindings(id);
|
jaroslav@374
|
214 |
}
|
jtulach@940
|
215 |
|
jaroslav@403
|
216 |
/** Invokes the provided runnable in the {@link #getContext() context}
|
jaroslav@403
|
217 |
* of the browser. If the caller is already on the right thread, the
|
jtulach@940
|
218 |
* <code>run.run()</code> is invoked immediately and synchronously.
|
jaroslav@403
|
219 |
* Otherwise the method returns immediately and the <code>run()</code>
|
jaroslav@403
|
220 |
* method is performed later
|
jtulach@940
|
221 |
*
|
jaroslav@403
|
222 |
* @param run the action to execute
|
jaroslav@403
|
223 |
*/
|
jaroslav@381
|
224 |
public void runInBrowser(Runnable run) {
|
jaroslav@574
|
225 |
context.execute(run);
|
jaroslav@381
|
226 |
}
|
jaroslav@383
|
227 |
|
jaroslav@598
|
228 |
/** Invokes the specified function index in the {@link #getContext() context}
|
jaroslav@598
|
229 |
* of the browser. If the caller is already on the right thread, the
|
jtulach@940
|
230 |
* index-th function is invoked immediately and synchronously.
|
jaroslav@598
|
231 |
* Otherwise the method returns immediately and the function is invoked
|
jaroslav@598
|
232 |
* later.
|
jtulach@940
|
233 |
*
|
jaroslav@598
|
234 |
* @param index the index of the function as will be passed to
|
jaroslav@598
|
235 |
* {@link Type#call(java.lang.Object, int, java.lang.Object, java.lang.Object)}
|
jaroslav@598
|
236 |
* method
|
jaroslav@598
|
237 |
* @param args array of arguments that will be passed as
|
jaroslav@598
|
238 |
* <code>data</code> argument of the <code>call</code> method.
|
jaroslav@598
|
239 |
* @since 0.7.6
|
jaroslav@598
|
240 |
*/
|
jaroslav@598
|
241 |
public void runInBrowser(final int index, final Object... args) {
|
jaroslav@598
|
242 |
context.execute(new Runnable() {
|
jaroslav@598
|
243 |
@Override
|
jaroslav@598
|
244 |
public void run() {
|
jaroslav@599
|
245 |
try {
|
jaroslav@599
|
246 |
type.call(obj, index, args, null);
|
jaroslav@599
|
247 |
} catch (Exception ex) {
|
jaroslav@599
|
248 |
ex.printStackTrace();
|
jaroslav@599
|
249 |
}
|
jaroslav@598
|
250 |
}
|
jaroslav@598
|
251 |
});
|
jaroslav@598
|
252 |
}
|
jtulach@940
|
253 |
|
jaroslav@403
|
254 |
/** Initializes the provided collection with a content of the <code>array</code>.
|
jtulach@940
|
255 |
* The initialization can only be done soon after the the collection
|
jaroslav@403
|
256 |
* is created, otherwise an exception is throw
|
jtulach@940
|
257 |
*
|
jaroslav@403
|
258 |
* @param to the collection to initialize (assumed to be empty)
|
jaroslav@403
|
259 |
* @param array the array to add to the collection
|
jaroslav@403
|
260 |
* @throws IllegalStateException if the system has already been initialized
|
jaroslav@403
|
261 |
*/
|
jaroslav@383
|
262 |
public void initTo(Collection<?> to, Object array) {
|
jaroslav@383
|
263 |
if (ko != null) {
|
jaroslav@383
|
264 |
throw new IllegalStateException();
|
jaroslav@383
|
265 |
}
|
jaroslav@383
|
266 |
if (to instanceof JSONList) {
|
jaroslav@383
|
267 |
((JSONList)to).init(array);
|
jaroslav@383
|
268 |
} else {
|
jaroslav@383
|
269 |
JSONList.init(to, array);
|
jaroslav@383
|
270 |
}
|
jaroslav@383
|
271 |
}
|
jaroslav@403
|
272 |
|
jaroslav@403
|
273 |
/** Takes an object representing JSON result and extract some of its
|
jaroslav@403
|
274 |
* properties. It is assumed that the <code>props</code> and
|
jaroslav@403
|
275 |
* <code>values</code> arrays have the same length.
|
jtulach@940
|
276 |
*
|
jaroslav@403
|
277 |
* @param json the JSON object (actual type depends on the associated
|
jaroslav@403
|
278 |
* {@link Technology})
|
jaroslav@403
|
279 |
* @param props list of properties to extract
|
jaroslav@403
|
280 |
* @param values array that will be filled with extracted values
|
jaroslav@403
|
281 |
*/
|
jaroslav@385
|
282 |
public void extract(Object json, String[] props, Object[] values) {
|
jaroslav@385
|
283 |
JSON.extract(context, json, props, values);
|
jaroslav@385
|
284 |
}
|
jaroslav@403
|
285 |
|
jaroslav@403
|
286 |
/** Converts raw JSON <code>data</code> into a Java {@link Model} class.
|
jtulach@940
|
287 |
*
|
jaroslav@403
|
288 |
* @param <T> type of the model class
|
jaroslav@403
|
289 |
* @param modelClass the type of the class to create
|
jaroslav@403
|
290 |
* @param data the raw JSON data
|
jaroslav@403
|
291 |
* @return newly created instance of the model class
|
jaroslav@403
|
292 |
*/
|
jaroslav@385
|
293 |
public <T> T read(Class<T> modelClass, Object data) {
|
jaroslav@385
|
294 |
return JSON.read(context, modelClass, data);
|
jaroslav@385
|
295 |
}
|
jaroslav@380
|
296 |
|
jtulach@650
|
297 |
/** Initializes asynchronous JSON connection to specified URL. Delegates
|
jtulach@650
|
298 |
* to {@link #loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...) }
|
jtulach@650
|
299 |
* with no extra parameters.
|
jtulach@940
|
300 |
*
|
jtulach@835
|
301 |
* @param index the callback index to be used when a reply is received
|
jtulach@835
|
302 |
* to call {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object)}.
|
jtulach@940
|
303 |
*
|
jtulach@835
|
304 |
* @param urlBefore the part of the URL before JSON-P callback parameter
|
jtulach@835
|
305 |
* @param urlAfter the rest of the URL or <code>null</code> if no JSON-P is used
|
jtulach@835
|
306 |
* @param method method to use for connection to the server
|
jtulach@835
|
307 |
* @param data string, number or a {@link Model} generated class to send to
|
jtulach@835
|
308 |
* the server when doing a query
|
jtulach@650
|
309 |
*/
|
jtulach@940
|
310 |
public void loadJSON(final int index,
|
jtulach@650
|
311 |
String urlBefore, String urlAfter, String method,
|
jtulach@650
|
312 |
final Object data
|
jtulach@650
|
313 |
) {
|
jtulach@650
|
314 |
loadJSON(index, urlBefore, urlAfter, method, data, new Object[0]);
|
jtulach@650
|
315 |
}
|
jtulach@940
|
316 |
|
jtulach@940
|
317 |
/** Initializes asynchronous JSON connection to specified URL. The
|
jaroslav@407
|
318 |
* method returns immediately and later does callback later.
|
jtulach@940
|
319 |
*
|
jaroslav@407
|
320 |
* @param index the callback index to be used when a reply is received
|
jaroslav@407
|
321 |
* to call {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object)}.
|
jtulach@940
|
322 |
*
|
jaroslav@407
|
323 |
* @param urlBefore the part of the URL before JSON-P callback parameter
|
jaroslav@407
|
324 |
* @param urlAfter the rest of the URL or <code>null</code> if no JSON-P is used
|
jaroslav@407
|
325 |
* @param method method to use for connection to the server
|
jaroslav@407
|
326 |
* @param data string, number or a {@link Model} generated class to send to
|
jaroslav@407
|
327 |
* the server when doing a query
|
jtulach@650
|
328 |
* @param params extra params to pass back when calling
|
jtulach@650
|
329 |
* {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object, java.lang.Object[])}
|
jtulach@650
|
330 |
* @since 0.8.1
|
jaroslav@407
|
331 |
*/
|
jtulach@940
|
332 |
public void loadJSON(final int index,
|
jtulach@940
|
333 |
String urlBefore, String urlAfter, String method,
|
jtulach@940
|
334 |
final Object data, final Object... params
|
jtulach@940
|
335 |
) {
|
jtulach@940
|
336 |
loadJSONWithHeaders(index, null, urlBefore, urlAfter, method, data, params);
|
jtulach@940
|
337 |
}
|
jtulach@940
|
338 |
|
jtulach@940
|
339 |
/** Initializes asynchronous JSON connection to specified URL. The
|
jtulach@940
|
340 |
* method returns immediately and later does callback later.
|
jtulach@940
|
341 |
*
|
jtulach@940
|
342 |
* @param index the callback index to be used when a reply is received
|
jtulach@940
|
343 |
* to call {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object)}.
|
jtulach@940
|
344 |
*
|
jtulach@940
|
345 |
* @param headers headers to use for the request or <code>null</code> to use default ones
|
jtulach@940
|
346 |
* @param urlBefore the part of the URL before JSON-P callback parameter
|
jtulach@940
|
347 |
* @param urlAfter the rest of the URL or <code>null</code> if no JSON-P is used
|
jtulach@940
|
348 |
* @param method method to use for connection to the server
|
jtulach@940
|
349 |
* @param data string, number or a {@link Model} generated class to send to
|
jtulach@940
|
350 |
* the server when doing a query
|
jtulach@940
|
351 |
* @param params extra params to pass back when calling
|
jtulach@940
|
352 |
* {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object, java.lang.Object[])}
|
jtulach@940
|
353 |
* @since 1.2
|
jtulach@940
|
354 |
*/
|
jtulach@940
|
355 |
public void loadJSONWithHeaders(final int index,
|
jtulach@940
|
356 |
String headers,
|
jaroslav@386
|
357 |
String urlBefore, String urlAfter, String method,
|
jtulach@650
|
358 |
final Object data, final Object... params
|
jaroslav@386
|
359 |
) {
|
jaroslav@386
|
360 |
class Rcvr extends RcvrJSON {
|
jaroslav@386
|
361 |
@Override
|
jaroslav@386
|
362 |
protected void onMessage(MsgEvnt msg) {
|
jtulach@650
|
363 |
type.onMessage(obj, index, 1, msg.getValues(), params);
|
jaroslav@386
|
364 |
}
|
jaroslav@386
|
365 |
|
jaroslav@386
|
366 |
@Override
|
jaroslav@386
|
367 |
protected void onError(MsgEvnt msg) {
|
jtulach@650
|
368 |
type.onMessage(obj, index, 2, msg.getException(), params);
|
jaroslav@386
|
369 |
}
|
jaroslav@386
|
370 |
}
|
jtulach@940
|
371 |
JSONCall call = PropertyBindingAccessor.createCall(
|
jtulach@940
|
372 |
context, new Rcvr(), headers, urlBefore, urlAfter, method, data
|
jtulach@940
|
373 |
);
|
jtulach@940
|
374 |
Transfer t = JSON.findTransfer(context);
|
jtulach@940
|
375 |
t.loadJSON(call);
|
jaroslav@386
|
376 |
}
|
jtulach@940
|
377 |
|
jtulach@940
|
378 |
/** Opens new WebSocket connection to the specified URL.
|
jtulach@940
|
379 |
*
|
jtulach@940
|
380 |
* @param index the index to use later during callbacks to
|
jaroslav@407
|
381 |
* {@link Type#onMessage(java.lang.Object, int, int, java.lang.Object)}
|
jaroslav@407
|
382 |
* @param url the <code>ws://</code> or <code>wss://</code> URL to connect to
|
jaroslav@407
|
383 |
* @param data data to send to server (usually <code>null</code>)
|
jaroslav@407
|
384 |
* @return returns a non-null object representing the socket
|
jaroslav@407
|
385 |
* which can be used when calling {@link #wsSend(java.lang.Object, java.lang.String, java.lang.Object) }
|
jaroslav@407
|
386 |
*/
|
jaroslav@387
|
387 |
public Object wsOpen(final int index, String url, Object data) {
|
jaroslav@387
|
388 |
class WSrcvr extends RcvrJSON {
|
jaroslav@387
|
389 |
@Override
|
jaroslav@387
|
390 |
protected void onError(MsgEvnt msg) {
|
jaroslav@387
|
391 |
type.onMessage(obj, index, 2, msg.getException());
|
jaroslav@387
|
392 |
}
|
jtulach@940
|
393 |
|
jaroslav@387
|
394 |
@Override
|
jaroslav@387
|
395 |
protected void onMessage(MsgEvnt msg) {
|
jaroslav@387
|
396 |
type.onMessage(obj, index, 1, msg.getValues());
|
jaroslav@387
|
397 |
}
|
jtulach@940
|
398 |
|
jaroslav@387
|
399 |
@Override
|
jaroslav@387
|
400 |
protected void onClose(MsgEvnt msg) {
|
jaroslav@387
|
401 |
type.onMessage(obj, index, 3, null);
|
jaroslav@387
|
402 |
}
|
jaroslav@387
|
403 |
|
jaroslav@387
|
404 |
@Override
|
jaroslav@387
|
405 |
protected void onOpen(MsgEvnt msg) {
|
jaroslav@387
|
406 |
type.onMessage(obj, index, 0, null);
|
jaroslav@387
|
407 |
}
|
jaroslav@387
|
408 |
}
|
jtulach@940
|
409 |
WS ws = WS.create(JSON.findWSTransfer(context), new WSrcvr());
|
jtulach@940
|
410 |
ws.send(context, null, url, data);
|
jtulach@940
|
411 |
return ws;
|
jaroslav@387
|
412 |
}
|
jtulach@940
|
413 |
|
jaroslav@407
|
414 |
/** Sends a message to existing socket.
|
jtulach@940
|
415 |
*
|
jaroslav@407
|
416 |
* @param webSocket the socket to send message to
|
jaroslav@407
|
417 |
* @param url the <code>ws://</code> or <code>wss://</code> URL to connect to,
|
jtulach@940
|
418 |
* preferably the same as the one used when the socket was
|
jaroslav@407
|
419 |
* {@link #wsOpen(int, java.lang.String, java.lang.Object) opened}
|
jaroslav@407
|
420 |
* @param data the data to send or <code>null</code> if the socket is
|
jaroslav@407
|
421 |
* supposed to be closed
|
jaroslav@407
|
422 |
*/
|
jaroslav@387
|
423 |
public void wsSend(Object webSocket, String url, Object data) {
|
jtulach@940
|
424 |
((JSON.WS)webSocket).send(context, null, url, data);
|
jaroslav@387
|
425 |
}
|
jaroslav@403
|
426 |
|
jaroslav@403
|
427 |
/** Converts raw data (one of its properties) to string representation.
|
jtulach@940
|
428 |
*
|
jaroslav@403
|
429 |
* @param data the object
|
jaroslav@403
|
430 |
* @param propName the name of object property or <code>null</code>
|
jaroslav@403
|
431 |
* if the whole object should be converted
|
jaroslav@403
|
432 |
* @return the string representation of the object or its property
|
jaroslav@403
|
433 |
*/
|
jaroslav@380
|
434 |
public String toString(Object data, String propName) {
|
jaroslav@380
|
435 |
return JSON.toString(context, data, propName);
|
jaroslav@380
|
436 |
}
|
jtulach@940
|
437 |
|
jaroslav@403
|
438 |
/** Converts raw data (one of its properties) to a number representation.
|
jtulach@940
|
439 |
*
|
jaroslav@403
|
440 |
* @param data the object
|
jaroslav@403
|
441 |
* @param propName the name of object property or <code>null</code>
|
jaroslav@403
|
442 |
* if the whole object should be converted
|
jaroslav@403
|
443 |
* @return the number representation of the object or its property
|
jaroslav@403
|
444 |
*/
|
jaroslav@380
|
445 |
public Number toNumber(Object data, String propName) {
|
jaroslav@380
|
446 |
return JSON.toNumber(context, data, propName);
|
jaroslav@380
|
447 |
}
|
jaroslav@407
|
448 |
|
jaroslav@408
|
449 |
/** Converts raw JSON data into a {@link Model} class representation.
|
jtulach@940
|
450 |
*
|
jaroslav@408
|
451 |
* @param <T> type of the model to create
|
jaroslav@408
|
452 |
* @param type class of the model to create
|
jaroslav@408
|
453 |
* @param data raw JSON data (depends on associated {@link Technology})
|
jaroslav@408
|
454 |
* @return new instances of the model class filled with values from the
|
jaroslav@408
|
455 |
* <code>data</code> object
|
jaroslav@408
|
456 |
*/
|
jaroslav@408
|
457 |
public <T> T toModel(Class<T> type, Object data) {
|
jaroslav@408
|
458 |
return JSON.toModel(context, type, data, null);
|
jaroslav@380
|
459 |
}
|
jaroslav@383
|
460 |
|
jaroslav@408
|
461 |
/** Creates new JSON like observable list.
|
jtulach@940
|
462 |
*
|
jaroslav@408
|
463 |
* @param <T> the type of the list elements
|
jaroslav@408
|
464 |
* @param propName name of a property this list is associated with
|
jaroslav@408
|
465 |
* @param onChange index of the property to use when the list is modified
|
jaroslav@408
|
466 |
* during callback to {@link Type#onChange(java.lang.Object, int)}
|
jaroslav@408
|
467 |
* @param dependingProps the array of {@link ComputedProperty derived properties}
|
jaroslav@408
|
468 |
* that depend on the value of the list
|
jaroslav@408
|
469 |
* @return new, empty list associated with this proto-object and its model
|
jaroslav@408
|
470 |
*/
|
jaroslav@383
|
471 |
public <T> List<T> createList(String propName, int onChange, String... dependingProps) {
|
jaroslav@383
|
472 |
return new JSONList<T>(this, propName, onChange, dependingProps);
|
jaroslav@383
|
473 |
}
|
jaroslav@408
|
474 |
|
jaroslav@408
|
475 |
/** Copies content of one collection to another, re-assigning all its
|
jaroslav@408
|
476 |
* elements from their current context to the new <code>ctx</code>.
|
jtulach@940
|
477 |
*
|
jaroslav@408
|
478 |
* @param <T> type of the collections
|
jaroslav@408
|
479 |
* @param to the target collection to be filled with cloned values
|
jaroslav@408
|
480 |
* @param ctx context for the new collection
|
jaroslav@408
|
481 |
* @param from original collection with its data
|
jaroslav@408
|
482 |
*/
|
jaroslav@383
|
483 |
public <T> void cloneList(Collection<T> to, BrwsrCtx ctx, Collection<T> from) {
|
jaroslav@383
|
484 |
Boolean isModel = null;
|
jaroslav@383
|
485 |
for (T t : from) {
|
jaroslav@383
|
486 |
if (isModel == null) {
|
jaroslav@383
|
487 |
isModel = JSON.isModel(t.getClass());
|
jaroslav@383
|
488 |
}
|
jaroslav@383
|
489 |
if (isModel) {
|
jaroslav@383
|
490 |
to.add(JSON.bindTo(t, ctx));
|
jaroslav@383
|
491 |
} else {
|
jaroslav@383
|
492 |
to.add(t);
|
jaroslav@383
|
493 |
}
|
jaroslav@383
|
494 |
}
|
jaroslav@383
|
495 |
}
|
jtulach@940
|
496 |
|
jaroslav@406
|
497 |
//
|
jaroslav@406
|
498 |
// internal state
|
jaroslav@406
|
499 |
//
|
jtulach@940
|
500 |
|
jtulach@779
|
501 |
final String toStr() {
|
jtulach@779
|
502 |
return "Proto[" + obj + "]@" + Integer.toHexString(System.identityHashCode(this));
|
jtulach@779
|
503 |
}
|
jtulach@940
|
504 |
|
jaroslav@406
|
505 |
final Bindings initBindings() {
|
jaroslav@406
|
506 |
if (ko == null) {
|
jaroslav@406
|
507 |
Bindings b = Bindings.apply(context, obj);
|
jaroslav@406
|
508 |
PropertyBinding[] pb = new PropertyBinding[type.propertyNames.length];
|
jaroslav@406
|
509 |
for (int i = 0; i < pb.length; i++) {
|
jaroslav@406
|
510 |
pb[i] = b.registerProperty(
|
jaroslav@406
|
511 |
type.propertyNames[i], i, obj, type, type.propertyReadOnly[i]
|
jaroslav@406
|
512 |
);
|
jaroslav@406
|
513 |
}
|
jaroslav@406
|
514 |
FunctionBinding[] fb = new FunctionBinding[type.functions.length];
|
jaroslav@406
|
515 |
for (int i = 0; i < fb.length; i++) {
|
jaroslav@406
|
516 |
fb[i] = FunctionBinding.registerFunction(
|
jaroslav@406
|
517 |
type.functions[i], i, obj, type
|
jaroslav@406
|
518 |
);
|
jaroslav@406
|
519 |
}
|
jaroslav@406
|
520 |
ko = b;
|
jaroslav@406
|
521 |
b.finish(obj, pb, fb);
|
jaroslav@406
|
522 |
}
|
jaroslav@406
|
523 |
return ko;
|
jaroslav@406
|
524 |
}
|
jaroslav@406
|
525 |
|
jaroslav@406
|
526 |
final Bindings getBindings() {
|
jaroslav@406
|
527 |
return ko;
|
jaroslav@406
|
528 |
}
|
jaroslav@406
|
529 |
|
jaroslav@406
|
530 |
final void onChange(int index) {
|
jaroslav@406
|
531 |
type.onChange(obj, index);
|
jaroslav@406
|
532 |
}
|
jaroslav@386
|
533 |
|
jtulach@785
|
534 |
final Observers observers(boolean create) {
|
jtulach@785
|
535 |
if (create && observers == null) {
|
jtulach@785
|
536 |
observers = new Observers();
|
jtulach@785
|
537 |
}
|
jtulach@783
|
538 |
return observers;
|
jtulach@774
|
539 |
}
|
jtulach@774
|
540 |
|
jaroslav@373
|
541 |
/** Functionality used by the code generated by annotation
|
jaroslav@373
|
542 |
* processor for the {@link net.java.html.json.Model} annotation.
|
jtulach@940
|
543 |
*
|
jaroslav@373
|
544 |
* @param <Model> the generated class
|
jaroslav@373
|
545 |
* @since 0.7
|
jaroslav@373
|
546 |
*/
|
jaroslav@373
|
547 |
public static abstract class Type<Model> {
|
jaroslav@373
|
548 |
private final Class<Model> clazz;
|
jaroslav@373
|
549 |
private final String[] propertyNames;
|
jaroslav@373
|
550 |
private final boolean[] propertyReadOnly;
|
jaroslav@373
|
551 |
private final String[] functions;
|
jaroslav@373
|
552 |
|
jaroslav@409
|
553 |
/** Constructor for subclasses generated by the annotation processor
|
jaroslav@409
|
554 |
* associated with {@link net.java.html.json.Model} annotation.
|
jtulach@940
|
555 |
*
|
jaroslav@409
|
556 |
* @param clazz the generated model class
|
jaroslav@409
|
557 |
* @param modelFor the original class annotated by the {@link net.java.html.json.Model} annotation.
|
jaroslav@409
|
558 |
* @param properties number of properties the class has
|
jaroslav@409
|
559 |
* @param functions number of functions the class has
|
jaroslav@409
|
560 |
*/
|
jaroslav@373
|
561 |
protected Type(
|
jaroslav@373
|
562 |
Class<Model> clazz, Class<?> modelFor, int properties, int functions
|
jaroslav@373
|
563 |
) {
|
jaroslav@374
|
564 |
assert getClass().getName().endsWith("$Html4JavaType");
|
jaroslav@420
|
565 |
try {
|
jtulach@795
|
566 |
assert clazz == modelFor || getClass().getDeclaringClass() == clazz;
|
jaroslav@420
|
567 |
} catch (SecurityException ex) {
|
jaroslav@420
|
568 |
// OK, no check
|
jaroslav@420
|
569 |
}
|
jaroslav@373
|
570 |
this.clazz = clazz;
|
jaroslav@373
|
571 |
this.propertyNames = new String[properties];
|
jaroslav@373
|
572 |
this.propertyReadOnly = new boolean[properties];
|
jaroslav@373
|
573 |
this.functions = new String[functions];
|
jaroslav@374
|
574 |
JSON.register(clazz, this);
|
jaroslav@373
|
575 |
}
|
jaroslav@409
|
576 |
|
jaroslav@409
|
577 |
/** Registers property for the type. It is expected each index
|
jaroslav@409
|
578 |
* is initialized only once.
|
jtulach@940
|
579 |
*
|
jaroslav@409
|
580 |
* @param name name of the property
|
jaroslav@409
|
581 |
* @param index index of the property
|
jaroslav@409
|
582 |
* @param readOnly is the property read only?
|
jaroslav@409
|
583 |
*/
|
jaroslav@373
|
584 |
protected final void registerProperty(String name, int index, boolean readOnly) {
|
jaroslav@373
|
585 |
assert propertyNames[index] == null;
|
jaroslav@373
|
586 |
propertyNames[index] = name;
|
jaroslav@373
|
587 |
propertyReadOnly[index] = readOnly;
|
jaroslav@373
|
588 |
}
|
jaroslav@409
|
589 |
|
jaroslav@409
|
590 |
/** Registers function of given name at given index.
|
jtulach@940
|
591 |
*
|
jaroslav@409
|
592 |
* @param name name of the function
|
jaroslav@409
|
593 |
* @param index name of the type
|
jaroslav@409
|
594 |
*/
|
jaroslav@373
|
595 |
protected final void registerFunction(String name, int index) {
|
jaroslav@373
|
596 |
assert functions[index] == null;
|
jaroslav@373
|
597 |
functions[index] = name;
|
jaroslav@373
|
598 |
}
|
jtulach@940
|
599 |
|
jaroslav@410
|
600 |
/** Creates new proto-object for given {@link Model} class bound to
|
jaroslav@410
|
601 |
* provided context.
|
jtulach@940
|
602 |
*
|
jaroslav@410
|
603 |
* @param obj instance of appropriate {@link Model} class
|
jaroslav@410
|
604 |
* @param context the browser context
|
jaroslav@410
|
605 |
* @return new proto-object that the generated class can use for
|
jaroslav@410
|
606 |
* communication with the infrastructure
|
jaroslav@410
|
607 |
*/
|
jaroslav@410
|
608 |
public Proto createProto(Object obj, BrwsrCtx context) {
|
jaroslav@374
|
609 |
return new Proto(obj, this, context);
|
jaroslav@373
|
610 |
}
|
jtulach@940
|
611 |
|
jaroslav@412
|
612 |
//
|
jaroslav@412
|
613 |
// Implemented by subclasses
|
jaroslav@412
|
614 |
//
|
jtulach@940
|
615 |
|
jaroslav@412
|
616 |
/** Sets value of a {@link #registerProperty(java.lang.String, int, boolean) registered property}
|
jaroslav@412
|
617 |
* to new value.
|
jtulach@940
|
618 |
*
|
jaroslav@412
|
619 |
* @param model the instance of {@link Model model class}
|
jaroslav@412
|
620 |
* @param index index of the property used during registration
|
jaroslav@412
|
621 |
* @param value the value to set the property to
|
jaroslav@412
|
622 |
*/
|
jaroslav@412
|
623 |
protected abstract void setValue(Model model, int index, Object value);
|
jtulach@940
|
624 |
|
jtulach@940
|
625 |
/** Obtains and returns value of a
|
jaroslav@412
|
626 |
* {@link #registerProperty(java.lang.String, int, boolean) registered property}.
|
jtulach@940
|
627 |
*
|
jaroslav@412
|
628 |
* @param model the instance of {@link Model model class}
|
jaroslav@412
|
629 |
* @param index index of the property used during registration
|
jaroslav@412
|
630 |
* @return current value of the property
|
jaroslav@412
|
631 |
*/
|
jaroslav@412
|
632 |
protected abstract Object getValue(Model model, int index);
|
jtulach@940
|
633 |
|
jaroslav@419
|
634 |
/** Invokes a {@link #registerFunction(java.lang.String, int)} registered function
|
jaroslav@412
|
635 |
* on given object.
|
jtulach@940
|
636 |
*
|
jaroslav@412
|
637 |
* @param model the instance of {@link Model model class}
|
jaroslav@412
|
638 |
* @param index index of the property used during registration
|
jaroslav@412
|
639 |
* @param data the currently selected object the function is about to operate on
|
jaroslav@412
|
640 |
* @param event the event that triggered the event
|
jaroslav@599
|
641 |
* @throws Exception the method can throw exception which is then logged
|
jaroslav@412
|
642 |
*/
|
jaroslav@599
|
643 |
protected abstract void call(Model model, int index, Object data, Object event)
|
jaroslav@599
|
644 |
throws Exception;
|
jtulach@940
|
645 |
|
jaroslav@412
|
646 |
/** Re-binds the model object to new browser context.
|
jtulach@940
|
647 |
*
|
jaroslav@412
|
648 |
* @param model the instance of {@link Model model class}
|
jaroslav@412
|
649 |
* @param ctx browser context to clone the object to
|
jaroslav@412
|
650 |
* @return new instance of the model suitable for new context
|
jaroslav@412
|
651 |
*/
|
jaroslav@412
|
652 |
protected abstract Model cloneTo(Model model, BrwsrCtx ctx);
|
jtulach@940
|
653 |
|
jaroslav@412
|
654 |
/** Reads raw JSON data and converts them to our model class.
|
jtulach@940
|
655 |
*
|
jaroslav@412
|
656 |
* @param c the browser context to work in
|
jaroslav@412
|
657 |
* @param json raw JSON data to get values from
|
jaroslav@412
|
658 |
* @return new instance of model class filled by the data
|
jaroslav@412
|
659 |
*/
|
jaroslav@412
|
660 |
protected abstract Model read(BrwsrCtx c, Object json);
|
jtulach@940
|
661 |
|
jaroslav@412
|
662 |
/** Called when a {@link #registerProperty(java.lang.String, int, boolean) registered property}
|
jaroslav@412
|
663 |
* changes its value.
|
jtulach@940
|
664 |
*
|
jaroslav@412
|
665 |
* @param model the object that has the property
|
jaroslav@412
|
666 |
* @param index the index of the property during registration
|
jaroslav@412
|
667 |
*/
|
jaroslav@412
|
668 |
protected abstract void onChange(Model model, int index);
|
jtulach@940
|
669 |
|
jaroslav@412
|
670 |
/** Finds out if there is an associated proto-object for given
|
jaroslav@412
|
671 |
* object.
|
jtulach@940
|
672 |
*
|
jaroslav@412
|
673 |
* @param object an object, presumably (but not necessarily) instance of Model class
|
jaroslav@412
|
674 |
* @return associated proto-object or <code>null</code>
|
jaroslav@412
|
675 |
*/
|
jaroslav@412
|
676 |
protected abstract Proto protoFor(Object object);
|
jaroslav@386
|
677 |
|
jtulach@940
|
678 |
/** Called to report results of asynchronous over-the-wire
|
jaroslav@413
|
679 |
* communication. Result of calling {@link Proto#wsOpen(int, java.lang.String, java.lang.Object)}
|
jtulach@650
|
680 |
* or {@link Proto#loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...)}.
|
jtulach@940
|
681 |
*
|
jaroslav@413
|
682 |
* @param model the instance of the model class
|
jaroslav@413
|
683 |
* @param index index used during initiating the communication (via <code>loadJSON</code> or <code>wsOpen</code> calls)
|
jaroslav@413
|
684 |
* @param type type of the message: 0 - onOpen, 1 - onMessage, 2 - onError, 3 - onClose -
|
jaroslav@413
|
685 |
* not all messages are applicable to all communication protocols (JSON has only 1 and 2).
|
jaroslav@413
|
686 |
* @param data <code>null</code> or string, number or a {@link Model} class
|
jaroslav@413
|
687 |
* obtained to the server as a response
|
jaroslav@386
|
688 |
*/
|
jtulach@650
|
689 |
protected void onMessage(Model model, int index, int type, Object data) {
|
jtulach@650
|
690 |
onMessage(model, index, type, data, new Object[0]);
|
jtulach@650
|
691 |
}
|
jtulach@940
|
692 |
|
jtulach@940
|
693 |
/** Called to report results of asynchronous over-the-wire
|
jtulach@650
|
694 |
* communication. Result of calling {@link Proto#wsOpen(int, java.lang.String, java.lang.Object)}
|
jtulach@650
|
695 |
* or {@link Proto#loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...)}.
|
jtulach@940
|
696 |
*
|
jtulach@650
|
697 |
* @param model the instance of the model class
|
jtulach@650
|
698 |
* @param index index used during initiating the communication (via <code>loadJSON</code> or <code>wsOpen</code> calls)
|
jtulach@650
|
699 |
* @param type type of the message: 0 - onOpen, 1 - onMessage, 2 - onError, 3 - onClose -
|
jtulach@650
|
700 |
* not all messages are applicable to all communication protocols (JSON has only 1 and 2).
|
jtulach@650
|
701 |
* @param data <code>null</code> or string, number or a {@link Model} class
|
jtulach@650
|
702 |
* obtained to the server as a response
|
jtulach@650
|
703 |
* @param params extra parameters as passed for example to
|
jtulach@650
|
704 |
* {@link Proto#loadJSON(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object...)}
|
jtulach@650
|
705 |
* method
|
jtulach@650
|
706 |
* @since 0.8.1
|
jtulach@650
|
707 |
*/
|
jtulach@650
|
708 |
protected void onMessage(Model model, int index, int type, Object data, Object[] params) {
|
jtulach@650
|
709 |
onMessage(model, index, type, data);
|
jtulach@650
|
710 |
}
|
jaroslav@386
|
711 |
|
jaroslav@411
|
712 |
//
|
jaroslav@411
|
713 |
// Various support methods the generated classes use
|
jaroslav@411
|
714 |
//
|
jaroslav@411
|
715 |
|
jaroslav@411
|
716 |
/** Converts and array of raw JSON objects into an array of typed
|
jaroslav@532
|
717 |
* Java {@link Model} classes.
|
jtulach@940
|
718 |
*
|
jaroslav@411
|
719 |
* @param <T> the type of the destination array
|
jaroslav@411
|
720 |
* @param context browser context to use
|
jaroslav@411
|
721 |
* @param src array of raw JSON objects
|
jaroslav@411
|
722 |
* @param destType type of the individual array elements
|
jaroslav@411
|
723 |
* @param dest array to be filled with read type instances
|
jaroslav@411
|
724 |
*/
|
jaroslav@386
|
725 |
public <T> void copyJSON(BrwsrCtx context, Object[] src, Class<T> destType, T[] dest) {
|
jaroslav@386
|
726 |
for (int i = 0; i < src.length && i < dest.length; i++) {
|
jaroslav@386
|
727 |
dest[i] = org.netbeans.html.json.impl.JSON.read(context, destType, src[i]);
|
jaroslav@386
|
728 |
}
|
jaroslav@386
|
729 |
}
|
jtulach@940
|
730 |
|
jaroslav@380
|
731 |
/** Compares two objects that can be converted to integers.
|
jaroslav@449
|
732 |
* @param a first value
|
jaroslav@449
|
733 |
* @param b second value
|
jaroslav@380
|
734 |
* @return true if they are the same
|
jaroslav@380
|
735 |
*/
|
jaroslav@380
|
736 |
public final boolean isSame(int a, int b) {
|
jaroslav@380
|
737 |
return a == b;
|
jaroslav@380
|
738 |
}
|
jaroslav@380
|
739 |
|
jaroslav@380
|
740 |
/** Compares two objects that can be converted to (floating point)
|
jaroslav@380
|
741 |
* numbers.
|
jaroslav@449
|
742 |
* @param a first value
|
jaroslav@449
|
743 |
* @param b second value
|
jaroslav@380
|
744 |
* @return true if they are the same
|
jaroslav@380
|
745 |
*/
|
jaroslav@380
|
746 |
public final boolean isSame(double a, double b) {
|
jaroslav@380
|
747 |
return a == b;
|
jaroslav@380
|
748 |
}
|
jaroslav@380
|
749 |
|
jaroslav@380
|
750 |
/** Compares two objects for being the same - e.g. either <code>==</code>
|
jaroslav@380
|
751 |
* or <code>equals</code>.
|
jaroslav@449
|
752 |
* @param a first value
|
jaroslav@449
|
753 |
* @param b second value
|
jaroslav@380
|
754 |
* @return true if they are equals
|
jtulach@940
|
755 |
*/
|
jaroslav@380
|
756 |
public final boolean isSame(Object a, Object b) {
|
jaroslav@380
|
757 |
if (a == b) {
|
jaroslav@380
|
758 |
return true;
|
jaroslav@380
|
759 |
}
|
jaroslav@380
|
760 |
if (a == null || b == null) {
|
jaroslav@380
|
761 |
return false;
|
jaroslav@380
|
762 |
}
|
jaroslav@380
|
763 |
return a.equals(b);
|
jaroslav@380
|
764 |
}
|
jaroslav@380
|
765 |
|
jaroslav@380
|
766 |
/** Cumulative hash function. Adds hashcode of the object to the
|
jaroslav@380
|
767 |
* previous value.
|
jaroslav@380
|
768 |
* @param o the object (or <code>null</code>)
|
jaroslav@380
|
769 |
* @param h the previous value of the hash
|
jaroslav@380
|
770 |
* @return new hash - the old one xor the object's one
|
jaroslav@380
|
771 |
*/
|
jaroslav@380
|
772 |
public final int hashPlus(Object o, int h) {
|
jaroslav@380
|
773 |
return o == null ? h : h ^ o.hashCode();
|
jaroslav@380
|
774 |
}
|
jtulach@940
|
775 |
|
jaroslav@380
|
776 |
/** Converts an object to its JSON value.
|
jtulach@940
|
777 |
*
|
jaroslav@380
|
778 |
* @param obj the object to convert
|
jaroslav@380
|
779 |
* @return JSON representation of the object
|
jaroslav@380
|
780 |
*/
|
jaroslav@380
|
781 |
public final String toJSON(Object obj) {
|
jaroslav@380
|
782 |
return JSON.toJSON(obj);
|
jaroslav@380
|
783 |
}
|
jtulach@940
|
784 |
|
jaroslav@380
|
785 |
/** Converts the value to string.
|
jtulach@940
|
786 |
*
|
jaroslav@380
|
787 |
* @param val the value
|
jaroslav@380
|
788 |
* @return the converted value
|
jaroslav@380
|
789 |
*/
|
jaroslav@380
|
790 |
public final String stringValue(Object val) {
|
jaroslav@380
|
791 |
return JSON.stringValue(val);
|
jaroslav@380
|
792 |
}
|
jaroslav@380
|
793 |
|
jaroslav@380
|
794 |
/** Converts the value to number.
|
jtulach@940
|
795 |
*
|
jaroslav@380
|
796 |
* @param val the value
|
jaroslav@380
|
797 |
* @return the converted value
|
jaroslav@380
|
798 |
*/
|
jaroslav@380
|
799 |
public final Number numberValue(Object val) {
|
jaroslav@380
|
800 |
return JSON.numberValue(val);
|
jaroslav@380
|
801 |
}
|
jaroslav@380
|
802 |
|
jaroslav@380
|
803 |
/** Converts the value to character.
|
jtulach@940
|
804 |
*
|
jaroslav@380
|
805 |
* @param val the value
|
jaroslav@380
|
806 |
* @return the converted value
|
jaroslav@380
|
807 |
*/
|
jaroslav@380
|
808 |
public final Character charValue(Object val) {
|
jaroslav@380
|
809 |
return JSON.charValue(val);
|
jaroslav@380
|
810 |
}
|
jaroslav@380
|
811 |
|
jaroslav@380
|
812 |
/** Converts the value to boolean.
|
jtulach@940
|
813 |
*
|
jaroslav@380
|
814 |
* @param val the value
|
jaroslav@380
|
815 |
* @return the converted value
|
jaroslav@380
|
816 |
*/
|
jaroslav@380
|
817 |
public final Boolean boolValue(Object val) {
|
jaroslav@380
|
818 |
return JSON.boolValue(val);
|
jaroslav@380
|
819 |
}
|
jtulach@940
|
820 |
|
jaroslav@380
|
821 |
/** Extracts value of specific type from given object.
|
jtulach@940
|
822 |
*
|
jaroslav@380
|
823 |
* @param <T> the type of object one is interested in
|
jaroslav@380
|
824 |
* @param type the type
|
jaroslav@380
|
825 |
* @param val the object to convert to type
|
jaroslav@380
|
826 |
* @return the converted value
|
jaroslav@380
|
827 |
*/
|
jaroslav@380
|
828 |
public final <T> T extractValue(Class<T> type, Object val) {
|
jaroslav@380
|
829 |
if (Number.class.isAssignableFrom(type)) {
|
jaroslav@380
|
830 |
val = numberValue(val);
|
jaroslav@380
|
831 |
}
|
jaroslav@380
|
832 |
if (Boolean.class == type) {
|
jaroslav@380
|
833 |
val = boolValue(val);
|
jaroslav@380
|
834 |
}
|
jaroslav@380
|
835 |
if (String.class == type) {
|
jaroslav@380
|
836 |
val = stringValue(val);
|
jaroslav@380
|
837 |
}
|
jaroslav@380
|
838 |
if (Character.class == type) {
|
jaroslav@380
|
839 |
val = charValue(val);
|
jaroslav@380
|
840 |
}
|
jaroslav@380
|
841 |
if (Integer.class == type) {
|
jaroslav@380
|
842 |
val = val instanceof Number ? ((Number) val).intValue() : 0;
|
jaroslav@380
|
843 |
}
|
jaroslav@380
|
844 |
if (Long.class == type) {
|
jaroslav@380
|
845 |
val = val instanceof Number ? ((Number) val).longValue() : 0;
|
jaroslav@380
|
846 |
}
|
jaroslav@380
|
847 |
if (Short.class == type) {
|
jaroslav@380
|
848 |
val = val instanceof Number ? ((Number) val).shortValue() : 0;
|
jaroslav@380
|
849 |
}
|
jaroslav@380
|
850 |
if (Byte.class == type) {
|
jaroslav@380
|
851 |
val = val instanceof Number ? ((Number) val).byteValue() : 0;
|
jaroslav@380
|
852 |
}
|
jaroslav@380
|
853 |
if (Double.class == type) {
|
jaroslav@380
|
854 |
val = val instanceof Number ? ((Number) val).doubleValue() : Double.NaN;
|
jaroslav@380
|
855 |
}
|
jaroslav@380
|
856 |
if (Float.class == type) {
|
jaroslav@380
|
857 |
val = val instanceof Number ? ((Number) val).floatValue() : Float.NaN;
|
jaroslav@380
|
858 |
}
|
jtulach@755
|
859 |
if (type.isEnum() && val instanceof String) {
|
jtulach@755
|
860 |
val = Enum.valueOf(type.asSubclass(Enum.class), (String)val);
|
jtulach@755
|
861 |
}
|
jaroslav@380
|
862 |
return type.cast(val);
|
jaroslav@380
|
863 |
}
|
jaroslav@384
|
864 |
|
jtulach@832
|
865 |
/** Special dealing with array & {@link List} values. This method
|
jtulach@824
|
866 |
* takes the provided collection, empties it and fills it again
|
jtulach@824
|
867 |
* with values extracted from <code>value</code> (which is supposed
|
jtulach@824
|
868 |
* to be an array).
|
jtulach@940
|
869 |
*
|
jtulach@824
|
870 |
* @param <T> the type of list elements
|
jtulach@824
|
871 |
* @param arr collection to fill with elements in value
|
jtulach@824
|
872 |
* @param type the type of elements in the collection
|
jtulach@824
|
873 |
* @param value array of elements to put into the collecition. If
|
jtulach@824
|
874 |
* value is not an array it is wrapped into array with only element
|
jtulach@824
|
875 |
* @since 1.0
|
jtulach@824
|
876 |
*/
|
jtulach@824
|
877 |
public final <T> void replaceValue(Collection<? super T> arr, Class<T> type, Object value) {
|
jtulach@824
|
878 |
Object[] newArr;
|
jtulach@824
|
879 |
if (value instanceof Object[]) {
|
jtulach@824
|
880 |
newArr = (Object[]) value;
|
jtulach@824
|
881 |
} else {
|
jtulach@824
|
882 |
newArr = new Object[] { value };
|
jtulach@824
|
883 |
}
|
jtulach@970
|
884 |
List<T> tmp = new ArrayList<T>(newArr.length);
|
jtulach@824
|
885 |
for (Object e : newArr) {
|
jtulach@970
|
886 |
tmp.add(extractValue(type, e));
|
jtulach@970
|
887 |
}
|
jtulach@970
|
888 |
if (arr instanceof JSONList) {
|
jtulach@970
|
889 |
JSONList jsList = (JSONList) arr;
|
jtulach@970
|
890 |
jsList.fastReplace(tmp);
|
jtulach@970
|
891 |
} else {
|
jtulach@970
|
892 |
arr.clear();
|
jtulach@970
|
893 |
arr.addAll(tmp);
|
jtulach@824
|
894 |
}
|
jtulach@824
|
895 |
}
|
jaroslav@373
|
896 |
}
|
jaroslav@373
|
897 |
}
|