001 ///////////////////////////////////////////////// 002 //This file is part of Sears project. 003 //Subtitle Editor And Re-Synch 004 //A tool to easily modify and resynch movies subtitles. 005 ///////////////////////////////////////////////// 006 //This program is free software; 007 //you can redistribute it and/or modify it under the terms 008 //of the GNU General Public License 009 //as published by the Free Software Foundation; 010 //either version 2 of the License, or (at your option) any later version. 011 ///////////////////////////////////////////////// 012 //Sears project is available under sourceforge 013 //at adress: http://sourceforge.net/projects/sears/ 014 //Copyright (C) 2005 Booba Skaya 015 //Mail: booba.skaya@gmail.com 016 ///////////////////////////////////////////////// 017 018 ////////////////////////////////////////////////////// 019 // this file comes from the EawtWrapper librairie // 020 // information: www.daem0n.fr/eawt // 021 ////////////////////////////////////////////////////// 022 023 package sears.tools.eawt; 024 025 import java.lang.reflect.InvocationHandler; 026 import java.lang.reflect.Method; 027 import java.lang.reflect.Proxy; 028 import java.util.EventListener; 029 030 /** 031 * Handler for communicate with com.apple.eawt.ApplicationListener class 032 */ 033 public class ApplicationListenerHandler implements InvocationHandler { 034 035 /** com.apple.eawt.ApplicationListener */ 036 @SuppressWarnings("unchecked") 037 private Class classApplicationListener = null; 038 // instance of application listener, use for call its methods 039 private ApplicationListener applicationListener = null; 040 041 /** 042 * Construct a new handler 043 * @param listener an instance of ApplicationListener subclass 044 */ 045 public ApplicationListenerHandler(ApplicationListener listener) { 046 applicationListener = listener; 047 try { 048 // we get back com.apple.eawt.ApplicationListener class: 049 classApplicationListener = Class.forName("com.apple.eawt.ApplicationListener"); 050 } catch (ClassNotFoundException e) { 051 e.printStackTrace(); 052 } 053 } 054 055 /** 056 * return the class object which represents com.apple.eawt.ApplicationListener class 057 * @return com.apple.eawt.ApplicationListener 'Class object' 058 */ 059 @SuppressWarnings("unchecked") 060 public Class getApplicationListenerInterface() { 061 return classApplicationListener; 062 } 063 064 /* 065 * (non-Javadoc) 066 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]) 067 */ 068 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 069 // we get back the method name: 070 String methodName = method.getName(); 071 // and compare with the ApplicationListener methods: 072 if(methodName.equals("handleAbout")) { 073 applicationListener.handleAbout(args[0]); 074 } else if(methodName.equals("handleOpenApplication")) { 075 applicationListener.handleOpenApplication(args[0]); 076 } else if(methodName.equals("handleOpenFile")) { 077 applicationListener.handleOpenFile(args[0]); 078 } else if(methodName.equals("handlePreferences")) { 079 applicationListener.handlePreferences(args[0]); 080 } else if(methodName.equals("handlePrintFile")) { 081 applicationListener.handlePrintFile(args[0]); 082 } else if(methodName.equals("handleQuit")) { 083 applicationListener.handleQuit(args[0]); 084 } else if(methodName.equals("handleReOpenApplication")) { 085 applicationListener.handleReOpenApplication(args[0]); 086 } 087 // always null: 088 return null; 089 } 090 091 /** 092 * this method returns an instance of com.apple.eawt.ApplicationListener interface, 093 * <br>wrapped by EventListener interface... 094 * @param listener will be an instance of the Application listener implementation class 095 * @return an instance of com.apple.eawt.ApplicationListener, null if... 096 */ 097 @SuppressWarnings("unchecked") 098 public static EventListener getWrappedApplicationListenerInstance(ApplicationListener listener) { 099 EventListener returnedListener = null; 100 101 ApplicationListenerHandler handler = 102 new ApplicationListenerHandler(listener); 103 // we get back the com.java.eawt.ApplicationListener: 104 Class applicationListener = handler.getApplicationListenerInterface(); 105 // and create an Object instance of EventListener, 106 // an instance of the com.java.eawt.ApplicationListener interface: 107 returnedListener = (EventListener) Proxy.newProxyInstance( 108 ClassLoader.getSystemClassLoader(), 109 new Class[] { applicationListener }, 110 handler ); 111 return returnedListener; 112 } 113 }