Author: kmorin Date: 2009-07-27 14:37:53 +0200 (Mon, 27 Jul 2009) New Revision: 1532 Added: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/SwingEventHandler.java trunk/guix-compiler-swing/src/test/java/org/ trunk/guix-compiler-swing/src/test/java/org/nuiton/ trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/ trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/SwingEventHandlerTest.java Removed: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/events/SwingEventHandler.java Modified: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java Log: add not null parameters condition in SwingEventHandler's addEvent method + add test for SwingEventHandler methods Copied: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/SwingEventHandler.java (from rev 1518, trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/events/SwingEventHandler.java) =================================================================== --- trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/SwingEventHandler.java (rev 0) +++ trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/SwingEventHandler.java 2009-07-27 12:37:53 UTC (rev 1532) @@ -0,0 +1,157 @@ +/** + * *##% 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 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 Swing events + * + * @author kmorin + */ +public class SwingEventHandler { + + /** log */ + Log log = LogFactory.getLog(SwingEventHandler.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 supposed to listen to the event + * @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].getListenerMethods(); + while (j < esds[i].getListenerMethods().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 if(map.get(esd).get(method) != null) { + map.get(esd).put(method, map.get(esd).get(method) + value); + } + 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 every EventSetDescriptor + for (EventSetDescriptor e : map.keySet()) { + //add new ListenerMethod + result.append(object) + .append(".") + .append(e.getAddListenerMethod().getName()) + .append("(new ") + //if only one event is handled for this EventSetDescriptor then add a listener, else add an adapter + .append(e.getListenerMethods().length == 1 ? + e.getListenerType().getCanonicalName() : + e.getListenerType().getCanonicalName().replace("Listener", "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(Class clazz : m.getParameterTypes()) + result.append(clazz.getCanonicalName()) + .append(" ") + .append(Character.toLowerCase(clazz.getName().charAt(clazz.getName().lastIndexOf('.') + 1))) + .append(clazz.getName().substring(clazz.getName().lastIndexOf('.') + 2)) + .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-swing/src/main/java/org/nuiton/guix/SwingEventHandler.java ___________________________________________________________________ Added: svn:mergeinfo + Deleted: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/events/SwingEventHandler.java =================================================================== --- trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/events/SwingEventHandler.java 2009-07-27 12:37:18 UTC (rev 1531) +++ trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/events/SwingEventHandler.java 2009-07-27 12:37:53 UTC (rev 1532) @@ -1,152 +0,0 @@ -/** - * *##% 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.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 Swing events - * - * @author kmorin - */ -public class SwingEventHandler { - - /** log */ - Log log = LogFactory.getLog(SwingEventHandler.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].getListenerMethods(); - while (j < esds[i].getListenerMethods().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 if(map.get(esd).get(method) != null) { - map.get(esd).put(method, map.get(esd).get(method) + value); - } - 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 every EventSetDescriptor - for (EventSetDescriptor e : map.keySet()) { - //add new ListenerMethod - result.append(object) - .append(".") - .append(e.getAddListenerMethod().getName()) - .append("(new ") - //if only one event is handled for this EventSetDescriptor then add a listener, else add an adapter - .append(e.getListenerMethods().length == 1 ? - e.getListenerType().getCanonicalName() : - e.getListenerType().getCanonicalName().replace("Listener", "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(Class clazz : m.getParameterTypes()) - result.append(clazz.getCanonicalName()) - .append(" ") - .append(Character.toLowerCase(clazz.getName().charAt(clazz.getName().lastIndexOf('.') + 1))) - .append(clazz.getName().substring(clazz.getName().lastIndexOf('.') + 2)) - .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-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java =================================================================== --- trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java 2009-07-27 12:37:18 UTC (rev 1531) +++ trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java 2009-07-27 12:37:53 UTC (rev 1532) @@ -34,7 +34,7 @@ import java.util.Map; import java.util.Map.Entry; import org.nuiton.guix.databinding.BindingUtils; -import org.nuiton.guix.events.SwingEventHandler; +import org.nuiton.guix.SwingEventHandler; import org.nuiton.guix.model.AttributeDescriptor; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.Rule; @@ -539,7 +539,7 @@ * Transform the css attributes into AttributeDescriptors * * @param gmo the GuixModelObject which has got the css attributes - * @param seh a SwingEventHandler + * @param seh a SwingEventHandlerTest * @param clazz gmo's class */ private void processCSSAttributes(GuixModelObject gmo, SwingEventHandler seh, Class clazz) { Added: trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/SwingEventHandlerTest.java =================================================================== --- trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/SwingEventHandlerTest.java (rev 0) +++ trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/SwingEventHandlerTest.java 2009-07-27 12:37:53 UTC (rev 1532) @@ -0,0 +1,79 @@ +/** + * *##% 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 java.beans.IntrospectionException; +import javax.swing.JComponent; +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 SwingEventHandlerTest { + + Log log = LogFactory.getLog(SwingEventHandlerTest.class); + + /** + * Tests the method addEvent + */ + @Test + public void testAddEvent() { + SwingEventHandler seh = new SwingEventHandler(); + try { + assertFalse(seh.addEvent(null, "", "")); + assertFalse(seh.addEvent(JComponent.class, null, "")); + assertFalse(seh.addEvent(JComponent.class, "", null)); + assertTrue(seh.addEvent(JComponent.class, "propertyChange", "test1")); + assertTrue(seh.addEvent(JComponent.class, "propertyChange", "test2")); + assertFalse(seh.addEvent(JComponent.class, "azerty", "")); + } + catch (IntrospectionException eee) { + if(log.isErrorEnabled()) { + log.error(eee); + } + } + } + + /** + * Tests the method generate + */ + @Test + public void testGenerate() { + SwingEventHandler seh = new SwingEventHandler(); + try { + assertNotNull(seh.generate("")); + assertEquals(seh.generate(""), ""); + seh.addEvent(JComponent.class, "propertyChange", ""); + assertTrue(seh.generate("").length() > 0); + } + catch (IntrospectionException eee) { + if(log.isErrorEnabled()) { + log.error(eee); + } + } + } +}