Services

The workbench defines a number of services that can be retrieved from the org.eclipse.ui.services.IServiceLocator. Services provide a way for components to retrieve information about the workbench without always having to go to the global singleton: PlatformUI.getWorkbench(). For example:

		IHandlerService handlerService = (IHandlerService) getSite()
				.getService(IHandlerService.class);

Some services provide 2 optional abilities, which are usually specified in the service interface javadoc:

See Associating a handler programmically with a command... for an example of the activation localization provided by the org.eclipse.ui.handlers.IHandlerService.

Services Provided by the Workbench

The services provided to by the workbench now includes some of the most common services that an org.eclipse.ui.IWorkbenchPart would require. Support for service related optional abilities are specified in the service javadoc. The availability of the service is at and below the level specified.

Service Description Availability
IBindingService Provides services related to the binding architecture (e.g., keyboard shortcuts) within the workbench. Globally
ICommandService Provides services related to the command architecture within the workbench. Globally
ICommandImageService Provides a look-up facility for images associated with commands. Globally
IContextService Provides services related to contexts in the Eclipse workbench, like context activation and definitions. Globally
IContributionService The IContributionService is a service provided at the workbench level that provides mechanisms that clients may use to work with user interface contributions. Currently, this is limited to providing sorters for particular contribution types but this may be expanded on in the future. An instance of this service is present for the entire lifetime of the workbench. Globally
IEvaluationService Evaluate a core expression against the workbench application context and report updates using a Boolean property. Also provides the main source for the workbench application context. Globally
IFocusService Tracks focusGained and focusLost events for a Control registered with this service, and provides the control and its registered ID as variables to the application evaluation context for evaluation by the various services. Globally
IHandlerService Provides services related to activating and deactivating handlers within the workbench. Globally
IMenuService Provides services related to the menu architecture within the workbench. It can be used to populate MenuManagers and ToolBarManagers by components. Globally
IPageService A page service tracks the page and perspective lifecycle events within a workbench window. Workbench Window
IPartService A part service tracks the creation and activation of parts within a workbench window. Workbench Window
IProgressService The progress service is the primary interface to the workbench progress support. Globally
IWorkbenchSiteProgressService The part progress service is an IProgressService that adds API for jobs that change the state in a IWorkbenchPartSite while they are being run. Part Site
ISaveablesLifecycleListener Parts that implement org.eclipse.ui.ISaveablesSource should notify their ISaveablesLifecycleListener, available as a service from their site, about org.eclipse.ui.Saveable objects that have been added to or removed from the part. Implementations of ISaveablesSource that are not parts should retrieve this service from the org.eclipse.ui.IWorkbench object. Globally
ISelectionService A selection service tracks the selection within an a workbench window. Workbench Window

Contributing a Service

Plug-in developers providing a framework in eclipse may also want to provide a service. This can be done using the org.eclipse.ui.services extension point. Define a service factory based on org.eclipse.ui.services.AbstractServiceFactory and then specify what services the factory can return. A factory can create more than one type of service, but multiple factories cannot contribute to the same service type.

      <serviceFactory
            factoryClass="org.eclipse.ui.examples.contributions.model.PersonServiceFactory">
         <service
               serviceClass="org.eclipse.ui.examples.contributions.model.IPersonService"/>
      </serviceFactory>

The create method of the org.eclipse.ui.services.AbstractServiceFactory passes in 3 parameters:

Workbench services are organized so there is a global service providing the needed functionality, and a chain of children services. The children services provide the listener cleanup and any data needed to localize the service request.

A service factory should return the global service when the parentLocator returns null when asked for the serviceInterface. The global service may be the implementation, or a proxy to a plugin provided manager or OSGi service.

		Object parentService = parentLocator.getService(IPersonService.class);
		if (parentService == null) {
			return new PersonService(locator);
		}
		return new PersonServiceSlave(locator, (IPersonService) parentService);
		
		// or ...
		Object parentService = parentLocator.getService(IPersonService.class);
		if (parentService == null) {
			return Activator.getDefault().getPersonManager();
		}
		return new PersonServiceSlave(locator, (IPersonService) parentService);
		
		//
		// or as a front to an OSGi service.
		Object parentService = parentLocator.getService(IPersonService.class);
		if (parentService == null) {
			PersonServiceProxy proxy = new PersonServiceProxy(locator,
			        Activator.getDefault().getBundle().getBundleContext(),
			        0);
			proxy.findService();
			return proxy;
		}
		PersonServiceProxy proxy = new PersonServiceProxy(locator,
		     Activator.getDefault().getBundle().getBundleContext(),
		     ((PersonServiceProxy)parentService).getLevel()+1);
		proxy.findService();
		return proxy;

In the OSGi service snippet above, instead of having each child IPersonService know its parent, each IPersonService talks directly to the OSGi service and uses the parent to calculate its "level" which allows it to localize any functionality it provides.