Search in sources :

Example 1 with ActionDescriptor

use of org.eclipse.ui.internal.ActionDescriptor 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)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ContextFunction (org.eclipse.e4.core.contexts.ContextFunction)1 IContextFunction (org.eclipse.e4.core.contexts.IContextFunction)1 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)1 AbstractPartRenderer (org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer)1 MMenu (org.eclipse.e4.ui.model.application.ui.menu.MMenu)1 MToolBar (org.eclipse.e4.ui.model.application.ui.menu.MToolBar)1 MenuManagerRenderer (org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRenderer)1 ToolBarManagerRenderer (org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer)1 IRendererFactory (org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory)1 IAction (org.eclipse.jface.action.IAction)1 MenuManager (org.eclipse.jface.action.MenuManager)1 ToolBarManager (org.eclipse.jface.action.ToolBarManager)1 ActionHandler (org.eclipse.jface.commands.ActionHandler)1 Composite (org.eclipse.swt.widgets.Composite)1 IActionBars (org.eclipse.ui.IActionBars)1 IHandlerService (org.eclipse.ui.handlers.IHandlerService)1 ActionDescriptor (org.eclipse.ui.internal.ActionDescriptor)1 ViewActionBuilder (org.eclipse.ui.internal.ViewActionBuilder)1 ViewDescriptor (org.eclipse.ui.internal.registry.ViewDescriptor)1