Dynamic Action Hookup Test : Dynamic Proxy « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Dynamic Proxy 




Dynamic Action Hookup Test

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Method;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class DynamicHookupTest extends JFrame {
  DynamicActionAdapter actionAdapter = new DynamicActionAdapter();

  JLabel label = new JLabel("Ready...", JLabel.CENTER);

  int count;

  public DynamicHookupTest() {
    JButton launchButton = new JButton("Launch!");
    getContentPane().add(launchButton, "South");
    getContentPane().add(label, "Center");
    actionAdapter.hookup(launchButton, this, "launchTheMissiles");
  }

  public void launchTheMissiles() {
    label.setText("Launched: " + count++);
  }

  public static void main(String[] args) {
    JFrame f = new DynamicHookupTest();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
    f.setSize(150150);
    f.setVisible(true);
  }
}

class DynamicActionAdapter implements ActionListener {
  Hashtable actions = new Hashtable();

  public void hookup(Object sourceObject, Object targetObject, String targetMethod) {
    actions.put(sourceObject, new Target(targetObject, targetMethod));
    invokeReflectedMethod(sourceObject, "addActionListener"new Object[] { this },
        new Class[] { ActionListener.class });
  }

  public void actionPerformed(ActionEvent e) {
    Target target = (Targetactions.get(e.getSource());
    if (target == null)
      throw new RuntimeException("unknown source");
    invokeReflectedMethod(target.object, target.methodName, null, null);
  }

  private void invokeReflectedMethod(Object target, String methodName, Object[] args,
      Class[] argTypes) {

    try {
      Method method = target.getClass().getMethod(methodName, argTypes);
      method.invoke(target, args);
    catch (Exception e) {
      throw new RuntimeException("invocation problem: " + e);
    }
  }

  class Target {
    Object object;

    String methodName;

    Target(Object object, String methodName) {
      this.object = object;
      this.methodName = methodName;
    }
  }
}

           
       














Related examples in the same category
1.Demonstrates a dangerous use of proxy names
2.Demonstrates the use of factories to generate proxies
3.Demonstrates a dynamic proxy
4.Demonstrates the usage of a counting proxy
5.Demonstrates the use of interface based proxies
6.Demonstrates the basic concept of proxies generated by clients to the proxies
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.