Home > Enterprise >  MacOS Eclipse RCP MenuItem Selection event invalidation
MacOS Eclipse RCP MenuItem Selection event invalidation

Time:12-05

MacOS 10.15.5 Eclipse RCP 2020-09

Problem : MenuItem Selection event invalidation

Code :

@Override
protected void configureShell(Shell newShell) {
    newShell.setImage( Images.getImageDescriptor(ImageConstants.IMG_LoL).createImage() ) ;
    newShell.setText( LoLPlugin.getPluginName() );
    newShell.setBackgroundMode( SWT.INHERIT_DEFAULT ) ;
    newShell.setLayout( new FillLayout() ) ;
    newShell.setMenuBar( createMenuBar(newShell) ) ;
    newShell.setSize( getInitialSize() ) ;
    newShell.setLocation( getInitialLocation( newShell ) ) ;
    newShell.addDisposeListener( e -> { newShell.getImage().dispose() ; saveLocation() ; } ) ;
    newShell.setData( window ) ;
}

private Menu createMenuBar(Shell shell) {       
    Menu mainMenu = new Menu( shell , SWT.BAR ) ;
    
    MenuItem bundleItem = new MenuItem( mainMenu , SWT.PUSH ) ;
    bundleItem.setText( Message.BundleBook_Name ) ;
    bundleItem.addListener(SWT.Selection, e -> showBook( BundleBook.ID ) );
    
    MenuItem resourceItem = new MenuItem( mainMenu , SWT.PUSH ) ;
    resourceItem.setText( Message.ResourceBook_Name ) ;
    resourceItem.addListener(SWT.Selection, e -> showBook( ResourceBook.ID ) );
    
    MenuItem guideItem = new MenuItem( mainMenu , SWT.PUSH ) ;
    guideItem.setText( Message.GuideBook_Name ) ;
    guideItem.addListener( SWT.Selection , e -> showBook( GuideBook.ID ) ) ;
    
    return mainMenu ;
}

private void showBook( String id ) {
    System.out.println( "MaC OS Cannot execute - Window OS Is OK" ) ;
    bookcase.showBook( id ) ;
}

result: MacOS cannot execute , Event not triggered

Window OS Is OK

CodePudding user response:

You can't have SWT.PUSH style menu items directly on the menu bar (at least not on macOS). You need to create SWT.CASCADE menu items with the push items as children.

Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);

MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
fileItem.setText("&File");

Menu fileSubMenu = new Menu(shell, SWT.DROP_DOWN);
fileItem.setMenu(fileSubMenu);

MenuItem item = new MenuItem(fileSubMenu, SWT.PUSH);
item.addListener(SWT.Selection, e -> System.out.println("Select All"));
item.setText("Select");

(Adapted from this SWT Snippet and tested on macOS 12.0.1)

Note: If you just want push buttons across the top of the shell use a ToolBar.

  • Related