javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PredefinedFields.java
author Jan Horvath <jan.horvath@oracle.com>
Thu, 21 Mar 2013 15:45:42 +0100
changeset 866 9b4751828ceb
child 1074 3ab2f70b300d
permissions -rw-r--r--
Dynamically generate classes representing elements on the HTML @Page
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    20 import java.io.IOException;
    21 import java.io.Writer;
    22 import java.util.HashMap;
    23 import java.util.Map;
    24 
    25 /**
    26  *
    27  * @author Jan Horvath <jhorvath@netbeans.org>
    28  */
    29 public class PredefinedFields {
    30     
    31     private static final Map<String, String> IMPORTS = new HashMap<String, String>() {
    32         {
    33             put("canvas", "import org.apidesign.bck2brwsr.core.JavaScriptBody;");
    34         }
    35     };
    36     
    37     private static final Map<String, String> FIELDS = new HashMap<String, String>() {
    38         {
    39             put("canvas", 
    40                     "    @JavaScriptBody(\n" +
    41                     "            args = {\"el\"},\n" +
    42                     "            body = \"var e = window.document.getElementById(el._id());\\n\"\n" +
    43                     "            + \"return e.getContext('2d');\\n\")\n" +
    44                     "    private native static Object getContextImpl(Canvas el);\n" +
    45                     "    \n" +
    46                     "    public GraphicsContext getContext() {\n" +
    47                     "        return new GraphicsContext(getContextImpl(this));\n" +
    48                     "    }");
    49         }
    50     };
    51     
    52     static void appendImports(Writer w, String tag) throws IOException {
    53         String text = IMPORTS.get(tag.toLowerCase());
    54         if (text != null) {
    55             w.append(text).append("\n");
    56         }
    57     }
    58     
    59     static void appendFields(Writer w, String tag) throws IOException {
    60         String text = FIELDS.get(tag.toLowerCase());
    61         if (text != null) {
    62             w.append(text).append("\n");
    63         }
    64     }
    65 }