KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > OSXAdapter


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.gui2;
21
22 import com.apple.eawt.ApplicationAdapter;
23 import com.apple.eawt.ApplicationEvent;
24
25 /*
26  * Based on sample code from Apple.
27  *
28  * This is the only class that uses the Apple specific EAWT classes.
29  * This class should only ever be referenced via reflection after
30  * checking that we are running on Mac OS X.
31  */

32 public class OSXAdapter extends ApplicationAdapter {
33
34     // pseudo-singleton model; no point in making multiple instances
35
// of the EAWT application or our adapter
36
private static OSXAdapter theAdapter;
37     private static com.apple.eawt.Application theApplication;
38
39     // reference to the app where the existing quit, about, prefs code is
40
private MainFrame mainApp;
41     
42     private OSXAdapter (MainFrame inApp) {
43         mainApp = inApp;
44     }
45     
46     // implemented handler methods. These are basically hooks into
47
// existing functionality from the main app, as if it came
48
// over from another platform.
49

50     @Override JavaDoc
51     public void handleAbout(ApplicationEvent ae) {
52         if (mainApp != null) {
53             ae.setHandled(true);
54                         // We need to invoke modal About Dialog asynchronously
55
// otherwise the Application queue is locked for the duration
56
// of the about Dialog, which results in a deadlock if a URL is
57
// selected, and we get a ReOpenApplication event when user
58
// switches back to Findbugs.
59
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
60                                 public void run() {
61                                     mainApp.about();
62                                 }
63                             });
64         } else {
65             throw new IllegalStateException JavaDoc("handleAbout: " +
66                                                         "MyApp instance detached from listener");
67         }
68     }
69     
70     @Override JavaDoc
71     public void handlePreferences(ApplicationEvent ae) {
72         if (mainApp != null) {
73 // mainApp.preferences();
74
ae.setHandled(true);
75         } else {
76             throw new IllegalStateException JavaDoc("handlePreferences: MyApp instance " +
77                                                         "detached from listener");
78         }
79     }
80     
81     @Override JavaDoc
82     public void handleQuit(ApplicationEvent ae) {
83         if (mainApp != null) {
84
85             /*
86                          * You MUST setHandled(false) if you want to
87              * delay or cancel the quit. This is important
88              * for cross-platform development -- have a
89              * universal quit routine that chooses whether
90              * or not to quit, so the functionality is
91              * identical on all platforms. This example
92              * simply cancels the AppleEvent-based quit and
93              * defers to that universal method.
94             */

95
96             ae.setHandled(false);
97             mainApp.callOnClose();
98         } else {
99             throw new IllegalStateException JavaDoc("handleQuit: MyApp instance detached " +
100                                                         "from listener");
101         }
102     }
103     
104     
105     // The main entry-point for this functionality. This is the only method
106
// that needs to be called at runtime, and it can easily be done using
107
// reflection (see MyApp.java)
108
public static void registerMacOSXApplication(MainFrame inApp) {
109         if (theApplication == null) {
110             theApplication = new com.apple.eawt.Application();
111         }
112         
113         if (theAdapter == null) {
114             theAdapter = new OSXAdapter(inApp);
115         }
116         theApplication.addApplicationListener(theAdapter);
117     }
118     
119     // Another static entry point for EAWT functionality. Enables the
120
// "Preferences..." menu item in the application menu.
121
public static void enablePrefs(boolean enabled) {
122         if (theApplication == null) {
123             theApplication = new com.apple.eawt.Application();
124         }
125         theApplication.setEnabledPreferencesMenu(enabled);
126     }
127 }
Popular Tags