Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » [expressions] toolbar dynamic contribution and visibleWhen expression
[expressions] toolbar dynamic contribution and visibleWhen expression [message #948729] Thu, 18 October 2012 10:06 Go to next message
Stephane Bouchet is currently offline Stephane BouchetFriend
Messages: 280
Registered: July 2009
Senior Member

Using eclipse 3.7, i cannot find a way to correctly set up an expression to make a toolbar contribution visible only when an editor is opened.

My use case is a little complex, because i must use the dynamic contribution to populate programmatically the toolbar. this toolbar creates several menus and actions programmatically, too.

I am able to create an expression programmatically and it is correctly called ( i suppose ) by the framework, because the setvisible method is called when the toolbar should change it's state.

BUT the toolbar is shown, because the toolbar contribution algorithm for isVisible [1] does not relies on the visible state, but on the contents of the toolbar ?!

Since i populate the toolbar with menus and actions, it always returns "true".

what i did for now :

1/ use new menu extension point :

( for those who are still reading, how do i set a label to this menucontribution , it does not appear on the customize dialog ?? )

2/ implements the contribution factory :
	public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
		ToolBarManager toolBarManager=new ToolBarManager(SWT.FLAT);
		ToolBarContributionItem toolbar=new ToolBarContributionItem(toolBarManager, "toolbar");
		Expression exp=new VisibleExpression();
		additions.addContributionItem(toolbar, exp);
		for (MyMenu myMenu: myMenus) {
			DropDownAction action=new DropDownAction();

3/ implemented a visible expression :
public final class VisibleExpression extends Expression {
	public EvaluationResult evaluate(IEvaluationContext context) {
		Object activeEditorId=context.getVariable("activeEditorId");
		if (activeEditorId != null
			&& activeEditorId.equals("editorId")))
			return EvaluationResult.TRUE;
		return EvaluationResult.FALSE;

	public void collectExpressionInfo(ExpressionInfo info) {

I think i am very close to make it work.

Any help greatly appreciated.

[1] snippet org.eclipse.jface.action.ToolBarContributionItem.isVisible():

    public boolean isVisible() {
        if (checkDisposed()) {
            return false;

        boolean visibleItem = false;
        if (toolBarManager != null) {
            IContributionItem[] contributionItems = toolBarManager.getItems();
            for (int i = 0; i < contributionItems.length; i++) {
                IContributionItem contributionItem = contributionItems[i];
                if ((!contributionItem.isGroupMarker())
                        && (!contributionItem.isSeparator())) {
                    visibleItem = true;

        return (visibleItem || super.isVisible());
[solved] [expressions] toolbar dynamic contribution and visibleWhen expression [message #962692 is a reply to message #948729] Mon, 29 October 2012 08:54 Go to previous message
Stephane Bouchet is currently offline Stephane BouchetFriend
Messages: 280
Registered: July 2009
Senior Member
I found a solution for getting this to work.

I created a new IContributionManagerOverrides class that will request a new Evaluation of the expression.

So i add :
toolBarManager.setOverrides(new MyContributionManagerOverrides(serviceLocator, exp));
in the ContributionFactory class and the ContributionManagerOverrides looks like :

	public Boolean getVisible(IContributionItem item) {
		// request an evaluation of the expression.
		IEvaluationService evaluationService=(IEvaluationService)serviceLocator.getService(IEvaluationService.class);
		try {
			EvaluationResult result=exp.evaluate(evaluationService.getCurrentState());
			if (result == EvaluationResult.TRUE)
				return true;
		catch (CoreException e) {
			// TODO log 
		return false;

Previous Topic:Custom welcome page defaults to standby mode?
Next Topic:Toolbar corrupted: how to reset?
Goto Forum:

Current Time: Thu Oct 29 12:00:35 GMT 2020

Powered by FUDForum. Page generated in 0.02121 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top