问题描述:

I'm trying to give functionality to the "Preferences.." MenuItem (in the Mac default System menu) for my app but for some reason the on click is not working and I can't see what I'm doing wrong. This is how I get the preferences menuitem:

protected MenuItem getPreferencesMenuItem() {

Menu guiMenu = Display.getDefault().getSystemMenu();

MenuItem preferencesMenuItem = null;

if (guiMenu != null) {

for (int i = 0; i < guiMenu.getItemCount(); i++) {

MenuItem currentMenuItem = guiMenu.getItem(i);

if (currentMenuItem.getText().contains("Preferences")) {

preferencesMenuItem = currentMenuItem;

break;

} // end if

} // end for

} // end if

return preferencesMenuItem;

}

and this is how I'm adding the listener. Is there something I'm missing?

MenuItem preferencesMenuItem = this.getPreferencesMenuItem();

if (preferencesMenuItem != null) {

System.out.println(preferencesMenuItem.getText());

preferencesMenuItem.addSelectionListener(new SelectionAdapter() {

public void selectedWidget(SelectionEvent event) {

System.out.println("Preferences selected.");

PreferencesDialog preferencesDialog = new PreferencesDialog(shell_, processPanel_.getImageManager());

preferencesDialog.open();

}});

}

网友答案:

I believe you want to open preference dialog.

here is sample code to open Preference dialog with selected preference page id. you should be looking at how to use ParameterizedCommand . Hope this helps.

public static boolean openPreferencePage(String preferencePageId) {

    if (StringUtils.isEmpty(preferencePageId))
      return false;
    IServiceLocator serviceLocator = PlatformUI.getWorkbench();

    ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);

    IHandlerService handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class);

    Command command = commandService.getCommand(IWorkbenchCommandConstants.WINDOW_PREFERENCES);
    IParameter parmDef = null;
    try {
      parmDef = command.getParameter(IWorkbenchCommandConstants.WINDOW_PREFERENCES_PARM_PAGEID);
    } catch (NotDefinedException e) {
      Logger.log(e);
      return false;
    }
    if (parmDef != null) {
      Parameterization[] params = new Parameterization[] { new Parameterization(parmDef, preferencePageId) };

      ParameterizedCommand paramCommand = new ParameterizedCommand(command, params);
      try {
        handlerService.executeCommand(paramCommand, null);
        return true;
      } catch (Exception e) {
        Logger.log(e);
        return false;
      }
    }
    return false;

  }
相关阅读:
Top