Search in sources :

Example 81 with ToolBarManager

use of org.eclipse.jface.action.ToolBarManager in project eclipse.platform.ui by eclipse-platform.

the class CompatibilityView method createPartControl.

@Override
protected boolean createPartControl(IWorkbenchPart legacyPart, Composite parent) {
    clearMenuItems();
    part.getContext().set(IViewPart.class, (IViewPart) legacyPart);
    final IEclipseContext partContext = getModel().getContext();
    IRendererFactory rendererFactory = partContext.get(IRendererFactory.class);
    // Some views (i.e. Console) require that the actual ToolBar be
    // instantiated before they are
    final IActionBars actionBars = ((IViewPart) legacyPart).getViewSite().getActionBars();
    ToolBarManager tbm = (ToolBarManager) actionBars.getToolBarManager();
    Composite toolBarParent = new Composite(parent, SWT.NONE);
    tbm.createControl(toolBarParent);
    MenuManager mm = (MenuManager) actionBars.getMenuManager();
    MMenu menu = getViewMenu();
    if (menu == null) {
        menu = modelService.createModelElement(MMenu.class);
        // If the id contains a ':' use the part before it as the descriptor
        // id
        String partId = part.getElementId();
        int colonIndex = partId.indexOf(':');
        String descId = colonIndex == -1 ? partId : partId.substring(0, colonIndex);
        menu.setElementId(descId);
        menu.getTags().add(StackRenderer.TAG_VIEW_MENU);
        menu.getTags().add(ContributionsAnalyzer.MC_MENU);
        part.getMenus().add(menu);
    }
    AbstractPartRenderer apr = rendererFactory.getRenderer(menu, parent);
    if (apr instanceof MenuManagerRenderer) {
        MenuManagerRenderer renderer = (MenuManagerRenderer) apr;
        renderer.linkModelToManager(menu, mm);
    }
    // Construct the toolbar (if necessary)
    MToolBar toolbar = part.getToolbar();
    if (toolbar == null) {
        toolbar = modelService.createModelElement(MToolBar.class);
        // If the id contains a ':' use the part before it as the descriptor
        // id
        String partId = part.getElementId();
        int colonIndex = partId.indexOf(':');
        String descId = colonIndex == -1 ? partId : partId.substring(0, colonIndex);
        toolbar.setElementId(descId);
        part.setToolbar(toolbar);
    } else {
        // clear out the model entries so they can be re-created by
        // contributions
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=402561
        toolbar.getChildren().clear();
    }
    apr = rendererFactory.getRenderer(toolbar, parent);
    if (apr instanceof ToolBarManagerRenderer) {
        ((ToolBarManagerRenderer) apr).linkModelToManager(toolbar, tbm);
    }
    super.createPartControl(legacyPart, parent);
    ViewDescriptor desc = reference.getDescriptor();
    if (desc != null && desc.getPluginId() != null) {
        parent.setData(new ContributionInfo(desc.getPluginId(), ContributionInfoMessages.ContributionInfo_View, null));
    }
    // dispose the tb, it will be re-created when the tab is shown
    toolBarParent.dispose();
    apr = rendererFactory.getRenderer(menu, parent);
    if (apr instanceof MenuManagerRenderer) {
        MenuManagerRenderer renderer = (MenuManagerRenderer) apr;
        // create opaque items for any contribution items that were added
        // directly to the manager
        renderer.reconcileManagerToModel(mm, menu);
    }
    apr = rendererFactory.getRenderer(toolbar, parent);
    if (apr instanceof ToolBarManagerRenderer) {
        // create opaque items for any contribution items that were added
        // directly to the manager
        ((ToolBarManagerRenderer) apr).reconcileManagerToModel(tbm, toolbar);
    }
    final AtomicBoolean toolbarContributed = new AtomicBoolean();
    final IContextFunction func = new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            if (toolbarContributed.getAndSet(true)) {
                // fix for bug 448873: don't contribute to the toolbar twice
                return null;
            }
            final ViewActionBuilder actionBuilder = new ViewActionBuilder();
            actionBuilder.readActionExtensions(getView());
            ActionDescriptor[] actionDescriptors = actionBuilder.getExtendedActions();
            if (actionDescriptors != null) {
                IHandlerService hs = partContext.get(IHandlerService.class);
                for (ActionDescriptor actionDescriptor : actionDescriptors) {
                    if (actionDescriptor != null) {
                        IAction action = actionDescriptor.getAction();
                        if (action != null && action.getActionDefinitionId() != null) {
                            hs.activateHandler(action.getActionDefinitionId(), new ActionHandler(action));
                        }
                    }
                }
            }
            actionBars.updateActionBars();
            Runnable dispose = actionBuilder::dispose;
            return dispose;
        }
    };
    if (toolbar.getWidget() == null) {
        toolbar.getTransientData().put(ToolBarManagerRenderer.POST_PROCESSING_FUNCTION, func);
    } else {
        toolbar.getTransientData().put(ToolBarManagerRenderer.POST_PROCESSING_DISPOSE, func.compute(partContext, null));
    }
    return true;
}
Also used : ActionDescriptor(org.eclipse.ui.internal.ActionDescriptor) ToolBarManager(org.eclipse.jface.action.ToolBarManager) ViewDescriptor(org.eclipse.ui.internal.registry.ViewDescriptor) IContextFunction(org.eclipse.e4.core.contexts.IContextFunction) IRendererFactory(org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory) IActionBars(org.eclipse.ui.IActionBars) Composite(org.eclipse.swt.widgets.Composite) ContributionInfo(org.eclipse.ui.testing.ContributionInfo) IAction(org.eclipse.jface.action.IAction) MMenu(org.eclipse.e4.ui.model.application.ui.menu.MMenu) MToolBar(org.eclipse.e4.ui.model.application.ui.menu.MToolBar) ViewActionBuilder(org.eclipse.ui.internal.ViewActionBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IContextFunction(org.eclipse.e4.core.contexts.IContextFunction) AbstractPartRenderer(org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer) ToolBarManagerRenderer(org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer) IHandlerService(org.eclipse.ui.handlers.IHandlerService) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MenuManager(org.eclipse.jface.action.MenuManager) MenuManagerRenderer(org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRenderer) ActionHandler(org.eclipse.jface.commands.ActionHandler)

Example 82 with ToolBarManager

use of org.eclipse.jface.action.ToolBarManager in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchMenuService method clearContributions.

/**
 * Disposes contributions created by service for given part. See bug 537046.
 *
 * @param site
 * @param part
 */
@Override
public void clearContributions(PartSite site, MPart part) {
    List<MToolBar> toolbars = getContributedToolbars(part);
    IEclipseContext context = part.getContext();
    IRendererFactory rendererFactory = context.get(IRendererFactory.class);
    IActionBars actionBars = site.getActionBars();
    if (toolbars != null) {
        for (MToolBar mToolBar : toolbars) {
            ((Notifier) mToolBar).eAdapters().clear();
            AbstractPartRenderer apr = rendererFactory.getRenderer(mToolBar, null);
            if (apr instanceof ToolBarManagerRenderer) {
                ToolBarManager tbm = (ToolBarManager) actionBars.getToolBarManager();
                ToolBarManagerRenderer tbmr = (ToolBarManagerRenderer) apr;
                tbmr.clearModelToManager(mToolBar, tbm);
                CompatibilityView.clearOpaqueToolBarItems(tbmr, mToolBar);
            }
            mToolBar.getTransientData().remove(ToolBarManagerRenderer.POST_PROCESSING_FUNCTION);
            final IPresentationEngine engine = context.get(IPresentationEngine.class);
            engine.removeGui(mToolBar);
            mToolBar.getTransientData().remove(ModelUtils.CONTAINING_PARENT);
        }
    }
    List<MMenu> menus = getContributedMenus(part);
    if (menus != null) {
        for (MMenu mMenu : menus) {
            ((Notifier) mMenu).eAdapters().clear();
            AbstractPartRenderer apr = rendererFactory.getRenderer(mMenu, null);
            if (apr instanceof MenuManagerRenderer) {
                MenuManager tbm = (MenuManager) actionBars.getMenuManager();
                MenuManagerRenderer tbmr = (MenuManagerRenderer) apr;
                tbmr.clearModelToManager(mMenu, tbm);
                CompatibilityView.clearOpaqueMenuItems(tbmr, mMenu);
            }
            final IPresentationEngine engine = context.get(IPresentationEngine.class);
            engine.removeGui(mMenu);
            mMenu.getTransientData().remove(ModelUtils.CONTAINING_PARENT);
        }
    }
}
Also used : IPresentationEngine(org.eclipse.e4.ui.workbench.IPresentationEngine) MMenu(org.eclipse.e4.ui.model.application.ui.menu.MMenu) ToolBarManager(org.eclipse.jface.action.ToolBarManager) MToolBar(org.eclipse.e4.ui.model.application.ui.menu.MToolBar) AbstractPartRenderer(org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer) ToolBarManagerRenderer(org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer) IRendererFactory(org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MenuManager(org.eclipse.jface.action.MenuManager) MenuManagerRenderer(org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRenderer) IActionBars(org.eclipse.ui.IActionBars)

Example 83 with ToolBarManager

use of org.eclipse.jface.action.ToolBarManager in project eclipse.platform.ui by eclipse-platform.

the class SupportTray method createContents.

@Override
protected Control createContents(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    if (providesSupport(getCurrentStatusAdapter()) == null && getBooleanValue(IStatusDialogConstants.TRAY_OPENED)) {
    }
    GridLayout layout = new GridLayout();
    layout.marginWidth = layout.marginHeight = 0;
    layout.verticalSpacing = 0;
    container.setLayout(layout);
    GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
    container.setLayoutData(layoutData);
    container.addListener(SWT.Dispose, event -> destroyImages());
    if (!hideSupportButtons) {
        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
        toolBarManager.createControl(container);
        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
        gd.grabExcessHorizontalSpace = true;
        toolBarManager.getControl().setLayoutData(gd);
        Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
        gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
        gd.heightHint = 1;
        separator.setLayoutData(gd);
        createActions();
        toolBarManager.add(closeAction);
        toolBarManager.update(true);
    }
    supportArea = new Composite(container, SWT.NONE);
    layout = new GridLayout();
    layout.marginWidth = layout.marginHeight = 0;
    layout.verticalSpacing = 0;
    supportArea.setLayout(layout);
    GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
    gd.horizontalSpan = 1;
    gd.grabExcessHorizontalSpace = true;
    gd.grabExcessVerticalSpace = true;
    supportArea.setLayoutData(gd);
    if (lastSelectedStatus != null)
        createSupportArea(supportArea, lastSelectedStatus);
    Point shellSize = supportArea.getShell().getSize();
    Point desiredSize = supportArea.getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
    if (desiredSize.y > shellSize.y) {
        supportArea.getShell().setSize(shellSize.x, Math.min(desiredSize.y, 500));
    }
    return container;
}
Also used : GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) GridData(org.eclipse.swt.layout.GridData) Label(org.eclipse.swt.widgets.Label) Point(org.eclipse.swt.graphics.Point) ToolBarManager(org.eclipse.jface.action.ToolBarManager)

Example 84 with ToolBarManager

use of org.eclipse.jface.action.ToolBarManager in project eclipse.platform.ui by eclipse-platform.

the class ApplicationActionBarAdvisor method fillCoolBar.

@Override
protected void fillCoolBar(ICoolBarManager coolBar) {
    IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.LEFT);
    coolBar.add(new ToolBarContributionItem(toolbar, "main"));
    for (Action action : switchToThemeActions) {
        toolbar.add(action);
    }
}
Also used : ToolBarContributionItem(org.eclipse.jface.action.ToolBarContributionItem) Action(org.eclipse.jface.action.Action) CssThemeSwitchAction(org.eclipse.e4.demo.cssbridge.ui.actions.CssThemeSwitchAction) IToolBarManager(org.eclipse.jface.action.IToolBarManager) IToolBarManager(org.eclipse.jface.action.IToolBarManager) ToolBarManager(org.eclipse.jface.action.ToolBarManager)

Example 85 with ToolBarManager

use of org.eclipse.jface.action.ToolBarManager in project eclipse.platform.ui by eclipse-platform.

the class ToolBarManagerRendererTest method testDynamicItem_Reconcile_RemoveOneByID.

@Test
public void testDynamicItem_Reconcile_RemoveOneByID() {
    MToolItem toolItem1 = ems.createModelElement(MDirectToolItem.class);
    toolItem1.setElementId("Item1");
    toolBar.getChildren().add(toolItem1);
    MToolItem toolItem2 = ems.createModelElement(MDirectToolItem.class);
    toolItem2.setElementId("Item2");
    toolBar.getChildren().add(toolItem2);
    contextRule.createAndRunWorkbench(window);
    ToolBarManagerRenderer renderer = getToolBarManagerRenderer();
    ToolBarManager tbm = getToolBarManager();
    assertEquals(2, toolBar.getChildren().size());
    assertEquals(2, tbm.getSize());
    tbm.remove("Item1");
    renderer.reconcileManagerToModel(tbm, toolBar);
    assertEquals(1, toolBar.getChildren().size());
    assertEquals(1, tbm.getSize());
    assertEquals("Item2", tbm.getItems()[0].getId());
}
Also used : MToolItem(org.eclipse.e4.ui.model.application.ui.menu.MToolItem) ToolBarManager(org.eclipse.jface.action.ToolBarManager) Test(org.junit.Test)

Aggregations

ToolBarManager (org.eclipse.jface.action.ToolBarManager)199 Composite (org.eclipse.swt.widgets.Composite)82 Action (org.eclipse.jface.action.Action)63 ToolBar (org.eclipse.swt.widgets.ToolBar)61 GridData (org.eclipse.swt.layout.GridData)54 IToolBarManager (org.eclipse.jface.action.IToolBarManager)49 Control (org.eclipse.swt.widgets.Control)47 GridLayout (org.eclipse.swt.layout.GridLayout)41 List (java.util.List)35 Separator (org.eclipse.jface.action.Separator)34 SWT (org.eclipse.swt.SWT)33 ArrayList (java.util.ArrayList)30 IAction (org.eclipse.jface.action.IAction)26 IMenuManager (org.eclipse.jface.action.IMenuManager)26 Images (name.abuchen.portfolio.ui.Images)24 Messages (name.abuchen.portfolio.ui.Messages)24 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)23 Inject (javax.inject.Inject)22 SimpleAction (name.abuchen.portfolio.ui.util.SimpleAction)22 IContributionItem (org.eclipse.jface.action.IContributionItem)22