From 3aec46c5bb839d171f5cda61d6e63890ac16224a Mon Sep 17 00:00:00 2001 From: codistmonk Date: Sat, 3 Jul 2010 14:41:12 +0000 Subject: [PATCH] [SubtitlesAdjuster] Added SubtitlesAdjusterTools.newListener to dynamically create listeners using proxy objects. git-svn-id: https://aprog.svn.sourceforge.net/svnroot/aprog/trunk@87 7cbf5e2b-b55d-4b93-acdd-c0d7b961df51 --- .../aprog/subtitlesadjuster/Components.java | 21 ++---- .../subtitlesadjuster/SubtitlesAdjusterTools.java | 84 ++++++++++++++++++++++ 2 files changed, 89 insertions(+), 16 deletions(-) diff --git a/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/Components.java b/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/Components.java index ed10e4f..319dc60 100644 --- a/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/Components.java +++ b/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/Components.java @@ -49,6 +49,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; @@ -196,26 +197,14 @@ public final class Components { result.setJMenuBar(newMenuBar(context)); result.add(newMainPanel(context)); - result.addWindowListener(new WindowAdapter() { - - @Override - public final void windowClosing(final WindowEvent event) { - quit(context); - } - - }); + result.addWindowListener(newListener(WindowListener.class, "windowClosing", + Actions.class, "quit", context)); invokeOnVariableChanged(context, FILE, Actions.class, "updateMainFrameTitle", context); invokeOnVariableChanged(context, FILE_MODIFIED, Actions.class, "updateMainFrameTitle", context); - Translator.getDefaultTranslator().addListener(new Translator.Listener() { - - @Override - public final void localeChanged(final LocaleChangedEvent event) { - packAndUpdateMinimumSize(result); - } - - }); + Translator.getDefaultTranslator().addListener(newListener(Translator.Listener.class, "localeChanged", + SwingTools.class, "packAndUpdateMinimumSize", result)); return center(packAndUpdateMinimumSize(result)); } diff --git a/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/SubtitlesAdjusterTools.java b/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/SubtitlesAdjusterTools.java index 0e2b81f..6440088 100644 --- a/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/SubtitlesAdjusterTools.java +++ b/SubtitlesAdjuster/src/net/sourceforge/aprog/subtitlesadjuster/SubtitlesAdjusterTools.java @@ -25,6 +25,11 @@ package net.sourceforge.aprog.subtitlesadjuster; import static net.sourceforge.aprog.i18n.Messages.*; +import static net.sourceforge.aprog.tools.Tools.*; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import javax.swing.JMenuItem; import javax.swing.KeyStroke; @@ -135,4 +140,83 @@ public final class SubtitlesAdjusterTools { .setShortcut(shortcut))); } + /** + * + * @param The listener type + * @param listenerClass + *
Not null + * @param listenerMethodName + *
Not null + * @param objectOrClass + *
Not null + * @param methodName + *
Not null + * @param arguments + *
Not null + * @return + *
Not null + *
New + */ + @SuppressWarnings("unchecked") + public static final L newListener(final Class listenerClass, final String listenerMethodName, + final Object objectOrClass, final String methodName, final Object... arguments) { + return (L) Proxy.newProxyInstance(getCallerClass().getClassLoader(), array(listenerClass), + new ListenerInvocationHandler(listenerMethodName, objectOrClass, methodName, arguments)); + } + + /** + * + * @author codistmonk (creation 2010-07-03) + */ + public static final class ListenerInvocationHandler implements InvocationHandler { + + private final String listenerMethodName; + + private final Object objectOrClass; + + private final String methodName; + + private final Object[] arguments; + + /** + * + * @param listenerMethodName + *
Not null + *
Shared + * @param objectOrClass + *
Not null + *
Shared + * @param methodName + *
Not null + *
Shared + * @param arguments + *
Not null + *
Shared + */ + public ListenerInvocationHandler(final String listenerMethodName, + final Object objectOrClass, final String methodName, final Object... arguments) { + this.listenerMethodName = listenerMethodName; + this.objectOrClass = objectOrClass; + this.methodName = methodName; + this.arguments = arguments; + } + + @Override + public final Object invoke(final Object proxy, final Method method, final Object[] arguments) throws Throwable { + debugPrint( + "\n", method, + "\n", method.getDeclaringClass(), + "\n", method.getDeclaringClass().isAssignableFrom(this.getClass())); + + if (method.getName().equals(this.listenerMethodName)) { + return Tools.invoke(this.objectOrClass, this.methodName, this.arguments); + } else if (method.getDeclaringClass().isAssignableFrom(this.getClass())) { + return method.invoke(this, arguments); + } + + return null; + } + + } + } -- 2.11.4.GIT