Capture, settings and exit the menu

I am using the current version of SWT to build my applications, and I want to run it under Mac OS X (Yosemite).
My problem is that I can’t fix clicks on the menu items "About the program", "Settings" and "Exit", which were automatically added to my application.
I have searched many times and found the following class that helps me a lot http://www.transparentech.com/files/CocoaUIEnhancer.java .

And my code to initialize it:

import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class Test { private Display display; private Shell shell; public Test(Display display) { this.display = display; initUI(); } public void open() { shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void initUI() { shell = new Shell(display); shell.setSize(808, 599); shell.setText("Test"); AboutHandler aboutHandler = new AboutHandler(); PreferencesHandler preferencesHandler = new PreferencesHandler(); QuitHandler quitHandler = new QuitHandler(); CocoaUIEnhancer uienhancer = new CocoaUIEnhancer("Test"); uienhancer.hookApplicationMenu(display, quitHandler, aboutHandler, preferencesHandler); } private class AboutHandler implements Listener { public void handleEvent(Event e) { } } private class PreferencesHandler implements Listener { public void handleEvent(Event e) { } } private class QuitHandler implements Listener { public void handleEvent(Event e) { } } } 

I can compile it without any errors, but if I run the program, I will get the following exception:

 Exception in thread "main" java.lang.NoSuchMethodError: actionProc at org.eclipse.swt.internal.Callback.bind(Native Method) at org.eclipse.swt.internal.Callback.<init>(Unknown Source) at org.eclipse.swt.internal.Callback.<init>(Unknown Source) at org.eclipse.swt.internal.Callback.<init>(Unknown Source) at CocoaUIEnhancer.initialize(CocoaUIEnhancer.java:124) at CocoaUIEnhancer.hookApplicationMenu(CocoaUIEnhancer.java:92) at Test.initUI(Test.java:50) at Test.<init>(Test.java:18) 

This is probably a mistake in the native libraries, but I can't figure it out!

+6
source share
3 answers

I did not use CocoaUIEnhancer at all, as this was causing problems.

So here is what I did in my applications:

 /** * Convenience method that takes care of special menu items (About, Preferences, Quit) * * @param name The name of the menu item * @param parent The parent {@link Menu} * @param listener The {@link Listener} to add to the item * @param id The <code>SWT.ID_*</code> id */ private void addMenuItem(String name, Menu parent, Listener listener, int id) { if (OSUtils.isMac()) { Menu systemMenu = Display.getDefault().getSystemMenu(); for (MenuItem systemItem : systemMenu.getItems()) { if (systemItem.getID() == id) { systemItem.addListener(SWT.Selection, listener); return; } } } /* We get here if we're not running on a Mac, or if we're running on a Mac, but the menu item with the given id hasn't been found */ MenuItem item = new MenuItem(parent, SWT.NONE); item.setText(name); item.addListener(SWT.Selection, listener); } 

Just name it SWT.ID_PREFERENCES , SWT.ID_ABOUT and SWT.ID_QUIT respectively. Hand in the backup menu item name, the backup Menu and the actual Listener that you want to add to the menu item.

So for example:

 addMenuItem("Quit", myMenu, new Listener() { @Override public void handleEvent(Event event) { // Close database connection for example } }, SWT.ID_QUIT); 
+8
source

It looks like actionProc

 int actionProc( int id, int sel, int arg0 ) 

in CocoaUIEnhancer , you may need to use long rather than int for arguments to work with 64-bit SWT.

+2
source

You need to modify CocoaUIEnhancer.java to work with a pure SWT application, as described in this tutorial :

  • Change the getProductName () method to return a String when a product is not found (instead of null)
  • Wrap the code in hookWorkbenchListener () in a try-catch block (IllegalStateException e)
  • Wrap the code in modifyShells () in a try-catch block (IllegalStateException e)
  • Add some code to the actionProc (...) method to call the About-Dialog and Preferences-Dialog dialog boxes (since we do not use commands):
  static long actionProc(long id, long sel, long arg0) throws Exception { // ... else if (sel == sel_preferencesMenuItemSelected_) { showPreferences(); } else if (sel == sel_aboutMenuItemSelected_) { showAbout(); } return 0; } private static void showAbout() { MessageDialog.openInformation(null, "About...", "Replace with a proper about text / dialog"); } private static void showPreferences() { System.out.println("Preferences..."); PreferenceManager manager = new PreferenceManager(); PreferenceDialog dialog = new PreferenceDialog(null, manager); dialog.open(); } // ... 

Finally, we add the following lines to our main () method:

 public static final String APP_NAME = "MyApp"; public static void main(String[] args) { //in your case change the Test constructor Display.setAppName(APP_NAME); Display display = Display.getDefault(); //insert in initUI method call the earlysetup if (SWT.getPlatform().equals("cocoa")) { new CocoaUIEnhancer().earlyStartup(); } Shell shell = new Shell(display); shell.setText(APP_NAME); ... } 

Quote Code.

+2
source

All Articles