javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnReceive.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 10 Apr 2013 12:19:32 +0200
branchmodel
changeset 964 df60ba2aeb87
parent 954 6448c284fe21
child 1023 ad9a37489365
permissions -rw-r--r--
Better Javadoc with a sample code
jaroslav@934
     1
/**
jaroslav@934
     2
 * Back 2 Browser Bytecode Translator
jaroslav@934
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@934
     4
 *
jaroslav@934
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@934
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@934
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@934
     8
 *
jaroslav@934
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@934
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@934
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@934
    12
 * GNU General Public License for more details.
jaroslav@934
    13
 *
jaroslav@934
    14
 * You should have received a copy of the GNU General Public License
jaroslav@934
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@934
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@934
    17
 */
jaroslav@934
    18
package org.apidesign.bck2brwsr.htmlpage.api;
jaroslav@934
    19
jaroslav@934
    20
import java.lang.annotation.ElementType;
jaroslav@934
    21
import java.lang.annotation.Retention;
jaroslav@934
    22
import java.lang.annotation.RetentionPolicy;
jaroslav@934
    23
import java.lang.annotation.Target;
jaroslav@934
    24
jaroslav@964
    25
/** Static methods in classes annotated by {@link Page}
jaroslav@964
    26
 * can be marked by this annotation to establish a 
jaroslav@964
    27
 * <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>
jaroslav@964
    28
 * communication point.
jaroslav@934
    29
 * The associated model page then gets new method to invoke a network
jaroslav@964
    30
 * connection. Example follows:
jaroslav@964
    31
 * 
jaroslav@964
    32
 * <pre>
jaroslav@964
    33
 * {@link Page @Page}(className="MyModel", xhtml="page.html", properties={
jaroslav@964
    34
 *   {@link Property @Property}(name = "people", type=Person.class, array=true)
jaroslav@964
    35
 * })
jaroslav@964
    36
 * class MyModelImpl {
jaroslav@964
    37
 *   {@link Model @Model}(className="Person", properties={
jaroslav@964
    38
 *     {@link Property @Property}(name = "firstName", type=String.class),
jaroslav@964
    39
 *     {@link Property @Property}(name = "lastName", type=String.class)
jaroslav@964
    40
 *   })
jaroslav@964
    41
 *   static class PersonImpl {
jaroslav@964
    42
 *     {@link ComputedProperty @ComputedProperty}
jaroslav@964
    43
 *     static String fullName(String firstName, String lastName) {
jaroslav@964
    44
 *       return firstName + " " + lastName;
jaroslav@964
    45
 *     }
jaroslav@964
    46
 *   }
jaroslav@964
    47
 * 
jaroslav@964
    48
 *   {@link OnReceive @OnReceive}(url = "{protocol}://your.server.com/person/{name}")
jaroslav@964
    49
 *   static void getANewPerson(MyModel m, Person p) {
jaroslav@964
    50
 *     {@link Element#alert Element.alert}("Adding " + p.getFullName() + '!');
jaroslav@964
    51
 *     m.getPeople().add(p);
jaroslav@964
    52
 *   }
jaroslav@964
    53
 * 
jaroslav@964
    54
 *   // the above will generate method <code>getANewPerson</code> in class <code>MyModel</code>.
jaroslav@964
    55
 *   // with <code>protocol</code> and <code>name</code> arguments
jaroslav@964
    56
 *   // which asynchronously contacts the server and in case of success calls
jaroslav@964
    57
 *   // your {@link OnReceive @OnReceive} with parsed in data
jaroslav@964
    58
 * 
jaroslav@964
    59
 *   {@link On @On}(event={@link OnEvent#CLICK OnEvent.CLICK}, id="rqst")
jaroslav@964
    60
 *   static void requestSmith(MyModel m) {
jaroslav@964
    61
 *     m.getANewPerson("http", "Smith");
jaroslav@964
    62
 *   }
jaroslav@964
    63
 * }
jaroslav@964
    64
 * </pre>
jaroslav@964
    65
 * When the server returns <code>{ "firstName" : "John", "lastName" : "Smith" }</code>
jaroslav@964
    66
 * the browser will show alert message <em>Adding John Smith!</em>.
jaroslav@934
    67
 * 
jaroslav@934
    68
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@964
    69
 * @since 0.6
jaroslav@934
    70
 */
jaroslav@934
    71
@Retention(RetentionPolicy.SOURCE)
jaroslav@934
    72
@Target(ElementType.METHOD)
jaroslav@934
    73
public @interface OnReceive {
jaroslav@934
    74
    /** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
jaroslav@934
    75
     * Those parameters will then become variables of the associated method.
jaroslav@934
    76
     * 
jaroslav@934
    77
     * @return the (possibly parametrized) url to connect to
jaroslav@934
    78
     */
jaroslav@934
    79
    String url();
jaroslav@954
    80
    
jaroslav@954
    81
    /** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
jaroslav@954
    82
     * a callback from the server generated page to a function defined in the
jaroslav@954
    83
     * system. The name of such function is usually specified as a property
jaroslav@954
    84
     * (of possibly different names). By defining the <code>jsonp</code> attribute
jaroslav@954
    85
     * one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> 
jaroslav@954
    86
     * transmission and specifies the name of the property. The property should
jaroslav@954
    87
     * also be used in the {@link #url()} attribute on appropriate place.
jaroslav@954
    88
     * 
jaroslav@954
    89
     * @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
jaroslav@954
    90
     *    callback function.
jaroslav@954
    91
     */
jaroslav@954
    92
    String jsonp() default "";
jaroslav@934
    93
}