Clover coverage report - Java Gui Builder - Code Coverage
Coverage timestamp: ven. juil. 11 2003 11:10:09 EDT
file stats: LOC: 122   Methods: 4
NCLOC: 90   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
RegisterTagHandler.java 100% 100% 100% 100%
coverage
 1   
 /**
 2   
  * Java Gui Builder - A library to build GUIs using an XML file.
 3   
  * Copyright 2002, 2003 (C) François Beausoleil
 4   
  *
 5   
  * This library is free software; you can redistribute it and/or
 6   
  * modify it under the terms of the GNU Lesser General Public
 7   
  * License as published by the Free Software Foundation; either
 8   
  * version 2.1 of the License, or (at your option) any later version.
 9   
  *
 10   
  * This library is distributed in the hope that it will be useful,
 11   
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13   
  * Lesser General Public License for more details.
 14   
  *
 15   
  * You should have received a copy of the GNU Lesser General Public
 16   
  * License along with this library; if not, write to the Free Software
 17   
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 18   
  */
 19   
 
 20   
 package jgb.handlers.swing;
 21   
 
 22   
 import jgb.builder.TagHandler;
 23   
 import jgb.builder.WindowContext;
 24   
 import org.xml.sax.SAXException;
 25   
 
 26   
 import java.lang.reflect.InvocationHandler;
 27   
 import java.lang.reflect.Method;
 28   
 import java.lang.reflect.Proxy;
 29   
 import java.util.EventObject;
 30   
 import java.util.Map;
 31   
 
 32   
 public class RegisterTagHandler extends AbstractTagHandler {
 33   
     public static final String ATTR_METHOD = "method";
 34   
     public static final String ATTR_MANAGER = "manager";
 35   
     public static final String ATTR_EVENT = "event";
 36   
 
 37  9
     protected void enterElement(Map atts) throws SAXException {
 38  9
         String className = (String)atts.get(ATTR_CLASS);
 39  9
         String eventName = (String)atts.get(ATTR_EVENT);
 40  9
         String managerName = (String)atts.get(ATTR_MANAGER);
 41  9
         String methodName = (String)atts.get(ATTR_METHOD);
 42   
 
 43  9
         try {
 44  9
             Class listenerClass;
 45  9
             try {
 46  9
                 listenerClass = Class.forName(className);
 47   
             } catch (ClassNotFoundException e) {
 48  2
                 try {
 49  2
                     listenerClass = Class.forName("java.awt.event." + className);
 50   
                 } catch (ClassNotFoundException e1) {
 51  1
                     listenerClass = Class.forName("javax.swing.event." + className);
 52   
                 }
 53   
             }
 54   
 
 55  9
             if (!listenerClass.isInterface()) {
 56  1
                 throwParsingException("register listener class must be an interface",
 57   
                         new IllegalArgumentException(listenerClass.getName()));
 58   
             }
 59   
 
 60  8
             final Object managerObject = getObject(managerName);
 61  8
             final Method reflectedEventMethod = managerObject.getClass()
 62   
                     .getMethod(methodName,
 63   
                             new Class[]{
 64   
                                 EventObject.class,
 65   
                                 WindowContext.class}
 66   
                     );
 67  8
             final Object listenerInstance = Proxy.newProxyInstance(
 68   
                     this.getClass().getClassLoader(),
 69   
                     new Class[]{listenerClass},
 70   
                     new EventReflector(
 71   
                             eventName, managerObject, reflectedEventMethod,
 72   
                             (WindowContext)tagContext.get(TagHandler.WINDOW_CONTEXT_KEY)
 73   
                     )
 74   
             );
 75   
 
 76  8
             final Object parentObject = getCurrentObject();
 77  8
             final Class parentObjectClass = parentObject.getClass();
 78  8
             final String listenerClassName;
 79  8
             final int lastDot = listenerClass.getName().lastIndexOf('.');
 80  8
             listenerClassName = listenerClass.getName().substring(1 + lastDot);
 81  8
             final Method addListenerMethod = parentObjectClass.getMethod(
 82   
                     "add" + listenerClassName, new Class[]{listenerClass});
 83  7
             addListenerMethod.invoke(parentObject,
 84   
                     new Object[]{listenerInstance});
 85   
         } catch (SAXException e) {
 86  1
             throw e;
 87   
         } catch (Exception e) {
 88  1
             throwParsingException("Unable to register event listener for event " +
 89   
                     eventName + " on object " + getCurrentObjectId() + " for " +
 90   
                     "class " + className, e);
 91   
         }
 92   
     }
 93   
 
 94  3
     protected void exitElement() throws SAXException {
 95   
     }
 96   
 
 97   
     private static final class EventReflector implements InvocationHandler {
 98   
         private final String eventName;
 99   
         private final Object managerObject;
 100   
         private final Method reflectedEventMethod;
 101   
         private final WindowContext windowContext;
 102   
 
 103  8
         public EventReflector(String eventName, Object managerObject, Method reflectedEventMethod, WindowContext windowContext) {
 104  8
             this.eventName = eventName;
 105  8
             this.managerObject = managerObject;
 106  8
             this.reflectedEventMethod = reflectedEventMethod;
 107  8
             this.windowContext = windowContext;
 108   
         }
 109   
 
 110  2
         public Object invoke(Object proxy, Method method, Object[] args)
 111   
                 throws Throwable {
 112  2
             if (eventName.equals(method.getName())) {
 113  1
                 reflectedEventMethod.invoke(
 114   
                         managerObject,
 115   
                         new Object[]{args[0], windowContext}
 116   
                 );
 117   
             }
 118  2
             return null;
 119   
         }
 120   
     }
 121   
 }
 122