Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Custom Contectmenu with toolbar
Custom Contectmenu with toolbar [message #857108] Thu, 26 April 2012 04:53 Go to next message
Stefan Nöbauer is currently offline Stefan Nöbauer
Messages: 20
Registered: January 2012
Junior Member
Hi,

I try to create a custom contextmenu in a treeviewer with opens on selection change. I based this menue on a tooltip which is not gaining focus on open.

On top of this Tooltip I added a toolbar with a ToolbarManager.

To add the Actions to the toolbar I used the MenuService:

 IMenuService menuService = (IMenuService) PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getService(IMenuService.class);

        toolbar = new ToolBar(composite, SWT.FLAT);

        toolBarManager = new ToolBarManager(toolbar);

        menuService.populateContributionManager(toolBarManager, "smart:"
                + menuId);
        toolBarManager.update(true);


So on opening the custom menu all attached commands are shown with the correct enable state.

But as soon I try to click one toolbar button all commands get grayed out and get disabled.

Which part of the command framework is updating my commands. What I saw is that the Shell of the tooltip is gaining the focus. Could that be my problem?

What shall I do to not loosing my enabled state?

What I can also see is that the EvaluationContext changes. Can I avoid that?

[Updated on: Thu, 26 April 2012 05:40]

Report message to a moderator

Re: Custom Contectmenu with toolbar [message #857335 is a reply to message #857108] Thu, 26 April 2012 09:14 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Where and how is the handler for one of your commands being contributed?

PW


Re: Custom Contectmenu with toolbar [message #857372 is a reply to message #857335] Thu, 26 April 2012 09:37 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan Nöbauer
Messages: 20
Registered: January 2012
Junior Member
via the Plugin.xml

command definition:
      <command
            id="eu.doccenter.kgu.client.workspace.lockUnlock"
            name="Sperren / Freigenben">
      </command>


handler definition:
      <handler
            commandId="eu.doccenter.kgu.client.workspace.lockUnlock">
         <class
               class="eu.doccenter.kgu.client.workspace.action.LockUnlockHandler">
         </class>
         <enabledWhen>
            <with
                  variable="selection">
               <not>
                  <test
                        property="kgu.cms.isEditing">
                  </test>
               </not>
            </with>
         </enabledWhen>
      </handler>
Re: Custom Contectmenu with toolbar [message #857649 is a reply to message #857372] Thu, 26 April 2012 14:44 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 208
Registered: February 2012
Location: Düsseldorf
Senior Member
I'd say the reason is that you use the selection variable. The selection variable contains the current active selection. If your viewer looses the focus (because you click the toolbar which then gains the focus), the viewer selection becomes inactive. Therefore the handlers enabled when expression disables all your commands. With context menu selections you should use the variable "activeMenuSelection". But I am not sure if this works in your special case.

Good Luck
Thorsten
Re: Custom Contectmenu with toolbar [message #857711 is a reply to message #857649] Thu, 26 April 2012 15:51 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan Nöbauer
Messages: 20
Registered: January 2012
Junior Member
The problem is that I use a toolbarmanager. And a toolbarmanager I cannot register as a contextmenu in the partSite to get the selection.
Re: Custom Contectmenu with toolbar [message #857712 is a reply to message #857711] Thu, 26 April 2012 15:53 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan Nöbauer
Messages: 20
Registered: January 2012
Junior Member
this is my entire class:

package custommenutest;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IExecutionListener;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.menus.IMenuService;

/**
 */
public class SmartMenu2 extends ToolTip {

    private boolean triggeredByMouse = true;

    private boolean enabled;

    private Set<String> commandIds = new HashSet<String>();

    private ToolBarManager toolBarManager;

    public SmartMenu2(Control control) {
        super(control, RECREATE, true);
        setEnabled(true);
        setHideOnMouseDown(false);

        setPopupDelay(200);
    }

    protected Composite createArea(Composite parent) {
        Composite composite = createToolTipContentAreaComposite(parent);

        ICommandService commandService = (ICommandService) PlatformUI
                .getWorkbench().getActiveWorkbenchWindow()
                .getService(ICommandService.class);

        IMenuService menuService = (IMenuService) PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getService(IMenuService.class);

        ToolBar toolbar = new ToolBar(composite, SWT.FLAT);

        toolBarManager = new ToolBarManager(toolbar);

        menuService.populateContributionManager(toolBarManager,
                "smart:workspace");
        toolBarManager.update(true);

        IContributionItem[] items = toolBarManager.getItems();
        for (IContributionItem iContributionItem : items) {
            commandIds.add(iContributionItem.getId());
        }

        commandService.addExecutionListener(new IExecutionListener() {

            @Override
            public void preExecute(String commandId, ExecutionEvent event) {

            }

            @Override
            public void postExecuteSuccess(String commandId, Object returnValue) {
                if (commandIds.contains(commandId)) {
                    hide();
                }
            }

            @Override
            public void postExecuteFailure(String commandId,
                    ExecutionException exception) {
                if (commandIds.contains(commandId)) {
                    hide();
                }

            }

            @Override
            public void notHandled(String commandId,
                    NotHandledException exception) {
                if (commandIds.contains(commandId)) {
                    System.out.println("notHandled");
                    hide();
                }

            }
        });

        composite.pack();
        return composite;
    }

    protected Composite createToolTipContentAreaComposite(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 1;
        gridLayout.marginWidth = 3;
        gridLayout.marginHeight = 3;
        composite.setLayout(gridLayout);
        composite.setBackground(composite.getDisplay().getSystemColor(
                SWT.COLOR_WHITE));
        composite.setBackgroundMode(SWT.INHERIT_DEFAULT);

        return composite;
    }

    public boolean isTriggeredByMouse() {
        return triggeredByMouse;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public static int getToolTipXShift() {
        if ("gtk".equals(SWT.getPlatform()) || "carbon".equals(SWT.getPlatform()) || "cocoa".equals(SWT.getPlatform())) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return -26;
        } else {
            return -23;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jface.window.ToolTip#createToolTipContentArea(org.eclipse
     * .swt.widgets.Event, org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Composite createToolTipContentArea(Event event, Composite parent) {
        return createArea(parent);
    }

}

Re: Custom Contectmenu with toolbar [message #858535 is a reply to message #857712] Fri, 27 April 2012 03:10 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 208
Registered: February 2012
Location: Düsseldorf
Senior Member
I see your problem. Your construction is a little special. If you really want to stay with it, you could try to enhance your expression by "or"-ing it together with an expression that somehow can access the TreeViewers selectionProvider. For example using "activePart". First check if the activePart does not become inactive when the toolbar gains focus. If it stays active then you could evaluate the "activePart" variable and add a property tester that can somehow test the selection.
If "activePart" does not work you may be able to retrieve the viewers selectionProvider somehow via the "activeFocusControl".

If all fails and there are no other suggestions from the forum you could implement a
"smartMenuSelectionSourceProvider" via the org.eclipse.ui.services extension point. That provider could provide the "smartMenuSelection" as a variable.

Or .... use a simple context menu. Yet I don't see the necessity for the toolbar.

Good Luck
Thorsten
Re: Custom Contectmenu with toolbar [message #858683 is a reply to message #858535] Fri, 27 April 2012 04:44 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan Nöbauer
Messages: 20
Registered: January 2012
Junior Member
Thanx very much Thosrten,

I`ll give it a try.

Do you have any Idea how to keep a view active while loosing the focus?

Stefan
Re: Custom Contectmenu with toolbar [message #858727 is a reply to message #858683] Fri, 27 April 2012 05:12 Go to previous message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 208
Registered: February 2012
Location: Düsseldorf
Senior Member
No I don't but if it really does, then go with the "activeFocusControl". That control must be somewhere within your TooltipAreaComposite - right? When you open the tooltip, pass a reference to the viewer into it and add the reference as a data object to the TooltipAreaComposite. Then with the activeFocusControl add a property tester that steps up the widget hierachy until it reaches the TooltipAreaComposite which could then check the selection. Btw: Since I am not very familiar with propertyTesters I am not sure if it can be implemented that way.

Regards
Thorsten
Previous Topic:null
Next Topic:Maximize/Restore events
Goto Forum:
  


Current Time: Fri Aug 01 12:00:43 EDT 2014

Powered by FUDForum. Page generated in 0.02627 seconds