DI fans will be glad to find out we have a support for various contexts
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 19 Apr 2013 11:55:34 +0200
changeset 5e9d240f0590d
parent 4 d519a7891d77
child 6 8f7eb5d6d47a
DI fans will be glad to find out we have a support for various contexts
json/src/main/java/net/java/html/json/Context.java
json/src/main/java/org/apidesign/html/json/impl/Bindings.java
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
json/src/main/java/org/apidesign/html/json/spi/ContextBuilder.java
json/src/test/java/net/java/html/json/ModelTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/json/src/main/java/net/java/html/json/Context.java	Fri Apr 19 11:55:34 2013 +0200
     1.3 @@ -0,0 +1,40 @@
     1.4 +/**
     1.5 + * HTML via Java(tm) Language Bindings
     1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details. apidesign.org
    1.16 + * designates this particular file as subject to the
    1.17 + * "Classpath" exception as provided by apidesign.org
    1.18 + * in the License file that accompanied this code.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License
    1.21 + * along with this program. Look for COPYING file in the top folder.
    1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    1.23 + */
    1.24 +package net.java.html.json;
    1.25 +
    1.26 +/** Represents context where the {@link Model} and other objects
    1.27 + * operate in. The context is usually a particular HTML page in a browser.
    1.28 + * The context is also associated with the actual HTML rendering technology
    1.29 + * in the HTML page - there is likely to be different context for 
    1.30 + * <a href="http://knockoutjs.com">knockout.js</a> and different one
    1.31 + * for <a href="http://angularjs.org">angular</a>.
    1.32 + *
    1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.34 + */
    1.35 +public final class Context {
    1.36 +    private Context() {
    1.37 +    }
    1.38 +    
    1.39 +    /** Dummy context without binding to any real browser or technology. 
    1.40 +     * Useful for simple unit testing of behavior of model classes.
    1.41 +     */
    1.42 +    public static final Context EMPTY = new Context();
    1.43 +}
     2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/Bindings.java	Fri Apr 19 11:29:49 2013 +0200
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/Bindings.java	Fri Apr 19 11:55:34 2013 +0200
     2.3 @@ -20,12 +20,14 @@
     2.4   */
     2.5  package org.apidesign.html.json.impl;
     2.6  
     2.7 +import net.java.html.json.Context;
     2.8 +
     2.9  /**
    2.10   *
    2.11   * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.12   */
    2.13  public final class Bindings {
    2.14 -    public static Bindings apply(Object model, String[] propsAndGetters, String[] functions) {
    2.15 +    public static Bindings apply(Context c, Object model, String[] propsAndGetters, String[] functions) {
    2.16          return null;
    2.17      }
    2.18      
     3.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Fri Apr 19 11:29:49 2013 +0200
     3.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Fri Apr 19 11:55:34 2013 +0200
     3.3 @@ -185,14 +185,16 @@
     3.4                  w.append("import net.java.html.json.*;\n");
     3.5                  w.append("public final class ").append(className).append(" implements Cloneable {\n");
     3.6                  w.append("  private boolean locked;\n");
     3.7 +                w.append("  private net.java.html.json.Context context;\n");
     3.8                  w.append("  private org.apidesign.html.json.impl.Bindings ko;\n");
     3.9                  w.append(body.toString());
    3.10                  w.append("  private static Class<" + inPckName(e) + "> modelFor() { return null; }\n");
    3.11 -                w.append("  public ").append(className).append("() {\n");
    3.12 +                w.append("  public ").append(className).append("(Context context) {\n");
    3.13 +                w.append("    this.context = context;\n");
    3.14                  w.append("  };\n");
    3.15                  w.append("  private org.apidesign.html.json.impl.Bindings intKnckt() {\n");
    3.16                  w.append("    if (ko != null) return ko;\n");
    3.17 -                w.append("    return ko = org.apidesign.html.json.impl.Bindings.apply(this, ");
    3.18 +                w.append("    return ko = org.apidesign.html.json.impl.Bindings.apply(context, this, ");
    3.19                  writeStringArray(propsGetSet, w);
    3.20                  w.append(", ");
    3.21                  writeStringArray(functions, w);
    3.22 @@ -915,7 +917,7 @@
    3.23      }
    3.24      private void writeClone(String className, Prprt[] props, Writer w) throws IOException {
    3.25          w.write("  public " + className + " clone() {\n");
    3.26 -        w.write("    " + className + " ret = new " + className + "();\n");
    3.27 +        w.write("    " + className + " ret = new " + className + "(context);\n");
    3.28          for (Prprt p : props) {
    3.29              if (!p.array()) {
    3.30                  boolean isModel[] = { false };
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/ContextBuilder.java	Fri Apr 19 11:55:34 2013 +0200
     4.3 @@ -0,0 +1,47 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package org.apidesign.html.json.spi;
    4.25 +
    4.26 +import net.java.html.json.Context;
    4.27 +
    4.28 +/** Support for providers of new {@link Context}. Providers of different
    4.29 + * technologies should be of particular interest in this class. End users
    4.30 + * designing their application with existing technologies should rather
    4.31 + * point their attention to {@link Context} and co.
    4.32 + *
    4.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.34 + */
    4.35 +public final class ContextBuilder {
    4.36 +    private ContextBuilder() {
    4.37 +    }
    4.38 +    
    4.39 +    public static ContextBuilder create() {
    4.40 +        return new ContextBuilder();
    4.41 +    }
    4.42 +    
    4.43 +    public ContextBuilder with() {
    4.44 +        return this;
    4.45 +    }
    4.46 +    
    4.47 +    public Context build() {
    4.48 +        return Context.EMPTY;
    4.49 +    }
    4.50 +}
     5.1 --- a/json/src/test/java/net/java/html/json/ModelTest.java	Fri Apr 19 11:29:49 2013 +0200
     5.2 +++ b/json/src/test/java/net/java/html/json/ModelTest.java	Fri Apr 19 11:55:34 2013 +0200
     5.3 @@ -48,7 +48,7 @@
     5.4      
     5.5      @BeforeMethod
     5.6      public void createModel() {
     5.7 -        model = new Modelik();
     5.8 +        model = new Modelik(Context.EMPTY);
     5.9      }
    5.10      
    5.11      @Test public void classGeneratedWithSetterGetter() {