json/src/main/java/net/java/html/json/Function.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 09 Dec 2015 21:39:13 +0100
changeset 1023 4f906bde3a2e
parent 901 4d20596b35bc
permissions -rw-r--r--
#257039: @Model.instance() to allow storage of private (and non-JSON like) state in a model
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@901
    45
import java.io.PrintStream;
jtulach@0
    46
import java.lang.annotation.ElementType;
jtulach@0
    47
import java.lang.annotation.Retention;
jtulach@0
    48
import java.lang.annotation.RetentionPolicy;
jtulach@0
    49
import java.lang.annotation.Target;
jtulach@901
    50
import org.netbeans.html.json.spi.FunctionBinding;
jtulach@0
    51
jaroslav@32
    52
/** Methods in class annotated by {@link Model} can be 
jtulach@0
    53
 * annotated by this annotation to signal that they should be available
jaroslav@233
    54
 * as functions to users of the model classes. The method
jtulach@1023
    55
 * should be non-private, static (unless {@link Model#instance() instance mode} is on)
jtulach@1023
    56
 * and return <code>void</code>.
jtulach@901
    57
 * It may take few arguments. The first argument can be the type of
jaroslav@233
    58
 * the associated model class, the other argument can be of any type,
jaroslav@233
    59
 * but has to be named <code>data</code> - this one represents the
jaroslav@233
    60
 * actual data the function was invoked on. Example:
jaroslav@233
    61
 * <pre>
jaroslav@233
    62
 * 
jaroslav@233
    63
 * {@link Model @Model}(className="Names", properties={
jaroslav@233
    64
 *   {@link Property @Property}(name = "selectedName", type=String.class),
jaroslav@233
    65
 *   {@link Property @Property}(name = "names", type=String.class, array = true)
jaroslav@233
    66
 * })
jaroslav@233
    67
 * static class NamesModel {
jaroslav@233
    68
 *   {@link Function @Function} static void <b>nameSelected</b>(Names myModel, String data) {
jaroslav@233
    69
 *     myModel.setSelectedName(data);
jaroslav@233
    70
 *   }
jaroslav@233
    71
 * 
jaroslav@233
    72
 *   static void initialize() {
jaroslav@233
    73
 *     Names pageModel = new Names("---", "Jarda", "Pepa", "Honza", "Jirka", "Tomáš");
jaroslav@233
    74
 *     pageModel.applyBindings();
jaroslav@233
    75
 *   }
jaroslav@233
    76
 * }
jaroslav@233
    77
 * 
jaroslav@549
    78
 * // associated <a target="_blank" href="http://knockoutjs.com/">Knockout</a> HTML page:
jaroslav@233
    79
 * 
jaroslav@549
    80
 * Selected name: &lt;span data-bind="text: selectedName"&gt;&lt;/span&gt;
jaroslav@233
    81
 * &lt;ul data-bind="foreach: names"&gt;
jaroslav@549
    82
 *   &lt;li&gt;
jaroslav@549
    83
 *     &lt;a data-bind="text: $data, click: $root.nameSelected" href="#"&gt;&lt;/a&gt;
jaroslav@549
    84
 *   &lt;/li&gt;
jaroslav@549
    85
 * &lt;/ul&gt; 
jaroslav@233
    86
 * </pre>
jaroslav@233
    87
 * The above example would render:
jaroslav@233
    88
 * <hr>
jaroslav@233
    89
 * Selected name: <span>---</span>
jaroslav@233
    90
 * <ul>
jaroslav@233
    91
 *   <li>Jarda</li>
jaroslav@233
    92
 *   <li>Pepa</li>
jaroslav@233
    93
 *   <li>Honza</li>
jaroslav@233
    94
 *   <li>Jirka</li>
jaroslav@233
    95
 *   <li>Tomáš</li>
jaroslav@233
    96
 * </ul>
jaroslav@233
    97
 * <hr>
jaroslav@233
    98
 * and after clicking on one of the names the <code>---</code> would be replaced
jaroslav@549
    99
 * by selected name. 
jaroslav@549
   100
 * Try <a target="_blank" href="http://dew.apidesign.org/dew/#8848505">this sample on-line</a>!
jtulach@901
   101
 * <p>
jtulach@901
   102
 * There can be additional arguments in the method which can extract information
jtulach@901
   103
 * from (typically event object) sent as a second parameter of the function
jtulach@901
   104
 * {@link FunctionBinding#call(java.lang.Object, java.lang.Object) dispatch method}.
jtulach@901
   105
 * Such arguments can be of primitive types (<code>int</code>, <code>double</code>
jtulach@901
   106
 * or {@link String}). Their names are used to extract values of appropriate
jtulach@901
   107
 * properties from the event object. The following function...
jtulach@901
   108
 * <pre>
jtulach@901
   109
 * {@link Function @Function} static void <b>meaningOfWorld</b>(Names myModel, String data, int answer) {
jtulach@901
   110
 *   {@link System}.out.{@link PrintStream#println(int) println}(answer);
jtulach@901
   111
 * }
jtulach@901
   112
 * // would print <b>42</b> if the dispatch method:
jtulach@901
   113
 * {@link FunctionBinding#call(java.lang.Object, java.lang.Object) meaningOfWorld.call}(model, json)
jtulach@901
   114
 * </pre>
jtulach@901
   115
 * is called with equivalent of <code>var json = { 'answer' : 42 }</code>.
jaroslav@233
   116
 * 
jtulach@655
   117
 * @author Jaroslav Tulach
jtulach@0
   118
 */
jtulach@0
   119
@Target(ElementType.METHOD)
jtulach@0
   120
@Retention(RetentionPolicy.SOURCE)
jtulach@0
   121
public @interface Function {
jtulach@0
   122
}