jtulach@0
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@0
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jtulach@0
|
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.
|
jtulach@0
|
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.
|
jtulach@0
|
42 |
*/
|
jtulach@0
|
43 |
package net.java.html.json;
|
jtulach@0
|
44 |
|
jtulach@0
|
45 |
import java.lang.annotation.ElementType;
|
jtulach@0
|
46 |
import java.lang.annotation.Retention;
|
jtulach@0
|
47 |
import java.lang.annotation.RetentionPolicy;
|
jtulach@0
|
48 |
import java.lang.annotation.Target;
|
jaroslav@232
|
49 |
import java.net.URL;
|
jtulach@788
|
50 |
import java.util.List;
|
jtulach@0
|
51 |
|
jaroslav@232
|
52 |
/** Defines a model class that represents a single
|
jtulach@881
|
53 |
* <a target="_blank" href="http://en.wikipedia.org/wiki/JSON">JSON</a>-like object
|
jaroslav@232
|
54 |
* named {@link #className()}. The generated class contains
|
jaroslav@232
|
55 |
* getters and setters for properties defined via {@link #properties()} and
|
jaroslav@232
|
56 |
* getters for other, derived properties defined by annotating methods
|
jaroslav@232
|
57 |
* of this class by {@link ComputedProperty}. Each property
|
jaroslav@232
|
58 |
* can be of primitive type, an {@link Enum enum type} or (in order to create
|
jtulach@881
|
59 |
* nested <a target="_blank" href="http://en.wikipedia.org/wiki/JSON">JSON</a> structure)
|
jaroslav@232
|
60 |
* of another {@link Model class generated by @Model} annotation. Each property
|
jaroslav@232
|
61 |
* can either represent a single value or be an array of its values.
|
jtulach@0
|
62 |
* <p>
|
jaroslav@232
|
63 |
* The {@link #className() generated class}'s <code>toString</code> method
|
jtulach@0
|
64 |
* converts the state of the object into
|
jtulach@881
|
65 |
* <a target="_blank" href="http://en.wikipedia.org/wiki/JSON">JSON</a> format. One can
|
jaroslav@232
|
66 |
* use {@link Models#parse(net.java.html.BrwsrCtx, java.lang.Class, java.io.InputStream)}
|
jaroslav@232
|
67 |
* method to read the JSON text stored in a file or other stream back into the Java model.
|
jaroslav@232
|
68 |
* One can also use {@link OnReceive @OnReceive} annotation to read the model
|
jaroslav@232
|
69 |
* asynchronously from a {@link URL}.
|
jaroslav@32
|
70 |
* <p>
|
jaroslav@229
|
71 |
* An example where one defines class <code>Person</code> with four
|
jaroslav@229
|
72 |
* properties (<code>firstName</code>, <code>lastName</code>, array of <code>addresses</code> and
|
jaroslav@32
|
73 |
* <code>fullName</code>) follows:
|
jaroslav@32
|
74 |
* <pre>
|
jaroslav@32
|
75 |
* {@link Model @Model}(className="Person", properties={
|
jtulach@791
|
76 |
* {@link Property @Property}(name = "firstName", type=String.<b>class</b>),
|
jtulach@791
|
77 |
* {@link Property @Property}(name = "lastName", type=String.<b>class</b>)
|
jtulach@791
|
78 |
* {@link Property @Property}(name = "addresses", type=Address.<b>class</b>, array = <b>true</b>)
|
jaroslav@32
|
79 |
* })
|
jtulach@791
|
80 |
* <b>static class</b> PersonModel {
|
jaroslav@32
|
81 |
* {@link ComputedProperty @ComputedProperty}
|
jtulach@791
|
82 |
* <b>static</b> String fullName(String firstName, String lastName) {
|
jtulach@791
|
83 |
* <b>return</b> firstName + " " + lastName;
|
jaroslav@32
|
84 |
* }
|
jaroslav@229
|
85 |
*
|
jtulach@788
|
86 |
* {@link ComputedProperty @ComputedProperty}
|
jtulach@791
|
87 |
* <b>static</b> String mainAddress({@link List List<Address>} addresses) {
|
jtulach@791
|
88 |
* <b>for</b> (Address a : addresses) {
|
jtulach@791
|
89 |
* <b>return</b> a.getStreet() + " " + a.getTown();
|
jtulach@788
|
90 |
* }
|
jtulach@791
|
91 |
* <b>return</b> "No address";
|
jtulach@788
|
92 |
* }
|
jtulach@788
|
93 |
*
|
jaroslav@229
|
94 |
* {@link Model @Model}(className="Address", properties={
|
jtulach@791
|
95 |
* {@link Property @Property}(name = "street", type=String.<b>class</b>),
|
jtulach@791
|
96 |
* {@link Property @Property}(name = "town", type=String.<b>class</b>)
|
jaroslav@229
|
97 |
* })
|
jtulach@791
|
98 |
* <b>static class</b> AddressModel {
|
jaroslav@229
|
99 |
* }
|
jaroslav@229
|
100 |
* }
|
jaroslav@229
|
101 |
* </pre>
|
jaroslav@232
|
102 |
* The generated model class has a default constructor, and also <em>quick
|
jaroslav@232
|
103 |
* instantiation</em> constructor that accepts all non-array properties
|
jaroslav@232
|
104 |
* (in the order used in the {@link #properties()} attribute) and vararg list
|
jaroslav@232
|
105 |
* for the first array property (if any). One can thus use following code
|
jaroslav@232
|
106 |
* to create an instance of the Person and Address classes:
|
jaroslav@229
|
107 |
* <pre>
|
jtulach@791
|
108 |
* Person p = <b>new</b> Person("Jaroslav", "Tulach",
|
jtulach@791
|
109 |
* <b>new</b> Address("Markoušovice", "Úpice"),
|
jtulach@791
|
110 |
* <b>new</b> Address("V Parku", "Praha")
|
jaroslav@232
|
111 |
* );
|
jtulach@881
|
112 |
* // p.toString() then returns equivalent of following <a target="_blank" href="http://en.wikipedia.org/wiki/JSON">JSON</a> object
|
jaroslav@229
|
113 |
* {
|
jaroslav@229
|
114 |
* "firstName" : "Jaroslav",
|
jaroslav@229
|
115 |
* "lastName" : "Tulach",
|
jaroslav@229
|
116 |
* "addresses" : [
|
jaroslav@232
|
117 |
* { "street" : "Markoušovice", "town" : "Úpice" },
|
jaroslav@232
|
118 |
* { "street" : "V Parku", "town" : "Praha" },
|
jaroslav@229
|
119 |
* ]
|
jaroslav@32
|
120 |
* }
|
jaroslav@32
|
121 |
* </pre>
|
jaroslav@232
|
122 |
* <p>
|
jtulach@881
|
123 |
* In case you are using <a target="_blank" href="http://knockoutjs.com/">Knockout technology</a>
|
jaroslav@232
|
124 |
* for Java then you can associate such model object with surrounding HTML page by
|
jtulach@921
|
125 |
* calling: <code>p.applyBindings();</code> (in case you specify {@link #targetId()}.
|
jtulach@921
|
126 |
* The page can then use regular
|
jtulach@881
|
127 |
* <a target="_blank" href="http://knockoutjs.com/">Knockout</a> bindings to reference your
|
jaroslav@232
|
128 |
* model and create dynamic connection between your model in Java and
|
jaroslav@232
|
129 |
* live DOM structure in the browser:
|
jtulach@904
|
130 |
* </p>
|
jaroslav@232
|
131 |
* <pre>
|
jaroslav@232
|
132 |
* Name: <span data-bind="text: fullName">
|
jaroslav@232
|
133 |
* <div data-bind="foreach: addresses">
|
jaroslav@232
|
134 |
* Lives in <span data-bind="text: town"/>
|
jaroslav@232
|
135 |
* </div>
|
jaroslav@232
|
136 |
* </pre>
|
jtulach@881
|
137 |
*
|
jtulach@904
|
138 |
* <h3>Access Raw <a target="_blank" href="http://knockoutjs.com/">Knockout</a> Observables</h3>
|
jtulach@881
|
139 |
*
|
jtulach@904
|
140 |
* One can obtain <em>raw</em> JavaScript object representing the
|
jtulach@881
|
141 |
* instance of {@link Model model class} (with appropriate
|
jtulach@881
|
142 |
* <a target="_blank" href="http://knockoutjs.com/">Knockout</a> <b>observable</b> properties)
|
jtulach@881
|
143 |
* by calling {@link Models#toRaw(java.lang.Object) Models.toRaw(p)}. For
|
jtulach@881
|
144 |
* example here is a way to obtain the value of <code>fullName</code> property
|
jtulach@881
|
145 |
* (inefficient as it switches between Java and JavaScript back and forth,
|
jtulach@881
|
146 |
* but functional and instructive) via a JavaScript call:
|
jtulach@881
|
147 |
* <pre>
|
jtulach@881
|
148 |
* {@link net.java.html.js.JavaScriptBody @JavaScriptBody}(args = "raw", javacall = true, body =
|
jtulach@881
|
149 |
* "return raw.fullName();" // yes, the <a target="_blank" href="http://knockoutjs.com/">Knockout</a> property is a function
|
jtulach@881
|
150 |
* )
|
jtulach@881
|
151 |
* static native String jsFullName(Object raw);
|
jtulach@881
|
152 |
* // and later
|
jtulach@881
|
153 |
* Person p = ...;
|
jtulach@881
|
154 |
* String fullName = jsFullName({@link Models#toRaw(java.lang.Object) Models.toRaw(p)});
|
jtulach@881
|
155 |
* </pre>
|
jtulach@881
|
156 |
* The above shows how to read a value from <a target="_blank" href="http://knockoutjs.com/">Knockout</a>
|
jtulach@881
|
157 |
* observable. There is a way to change the value too:
|
jtulach@881
|
158 |
* One can pass a parameter to the property-function and then
|
jtulach@881
|
159 |
* it acts like a setter (of course not in the case of read only <code>fullName</code> property,
|
jtulach@881
|
160 |
* but for <code>firstName</code> or <code>lastName</code> the setter is
|
jtulach@881
|
161 |
* available). Everything mentioned in this paragraph applies only when
|
jtulach@881
|
162 |
* <a target="_blank" href="http://knockoutjs.com/">Knockout</a> technology is active
|
jtulach@881
|
163 |
* other technologies may behave differently.
|
jtulach@881
|
164 |
*
|
jtulach@881
|
165 |
* <h4>Copy to Plain JSON</h4>
|
jtulach@881
|
166 |
* There is a way to pass a value of a Java {@link Model model class} instance
|
jtulach@881
|
167 |
* by copy and convert
|
jtulach@881
|
168 |
* the {@link Model the whole object} into plain
|
jtulach@881
|
169 |
* <a target="_blank" href="http://en.wikipedia.org/wiki/JSON">JSON</a>. Just
|
jtulach@881
|
170 |
* print it as a string and parse it in JavaScript:
|
jtulach@881
|
171 |
* <pre>
|
jtulach@881
|
172 |
* {@link net.java.html.js.JavaScriptBody @JavaScriptBody}(args = { "txt" }, body =
|
jtulach@881
|
173 |
* "return JSON.parse(txt);"
|
jtulach@881
|
174 |
* )
|
jtulach@881
|
175 |
* private static native Object parseJSON(String txt);
|
jtulach@881
|
176 |
*
|
jtulach@881
|
177 |
* public static Object toPlainJSON(Object model) {
|
jtulach@881
|
178 |
* return parseJSON(model.toString());
|
jtulach@881
|
179 |
* }
|
jtulach@881
|
180 |
* </pre>
|
jtulach@881
|
181 |
* The newly returned instance is a one time copy of the original model and is no longer
|
jtulach@881
|
182 |
* connected to it. The copy based behavior is independent on any
|
jtulach@881
|
183 |
* particular technology and should work
|
jtulach@881
|
184 |
* in <a target="_blank" href="http://knockoutjs.com/">Knockout</a> as well as other
|
jtulach@881
|
185 |
* technology implementations.
|
jtulach@881
|
186 |
*
|
jtulach@881
|
187 |
* <h4>References</h4>
|
jtulach@881
|
188 |
*
|
jaroslav@549
|
189 |
* Visit an <a target="_blank" href="http://dew.apidesign.org/dew/#7510833">on-line demo</a>
|
jaroslav@549
|
190 |
* to see a histogram driven by the {@link Model} annotation or try
|
jaroslav@549
|
191 |
* a little <a target="_blank" href="http://dew.apidesign.org/dew/#7263102">math test</a>.
|
jtulach@0
|
192 |
*
|
jtulach@655
|
193 |
* @author Jaroslav Tulach
|
jtulach@0
|
194 |
*/
|
jtulach@0
|
195 |
@Retention(RetentionPolicy.SOURCE)
|
jtulach@0
|
196 |
@Target(ElementType.TYPE)
|
jtulach@0
|
197 |
public @interface Model {
|
jtulach@835
|
198 |
/** Name of the model class.
|
jtulach@835
|
199 |
* @return valid Java identifier to use as a name of the model class
|
jtulach@835
|
200 |
*/
|
jtulach@0
|
201 |
String className();
|
jtulach@0
|
202 |
/** List of properties in the model.
|
jtulach@835
|
203 |
* @return array of property definitions
|
jtulach@0
|
204 |
*/
|
jtulach@0
|
205 |
Property[] properties();
|
jtulach@920
|
206 |
|
jtulach@920
|
207 |
/** The id of an element to bind this model too. If this
|
jtulach@920
|
208 |
* property is specified an <code>applyBindings</code> method
|
jtulach@920
|
209 |
* in the model class is going to be generated which later calls
|
jtulach@920
|
210 |
* {@link Models#applyBindings(java.lang.Object, java.lang.String)}
|
jtulach@920
|
211 |
* with appropriate <code>targetId</code>. If the <code>targetId</code>
|
jtulach@920
|
212 |
* is specified as empty string, <code>null</code> value is passed
|
jtulach@920
|
213 |
* to {@link Models#applyBindings(java.lang.Object, java.lang.String)} method.
|
jtulach@920
|
214 |
* If the <code>targetId</code> is not specified at all, no public
|
jtulach@920
|
215 |
* <code>applyBindings</code> method is generated at all (a change compared
|
jtulach@920
|
216 |
* to previous versions of this API).
|
jtulach@920
|
217 |
*
|
jtulach@920
|
218 |
* @return an empty string (means apply globally), or ID of a (usually DOM)
|
jtulach@920
|
219 |
* element to apply this model after calling its generated
|
jtulach@920
|
220 |
* <code>applyBindings()</code> method to
|
jtulach@920
|
221 |
* @since 1.1
|
jtulach@920
|
222 |
*/
|
jtulach@920
|
223 |
String targetId() default "";
|
jtulach@1017
|
224 |
|
jtulach@1017
|
225 |
/** Controls whether builder-like setters shall be generated. Once this
|
jtulach@1017
|
226 |
* attribute is set, all {@link #properties()} will get a builder like
|
jtulach@1017
|
227 |
* setter (takes value of the property and returns <code>this</code>
|
jtulach@1017
|
228 |
* so invocations can be chained). When this attribute is specified,
|
jtulach@1017
|
229 |
* the non-default constructor isn't generated at all.
|
jtulach@1017
|
230 |
* <p>
|
jtulach@1017
|
231 |
* Specifying <code>builder="assign"</code>
|
jtulach@1017
|
232 |
* and having {@link #properties() properties} <code>name</code> and
|
jtulach@1017
|
233 |
* <code>age</code> will generate method: <pre>
|
jtulach@1017
|
234 |
* <b>public</b> MyModel assignName(String name) { ... }
|
jtulach@1017
|
235 |
* <b>public</b> MyModel assignAge(int age) { ... }
|
jtulach@1017
|
236 |
* </pre>
|
jtulach@1017
|
237 |
* These methods can then be chained as <pre>
|
jtulach@1017
|
238 |
* MyModel m = <b>new</b> MyModel().assignName("name").assignAge(3);
|
jtulach@1017
|
239 |
* </pre>
|
jtulach@1017
|
240 |
* The <code>builder</code> attribute can be set to empty string <code>""</code> -
|
jtulach@1017
|
241 |
* then it is possible that some property names clash with Java keywords.
|
jtulach@1017
|
242 |
* It's responsibility of the user to specify valid builder prefix,
|
jtulach@1017
|
243 |
* so the generated methods are compilable.
|
jtulach@1017
|
244 |
*
|
jtulach@1017
|
245 |
* @return the prefix to put before {@link Property property} names when
|
jtulach@1017
|
246 |
* generating their builder methods
|
jtulach@1017
|
247 |
* @since 1.3
|
jtulach@1017
|
248 |
*/
|
jtulach@1017
|
249 |
String builder() default "";
|
jtulach@1023
|
250 |
|
jtulach@1023
|
251 |
/** Controls keeping of per-instance private state. Sometimes
|
jtulach@1023
|
252 |
* the class generated by the {@link Model @Model annotation} needs to
|
jtulach@1023
|
253 |
* keep non-public, or non-JSON like state. One can achieve that by
|
jtulach@1023
|
254 |
* specifying <code>instance=true</code> when using the annotation. Then
|
jtulach@1023
|
255 |
* the generated class gets a pointer to the instance of the annotated
|
jtulach@1023
|
256 |
* class (there needs to be default constructor) and all the {@link ModelOperation @ModelOperation},
|
jtulach@1023
|
257 |
* {@link Function @Function}, {@link OnPropertyChange @OnPropertyChange}
|
jtulach@1023
|
258 |
* and {@link OnReceive @OnReceive} methods may be non-static. The
|
jtulach@1023
|
259 |
* instance of the implementation class isn't accessible directly, just
|
jtulach@1023
|
260 |
* through calls to above defined (non-static) methods. Example:
|
jtulach@1023
|
261 |
* <pre>
|
jtulach@1023
|
262 |
* {@link Model @Model}(className="Data", instance=<b>true</b>, properties={
|
jtulach@1023
|
263 |
* {@link Property @Property}(name="message", type=String.<b>class</b>)
|
jtulach@1023
|
264 |
* })
|
jtulach@1023
|
265 |
* <b>final class</b> DataPrivate {
|
jtulach@1023
|
266 |
* <b>private int</b> count;
|
jtulach@1023
|
267 |
*
|
jtulach@1023
|
268 |
* {@link ModelOperation @ModelOperation} <b>void</b> increment(Data model) {
|
jtulach@1023
|
269 |
* count++;
|
jtulach@1023
|
270 |
* }
|
jtulach@1023
|
271 |
*
|
jtulach@1023
|
272 |
* {@link ModelOperation @ModelOperation} <b>void</b> hello(Data model) {
|
jtulach@1023
|
273 |
* model.setMessage("Hello " + count + " times!");
|
jtulach@1023
|
274 |
* }
|
jtulach@1023
|
275 |
* }
|
jtulach@1023
|
276 |
* Data data = <b>new</b> Data();
|
jtulach@1023
|
277 |
* data.increment();
|
jtulach@1023
|
278 |
* data.increment();
|
jtulach@1023
|
279 |
* data.increment();
|
jtulach@1023
|
280 |
* data.hello();
|
jtulach@1023
|
281 |
* <b>assert</b> data.getMessage().equals("Hello 3 times!");
|
jtulach@1023
|
282 |
* </pre>
|
jtulach@1023
|
283 |
* The methods annotated by {@link ComputedProperty} need to remain static, as
|
jtulach@1023
|
284 |
* they are supposed to be <em>pure</em> functions (e.g. depend only on their parameters)
|
jtulach@1023
|
285 |
* and shouldn't use any internal state.
|
jtulach@1023
|
286 |
*
|
jtulach@1023
|
287 |
* @return <code>true</code> if the model class should keep pointer to
|
jtulach@1023
|
288 |
* instance of the implementation class
|
jtulach@1023
|
289 |
* @since 1.3
|
jtulach@1023
|
290 |
*/
|
jtulach@1023
|
291 |
boolean instance() default false;
|
jtulach@0
|
292 |
}
|