# HG changeset patch # User Jaroslav Tulach # Date 1367482115 -7200 # Node ID 9321b4016d5c35ed2a96129ed3344006b1ea13ff # Parent 3800d11c0bdb0e504f731538de3206b2a0d32d05 Hopefully resuscitating the original code from rev. 146ae7b52b64 by manual merge diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Attributes.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Attributes.java Thu May 02 10:08:35 2013 +0200 @@ -0,0 +1,57 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage; + +import java.util.HashMap; +import java.util.Map; + +/** + * Temporary storing the type of attributes here. This should be implemented in HTML 5 model + * + * @author Jan Horvath + */ +public class Attributes { + + static final Map TYPES = new HashMap() { + { + // HTML Global Attributes + // id attribute is already defined in Element, don't add it again + put("accesskey", "String"); + put("class", "String"); + put("contenteditable", "Boolean"); + put("contextmenu", "String"); + put("dir", "String"); + put("draggable", "Boolean"); + put("dropzone", "String"); + put("hidden", "Boolean"); + put("lang", "String"); + put("spellcheck", "Boolean"); + put("style", "String"); + put("tabindex", "String"); + put("title", "String"); + put("translate", "Boolean"); + put("width", "Integer"); + put("height", "Integer"); + + put("value", "String"); + put("disabled", "Boolean"); + +// put("text", "String"); 'text' field is used to set innerHTML of element + } + }; +} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ElementGenerator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ElementGenerator.java Thu May 02 10:08:35 2013 +0200 @@ -0,0 +1,175 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.ServiceLoader; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Element; +import javax.tools.Diagnostic; +import javax.tools.FileObject; +import org.netbeans.modules.html.editor.lib.api.HtmlVersion; +import org.netbeans.modules.html.editor.lib.api.model.HtmlModel; +import org.netbeans.modules.html.editor.lib.api.model.HtmlModelProvider; +import org.netbeans.modules.html.editor.lib.api.model.HtmlTag; +import org.netbeans.modules.html.editor.lib.api.model.HtmlTagAttribute; + +/** + * + * @author Jan Horvath + */ +public class ElementGenerator { + + static final Map NAMING_EXCEPTIONS = new HashMap() { + { + put("img", "Image"); + put("class", "Clazz"); + } + }; + + static final String javaKeywords[] = { + "abstract", "assert", "boolean", "break", "byte", "case", + "catch", "char", "class", "const", "continue", "default", + "do", "double", "else", "extends", "false", "final", "finally", + "float", "for", "goto", "if", "implements", "import", + "instanceof", "int", "interface", "long", "native", "new", + "null", "package", "private", "protected", "public", + "return", "short", "static", "strictfp", "super", + "switch", "synchronized", "this", "throw", "throws", + "transient", "true", "try", "void", "volatile", "while" + }; + + private static Map elements = new HashMap(); + private final ProcessingEnvironment processingEnv; + private HtmlModel model = null; + + ElementGenerator(ProcessingEnvironment processingEnv) { + this.processingEnv = processingEnv; + } + + String getType(String pkg, String tag, Element e) { + String className = elements.get(tag); + if (className == null) { + className = createClass(pkg, tag, e); + elements.put(tag, className); + } + return className; + } + + private String createClass(String pkg, String tag, Element e) { + String className = className(tag); + Writer w; + try { + FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e); + w = new OutputStreamWriter(java.openOutputStream()); + try { + w.append("package " + pkg + ";\n\n"); + w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n"); + PredefinedFields.appendImports(w, tag); + w.append("\n"); + + w.append("class ").append(className).append(" extends Element {\n\n"); + w.append(" public ").append(className).append("(String id) {\n"); + w.append(" super(id);\n"); + w.append(" }\n\n"); + for (Entry entry : getAttributes(tag).entrySet()) { + String attrName = entry.getKey(); + String attrType = entry.getValue(); + // getter + w.append(" public ").append(attrType).append(" ") + .append("get").append(className(attrName)).append("() {\n"); + w.append(" return (").append(attrType).append(")getAttribute(\"") + .append(attrName).append("\");\n"); + w.append(" }\n\n"); + // setter + w.append(" public void ") + .append("set").append(className(attrName)).append("(") + .append(attrType).append(" ").append(attributeName(attrName)).append(") {\n"); + w.append(" setAttribute(\"").append(attrName).append("\", ").append(attributeName(attrName)).append(");\n"); + w.append(" }\n\n"); + } + PredefinedFields.appendFields(w, tag); + w.append("}\n"); + } finally { + w.close(); + } + } catch (IOException ex) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e); + return null; + } + return className; + } + + Map getAttributes(String tagName) { + Map result = new HashMap(); + + if (model == null) { + // HtmlModelProvider modelProvider = Lookup.getDefault().lookup(HtmlModelProvider.class); + ServiceLoader hmpLoader = + ServiceLoader.load(HtmlModelProvider.class, this.getClass().getClassLoader()); + for (HtmlModelProvider htmlModelProvider : hmpLoader) { + model = htmlModelProvider.getModel(HtmlVersion.HTML5); + if (model != null) { + break; + } + } + } + + if (model == null) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, + "HTML 5 model provider was not found on classpath"); + return Collections.emptyMap(); + } + HtmlTag tag = model.getTag(tagName); + for (HtmlTagAttribute attr : tag.getAttributes()) { + String name = attr.getName(); + String type = Attributes.TYPES.get(name); + if (type != null) { + result.put(name, type); + } + } + + return result; + } + + private String className(String s) { + if (s.length() == 0) { + return s; + } + String name = NAMING_EXCEPTIONS.get(s.toLowerCase()); + if (name == null) { + name = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); + } + return name; + } + + private String attributeName(String s) { + if (Arrays.binarySearch(javaKeywords, s) >= 0) { + return String.format("%sAttr", s); + } + return s; + } + +} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Thu May 02 09:18:22 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Thu May 02 10:08:35 2013 +0200 @@ -299,109 +299,85 @@ } String pkg = findPkgName(e); - ProcessPage pp; - try (InputStream is = openStream(pkg, p.xhtml())) { - pp = ProcessPage.readPage(is); - is.close(); - } catch (IOException iOException) { - error("Can't read " + p.xhtml() + " as " + iOException.getMessage(), e); - ok = false; - pp = null; - } - Writer w; - String className = p.className(); - if (className.isEmpty()) { - int indx = p.xhtml().indexOf('.'); - className = p.xhtml().substring(0, indx); - } - try { - StringWriter body = new StringWriter(); - List propsGetSet = new ArrayList<>(); - List functions = new ArrayList<>(); - Map> propsDeps = new HashMap<>(); - Map> functionDeps = new HashMap<>(); - - Prprt[] props = createProps(e, p.properties()); - if (!generateComputedProperties(body, props, e.getEnclosedElements(), propsGetSet, propsDeps)) { - ok = false; + ProcessPage pp; + ElementGenerator eGen = new ElementGenerator(processingEnv); + try { + InputStream is = openStream(pkg, p.xhtml()); + pp = ProcessPage.readPage(is); + is.close(); + } catch (IOException iOException) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e); + return false; } - if (!generateOnChange(e, propsDeps, props, className, functionDeps)) { - ok = false; + Writer w; + String className = p.className(); + if (className.isEmpty()) { + int indx = p.xhtml().indexOf('.'); + className = p.xhtml().substring(0, indx); } - if (!generateProperties(e, body, props, propsGetSet, propsDeps, functionDeps)) { - ok = false; - } - if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) { - ok = false; - } - if (!generateReceive(e, body, className, e.getEnclosedElements(), functions)) { - ok = false; - } - - FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e); - w = new OutputStreamWriter(java.openOutputStream()); try { - w.append("package " + pkg + ";\n"); - w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n"); - w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n"); - w.append("final class ").append(className).append(" {\n"); - w.append(" private boolean locked;\n"); - if (!initializeOnClick(className, (TypeElement) e, w, pp)) { - ok = false; - } else { - if (pp != null) for (String id : pp.ids()) { + FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e); + w = new OutputStreamWriter(java.openOutputStream()); + try { + w.append("package " + pkg + ";\n"); + w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n"); + w.append("final class ").append(className).append(" {\n"); + w.append(" private boolean locked;\n"); + if (!initializeOnClick(className, (TypeElement) e, w, pp)) { + return false; + } + for (String id : pp.ids()) { String tag = pp.tagNameForId(id); - String type = type(tag); + String type = eGen.getType(pkg, tag, e); w.append(" ").append("public final "). append(type).append(' ').append(cnstnt(id)).append(" = new "). append(type).append("(\"").append(id).append("\");\n"); } + List propsGetSet = new ArrayList(); + Map> functionDeps = new HashMap<>(); + Map> propsDeps = new HashMap>(); + List functions = new ArrayList<>(); + Prprt[] props = createProps(e, p.properties()); + if (!generateComputedProperties(w, props, e.getEnclosedElements(), propsGetSet, propsDeps)) { + ok = false; + } + if (!generateOnChange(e, propsDeps, props, className, functionDeps)) { + ok = false; + } + if (!generateProperties(e, w, props, propsGetSet, propsDeps, functionDeps)) { + ok = false; + } + if (!generateFunctions(e, w, className, e.getEnclosedElements(), functions)) { + ok = false; + } + if (!generateReceive(e, w, className, e.getEnclosedElements(), functions)) { + ok = false; + } + w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n"); + if (!propsGetSet.isEmpty()) { + w.write("public " + className + " applyBindings() {\n"); + w.write(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings("); + w.write(className + ".class, this, "); + writeStringArray(propsGetSet, w); + w.append(", "); + writeStringArray(functions, w); + w.write(");\n return this;\n}\n"); + + w.write("public void triggerEvent(Element e, OnEvent ev) {\n"); + w.write(" org.apidesign.bck2brwsr.htmlpage.Knockout.triggerEvent(e.getId(), ev.getElementPropertyName());\n"); + w.write("}\n"); + } + w.append("}\n"); + } finally { + w.close(); } - w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n"); - w.append(body.toString()); - if (!propsGetSet.isEmpty()) { - w.write("public " + className + " applyBindings() {\n"); - w.write(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings("); - w.write(className + ".class, this, "); - writeStringArray(propsGetSet, w); - w.append(", "); - writeStringArray(functions, w); - w.write(");\n return this;\n}\n"); - - w.write("public void triggerEvent(Element e, OnEvent ev) {\n"); - w.write(" org.apidesign.bck2brwsr.htmlpage.Knockout.triggerEvent(e.getId(), ev.getElementPropertyName());\n"); - w.write("}\n"); - } - w.append("}\n"); - } finally { - w.close(); + } catch (IOException ex) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e); + return false; } - } catch (IOException ex) { - error("Can't create " + className + ".java", e); - return false; - } return ok; } - private static String type(String tag) { - if (tag.equals("title")) { - return "Title"; - } - if (tag.equals("button")) { - return "Button"; - } - if (tag.equals("input")) { - return "Input"; - } - if (tag.equals("canvas")) { - return "Canvas"; - } - if (tag.equals("img")) { - return "Image"; - } - return "Element"; - } - private static String cnstnt(String id) { return id.replace('.', '_').replace('-', '_'); } @@ -409,7 +385,6 @@ private boolean initializeOnClick( String className, TypeElement type, Writer w, ProcessPage pp ) throws IOException { - boolean ok = true; TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType(); { //for (Element clazz : pe.getEnclosedElements()) { // if (clazz.getKind() != ElementKind.CLASS) { @@ -422,27 +397,58 @@ On oc = method.getAnnotation(On.class); if (oc != null) { for (String id : oc.id()) { - if (pp == null) { - error("id = " + id + " not found in HTML page.", method); - ok = false; - continue; - } if (pp.tagNameForId(id) == null) { - error("id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method); - ok = false; - continue; + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method); + return false; } ExecutableElement ee = (ExecutableElement)method; - CharSequence params = wrapParams(ee, id, className, "ev", null); + StringBuilder params = new StringBuilder(); + { + boolean first = true; + for (VariableElement ve : ee.getParameters()) { + if (!first) { + params.append(", "); + } + first = false; + if (ve.asType() == stringType) { + if (ve.getSimpleName().contentEquals("id")) { + params.append('"').append(id).append('"'); + continue; + } + params.append("org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toString(ev, \""); + params.append(ve.getSimpleName().toString()); + params.append("\")"); + continue; + } + if (processingEnv.getTypeUtils().getPrimitiveType(TypeKind.DOUBLE) == ve.asType()) { + params.append("org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toDouble(ev, \""); + params.append(ve.getSimpleName().toString()); + params.append("\")"); + continue; + } + String rn = ve.asType().toString(); + int last = rn.lastIndexOf('.'); + if (last >= 0) { + rn = rn.substring(last + 1); + } + if (rn.equals(className)) { + params.append(className).append(".this"); + continue; + } + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, + "@On method can only accept String named 'id' or " + className + " arguments", + ee + ); + return false; + } + } if (!ee.getModifiers().contains(Modifier.STATIC)) { - error("@On method has to be static", ee); - ok = false; - continue; + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee); + return false; } if (ee.getModifiers().contains(Modifier.PRIVATE)) { - error("@On method can't be private", ee); - ok = false; - continue; + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method can't be private", ee); + return false; } w.append(" OnEvent." + oc.event()).append(".of(").append(cnstnt(id)). append(").perform(new OnDispatch(" + dispatchCnt + "));\n"); @@ -473,7 +479,7 @@ } - return ok; + return true; } @Override @@ -487,7 +493,8 @@ Element cls = findClass(element); Page p = cls.getAnnotation(Page.class); - String pkg = findPkgName(cls); + PackageElement pe = (PackageElement) cls.getEnclosingElement(); + String pkg = pe.getQualifiedName().toString(); ProcessPage pp; try { InputStream is = openStream(pkg, p.xhtml()); @@ -497,7 +504,7 @@ return Collections.emptyList(); } - List cc = new ArrayList<>(); + List cc = new ArrayList(); userText = userText.substring(1); for (String id : pp.ids()) { if (id.startsWith(userText)) { @@ -771,9 +778,9 @@ } private boolean generateFunctions( - Element clazz, StringWriter body, String className, + Element clazz, Writer body, String className, List enclosedElements, List functions - ) { + ) throws IOException { for (Element m : enclosedElements) { if (m.getKind() != ElementKind.METHOD) { continue; @@ -868,9 +875,9 @@ } private boolean generateReceive( - Element clazz, StringWriter body, String className, + Element clazz, Writer body, String className, List enclosedElements, List functions - ) { + ) throws IOException { for (Element m : enclosedElements) { if (m.getKind() != ElementKind.METHOD) { continue; diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PredefinedFields.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PredefinedFields.java Thu May 02 10:08:35 2013 +0200 @@ -0,0 +1,65 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage; + +import java.io.IOException; +import java.io.Writer; +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Jan Horvath + */ +public class PredefinedFields { + + private static final Map IMPORTS = new HashMap() { + { + put("canvas", "import org.apidesign.bck2brwsr.core.JavaScriptBody;"); + } + }; + + private static final Map FIELDS = new HashMap() { + { + put("canvas", + " @JavaScriptBody(\n" + + " args = {\"el\"},\n" + + " body = \"var e = window.document.getElementById(el._id());\\n\"\n" + + " + \"return e.getContext('2d');\\n\")\n" + + " private native static Object getContextImpl(Canvas el);\n" + + " \n" + + " public GraphicsContext getContext() {\n" + + " return new GraphicsContext(getContextImpl(this));\n" + + " }"); + } + }; + + static void appendImports(Writer w, String tag) throws IOException { + String text = IMPORTS.get(tag.toLowerCase()); + if (text != null) { + w.append(text).append("\n"); + } + } + + static void appendFields(Writer w, String tag) throws IOException { + String text = FIELDS.get(tag.toLowerCase()); + if (text != null) { + w.append(text).append("\n"); + } + } +} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Button.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Button.java Thu May 02 09:18:22 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.htmlpage.api; - -/** - * - * @author Jaroslav Tulach - */ -public final class Button extends Element { - public Button(String id) { - super(id); - } - - @Override - void dontSubclass() { - } - - public void setDisabled(boolean state) { - setAttribute(this, "disabled", state); - } -} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Canvas.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Canvas.java Thu May 02 09:18:22 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.htmlpage.api; - -import org.apidesign.bck2brwsr.core.JavaScriptBody; -import static org.apidesign.bck2brwsr.htmlpage.api.Element.getAttribute; - -/** - * - * @author Anton Epple - */ -public class Canvas extends Element { - - public Canvas(String id) { - super(id); - } - - public void setHeight(int height) { - setAttribute(this, "height", height); - } - - public int getHeight() { - Object ret = getAttribute(this, "height"); - return (ret instanceof Number) ? ((Number)ret).intValue(): Integer.MIN_VALUE; - } - - public void setWidth(int width) { - setAttribute(this, "width", width); - } - - public int getWidth() { - Object ret = getAttribute(this, "width"); - return (ret instanceof Number) ? ((Number)ret).intValue(): Integer.MIN_VALUE; - } - - @JavaScriptBody( - args = {"el"}, - body = "var e = window.document.getElementById(el._id());\n" - + "return e.getContext('2d');\n") - private native static Object getContextImpl(Canvas el); - - public GraphicsContext getContext() { - return new GraphicsContext(getContextImpl(this)); - } - - @Override - void dontSubclass() { - } -} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java Thu May 02 09:18:22 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java Thu May 02 10:08:35 2013 +0200 @@ -37,7 +37,13 @@ return id; } - abstract void dontSubclass(); + public String getText() { + return (String)getAttribute("innerHTML"); + } + + public void setText(String text) { + setAttribute("innerHTML", text); + } @JavaScriptBody( args={"el", "property", "value"}, diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/GraphicsContext.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/GraphicsContext.java Thu May 02 09:18:22 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/GraphicsContext.java Thu May 02 10:08:35 2013 +0200 @@ -27,7 +27,7 @@ Object context; - GraphicsContext(Object contextImpl) { + public GraphicsContext(Object contextImpl) { this.context = contextImpl; } @@ -113,15 +113,15 @@ @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);") public native void scale(double x, double y); - public void drawImage(Image image, double x, double y) { + public void drawImage(Element image, double x, double y) { drawImageImpl(context, Element.getElementById(image), x, y); } - public void drawImage(Image image, double x, double y, double width, double height) { + public void drawImage(Element image, double x, double y, double width, double height) { drawImageImpl(context, Element.getElementById(image), x, y, width, height); } - public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) { + public void drawImage(Element image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) { drawImageImpl(context, Element.getElementById(image), sx, sy, sWidth, sHeight, x, y, width, height); } @@ -319,12 +319,12 @@ @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);") private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1); - public Pattern createPattern(Image image, String repeat) { + public Pattern createPattern(Element image, String repeat) { return new Pattern(createPatternImpl(context, image, repeat)); } @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);") - private static native Object createPatternImpl(Object context, Image image, String repeat); + private static native Object createPatternImpl(Object context, Element image, String repeat); public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) { return new RadialGradient(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1)); diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Image.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Image.java Thu May 02 09:18:22 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.htmlpage.api; - -/** - * - * @author Anton Epple - */ -public class Image extends Element{ - - public Image(String id) { - super(id); - } - - - - @Override - void dontSubclass() { - } - -} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Input.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Input.java Thu May 02 09:18:22 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.htmlpage.api; - -/** - * - * @author Jaroslav Tulach - */ -public final class Input extends Element { - public Input(String id) { - super(id); - } - - @Override - void dontSubclass() { - } - - public void setAutocomplete(boolean state) { - setAttribute(this, "autocomplete", state); - } - - public final String getValue() { - return (String)getAttribute(this, "value"); - } - - public final void setValue(String txt) { - setAttribute(this, "value", txt); - } -} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Title.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Title.java Thu May 02 09:18:22 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.htmlpage.api; - -/** - * - * @author Jaroslav Tulach - */ -public class Title extends Element { - public Title(String id) { - super(id); - } - - @Override - void dontSubclass() { - } - - public final void setText(String text) { - setAttribute(this, "innerHTML", text); - } -} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ElementGeneratorTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ElementGeneratorTest.java Thu May 02 10:08:35 2013 +0200 @@ -0,0 +1,35 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage; + +import java.util.Map; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Jan Horvath + */ +public class ElementGeneratorTest { + + @Test public void testGetAttributes() { + ElementGenerator gen = new ElementGenerator(null); + Map attrs = gen.getAttributes("input"); + assertEquals(attrs.get("width"), "Integer", "Expected type of width attribute is Integer"); + } +} diff -r 3800d11c0bdb -r 9321b4016d5c javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageTest.java --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageTest.java Thu May 02 09:18:22 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.htmlpage; - -import java.io.IOException; -import java.util.Locale; -import javax.tools.Diagnostic; -import javax.tools.JavaFileObject; -import static org.testng.Assert.*; -import org.testng.annotations.Test; - -/** Verify errors emitted by the processor. - * - * @author Jaroslav Tulach - */ -public class PageTest { - @Test public void verifyWrongType() throws IOException { - String html = "" - + ""; - String code = "package x.y.z;\n" - + "import org.apidesign.bck2brwsr.htmlpage.api.*;\n" - + "@Page(xhtml=\"index.xhtml\", className=\"Model\", properties={\n" - + " @Property(name=\"prop\", type=Runnable.class)\n" - + "})\n" - + "class X {\n" - + "}\n"; - - Compile c = Compile.create(html, code); - assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors()); - for (Diagnostic e : c.getErrors()) { - String msg = e.getMessage(Locale.ENGLISH); - if (!msg.contains("Runnable")) { - fail("Should contain warning about Runnable: " + msg); - } - } - } - -}