Reaching value of a node attribute from actionListener of JFrame

Hi,
I'm trying to change a node's attribute value from keymap editor. To do this I created a class called TestFrame and a JFrame including a TextBox needs to take the value of the TextBox when the JButton Select is clicked. However, when I click select nothing happens. Before entering the actionListener, the node's prop value is set to "test". I
wonder why I can't reach the node inside the actionPerformed method. Are they using different threads, so it can't see the node? or anything else?

Test Keymap Code
keymap test              
          
everyModel false         
          
applicable concept: TestStruct          
          
keymap items:            
          
item description : <no description>     
     keystrokes : <alt> + <VK_ENTER>    
     caret policy : ANY_POSITION        
     show in popup : false              
     menu always shown : false          
     is applicable : <always>           
     execute : (node, selectedNodes, editorContext)->void { 
 TestFrame f = new TestFrame(node); 
     };  
prettyPrint();
      
Test Frame Code
public class TestFrame extends JFrame { 
  private JTextField test; 
  private JButton selectButton; 
   
  public TestFrame(final node<TestStruct> node) { 
    super(); 
    selectButton = new JButton("Select"); 
    node.prop = "test"; //works and updates the prop value of model in the sandbox
    test = new JTextField(); 
    selectButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent p0) { 
           // if dispose() method call is made here, it works
           node.prop = test.getText(); 
           // dispose() doesn't work here
      } 
    }); 
    init(); 
  } 
   
  private void init() { 
     
    JPanel subValuePanel = new JPanel(); 
    subValuePanel.setLayout(new BoxLayout(subValuePanel, BoxLayout.PAGE_AXIS)); 
     
     
    subValuePanel.add(selectButton); 
    subValuePanel.add(test); 
    subValuePanel.setSize(400, 300); 
     
    this.add(subValuePanel); 
    this.setSize(400, 300); 
    this.setVisible(true); 
     
     
  } 
   
}
prettyPrint();

Any help on this issue is appreciated.
Thanks.
Zehra
4 comments
I'm assuming the code "node.prop = ..." throws an exception that indicates that you need to obtain a lock on the model. Is that right?
If so, you may need to properly write-lock the model - check out the "Concurrent access" section of https://confluence.jetbrains.com/display/MPSD32/Open+API+-+accessing+models+from+code
0
Hi Vaclav,
Thanks for your reply.
Actually, I don't receive any exception and I don't think that is the problem. I think ActionListener runs on a different thread and when I try to reference the node variable, since it has a copy of it and that copy is probably null, I don't receive any exception but the program does not run as expected.
So, after a couple of hours of experiments we noticed that we want to reach the actual instance of the node we have and modify it.
Is that possible?
Thanks,
Zehra.
0
Hi,

you are right that the action listener runs in a different thread. This is the reason why it needs to obtain a write lock in order to be able to modify the model. If you'd added try-catches in the handler, you should see the exception that complains about improper locking. Unless I misunderstood your problem, if you get the lock, things should start working:
public class MyFrame extends JFrame { 
   
  public MyFrame(final node<ParallelFor> node, final SRepository repository) { 
    super("Demo"); 
    Button button = new Button("Demo"); 
    node.loopVariable.name = "foo"; 
    button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent p0) { 
        try { 
          repository.getModelAccess().executeCommand(new Runnable() { 
            public void run() { 
              try { 
 node.loopVariable.name = "bar"; 
              } catch (Throwable t) { 
 error "Nested exception", t; 
              } 
              info "Done"; 
            } 
          }); 
        } catch (Exception e) { 
          error "Exception", e; 
        } 
      } 
    }); 
    this.add(button); 
    this.setVisible(true); 
    this.pack(); 
  } 
}
prettyPrint();

The Repository instance can be obtained through editorContext.getRepository().

Vaclav
0
It worked out after making the changes you offered.
Thank you so much!
0

Please sign in to leave a comment.