Skip to content

How to use SelectionService

chqu1012 edited this page Jun 4, 2019 · 2 revisions

This SelectionService is used like in Eclipse RCP SelectionService. All interested members can add as provider to this service and wait for the selection.

This service can be access by Dependency Injection

@Inject ISelectionService selectionService;

or directly

EmfWorkbenchContext.getInstance(ISelectionService.class);

A provider should be a type of ObjectProperty. All the views in JavaFX like TableView, TreeView, ListView and ComboBox has a SelectionModel, which returns a ObjectProperty for a selection. So for registrating a selection of a view, this can be implemented like this

EmfWorkbenchContext.getInstance(ISelectionService.class).registerProvider(view.getSelectionModel().selectedItemProperty());

It can also register a custom ObjectProperty like the following example. Each time the stringpovider is changed, the ProviderView will inform the service. The interested class can get the current selection or change by the service.

class ProviderView extends EmfView{
   private ObjectProperty<String> stringProvider;
   
   public InterestedMemberView(){
       stringProvider = new SimpleObjectProperty<String>("Hello World");
       EmfWorkbenchContext.getInstance(ISelectionService.class).registerProvider(stringProvider);
   }
}
public class RunCommand extends EmfCommand {
	@Override
	public void execute() {
		ISelectionService selectionService = EmfWorkbenchContext.getInstance(ISelectionService.class);
		System.out.println("Run Java Code! Selection: "+selectionService.getSelection().get());
	}
}

Observe for changes

public class EmfExampleView extends EmfCommand implements ChangeListener{
	...

        public EmfExampleView(){
            ...
            // This step must be taken to update the view.
            EmfWorkbenchContext.getInstance(ISelectionService.class).addListener(this);
        }

        @Override
	public void changed(ObservableValue observable, Object oldValue, Object newValue) {
		if (newValue!=null) {
			System.out.println(newValue.toString());
		}
	}
}

Viewparts can be added as ChangeListener to SelectionService via `EmfWorkbenchContext.getInstance(ISelectionService.class).addListener(this);``or optional via the workbench configuration.

ChangeListener Registration

Clone this wiki locally