wiki:Migrate_View_To_Part

Migrating a 3.x View to e4 Part

This section talks about generic migration of Eclipse 3.x View to e4 Part

Creating Part Control

  • replace createPartControl(Composite parent) with any method named xyz(Composite parent) and add a @PostConstruct annotation to it.
  • Keep all the control creation part in it like creating Text or TreeViewer.
  • if the controls had listeners you can still keep the listeners
    • but set the current selection in the selectionService object (so that other parts can use it)
    • for this you have to add a field of type ESelectionService and add @Inject annotation to it

Here is a short code snippet for doing above simple steps. You can build upon later with this

code in 3.x View

	private TableViewer tableViewer = null;

	@Override
	public void createPartControl(Composite parent)
	{

	....

		// you create controls and might add listeners

		tableViewer.addSelectionChangedListener(new ISelectionChangedListener()
		{
			@Override
			public void selectionChanged(SelectionChangedEvent event)
			{
				// doing more stuff here
			}
		});

	....

	}

replace with code in e4 Part

	private TableViewer tableViewer = null;
	@Inject private ESelectionService selectionService = null;

	@PostConstruct
	public void xyz(Composite parent)
	{
		....

		// you create controls and might add listeners

		tableViewer.addSelectionChangedListener(new ISelectionChangedListener()
		{
			@Override
			public void selectionChanged(SelectionChangedEvent event)
			{
				// do less stuff here and set the selection
				selectionService.setSelection(
					((StructuredSelection) event.getSelection()
								).getFirstElement());
				// the classes that are looking for this selection
				// will themselves do it
				// by getting the ESelectionService 
				// object in the parameter of an injected method
				// as this will re-inject the
				// ESelectionService object

			}
		});

	}

Setting e4 part dirty

if this e4 part is dirtyable then also add a MDirtyable object in the e4 Part class

	private TableViewer tableViewer = null;
	@Inject private ESelectionService selectionService = null;
	...
	@Inject private MDirtyable dirtyable = null;

	...
	// set dirty where you need to show part dirty after some changes
	dirtyable.setDirty(true);

Resetting e4 Part or refreshing some UI component

Based on changes in some other part, whose object is being displayed here, you might want to change the values in this Part. Let's say Part someOtherPart is displaying its Abc object and now this Abc changed in that Part. For this you have to inject a method resetAbc() as shown

	@Inject
	void resetAbc(@Optional Abc abc)
	{
	 // do the resetting of ui controls etc here
	}

  • Keeping Abc @Optional will help you avoid Exceptions when Abc object might not be injected during initialization
  • if you know Abc abc object will be injected through some ESelectionService (easiest and most common way of injecting non-Eclipse objects) then you can also add @Named(IServiceConstants.ACTIVE_SELECTION) to it as shown
	@Inject
	void resetAbc(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) Abc abc)
	{
	 ...
	}

Other Helpful Annotations

For replacing some of the methods like setFocus or doSaveAs use the annotations @Focus and @Persist respectively.

E4 Part Tutorial

Last modified 4 years ago Last modified on 03/22/2016 09:52:46 PM