1 19 20 package org.netbeans.modules.applemenu; 21 22 import com.apple.eawt.*; 23 24 import java.beans.Beans ; 25 import java.awt.event.ActionEvent ; 26 import java.io.IOException ; 27 import javax.swing.Action ; 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 40 41 class NbApplicationAdapter implements ApplicationListener { 42 43 private static final String OPTIONS_ACTION = 44 "Actions/Window/org-netbeans-modules-options-OptionsWindowAction.instance"; private static final String ABOUT_ACTION = 46 "Actions/Help/org-netbeans-core-actions-AboutAction.instance"; private static final String OPENFILE_ACTION = 48 "Menu/File/org-netbeans-modules-openfile-OpenFileAction"; private static final String EXIT_ACTION = 50 "Actions/System/org-netbeans-core-actions-SystemExit.instance"; 52 private static ApplicationListener al = null; 53 54 private NbApplicationAdapter() { 55 } 56 57 static void install() { 58 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 } 99 100 public void handleQuit (ApplicationEvent e) { 101 e.setHandled (!performAction (EXIT_ACTION)); 103 } 104 105 public void handleReOpenApplication (ApplicationEvent e) { 106 } 107 108 private boolean performAction (String key) { 109 return performAction (key, null); 110 } 111 112 private boolean performAction (String key, String command) { 113 Action a = findAction (key); 114 if (a == null) { 115 return false; 116 } 117 if (command == null) { 118 command = "foo"; } 120 ActionEvent ae = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, 121 command); 122 try { 123 a.actionPerformed(ae); 124 return true; 125 } catch (Exception e) { 126 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 127 return false; 128 } 129 } 130 131 private Action findAction (String 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 instance = ic.instanceCreate(); 143 if (instance instanceof Action ) { 144 return (Action ) instance; 145 } 146 } 147 } catch (Exception e) { 148 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 149 return null; 150 } 151 } 152 return null; 153 } 154 } 155 | Popular Tags |