Search in sources :

Example 51 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class EclipseContext method internalGet.

public Object internalGet(EclipseContext originatingContext, String name, boolean local) {
    if (this == originatingContext) {
        ValueComputation valueComputation = localValueComputations.get(name);
        if (valueComputation != null) {
            Object result = valueComputation.get();
            if (result != IInjector.NOT_A_VALUE) {
                return result;
            }
        }
    }
    Object result = null;
    // 1. try for local value
    if (localValues.containsKey(name)) {
        result = localValues.get(name);
        if (result == null)
            return null;
    } else
        result = lookup(name, originatingContext);
    // if we found something, compute the concrete value and return
    if (result != null) {
        if (result instanceof IContextFunction) {
            ValueComputation valueComputation = new ValueComputation(name, originatingContext, ((IContextFunction) result));
            // do calculations before adding listeners
            result = valueComputation.get();
            originatingContext.localValueComputations.put(name, valueComputation);
        }
        if (result != IInjector.NOT_A_VALUE) {
            return result;
        }
    }
    // 3. delegate to parent
    if (!local) {
        IEclipseContext parent = (IEclipseContext) localValues.get(PARENT);
        if (parent != null) {
            return ((EclipseContext) parent).internalGet(originatingContext, name, local);
        }
    }
    return null;
}
Also used : IContextFunction(org.eclipse.e4.core.contexts.IContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext)

Example 52 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class ContextObjectSupplier method get.

@Override
public void get(IObjectDescriptor[] descriptors, Object[] actualArgs, final IRequestor requestor, boolean initial, boolean track, boolean group) {
    final String[] keys = new String[descriptors.length];
    final boolean[] active = new boolean[descriptors.length];
    for (int i = 0; i < descriptors.length; i++) {
        String key = getKey(descriptors[i]);
        if ((actualArgs[i] == IInjector.NOT_A_VALUE))
            keys[i] = key;
        else if (// allow provider to override IEclipseContext
        ECLIPSE_CONTEXT_NAME.equals(key))
            keys[i] = ECLIPSE_CONTEXT_NAME;
        else
            keys[i] = null;
        if (descriptors[i] == null)
            active[i] = false;
        else
            active[i] = (descriptors[i].hasQualifier(Active.class));
    }
    if (requestor != null && track) {
        // only track if requested
        if (initial) {
            RunAndTrack trackable = new ContextInjectionListener(context, actualArgs, keys, active, requestor, group);
            context.runAndTrack(trackable);
        } else {
            // we do track if this is done inside a computation, but don't create another runnable
            fillArgs(actualArgs, keys, active);
        }
    } else {
        if (descriptors.length > 0) {
            pauseRecording();
            try {
                fillArgs(actualArgs, keys, active);
            } finally {
                resumeRecording();
            }
        }
    }
}
Also used : Active(org.eclipse.e4.core.contexts.Active) RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack)

Example 53 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class ContextInjectionFactory method make.

/**
 * Obtain an instance of the specified class and inject it with the context. This method
 * allows extra values that don't need to be tracked to be passed to the object using staticContext.
 * <p>
 * If values for the same key present in both the context and the static context, the values from
 * the static context are injected.
 * </p>
 * <p>
 * Class'es scope dictates if a new instance of the class will be created, or existing instance
 * will be reused.
 * </p>
 * @param clazz The class to be instantiated
 * @param context The context to obtain injected values from
 * @param staticContext The context containing extra values; not tracked
 * @return an instance of the specified class
 * @throws InjectionException if an exception occurred while performing this operation
 * @see #make(Class, IEclipseContext)
 */
public static <T> T make(Class<T> clazz, IEclipseContext context, IEclipseContext staticContext) throws InjectionException {
    PrimaryObjectSupplier supplier = ContextObjectSupplier.getObjectSupplier(context, injector);
    PrimaryObjectSupplier tempSupplier = ContextObjectSupplier.getObjectSupplier(staticContext, injector);
    return injector.make(clazz, supplier, tempSupplier);
}
Also used : PrimaryObjectSupplier(org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)

Example 54 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class ContextInjectionFactory method invoke.

/**
 * Call a method, injecting the parameters from two contexts. This method is useful when the method needs
 * to receive some values not present in the context. In this case a local context can be created and
 * populated with additional values.
 * <p>
 * If values for the same key present in both the context and the local context, the values from
 * the local context are injected.
 * </p>
 * <p>
 * If no matching method is found on the class, the defaultValue will be returned.
 * </p>
 * @param object The object to perform injection on
 * @param qualifier the annotation tagging method to be called
 * @param context The context to obtain injected values from
 * @param localContext The context to obtain addition injected values from
 * @param defaultValue A value to be returned if the method cannot be called, might be <code>null</code>
 * @return the return value of the method call, might be <code>null</code>
 * @throws InjectionException if an exception occurred while performing this operation
 */
public static Object invoke(Object object, Class<? extends Annotation> qualifier, IEclipseContext context, IEclipseContext localContext, Object defaultValue) throws InjectionException {
    PrimaryObjectSupplier supplier = ContextObjectSupplier.getObjectSupplier(context, injector);
    PrimaryObjectSupplier tempSupplier = ContextObjectSupplier.getObjectSupplier(localContext, injector);
    return injector.invoke(object, qualifier, defaultValue, supplier, tempSupplier);
}
Also used : PrimaryObjectSupplier(org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)

Example 55 with IEclipseContext

use of org.eclipse.e4.core.contexts.IEclipseContext in project eclipse.platform.runtime by eclipse.

the class ContextInjectionFactory method setDefault.

/**
 * Specifies context used by the injector to create its internal objects.
 * Providing this context allows injector to become aware of higher-level
 * constructs, such as application logging and synchronization.
 * @param context the context to be used as a data source by the injector
 * @since 1.2
 */
public static void setDefault(IEclipseContext context) {
    PrimaryObjectSupplier supplier = ContextObjectSupplier.getObjectSupplier(context, injector);
    injector.setDefaultSupplier(supplier);
}
Also used : PrimaryObjectSupplier(org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)

Aggregations

IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)197 Test (org.junit.Test)142 RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)18 ContextFunction (org.eclipse.e4.core.contexts.ContextFunction)17 BundleContext (org.osgi.framework.BundleContext)15 IEntity (org.whole.lang.model.IEntity)11 IEntityPartViewer (org.whole.lang.ui.viewers.IEntityPartViewer)9 Execute (org.eclipse.e4.core.di.annotations.Execute)7 MPart (org.eclipse.e4.ui.model.application.ui.basic.MPart)6 IImportAsModelDialogFactory (org.whole.lang.ui.dialogs.IImportAsModelDialogFactory)6 Hashtable (java.util.Hashtable)5 MApplication (org.eclipse.e4.ui.model.application.MApplication)5 PrimaryObjectSupplier (org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)4 Named (javax.inject.Named)3 PerformanceTestRunner (org.eclipse.core.tests.harness.PerformanceTestRunner)3 InjectionException (org.eclipse.e4.core.di.InjectionException)3 CanExecute (org.eclipse.e4.core.di.annotations.CanExecute)3 Optional (org.eclipse.e4.core.di.annotations.Optional)3 UISynchronize (org.eclipse.e4.ui.di.UISynchronize)3 ContextMenuProvider (org.eclipse.gef.ContextMenuProvider)3