KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.umd.cs.findbugs.gui;
2
3 import com.apple.eawt.ApplicationAdapter;
4 import com.apple.eawt.ApplicationEvent;
5
6 /*
7  * Based on sample code from Apple.
8  *
9  * This is the only class that uses the Apple specific EAWT classes.
10  * This class should only ever be referenced via reflection after
11  * checking that we are running on Mac OS X.
12  */

13 public class OSXAdapter extends ApplicationAdapter {
14
15     // pseudo-singleton model; no point in making multiple instances
16
// of the EAWT application or our adapter
17
private static OSXAdapter theAdapter;
18     private static com.apple.eawt.Application theApplication;
19
20     // reference to the app where the existing quit, about, prefs code is
21
private FindBugsFrame mainApp;
22     
23     private OSXAdapter (FindBugsFrame inApp) {
24         mainApp = inApp;
25     }
26     
27     // implemented handler methods. These are basically hooks into
28
// existing functionality from the main app, as if it came
29
// over from another platform.
30

31     @Override JavaDoc
32     public void handleAbout(ApplicationEvent ae) {
33         if (mainApp != null) {
34             ae.setHandled(true);
35                         // We need to invoke modal About Dialog asynchronously
36
// otherwise the Application queue is locked for the duration
37
// of the about Dialog, which results in a deadlock if a URL is
38
// selected, and we get a ReOpenApplication event when user
39
// switches back to Findbugs.
40
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
41                                 public void run() {
42                                     mainApp.about();
43                                 }
44                             });
45         } else {
46             throw new IllegalStateException JavaDoc("handleAbout: " +
47                                                         "MyApp instance detached from listener");
48         }
49     }
50     
51     @Override JavaDoc
52     public void handlePreferences(ApplicationEvent ae) {
53         if (mainApp != null) {
54 // mainApp.preferences();
55
ae.setHandled(true);
56         } else {
57             throw new IllegalStateException JavaDoc("handlePreferences: MyApp instance " +
58                                                         "detached from listener");
59         }
60     }
61     
62     @Override JavaDoc
63     public void handleQuit(ApplicationEvent ae) {
64         if (mainApp != null) {
65
66             /*
67                          * You MUST setHandled(false) if you want to
68              * delay or cancel the quit. This is important
69              * for cross-platform development -- have a
70              * universal quit routine that chooses whether
71              * or not to quit, so the functionality is
72              * identical on all platforms. This example
73              * simply cancels the AppleEvent-based quit and
74              * defers to that universal method.
75             */

76
77             ae.setHandled(false);
78             mainApp.exitFindBugs();
79         } else {
80             throw new IllegalStateException JavaDoc("handleQuit: MyApp instance detached " +
81                                                         "from listener");
82         }
83     }
84     
85     
86     // The main entry-point for this functionality. This is the only method
87
// that needs to be called at runtime, and it can easily be done using
88
// reflection (see MyApp.java)
89
public static void registerMacOSXApplication(FindBugsFrame inApp) {
90         if (theApplication == null) {
91             theApplication = new com.apple.eawt.Application();
92         }
93         
94         if (theAdapter == null) {
95             theAdapter = new OSXAdapter(inApp);
96         }
97         theApplication.addApplicationListener(theAdapter);
98     }
99     
100     // Another static entry point for EAWT functionality. Enables the
101
// "Preferences..." menu item in the application menu.
102
public static void enablePrefs(boolean enabled) {
103         if (theApplication == null) {
104             theApplication = new com.apple.eawt.Application();
105         }
106         theApplication.setEnabledPreferencesMenu(enabled);
107     }
108 }
Popular Tags