KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > applemenu > NbApplicationAdapter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.applemenu;
21
22 import com.apple.eawt.*;
23
24 import java.beans.Beans JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.io.IOException JavaDoc;
27 import javax.swing.Action JavaDoc;
28
29 import org.openide.ErrorManager;
30 import org.openide.filesystems.*;
31 import org.openide.actions.*;
32 import org.openide.cookies.InstanceCookie;
33 import org.openide.loaders.DataObject;
34
35 /** Adapter class which intercepts action events and passes them to the
36  * correct action instance as defined in the system filesystem.
37  *
38  * @author Tim Boudreau
39  */

40
41 class NbApplicationAdapter implements ApplicationListener {
42     
43     private static final String JavaDoc OPTIONS_ACTION =
44         "Actions/Window/org-netbeans-modules-options-OptionsWindowAction.instance"; //NOI18N
45
private static final String JavaDoc ABOUT_ACTION =
46         "Actions/Help/org-netbeans-core-actions-AboutAction.instance"; //NOI18N
47
private static final String JavaDoc OPENFILE_ACTION =
48         "Menu/File/org-netbeans-modules-openfile-OpenFileAction"; //NOI18N
49
private static final String JavaDoc EXIT_ACTION =
50         "Actions/System/org-netbeans-core-actions-SystemExit.instance"; //NOI18N
51

52     private static ApplicationListener al = null;
53     
54     private NbApplicationAdapter() {
55     }
56
57     static void install() {
58         //Thanks to Scott Kovatch from Apple for this fix - enabling the preferences menu
59
//requires that Beans.isDesignTime() be false
60
boolean wasDesignTime = Beans.isDesignTime();
61         
62         try {
63             Beans.setDesignTime (false);
64
65             al = new NbApplicationAdapter();
66             Application.getApplication().addApplicationListener(al);
67             Application.getApplication().setEnabledAboutMenu(true);
68             Application.getApplication().setEnabledPreferencesMenu(true);
69         } finally {
70             Beans.setDesignTime(wasDesignTime);
71         }
72     }
73
74     static void uninstall() {
75         if (al != null) {
76             Application.getApplication().removeApplicationListener(al);
77             al = null;
78         }
79     }
80     
81     public void handleAbout(ApplicationEvent e) {
82         e.setHandled (performAction (ABOUT_ACTION));
83     }
84     
85     public void handleOpenApplication (ApplicationEvent e) {
86     }
87     
88     public void handleOpenFile (ApplicationEvent e) {
89         e.setHandled(performAction (OPENFILE_ACTION, e.getFilename()));
90     }
91     
92     public void handlePreferences (ApplicationEvent e) {
93         e.setHandled(performAction(OPTIONS_ACTION));
94     }
95     
96     public void handlePrintFile (ApplicationEvent e) {
97         //do nothing - what invokes this?
98
}
99     
100     public void handleQuit (ApplicationEvent e) {
101         //Set it to false to abort the quit, our code will handle shutdown
102
e.setHandled (!performAction (EXIT_ACTION));
103     }
104     
105     public void handleReOpenApplication (ApplicationEvent e) {
106     }
107     
108     private boolean performAction (String JavaDoc key) {
109         return performAction (key, null);
110     }
111     
112     private boolean performAction (String JavaDoc key, String JavaDoc command) {
113         Action JavaDoc a = findAction (key);
114         if (a == null) {
115             return false;
116         }
117         if (command == null) {
118             command = "foo"; //XXX ???
119
}
120         ActionEvent JavaDoc ae = new ActionEvent JavaDoc (this, ActionEvent.ACTION_PERFORMED,
121             command);
122         try {
123             a.actionPerformed(ae);
124             return true;
125         } catch (Exception JavaDoc e) {
126             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
127             return false;
128         }
129     }
130     
131     private Action JavaDoc findAction (String JavaDoc key) {
132         FileObject fo =
133             Repository.getDefault().getDefaultFileSystem().findResource(key);
134         
135         if (fo != null && fo.isValid()) {
136             try {
137                 DataObject dob = DataObject.find (fo);
138                 InstanceCookie ic =
139                     (InstanceCookie) dob.getCookie(InstanceCookie.class);
140                 
141                 if (ic != null) {
142                     Object JavaDoc instance = ic.instanceCreate();
143                     if (instance instanceof Action JavaDoc) {
144                         return (Action JavaDoc) instance;
145                     }
146                 }
147             } catch (Exception JavaDoc e) {
148                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
149                 return null;
150             }
151         }
152         return null;
153     }
154 }
155
Popular Tags