ko4j/src/main/java/org/netbeans/html/ko4j/KO4J.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
child 886 88d62267a0b5
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
jaroslav@443
     1
/**
jaroslav@442
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@442
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@442
     5
 *
jaroslav@442
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@442
     7
 * Other names may be trademarks of their respective owners.
jaroslav@442
     8
 *
jaroslav@442
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@442
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@442
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@442
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@442
    13
 * License. You can obtain a copy of the License at
jaroslav@442
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@442
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@442
    16
 * specific language governing permissions and limitations under the
jaroslav@442
    17
 * License.  When distributing the software, include this License Header
jaroslav@442
    18
 * Notice in each file and include the License file at
jaroslav@442
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@442
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@442
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@442
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@442
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@442
    24
 * your own identifying information:
jaroslav@442
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@442
    26
 *
jaroslav@443
    27
 * Contributor(s):
jaroslav@443
    28
 *
jaroslav@443
    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@443
    31
 *
jaroslav@442
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@442
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@442
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@442
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@442
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@442
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@442
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@442
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@442
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@442
    41
 * made subject to such option by the copyright holder.
jaroslav@442
    42
 */
jaroslav@442
    43
package org.netbeans.html.ko4j;
jaroslav@442
    44
jaroslav@442
    45
import net.java.html.json.Model;
jtulach@838
    46
import org.netbeans.html.boot.spi.Fn;
jtulach@838
    47
import org.netbeans.html.context.spi.Contexts;
jtulach@838
    48
import org.netbeans.html.context.spi.Contexts.Provider;
jtulach@838
    49
import org.netbeans.html.json.spi.Technology;
jtulach@838
    50
import org.netbeans.html.json.spi.Transfer;
jtulach@838
    51
import org.netbeans.html.json.spi.WSTransfer;
jaroslav@442
    52
import org.openide.util.lookup.ServiceProvider;
jaroslav@442
    53
jaroslav@442
    54
/** Support for <a href="http://knockoutjs.com">knockout.js</a>
jaroslav@442
    55
 * and its Java binding via {@link Model model classes}.
jaroslav@533
    56
 * Registers {@link Provider}, so {@link java.util.ServiceLoader} can find it.
jaroslav@442
    57
 *
jtulach@790
    58
 * @author Jaroslav Tulach
jaroslav@444
    59
 * @since 0.7
jaroslav@442
    60
 */
jaroslav@533
    61
@ServiceProvider(service = Provider.class)
jaroslav@533
    62
public final class KO4J implements Provider {
jaroslav@444
    63
    private final Fn.Presenter presenter;
jaroslav@444
    64
    private FXContext c;
jaroslav@444
    65
    
jaroslav@444
    66
    public KO4J() {
jaroslav@444
    67
        this(null);
jaroslav@444
    68
    }
jaroslav@444
    69
    
jaroslav@444
    70
    public KO4J(Fn.Presenter presenter) {
jaroslav@444
    71
        this.presenter = presenter;
jaroslav@444
    72
    }
jaroslav@444
    73
    
jaroslav@444
    74
    private FXContext getKO() {
jaroslav@444
    75
        if (c == null) {
jaroslav@444
    76
            c = new FXContext(presenter == null ? Fn.activePresenter() : presenter);
jaroslav@444
    77
        }
jaroslav@444
    78
        return c;
jaroslav@444
    79
    }
jaroslav@444
    80
    
jaroslav@444
    81
    /** Return instance of the knockout.js for Java technology.
jaroslav@444
    82
     * @return non-null instance
jaroslav@444
    83
     */
jaroslav@444
    84
    public Technology knockout() {
jaroslav@444
    85
        return getKO();
jaroslav@444
    86
    }
jaroslav@444
    87
    
jaroslav@446
    88
    /** Browser based implementation of transfer interface. Uses
jaroslav@446
    89
     * browser method to convert string to JSON.
jaroslav@446
    90
     * 
jaroslav@446
    91
     * @return non-null instance
jaroslav@444
    92
     */
jaroslav@446
    93
    public Transfer transfer() {
jaroslav@444
    94
        return getKO();
jaroslav@444
    95
    }
jaroslav@444
    96
    
jaroslav@444
    97
    /** Returns browser based implementation of websocket transfer.
jaroslav@446
    98
     * If available (for example JavaFX WebView on JDK7 does not have
jaroslav@446
    99
     * this implementation).
jaroslav@444
   100
     * 
jaroslav@444
   101
     * @return an instance or <code>null</code>, if there is no
jaroslav@444
   102
     *   <code>WebSocket</code> object in the browser
jaroslav@444
   103
     */
jaroslav@446
   104
    public WSTransfer<?> websockets() {
jaroslav@444
   105
        return getKO().areWebSocketsSupported() ? getKO() : null;
jaroslav@444
   106
    }
jaroslav@442
   107
jaroslav@444
   108
    /** Registers technologies at position 100:
jaroslav@444
   109
     * <ul>
jaroslav@444
   110
     *   <li>{@link #knockout()}</li>
jaroslav@446
   111
     *   <li>{@link #transfer()}</li>
jaroslav@446
   112
     *   <li>{@link #websockets()()} - if browser supports web sockets</li>
jaroslav@444
   113
     * </ul>
jaroslav@444
   114
     * @param context the context to register to
jaroslav@444
   115
     * @param requestor the class requesting the registration
jaroslav@444
   116
     */
jaroslav@442
   117
    @Override
jaroslav@442
   118
    public void fillContext(Contexts.Builder context, Class<?> requestor) {
jaroslav@575
   119
        context.register(Technology.class, knockout(), 100);
jaroslav@575
   120
        context.register(Transfer.class, transfer(), 100);
jaroslav@589
   121
        context.register(WSTransfer.class, websockets(), 100);
jaroslav@442
   122
    }
jaroslav@442
   123
    
jaroslav@442
   124
}