As I promised yesterday, today we will look at opening the inspector automatically using a selection listener, along with a different API to open the inspector.
Selection listeners
The main editor UI component class in MPS, EditorComponent
, delegates some of its responsibilities to other classes.
One such class is SelectionManager
, obtainable by calling EditorComponent#getSelectionManager()
. It manages the
current selection and a collection of listeners that receive notifications when selection changes.
We can create a listener to open the inspector when the selection changes. For example, like this:
public class OpenInspectorWhenNodeIsOpened implements SelectionListener {
private final MPSProject mpsProject;
public OpenInspectorWhenNodeIsOpened(MPSProject mpsProject) {
this.mpsProject = mpsProject;
}
@Override
public void selectionChanged(EditorComponent editorComponent,
Selection oldSelection,
Selection newSelection) {
boolean shouldOpenInspector = ...;
if (shouldOpenInspector) {
mpsProject.getComponent(InspectorTool.class).openTool(false);
}
}
}
Note the API that is used above to open the inspector. The problem with EditorContext#openInspector()
that I described
yesterday is that the inspector gets focused after opening. Instead, I use the InspectorTool#openTool(boolean activate)
method, passing false
as the parameter to avoid the focus switch.
How do we now make sure that our listener gets called? How do we add it to all editor components in MPS, including future ones? We will look at this tomorrow.