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