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.Method;
026    import java.util.EventListener;
027    
028    /**
029     * this class is a wrap class of class <strong>com.apple.eawt.Application</strong>
030     * <br>With this class we can access to the method of the Apple class.
031     */
032    public class ApplicationWrapper {
033    
034            /** com.apple.eawt.Application */
035            @SuppressWarnings("unchecked")
036            private Class classApplication = null;
037            /** com.apple.eawt.ApplicationListener */
038            @SuppressWarnings("unchecked")
039            private Class classApplicationListener = null;
040    
041            // all the methods of class Application:
042            private static Method applicationGetApplication = null;
043            private Method applicationAddPreferencesMenuItem = null;
044            private Method applicationSetEnabledPreferencesMenu = null;
045            private Method applicationAddApplicationListener = null;
046    
047            /** null array of objects, use to past null array argument */
048            private static final Object[] nullObject = null;
049    
050            /** instance of Application class */
051            private Object app = null;
052    
053            /**
054             * Construct a new ApplicationWrapper object,
055             * get back Classes and methods
056             * @throws AppException if an error occurs during initialization
057             */
058            @SuppressWarnings("unchecked")
059            public ApplicationWrapper() throws AppException {               
060                    // we get back classes
061                    try {
062                            classApplication = Class.forName("com.apple.eawt.Application");
063                            classApplicationListener = Class.forName("com.apple.eawt.ApplicationListener");
064                            // and the interrested methods
065                            applicationGetApplication = classApplication.getMethod("getApplication");
066                            applicationAddPreferencesMenuItem = classApplication.getMethod("addPreferencesMenuItem");
067                            applicationSetEnabledPreferencesMenu = classApplication.getMethod("setEnabledPreferencesMenu", boolean.class);
068                            applicationAddApplicationListener = classApplication.getMethod("addApplicationListener", classApplicationListener);
069                            // we create an instance of Application class with a call to getApplication() static method:
070                            app = applicationGetApplication.invoke(nullObject, nullObject);
071                    } catch (Exception e) {
072                            throw new AppException();
073                    }       
074            }
075    
076            /**
077             * this method return an instance object of the class
078             * @return an instance of the class
079             * @throws AppException if instantiation failed
080             */
081            public static ApplicationWrapper getApplication() throws AppException {         
082                    return new ApplicationWrapper();
083            }
084    
085            /**
086             * <u>com.apple.eawt.Application#addPreferencesMenuItem()</u>
087             * <br>Adds the Preferences item to the application menu if the item is not already present.
088             */
089            public void addPreferencesMenuItem() {
090                    try {
091                            if(app != null) {
092                                    applicationAddPreferencesMenuItem.invoke(app, nullObject);
093                            }
094                    } catch (Exception e) {
095                            e.printStackTrace();
096                    }       
097            }
098    
099            /**
100             * <u>com.apple.eawt.ApplicationListener#setEnabledPreferencesMenu(boolean)</u>
101             * <br>Enables the About item in the application menu. 
102             * @param enable specifies whether the About item in the application menu should be enabled (true) or not (false)
103             */
104            public void setEnabledPreferencesMenu(boolean enable) {
105                    try {
106                            if(app != null) {
107                                    applicationSetEnabledPreferencesMenu.invoke(app, enable);
108                            }
109                    } catch (Exception e) {
110                            e.printStackTrace();
111                    }
112            }
113    
114            /**
115             * <u>com.apple.eawt.ApplicationListener#addApplicationListener(com.apple.eawt.ApplicationEvent)</u>
116             * <br>Adds the specified ApplicationListener as a receiver of callbacks from this class.
117             * <i>if listener is not an implementation of ApplicationListener this method is no op</i>
118             * @param listener      an implementation of ApplicationListener that handles 
119             *                                      ApplicationEvents generated by this class
120             */
121            public void addApplicationListener(EventListener listener) {
122                    try {
123                            if(app != null) {
124                                    // if object listener is instanceof ApplicationListener
125                                    if(classApplicationListener.isInstance(listener)) {                                     
126                                            applicationAddApplicationListener.invoke(app, 
127                                                            classApplicationListener.cast(listener));
128                                    }
129                            }
130                    } catch (Exception e) {
131                            e.printStackTrace();
132                    }
133            }
134    }