-1

I want to add a context menu to a dialogue. I want it in such a way that when clicked anywhere where it is empty a deafaul context menu appears. I have seen example of context menu added to table and tree but not a dialogue as a whole any snippets or examples will be greatly appreciated.

This is what I have tried.

import org.eclipse.jface.action.MenuManager;

@Override
protected Control createDialogArea(Composite parent) {

    Composite container = (Composite) super.createDialogArea(parent);
    GridLayout layout = new GridLayout(4, false);
    layout.marginRight = 5;
    layout.marginLeft = 10;
    container.setLayout(layout);

    MenuManager menuMgr = new MenuManager();
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.add(new Action("New Thing") {

        /* (non-Javadoc)
         * @see org.eclipse.jface.action.Action#run()
         */
        @Override
        public void run() {
            System.out.println("came in options");
        }
    });

    parent.setMenu(menuMgr.createContextMenu(parent));
    productListTreeCheckBox(parent);
    return super.createDialogArea(parent);
}
Praveen
  • 101
  • 14

1 Answers1

0

Try creating the menu in a similar way to the table/tree menu, but using to top level Composite for the dialog. Using a menu manager that might be something like:

Control topLevelComposite = ... get top level composite

MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);

menuMgr.addMenuListener(... you menu listener....);

final Menu menu = menuMgr.createContextMenu(topLevelComposite);

topLevelComposite.setMenu(menu);

You will then have to call setMenu on every Composite and control in the dialog which you want to use this menu. You can just use:

control.setMenu(parent.getMenu());

for this (as long as you do it on everything starting from the children of topLevelComposite).

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • either I am doing it wrong or I am touching wrong component. I will put my code snippet and that might tell what I am doing wrong. – Praveen Jan 31 '15 at 23:44
  • Also swapped parent with container did not work.Similar snippets works for showing context menu inside a table. – Praveen Jan 31 '15 at 23:51
  • You have to call `setMenu` on every Composite and Control you create in the dialog. – greg-449 Feb 01 '15 at 08:47
  • parent.setMenu(menuMgr.createContextMenu(parent)); is this not what you mean by setmenu? – Praveen Feb 01 '15 at 18:42
  • You use that on the top level composite and the other call on all the children – greg-449 Feb 01 '15 at 18:50