Author: kmorin Date: 2009-07-27 14:37:18 +0200 (Mon, 27 Jul 2009) New Revision: 1531 Added: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/GwtEventHandler.java trunk/guix-compiler-gwt/src/test/java/org/ trunk/guix-compiler-gwt/src/test/java/org/nuiton/ trunk/guix-compiler-gwt/src/test/java/org/nuiton/guix/ trunk/guix-compiler-gwt/src/test/java/org/nuiton/guix/GwtEventHandlerTest.java Removed: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/events/GwtEventHandler.java Modified: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java Log: add not null parameters condition in GwtEventHandler's addEvent method + add test for GwtEventHandler methods Copied: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/GwtEventHandler.java (from rev 1518, trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/events/GwtEventHandler.java) =================================================================== --- trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/GwtEventHandler.java (rev 0) +++ trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/GwtEventHandler.java 2009-07-27 12:37:18 UTC (rev 1531) @@ -0,0 +1,150 @@ +/** + * *##% guix-compiler-gwt + * Copyright (C) 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.nuiton.guix; + +import java.beans.BeanInfo; +import java.beans.EventSetDescriptor; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Handles the events of GWT + * + * @author kmorin + */ +public class GwtEventHandler { + /** log */ + Log log = LogFactory.getLog(GwtEventHandler.class); + /** Maps EventSetDescriptors with a map of the listener method and actions to execute */ + Map<EventSetDescriptor, Map<Method, String>> map = new HashMap<EventSetDescriptor, Map<Method, String>>(); + + /** + * Add an event to the map + * + * @param clazz the class of the object... + * @param name name of the event + * @param value code to execute when the events occurs + * @return true if the name corresponds with an event + * @throws java.beans.IntrospectionException if the JVM failed while getting the bean infos of the class + */ + public boolean addEvent(Class clazz, String name, String value) throws IntrospectionException { + if(clazz == null || name == null || value == null) { + return false; + } + //get the bean info of the class + BeanInfo bi = Introspector.getBeanInfo(clazz); + //get the array of EventSetDescriptor of the class + EventSetDescriptor[] esds = bi.getEventSetDescriptors(); + + int i = 0; + int j = 0; + EventSetDescriptor esd = null; + Method method = null; + + //while any event with the name "name" has been found, browse the EventSetDescriptors + while (i < esds.length && method == null) { + j = 0; + //get the listener methods of the EventSetDescriptor + Method[] methods = esds[i].getAddListenerMethod().getParameterTypes()[0].getMethods(); + while (j < methods.length && method == null) { + if(methods[j].getName().equals(name)) { + method = methods[j]; + esd = esds[i]; + } + j++; + } + i++; + } + //if the event has been found in the bean info + if (method != null) { + //remove braces + while(value.startsWith("{") && value.endsWith("}")) + value = value.substring(1, value.length() - 1); + //add final semicolon + if(!value.endsWith(";")) + value += ";"; + //if the EventSetDescriptor has not been yet recorded for another event + if (map.get(esd) == null) { + Map<Method, String> methods = new HashMap<Method, String>(); + methods.put(method, value); + map.put(esd, methods); + } + else { + map.get(esd).put(method, value); + } + } + else if(log.isWarnEnabled()) { + log.warn("Event " + name + " not found."); + } + return method != null; + } + + /** + * Generates the code for all the events applied to an object + * + * @param object the objects to add the listeners + * @return the generated code + */ + public String generate(String object) { + //result + StringBuffer result = new StringBuffer(); + //for each EventSetDescriptor + for (EventSetDescriptor e : map.keySet()) { + //add new ListenerMethod + result.append(object) + .append(".") + .append(e.getAddListenerMethod().getName()) + .append("(new ") + .append(e.getListenerType().getCanonicalName()) + //if only one event is handled for this EventSetDescriptor then add a listener, else add an adapter + .append(e.getAddListenerMethod().getParameterTypes()[0].getMethods().length == 1 ? "" : "Adapter") + .append("() {\n"); + //for every event handled for the EventSetDescriptor + for(Method m : map.get(e).keySet()) { + for(Annotation a : m.getDeclaredAnnotations()) + result.append(a.toString()).append("\n"); + //add the method that handles the event + result.append("public ") + .append(m.getReturnType().getCanonicalName()) + .append(" ") + .append(m.getName()) + .append("("); + for(int n = 0 ; n < m.getParameterTypes().length ; n++) { + result.append(m.getParameterTypes()[n].getCanonicalName()) + .append(" arg") + .append(n) + .append(", "); + } + result.delete(result.length() - 2, result.length()) + .append(")") + .append("{\n") + .append(map.get(e).get(m)) + .append("\n}\n"); + } + result.append("});\n\n"); + } + return result.toString(); + } +} Property changes on: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/GwtEventHandler.java ___________________________________________________________________ Added: svn:mergeinfo + Deleted: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/events/GwtEventHandler.java =================================================================== --- trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/events/GwtEventHandler.java 2009-07-27 07:29:15 UTC (rev 1530) +++ trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/events/GwtEventHandler.java 2009-07-27 12:37:18 UTC (rev 1531) @@ -1,147 +0,0 @@ -/** - * *##% guix-compiler-gwt - * Copyright (C) 2009 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.nuiton.guix.events; - -import java.beans.BeanInfo; -import java.beans.EventSetDescriptor; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Handles the events of GWT - * - * @author kmorin - */ -public class GwtEventHandler { - /** log */ - Log log = LogFactory.getLog(GwtEventHandler.class); - /** Maps EventSetDescriptors with a map of the listener method and actions to execute */ - Map<EventSetDescriptor, Map<Method, String>> map = new HashMap<EventSetDescriptor, Map<Method, String>>(); - - /** - * Add an event to the map - * - * @param clazz the class of the object... - * @param name name of the event - * @param value code to execute when the events occurs - * @return true if the name corresponds with an event - * @throws java.beans.IntrospectionException if the JVM failed while getting the bean infos of the class - */ - public boolean addEvent(Class clazz, String name, String value) throws IntrospectionException { - //get the bean info of the class - BeanInfo bi = Introspector.getBeanInfo(clazz); - //get the array of EventSetDescriptor of the class - EventSetDescriptor[] esds = bi.getEventSetDescriptors(); - - int i = 0; - int j = 0; - EventSetDescriptor esd = null; - Method method = null; - - //while any event with the name "name" has been found, browse the EventSetDescriptors - while (i < esds.length && method == null) { - j = 0; - //get the listener methods of the EventSetDescriptor - Method[] methods = esds[i].getAddListenerMethod().getParameterTypes()[0].getMethods(); - while (j < methods.length && method == null) { - if(methods[j].getName().equals(name)) { - method = methods[j]; - esd = esds[i]; - } - j++; - } - i++; - } - //if the event has been found in the bean info - if (method != null) { - //remove braces - while(value.startsWith("{") && value.endsWith("}")) - value = value.substring(1, value.length() - 1); - //add final semicolon - if(!value.endsWith(";")) - value += ";"; - //if the EventSetDescriptor has not been yet recorded for another event - if (map.get(esd) == null) { - Map<Method, String> methods = new HashMap<Method, String>(); - methods.put(method, value); - map.put(esd, methods); - } - else { - map.get(esd).put(method, value); - } - } - else - if(log.isWarnEnabled()) - log.warn("Event " + name + " not found."); - return method != null; - } - - /** - * Generates the code for all the events applied to an object - * - * @param object the objects to add the listeners - * @return the generated code - */ - public String generate(String object) { - //result - StringBuffer result = new StringBuffer(); - //for each EventSetDescriptor - for (EventSetDescriptor e : map.keySet()) { - //add new ListenerMethod - result.append(object) - .append(".") - .append(e.getAddListenerMethod().getName()) - .append("(new ") - .append(e.getListenerType().getCanonicalName()) - //if only one event is handled for this EventSetDescriptor then add a listener, else add an adapter - .append(e.getAddListenerMethod().getParameterTypes()[0].getMethods().length == 1 ? "" : "Adapter") - .append("() {\n"); - //for every event handled for the EventSetDescriptor - for(Method m : map.get(e).keySet()) { - for(Annotation a : m.getDeclaredAnnotations()) - result.append(a.toString()).append("\n"); - //add the method that handles the event - result.append("public ") - .append(m.getReturnType().getCanonicalName()) - .append(" ") - .append(m.getName()) - .append("("); - for(int n = 0 ; n < m.getParameterTypes().length ; n++) { - result.append(m.getParameterTypes()[n].getCanonicalName()) - .append(" arg") - .append(n) - .append(", "); - } - result.delete(result.length() - 2, result.length()) - .append(")") - .append("{\n") - .append(map.get(e).get(m)) - .append("\n}\n"); - } - result.append("});\n\n"); - } - return result.toString(); - } -} Modified: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java =================================================================== --- trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java 2009-07-27 07:29:15 UTC (rev 1530) +++ trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java 2009-07-27 12:37:18 UTC (rev 1531) @@ -30,7 +30,7 @@ import java.util.List; import java.util.Map; import org.nuiton.guix.databinding.BindingUtils; -import org.nuiton.guix.events.GwtEventHandler; +import org.nuiton.guix.GwtEventHandler; import org.nuiton.guix.model.AttributeDescriptor; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.StyleSheet; Added: trunk/guix-compiler-gwt/src/test/java/org/nuiton/guix/GwtEventHandlerTest.java =================================================================== --- trunk/guix-compiler-gwt/src/test/java/org/nuiton/guix/GwtEventHandlerTest.java (rev 0) +++ trunk/guix-compiler-gwt/src/test/java/org/nuiton/guix/GwtEventHandlerTest.java 2009-07-27 12:37:18 UTC (rev 1531) @@ -0,0 +1,80 @@ +/** + * *##% guix-compiler-swing + * Copyright (C) 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ +package org.nuiton.guix; + +import com.google.gwt.user.client.ui.FocusWidget; +import com.google.gwt.user.client.ui.Widget; +import java.beans.IntrospectionException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Test; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests the methods of the class SwingEventTestHandler + * + * @author kmorin + */ +public class GwtEventHandlerTest { + + Log log = LogFactory.getLog(GwtEventHandlerTest.class); + + /** + * Tests the method addEvent + */ + @Test + public void testAddEvent() { + GwtEventHandler geh = new GwtEventHandler(); + try { + assertFalse(geh.addEvent(null, "", "")); + assertFalse(geh.addEvent(Widget.class, null, "")); + assertFalse(geh.addEvent(Widget.class, "", null)); + assertTrue(geh.addEvent(FocusWidget.class, "onClick", "test1")); + assertTrue(geh.addEvent(FocusWidget.class, "onClick", "test2")); + assertFalse(geh.addEvent(FocusWidget.class, "azerty", "")); + } + catch (IntrospectionException eee) { + if(log.isErrorEnabled()) { + log.error(eee); + } + } + } + + /** + * Tests the method generate + */ + @Test + public void testGenerate() { + GwtEventHandler seh = new GwtEventHandler(); + try { + assertNotNull(seh.generate("")); + assertEquals(seh.generate(""), ""); + seh.addEvent(FocusWidget.class, "onClick", ""); + assertTrue(seh.generate("").length() > 0); + } + catch (IntrospectionException eee) { + if(log.isErrorEnabled()) { + log.error(eee); + } + } + } +}