Delete multiple nodes by action map. So urgent

Hello MPS team,

My question is so simple and so urgent. How I can detect delete action of multiple selected nodes. I detected delete action of a single node by action map but when I select multiple nodes and press delete or backspace button, the delete action I defined doesnt work well. 

 

Hope a quick answer by your side

 

Thanks in advance.

8 comments

One way would be to add a Listener to your model.

SNodeChangeListener newListener = new SNodeChangeListener() {
  public void propertyChanged(@NotNull() SPropertyChangeEvent p0) {
  }
   
  public void referenceChanged(@NotNull() SReferenceChangeEvent p0) {
  }
   
  public void nodeAdded(@NotNull() SNodeAddEvent p0) {
  }
   
  public void nodeRemoved(@NotNull() SNodeRemoveEvent p0) {
     // This will be called every time a node is deleted
  }
}

node.model/.addChangeListener(newListener)  <-- register the listener on the model.

 

But listeners can be tricky. You should search this forum for the word "listener" there are several post about that.

 

------

To your action problem. I had a similar problem. As far as I understand the actions you are looking for are not bound to a cell , like the action you can implement in mps , but to a NodeRangeSelection which holds its own type of actions. The problem ist that there is no way to change or manipulate these action of the selection, than to go into the source code of mps and make changes there.

0

Thank you so much Jochen for your answer but sorry as I am newer to mps could you tell me how I can add a listener to a model step by step (From where and how)

Thanks in advance

Khalil

0

First step :

When and where should your listener track changes. If you want to start your listener when your project starts, a plugin solution is the right way.

Next step create a Project Plugin. This will be called on start and on close of your project :

 

In this ProjectPlugin you can :

... and...

Now you have a listener running every time you start your project.

BUT be carefull there is a lot more to say about  listeners. They also should be removed properly when the project is closed. Which is not done in this very basic example.

Anymore question, dont hesitate to ask.

0

Thank you so much and am sorry for being too late. 

I created plugin solution and when I try to import model as shown in the second step I dont see this model (SNodeChangeListener). I tried to import org.jetbrains.mps.openapi.model@java_stub in each editor model and in the plugin solution but for nothing. how I can do that?

Second question is how I can select the model that should listen to our action. You mention in your screenshots model listenerModel=project.... which method should I use.

 

Best regards.

khalil 

 

0

1) SNodechangeLister Problem . Go into the editor press cmd (OS X) +R for the import window. Dont forget to set the little hook in the top right corner for "include stubs and non-project models"(!) . Start typing "SnodeChangeListener ". The corrrect import should appear just like in the picture.

 

2) Finding the right model to add the listener. Here is the worst way to do it. Comparing name strings.

2.1) maybe this works better

 

0

Jochen Thank you so much for your help and sorry for distrubing you.

I do what you mentioned but unfortunately I couldnt see SNodeChangeLıstener even I make the dependencies of our plugin solution refers to org.jetbrains.mps.openapi.model & org.jetbrains.mps.openapi.module but I didnt see any thing related to SNodeChangeListener and addChangeLıstener. I think it is dependency porblem. I tried to see the GitHub code this SNodeChangeListener is under packge org.jetbrains.mps.openapi.model and I import it but unfortunately I could see and use SNodeChangeListener.

Instead of that I could see SModelListner interface  and addModelListener but these two methods dont serve us in our case. Folllowing is my code I coded but this wont go well with me. 

model listenerModel = null;
for (SModule curreModule : project.getModules()) {
for (SModel currentModel : curreModule.getModels()) {
if (currentModel.getModelName().equals("editor")) {
listenerModel = currentModel;

listenerModel/.addModelListener(new SModelListener() {
public void modelLoaded(SModel model, boolean b) {
JOptionPane.showMessageDialog(null, "Hello");
}
public void modelReplaced(SModel model) {
JOptionPane.showMessageDialog(null, "Hello");
}
public void modelUnloaded(SModel model) {
JOptionPane.showMessageDialog(null, "Hello");
}
public void modelSaved(SModel model) {
JOptionPane.showMessageDialog(null, "Hello");
}
public void conflictDetected(SModel model) {
JOptionPane.showMessageDialog(null, "Hello");
}
public void problemsDetected(SModel model, Iterable<SModel.Problem> iterable) {
JOptionPane.showMessageDialog(null, "Hello");
}
});
}
}
}

and I make the dependency of our plugin

Thank you so much in advance.

 

Best regards

0

İs it Mps version problem? 

I checked the code in Github: They mention that openapi.model@java_stub which contains SNodeChangeListener is available for jdk 8 and 3.4 & 3.5 version of Mps. I think that's the problem. unfortunatelyI couldnt upgrade my mps version as it is our company decision. please could you confirm this issue? 

Thank you so much in advane.

Best regards.

Khalil

0

I can't confirm that it is a version problem, because i have never used anything older than 3.2. But I think you are right . I think I remember having red, that MPS used to not have the capability of listening to nodes.

If you cant change versions you might try this :

http://mbeddr.com/platform.html#modellisteners

mbeddr has provided their own listeners. You could import them and use them instead of the SNodeChangeListener. Maybe that will help you. I myself never needed to use them, so I cant help you there.

0

Please sign in to leave a comment.