package com.jeromecompsci.dgui;

import java.lang.reflect.InvocationTargetException;

/**
 * @author Derek Wang
 */
abstract class BindableWidget extends Widget {
    public void bind(final Object obj, final String methodName) {
        addBindingAsListener(new Binding() {
            @Override Class getTargetClass() {
                return obj.getClass();
            }
            @Override Object getTargetObject() {
                return obj;
            }
            @Override String getTargetMethodName() {
                return methodName;
            }
        });
    }

    public void bind(final Class cls, final String methodName) {
        addBindingAsListener(new Binding() {
            @Override Class getTargetClass() {
                return cls;
            }
            @Override Object getTargetObject() {
                return null;
            }
            @Override String getTargetMethodName() {
                return methodName;
            }
        });
    }

    abstract void addBindingAsListener(final Binding binding);
}

/**
 * @author Derek Wang
 */
abstract class Binding {

    abstract Class getTargetClass();
    abstract Object getTargetObject();
    abstract String getTargetMethodName();

    void executeBoundMethod() {
        try {
            getTargetClass().getMethod(getTargetMethodName()).invoke(getTargetObject());
        } catch (NoSuchMethodException e) {
            e.printStackTrace(System.err);
        } catch (IllegalAccessException e) {
            e.printStackTrace(System.err);
        } catch (InvocationTargetException e) {
            e.printStackTrace(System.err);
        }
    }
}

@@ -267,726 +267,178 @@
 ing(
-) %7B%0A            @Override Class getTargetClass() %7B%0A                return obj.getClass();%0A            %7D%0A            @Override Object getTargetObject() %7B%0A                return obj;%0A            %7D%0A            @Override String getTargetMethodName() %7B%0A                return methodName;%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public void bind(final Class cls, final String methodName) %7B%0A        addBindingAsListener(new Binding() %7B%0A            @Override Class getTargetClass() %7B%0A                return cls;%0A            %7D%0A            @Override Object getTargetObject() %7B%0A                return null;%0A            %7D%0A            @Override String getTargetMethodName() %7B%0A                return methodName;%0A            %7D%0A        %7D
+obj.getClass(), obj, methodName));%0A    %7D%0A%0A    public void bind(final Class cls, final String methodName) %7B%0A        addBindingAsListener(new Binding(cls, null, methodName)
 );%0A 
@@ -535,33 +535,24 @@
 ek Wang%0A */%0A
-abstract 
 class Bindin
@@ -564,27 +564,23 @@
     
-abstract
+private
  Class 
-getT
+t
 arge
@@ -589,32 +589,29 @@
 lass
-()
 ;%0A    
-abstract
+private
  Object 
 getT
@@ -606,20 +606,17 @@
  Object 
-getT
+t
 argetObj
@@ -622,54 +622,271 @@
 ject
-()
 ;%0A    
-abstract String getTargetMethodName();
+private String targetMethodName;%0A%0A    public Binding(Class targetClass, Object targetObject, String targetMethodName) %7B%0A        this.targetClass = targetClass;%0A        this.targetObject = targetObject;%0A        this.targetMethodName = targetMethodName;%0A    %7D
 %0A%0A  
@@ -941,20 +941,17 @@
         
-getT
+t
 argetCla
@@ -952,18 +952,16 @@
 getClass
-()
 .getMeth
@@ -963,20 +963,17 @@
 tMethod(
-getT
+t
 argetMet
@@ -979,18 +979,16 @@
 thodName
-()
 ).invoke
@@ -992,12 +992,9 @@
 oke(
-getT
+t
 arge
@@ -1000,18 +1000,16 @@
 etObject
-()
 );%0A     

Simplify Binding logic -- no need for abstract methods, a custom constructor does the job nicely