Custom Contectmenu with toolbar [message #857108] |
Thu, 26 April 2012 08:53 |
|
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 09:40] Report message to a moderator
|
|
|
|
Re: Custom Contectmenu with toolbar [message #857372 is a reply to message #857335] |
Thu, 26 April 2012 13:37 |
|
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 #857712 is a reply to message #857711] |
Thu, 26 April 2012 19:53 |
|
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);
}
}
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05032 seconds