What is the best way to run a script everytime a root node is opened for editing in my standalone IDE

I want to check the credentials of my domain expert before everytime she starts to code in my DSL. 

The first idea I had it was putting the code to check the credentials and saving the token in the constructor behavior of my root node. The problem is that the behavior only runs when I create the node.

I have asked at Slack, and Mr. @hnehls suggested me to use a FileEditorManagerListener and its selectionChanged() method. He wrote this.

>You may have a look at the `FileEditorManagerListener` and its `selectionChanged()` event method.
>The `FileEditorManagerListener` must be attached to the `MessageBusConnection`, using its `subscribe()` method.

>e.g.

>`FileEditorManagerListener listener = new FileEditorManagerListener() {`
>`public void fileOpened(@NotNull() FileEditorManager source, @NotNull() VirtualFile file) {`
>`// handled by selectionChanged`
>`}`
>`public void fileClosed(@NotNull() FileEditorManager source, @NotNull() VirtualFile file) {`
>`// handled by selectionChanged`
>`}`
>`public void selectionChanged(@NotNull() FileEditorManagerEvent event) {`
>`FileEditor newEditor = event.getNewEditor();`
>`MPSFileNodeEditor mpsFileNodeEditor = (MPSFileNodeEditor) newEditor;`
>`Editor nodeEditor = mpsFileNodeEditor.getNodeEditor();`
>`read action with nodeEditor.getEditorContext().getRepository() {`
>`SNode editedNode = nodeEditor.getCurrentlyEditedNode().resolve(nodeEditor.getEditorContext().getRepository());`
>`node<> root = editedNode;`
>`}`
`}`

`MessageBusConnection connection = project.getMessageBus().connect(project);`
`connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, listener);`

Then I asked where I am supposed to write this in my Language and Mr. hnehls kindly replied me:

>To achieve that the listener is subscribed exactly once and at the right time, you can do this e.g. within a project plugin.
>See jetbrains.mps.lang.plugin.standalone.ProjectPluginDeclaration (https://confluence.jetbrains.com/display/MPSD20183/Plugin#Plugin->Custompluginparts(ProjectPlugin,ApplicationPlugin))

I had tried to create a plugin solution in my project and a project plugin. But I don't know how to attach it to the correspondent node. It lacks of content on the internet.

Can anyone help me adding this java script into my editor every time it opens?

1 comment
Comment actions Permalink

Hi,

have you tried to look at other projectplugins as a blueprint?

In MPS, the best documentation you often get is by looking for other instances in the MPS and/or mbeddr code base.

To do so, you must know what you are looking for. In your case, a project plugin. So you go to the declaration (ProjectPluginDeclaration), select the name and press Ctrl+Alt+Shift+F7 (on WIndows) or go to the context menu "Find Usages Settings ..." (same thing) to open this dialog:

Search for instances in you global scope. This will allow you to browse other instances of what you are trying to use.

If you want to see how the MessageBusConnection is used, go to its declaration and search the same way as shown above, only instead of "Concept instances" search for "Node usages" (last checkbox). Same is true for "FileEditorManagerListener". Search for its usage and you might find your solution.

I'm sorry to not give you a more specific answer, but I don't particularly know what you mean by "attach it to the corresponding node". A project plugin is a plugin that is started when your project is being loaded (my understanding). During its initialized, you can, for example, add listeners to certain events. These listeners are executed when the event is fired.

0

Please sign in to leave a comment.