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    
027    /**
028     * this class is a wrap class of class <strong>com.apple.eawt.ApplicationEvent</strong> 
029     * <br>With this class we can access to the method of the Apple class.
030     */
031    public class ApplicationEventWrapper {
032            /** com.apple.eawt.ApplicationEvent */
033            @SuppressWarnings("unchecked")
034            private Class classApplicationEvent = null;
035    
036            // all the methods of com.apple.eawt.ApplicationEvent:
037            private Method applicationEventSetHandled = null;
038            private Method applicationEventIsHandled = null;
039            private Method applicationGetFilename = null;
040    
041            // use to call the methods, applicationEventObject instanceof ApplicationEvent
042            private Object applicationEventObject = null;
043    
044            /**
045             * Construct a new ApplicationEventWrapper
046             */
047            @SuppressWarnings("unchecked")
048            public ApplicationEventWrapper() {              
049                    try {
050                            
051                            // we get back com.apple.eawt.ApplicationListener class:
052                            classApplicationEvent = Class.forName("com.apple.eawt.ApplicationEvent");
053                            // and its methods:
054                            applicationEventSetHandled = classApplicationEvent.getMethod("setHandled", boolean.class);
055                            applicationEventIsHandled = classApplicationEvent.getMethod("isHandled");
056                            applicationGetFilename = classApplicationEvent.getMethod("getFilename");
057                            
058                    } catch (Exception e) {
059                            e.printStackTrace();
060                    }
061            }
062            
063            /**
064             * set the ApplicationEvent object
065             * @param anApplicationEventObject must be instance of ApplicationEvent
066             */
067            public void applicationEventObject(Object anApplicationEventObject) {
068                    applicationEventObject = anApplicationEventObject;
069            }
070    
071            /**
072             * <u>com.apple.eawt.ApplicationEvent#getFilename()</u>
073             * <br>Provides the filename associated with a particular AppleEvent.
074             * <br><i>If applicationEventObject isn't set or is not instance of ApplicationEvent or is null,
075             * <br>null is return.</i>
076             * @return the full path to the file associated with the event, if applicable, otherwise null
077             */
078            public String getFilename() {
079                    String filename = null;
080                    // check conditions:
081                    if(applicationEventObject != null && 
082                                    classApplicationEvent.isInstance(applicationEventObject)) {
083                            try {
084                                    filename = (String)applicationGetFilename.invoke(applicationEventObject);
085                            } catch (Exception e) {
086                                    e.printStackTrace();
087                            }
088                    }
089                    return filename;
090            }
091            
092            /**
093             * <u>com.apple.eawt.ApplicationEvent#isHandled()</u>
094             * <br>Determines whether an ApplicationListener has acted on a particular event.
095             * <br><i>If applicationEventObject isn't set or is not instance of ApplicationEvent or is null,
096             * <br>false is return.</i>
097             * @return true if the event has been handled, otherwise false
098             */
099            public boolean isHandled() {
100                    boolean result = false;
101                    // check conditions:
102                    if(applicationEventObject != null && 
103                                    classApplicationEvent.isInstance(applicationEventObject)) {
104                            try {
105                                    result = (Boolean)applicationEventIsHandled.invoke(applicationEventObject);
106                            } catch (Exception e) {
107                                    e.printStackTrace();
108                            }
109                    }
110                    return result;
111            }
112            
113            /**
114             * <u>com.apple.eawt.ApplicationEvent#setHandled(boolean)</u>
115             * <br>Sets the state of the event.
116             * <br><i>If applicationEventObject isn't set or is not instance of ApplicationEvent or is null,
117             * <br>this method is a no op</i>
118             * @param state true if the event has been handled, otherwise false.
119             */
120            public void setHandled(boolean state) {
121                    if(applicationEventObject != null && 
122                                    classApplicationEvent.isInstance(applicationEventObject)) {
123                            try {
124                                    applicationEventSetHandled.invoke(applicationEventObject, state);
125                            } catch (Exception e) {
126                                    e.printStackTrace();
127                            }
128                    }
129            }
130    }