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            public ApplicationWrapper() throws AppException {               
059                    // we get back classes
060                    try {
061                            classApplication = Class.forName("com.apple.eawt.Application");
062                            classApplicationListener = Class.forName("com.apple.eawt.ApplicationListener");
063                            // and the interrested methods
064                            applicationGetApplication = classApplication.getMethod("getApplication");
065                            applicationAddPreferencesMenuItem = classApplication.getMethod("addPreferencesMenuItem");
066                            applicationSetEnabledPreferencesMenu = classApplication.getMethod("setEnabledPreferencesMenu", boolean.class);
067                            applicationAddApplicationListener = classApplication.getMethod("addApplicationListener", classApplicationListener);
068                            // we create an instance of Application class with a call to getApplication() static method:
069                            app = applicationGetApplication.invoke(nullObject, nullObject);
070                    } catch (Exception e) {
071                            throw new AppException();
072                    }       
073            }
074    
075            /**
076             * this method return an instance object of the class
077             * @return an instance of the class
078             * @throws AppException if instantiation failed
079             */
080            public static ApplicationWrapper getApplication() throws AppException {         
081                    return new ApplicationWrapper();
082            }
083    
084            /**
085             * <u>com.apple.eawt.Application#addPreferencesMenuItem()</u>
086             * <br>Adds the Preferences item to the application menu if the item is not already present.
087             */
088            public void addPreferencesMenuItem() {
089                    try {
090                            if(app != null) {
091                                    applicationAddPreferencesMenuItem.invoke(app, nullObject);
092                            }
093                    } catch (Exception e) {
094                            e.printStackTrace();
095                    }       
096            }
097    
098            /**
099             * <u>com.apple.eawt.ApplicationListener#setEnabledPreferencesMenu(boolean)</u>
100             * <br>Enables the About item in the application menu. 
101             * @param enable specifies whether the About item in the application menu should be enabled (true) or not (false)
102             */
103            public void setEnabledPreferencesMenu(boolean enable) {
104                    try {
105                            if(app != null) {
106                                    applicationSetEnabledPreferencesMenu.invoke(app, enable);
107                            }
108                    } catch (Exception e) {
109                            e.printStackTrace();
110                    }
111            }
112    
113            /**
114             * <u>com.apple.eawt.ApplicationListener#addApplicationListener(com.apple.eawt.ApplicationEvent)</u>
115             * <br>Adds the specified ApplicationListener as a receiver of callbacks from this class.
116             * <i>if listener is not an implementation of ApplicationListener this method is no op</i>
117             * @param listener      an implementation of ApplicationListener that handles 
118             *                                      ApplicationEvents generated by this class
119             */
120            public void addApplicationListener(EventListener listener) {
121                    try {
122                            if(app != null) {
123                                    // if object listener is instanceof ApplicationListener
124                                    if(classApplicationListener.isInstance(listener)) {                                     
125                                            applicationAddApplicationListener.invoke(app, 
126                                                            classApplicationListener.cast(listener));
127                                    }
128                            }
129                    } catch (Exception e) {
130                            e.printStackTrace();
131                    }
132            }
133    }