Search in sources :

Example 11 with IContributionFactory

use of org.eclipse.e4.core.services.contributions.IContributionFactory in project eclipse.platform.ui by eclipse-platform.

the class ToolControlRenderer method createWidget.

@Override
public Object createWidget(final MUIElement element, Object parent) {
    if (!(element instanceof MToolControl) || !(parent instanceof ToolBar || parent instanceof Composite))
        return null;
    Composite parentComp = (Composite) parent;
    MToolControl toolControl = (MToolControl) element;
    if (((Object) toolControl.getParent()) instanceof MToolBar) {
        IRendererFactory factory = context.get(IRendererFactory.class);
        AbstractPartRenderer renderer = factory.getRenderer(toolControl.getParent(), parent);
        if (renderer instanceof ToolBarManagerRenderer) {
            return null;
        }
    }
    Widget parentWidget = (Widget) parent;
    IEclipseContext parentContext = getContextForParent(element);
    ToolItem sep = null;
    if (parent instanceof ToolBar) {
        sep = new ToolItem((ToolBar) parentWidget, SWT.SEPARATOR);
    }
    // final Composite newComposite = new Composite((Composite)
    // parentWidget,
    // SWT.NONE);
    // newComposite.setLayout(new FillLayout());
    // bindWidget(element, newComposite);
    // Create a context just to contain the parameters for injection
    IContributionFactory contributionFactory = parentContext.get(IContributionFactory.class);
    IEclipseContext localContext = EclipseContextFactory.create();
    localContext.set(Composite.class.getName(), parentComp);
    localContext.set(MToolControl.class.getName(), toolControl);
    Object tcImpl = contributionFactory.create(toolControl.getContributionURI(), parentContext, localContext);
    toolControl.setObject(tcImpl);
    Control[] kids = parentComp.getChildren();
    // No kids means that the trim failed curing creation
    if (kids.length == 0)
        return null;
    // The new control is assumed to be the last child created
    // We could safe this up even more by asserting that the
    // number of children should go up by *one* during injection
    Control newCtrl = kids[kids.length - 1];
    if (sep != null && newCtrl != null) {
        sep.setControl(newCtrl);
        newCtrl.pack();
        sep.setWidth(newCtrl.getSize().x);
    }
    bindWidget(toolControl, newCtrl);
    boolean vertical = false;
    MUIElement parentElement = element.getParent();
    if (parentElement instanceof MTrimBar) {
        MTrimBar bar = (MTrimBar) parentElement;
        vertical = bar.getSide() == SideValue.LEFT || bar.getSide() == SideValue.RIGHT;
    }
    CSSRenderingUtils cssUtils = parentContext.get(CSSRenderingUtils.class);
    MUIElement modelElement = (MUIElement) newCtrl.getData(AbstractPartRenderer.OWNING_ME);
    boolean draggable = ((modelElement != null) && (modelElement.getTags().contains(IPresentationEngine.DRAGGABLE)));
    newCtrl = cssUtils.frameMeIfPossible(newCtrl, null, vertical, draggable);
    boolean hideable = isHideable(toolControl);
    boolean showRestoreMenu = isRestoreMenuShowable(toolControl);
    if (showRestoreMenu || hideable) {
        createToolControlMenu(toolControl, newCtrl, hideable);
    }
    return newCtrl;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) CSSRenderingUtils(org.eclipse.e4.ui.internal.workbench.swt.CSSRenderingUtils) Widget(org.eclipse.swt.widgets.Widget) MTrimBar(org.eclipse.e4.ui.model.application.ui.basic.MTrimBar) MToolBar(org.eclipse.e4.ui.model.application.ui.menu.MToolBar) MToolControl(org.eclipse.e4.ui.model.application.ui.menu.MToolControl) Control(org.eclipse.swt.widgets.Control) AbstractPartRenderer(org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer) MToolControl(org.eclipse.e4.ui.model.application.ui.menu.MToolControl) IRendererFactory(org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory) ToolBar(org.eclipse.swt.widgets.ToolBar) MToolBar(org.eclipse.e4.ui.model.application.ui.menu.MToolBar) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MUIElement(org.eclipse.e4.ui.model.application.ui.MUIElement) IContributionFactory(org.eclipse.e4.core.services.contributions.IContributionFactory) ToolItem(org.eclipse.swt.widgets.ToolItem)

Example 12 with IContributionFactory

use of org.eclipse.e4.core.services.contributions.IContributionFactory in project eclipse.platform.ui by eclipse-platform.

the class E4Application method loadApplicationModel.

private MApplication loadApplicationModel(IApplicationContext appContext, IEclipseContext eclipseContext) {
    Location instanceLocation = WorkbenchSWTActivator.getDefault().getInstanceLocation();
    URI applicationModelURI = determineApplicationModelURI(appContext);
    eclipseContext.set(E4Workbench.INITIAL_WORKBENCH_MODEL_URI, applicationModelURI);
    // Save and restore
    Boolean saveAndRestore = getArgValue(IWorkbench.PERSIST_STATE, appContext, false).map(Boolean::parseBoolean).orElse(Boolean.TRUE);
    eclipseContext.set(IWorkbench.PERSIST_STATE, saveAndRestore);
    // when -data @none or -data @noDefault options
    if (instanceLocation != null && instanceLocation.getURL() != null) {
        eclipseContext.set(E4Workbench.INSTANCE_LOCATION, instanceLocation);
    } else {
        eclipseContext.set(IWorkbench.PERSIST_STATE, false);
    }
    // Persisted state
    Boolean clearPersistedState = getArgValue(IWorkbench.CLEAR_PERSISTED_STATE, appContext, true).map(Boolean::parseBoolean).orElse(Boolean.FALSE);
    eclipseContext.set(IWorkbench.CLEAR_PERSISTED_STATE, clearPersistedState);
    String resourceHandler = getArgValue(IWorkbench.MODEL_RESOURCE_HANDLER, appContext, false).orElse("bundleclass://org.eclipse.e4.ui.workbench/" + ResourceHandler.class.getName());
    IContributionFactory factory = eclipseContext.get(IContributionFactory.class);
    handler = (IModelResourceHandler) factory.create(resourceHandler, eclipseContext);
    eclipseContext.set(IModelResourceHandler.class, handler);
    Resource resource = handler.loadMostRecentModel();
    return (MApplication) resource.getContents().get(0);
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI) IContributionFactory(org.eclipse.e4.core.services.contributions.IContributionFactory) Location(org.eclipse.osgi.service.datalocation.Location) MApplication(org.eclipse.e4.ui.model.application.MApplication)

Example 13 with IContributionFactory

use of org.eclipse.e4.core.services.contributions.IContributionFactory in project eclipse.platform.ui by eclipse-platform.

the class PartRenderingEngine method initialize.

/**
 * Initialize a part renderer from the extension point.
 *
 * @param context
 *            the context for the part factories
 */
@PostConstruct
void initialize(IEclipseContext context) {
    this.appContext = context;
    // initialize the correct key-binding display formatter
    KeyFormatterFactory.setDefault(SWTKeySupport.getKeyFormatterForPlatform());
    // Add the renderer to the context
    context.set(IPresentationEngine.class, this);
    IRendererFactory factory = null;
    IContributionFactory contribFactory = context.get(IContributionFactory.class);
    try {
        factory = (IRendererFactory) contribFactory.create(factoryUrl, context);
    } catch (Exception e) {
        logger.warn(e, "Could not create rendering factory");
    }
    // Try to load the default one
    if (factory == null) {
        try {
            factory = (IRendererFactory) contribFactory.create(defaultFactoryUrl, context);
        } catch (Exception e) {
            logger.error(e, "Could not create default rendering factory");
        }
    }
    if (factory == null) {
        throw new IllegalStateException("Could not create any rendering factory. Aborting ...");
    }
    curFactory = factory;
    context.set(IRendererFactory.class, curFactory);
    cssThemeChangedHandler = new StylingPreferencesHandler(context.get(Display.class));
}
Also used : IRendererFactory(org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory) IContributionFactory(org.eclipse.e4.core.services.contributions.IContributionFactory) IOException(java.io.IOException) InjectionException(org.eclipse.e4.core.di.InjectionException) InvalidRegistryObjectException(org.eclipse.core.runtime.InvalidRegistryObjectException) PostConstruct(javax.annotation.PostConstruct)

Example 14 with IContributionFactory

use of org.eclipse.e4.core.services.contributions.IContributionFactory in project eclipse.platform.ui by eclipse-platform.

the class ModelAssembler method runProcessor.

private void runProcessor(IConfigurationElement ce) {
    IEclipseContext localContext = EclipseContextFactory.create();
    IContributionFactory factory = context.get(IContributionFactory.class);
    for (IConfigurationElement ceEl : ce.getChildren("element")) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        String id = ceEl.getAttribute("id");
        if (id == null) {
            // $NON-NLS-1$
            log(LogLevel.WARN, "No element id given");
            continue;
        }
        // $NON-NLS-1$
        String key = ceEl.getAttribute("contextKey");
        if (key == null) {
            key = id;
        }
        MApplicationElement el = ModelUtils.findElementById(application, id);
        if (el == null) {
            // $NON-NLS-1$
            log(LogLevel.WARN, "Could not find element with id {}", id);
        }
        localContext.set(key, el);
    }
    try {
        Object o = // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        factory.create(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        "bundleclass://" + ce.getContributor().getName() + "/" + ce.getAttribute("class"), context, localContext);
        if (o == null) {
            log(// $NON-NLS-1$
            LogLevel.WARN, // $NON-NLS-1$
            "Unable to create processor {} from {}", // $NON-NLS-1$
            ce.getAttribute("class"), ce.getContributor().getName());
        } else {
            ContextInjectionFactory.invoke(o, Execute.class, context, localContext, null);
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        log(LogLevel.WARN, "Could not run processor: {}", e);
    }
}
Also used : MApplicationElement(org.eclipse.e4.ui.model.application.MApplicationElement) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) EObject(org.eclipse.emf.ecore.EObject) IContributionFactory(org.eclipse.e4.core.services.contributions.IContributionFactory) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)

Example 15 with IContributionFactory

use of org.eclipse.e4.core.services.contributions.IContributionFactory in project eclipse.platform.ui by eclipse-platform.

the class HandlerProcessingAddon method processActiveHandler.

/**
 * @param handler
 * @param context
 */
private void processActiveHandler(MHandler handler, IEclipseContext context) {
    MCommand command = handler.getCommand();
    if (command == null) {
        return;
    }
    String commandId = command.getElementId();
    if (handler.getObject() == null) {
        IContributionFactory contributionFactory = context.get(IContributionFactory.class);
        handler.setObject(contributionFactory.create(handler.getContributionURI(), context));
    }
    EHandlerService handlerService = context.get(EHandlerService.class);
    handlerService.activateHandler(commandId, handler.getObject());
}
Also used : EHandlerService(org.eclipse.e4.core.commands.EHandlerService) MCommand(org.eclipse.e4.ui.model.application.commands.MCommand) IContributionFactory(org.eclipse.e4.core.services.contributions.IContributionFactory)

Aggregations

IContributionFactory (org.eclipse.e4.core.services.contributions.IContributionFactory)16 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)9 IPresentationEngine (org.eclipse.e4.ui.workbench.IPresentationEngine)5 EObject (org.eclipse.emf.ecore.EObject)5 EHandlerService (org.eclipse.e4.core.commands.EHandlerService)3 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)2 MApplication (org.eclipse.e4.ui.model.application.MApplication)2 MDirectMenuItem (org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem)2 MHandledMenuItem (org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem)2 IRendererFactory (org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory)2 Composite (org.eclipse.swt.widgets.Composite)2 Widget (org.eclipse.swt.widgets.Widget)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 PostConstruct (javax.annotation.PostConstruct)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 IStatus (org.eclipse.core.runtime.IStatus)1 InvalidRegistryObjectException (org.eclipse.core.runtime.InvalidRegistryObjectException)1 Status (org.eclipse.core.runtime.Status)1 ExpressionContext (org.eclipse.e4.core.commands.ExpressionContext)1