json/src/main/java/org/apidesign/html/json/spi/Technology.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 575 3e6c5f24b12d
child 835 14fcf4844fad
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package org.apidesign.html.json.spi;
    44 
    45 import net.java.html.BrwsrCtx;
    46 import net.java.html.json.Model;
    47 import net.java.html.json.Models;
    48 
    49 /** An implementation of a binding between model classes (see {@link Model})
    50  * and particular technology like <a href="http://knockoutjs.com">knockout.js</a>
    51  * in a browser window, etc.
    52  *
    53  * @author Jaroslav Tulach
    54  */
    55 public interface Technology<Data> {
    56     /** Creates an object to wrap the provided model object. The model
    57      * has previously been generated by annotation processor associated 
    58      * with {@link Model} annotation.
    59      * 
    60      * @param model the model generated from {@link Model}
    61      * @return internal object representing the model
    62      */
    63     public Data wrapModel(Object model);
    64     
    65     /** Converts an element potentially representing a model into the model.
    66      * @param modelClass expected class to convert the data to
    67      * @param data the current data provided from the browser
    68      * @return the instance of modelClass somehow extracted from the data, may return <code>null</code>
    69      */
    70     public <M> M toModel(Class<M> modelClass, Object data);
    71     
    72     /** Binds a property between the model and the data as used by the technology.
    73      * 
    74      * @param b the description of the requested binding
    75      * @param model the original instance of the model
    76      * @param data the data to bind with the model
    77      */
    78     public void bind(PropertyBinding b, Object model, Data data);
    79 
    80     /** Model for given data has changed its value. The technology is
    81      * supposed to update its state (for example DOM nodes associated
    82      * with the model). The update usually happens asynchronously.
    83      * 
    84      * @param data technology's own representation of the model
    85      * @param propertyName name of the model property that changed
    86      */
    87     public void valueHasMutated(Data data, String propertyName);
    88 
    89     public void expose(FunctionBinding fb, Object model, Data d);
    90     
    91     /** Applies given data to current context (usually an HTML page).
    92      * @param data the data to apply
    93      */
    94     public void applyBindings(Data data);
    95     
    96     /**
    97      * Some technologies may require wrapping a Java array into a special
    98      * object. In such case they may return it from this method.
    99      *
   100      * @param arr original array
   101      * @return wrapped array
   102      */
   103     public Object wrapArray(Object[] arr);
   104     
   105     /** 
   106      * Run given runnable in a safe mode. If the runnable can be executed
   107      * immediately, do it. If we need to switch to some other thread, do it
   108      * and invoke r asynchronously immediately returning from the call to
   109      * runSafe method.
   110      * 
   111      * @param r the runnable to execute
   112      * @deprecated Use {@link BrwsrCtx#execute(java.lang.Runnable)}
   113      */
   114     @Deprecated
   115     public void runSafe(Runnable r);
   116 
   117     /** For certain rendering technologies it may be more efficient to register
   118      * property and function bindings for one instance of the model at once, 
   119      * rather then doing it incrementally via 
   120      * {@link Technology#expose(org.apidesign.html.json.spi.FunctionBinding, java.lang.Object, java.lang.Object) }
   121      * and 
   122      * {@link Technology#bind(org.apidesign.html.json.spi.PropertyBinding, java.lang.Object, java.lang.Object) }.
   123      * In such case implement the {@link #wrapModel(java.lang.Object, org.apidesign.html.json.spi.PropertyBinding[], org.apidesign.html.json.spi.FunctionBinding[]) }
   124      * method of this interface and it will be called instead of the 
   125      * previous two ones.
   126      * 
   127      * @since 0.6
   128      */
   129     public static interface BatchInit<D> extends Technology<D> {
   130         /** Wrap the given model into redering technology appropriate object 
   131          * <code>D</code> and expose given properties and functions on it.
   132          * 
   133          * @param model the {@link Models#isModel(java.lang.Class) model} in Java
   134          * @param propArr array of property bindings to expose
   135          * @param funcArr array of functions to expose
   136          * @return appropriate wrapper around the model
   137          */
   138         public D wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr);
   139     }
   140 
   141     /** Some technologies are more effective when number of calls between
   142      * Java and JavaScript is limited - to do that when a value of property
   143      * is changed they should implement this additional interface.
   144      * 
   145      * @param <D> internal type of the technology
   146      * @since 0.7.6
   147      */
   148     public static interface ValueMutated<D> extends Technology<D> {
   149         /** Model for given data has changed its value. The technology is
   150          * supposed to update its state (for example DOM nodes associated
   151          * with the model). The update usually happens asynchronously.
   152          * <p>
   153          * If both <code>oldValue</code> and <code>newValue</code> are 
   154          * <code>null</code> then the real value of the technology is
   155          * not known.
   156          * <p>
   157          * If this method is present, then it is called instead of 
   158          * old, plain {@link #valueHasMutated(java.lang.Object, java.lang.String)}
   159          * which is never called by the infrastructure then.
   160          * 
   161          * @param data technology's own representation of the model
   162          * @param propertyName name of the model property that changed
   163          * @param oldValue provides previous value of the property
   164          * @param newValue provides new value of the property
   165          */
   166         public void valueHasMutated(D data, String propertyName, Object oldValue, Object newValue);
   167     }
   168 }