Write access for node from JComponent

  • 3
  • 17

Hello, I created a custom component with swing but I can't change properties of the corresponding concept (properties: isFinished : boolean)

component provider: (node, editorContext)->JComponent { 
  final JCheckBox check = new JCheckBox(); 
  check.addItemListener(new ItemListener() { 
    public void itemStateChanged(ItemEvent e) { 
      node.model/.getRepository().getModelAccess().runWriteAction({ => node.isFinished = check.isSelected(); }); 
    } 
  }); 
  check; 
}

The error message I get when clicking the checkbox is

IllegalModelChangeError: registered node can only be modified inside undoable command
or in 'loading' model [items]

How can I accomplish this? Thanks in advance.
Take a look at the UI language in the NYoSh code base (org.campagnelab.UI
and org.campagnelab.ui.code).

http://campagnelab.org/extending-mps-editors-with-buttons-just-got-easier/

The org.campagnelab.ui.code.Swing.Button class uses the
runWriteActionInCommand:

public void actionPerformed(ActionEvent event) {

ModelAccess.instance().runWriteActionInCommand(callback);
}

runWriteActionInCommand is marked deprecated in the MPS platform, but there
is no alternative suggested in the deprecated annotation, and it seems to
be the only one of these methods that works in this case.

You can get the UI code in the NYoSh repo at
https://github.com/CampagneLaboratory/NYoSh

Fabien




On Sat, Aug 23, 2014 at 8:01 AM, fxlex - Meta Programming System <
jetforum@jetbrains.com> wrote:

* Meta Programming System
<http://forum.jetbrains.com/forum/Meta-Programming-System> * > * Write
access for node from JComponent
<http://forum.jetbrains.com/thread/Meta-Programming-System-1015> * 8:01 am
fxlex <http://forum.jetbrains.com/user/fxlex>

Hello, I created a custom component with swing but I can't change
properties of the corresponding concept (properties: isFinished : boolean)

component provider: (node, editorContext)->JComponent {
final JCheckBox check = new JCheckBox();
check.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
node.model/.getRepository().getModelAccess().runWriteAction({ => node.isFinished = check.isSelected(); });
}
});
check;
}


The error message I get when clicking the checkbox is

IllegalModelChangeError: registered node can only be modified inside undoable command
or in 'loading' model [items]


How can I accomplish this? Thanks in advance.

JetBrains Forum | Build #176 (May/07/2014 4:41PM) | Feedback
<http://confluence.jetbrains.net/display/JETF/Feedback>




--
Fabien Campagne, PhD – http://campagnelab.org

Assistant Professor, Dept. of Physiology and Biophysics
Institute for Computational Biomedicine
Associate Director, Biomedical Informatics Core,
Clinical Translational Science Center

Weill Medical College of Cornell University
phone: (646)-962-5613 1305 York Avenue
fax: (646)-962-0383 Box 140
New York, NY 10021

Do you speak next-gen?

See how GobyWeb can help simplify your NGS projects at
http://gobyweb.campagnelab.org

Your solution nearly worked for me. I imported jetbrains.mps.smodel@java_stub in editor and changed the code to

component provider: (node, editorContext)->JComponent { 
  final JCheckBox check = new JCheckBox(); 
  check.addItemListener(new ItemListener() { 
    public void itemStateChanged(ItemEvent e) { 
      if (e.getStateChange() == ItemEvent.SELECTED) { 
        ModelAccess.instance().runWriteActionInCommand(new Runnable() { 
          public void run() { 
            node.isFinished = true; 
          } 
        }); 
      } else { 
        ModelAccess.instance().runWriteActionInCommand(new Runnable() { 
          public void run() { 
            node.isFinished = false; 
          } 
        }); 
      } 
    } 
  }); 
  check; 
}

The checkbox can be unchecked but checking takes 2 clicks. check.isSelected() is true after the first click but ticked only after 2 clicks which is a bit annoying. Any ideas?

There'a an access language for this.
Look at "read action"/"write action"/"command". The last seems to be what you need