The Eclipse context can supply not only services but also dynamically calculated values. These are supplied via an interface IContextFuction
. By registering a service with that class name, and a key name with the service.context.key
it is possible to create a value upon request.
Create a class called
RandomFunction
which extendsContextFunction
and which returns a random value:package com.packtpub.e4.application; import org.eclipse.e4.core.contexts.IContextFunction; import org.eclipse.e4.core.contexts.IEclipseContext; public final class RandomFunction extends ContextFunction { @Override public Object compute(final IEclipseContext context) { return Math.random(); } }
To allow E4 to recognize the function, register an instance with the OSGi runtime. This could be done within the
Activator
, but currently a bug prevents this from happening. Instead, register it using declarative services. Create a file calledrandom.xml
in a folder called...