Need help on jArchi script to handle event listeners using SWT

Started by Manj75, August 18, 2021, 15:50:13 PM

Previous topic - Next topic

Manj75

Can I please get some help with a jArchi script I'm trying to write using SWT widgets which I plan to use to move away from my existing script that uses Swing.

Basically, I'm trying to get event listeners to work and not having any luck.  In my Swing script I am using the CaretListener which is not directly available in SWT and having to use ModifyListener.  I've put together a simple script attached and using a Text field widget so that when it is modified the listener is called and the widget is changed.

I am able to addModifyListener and can see that the console.log is being output for every key press in the Text widget, however the objective is to change the text or color of the Text widget and this does not work at all.

snippet of the listener object method:

textFieldListener: function ()
{
console.log("This is a test");

// With the next line I don't see any change to the textfield and only see the above console.log.
// If below line is commented then the 2nd console.log appears.
// I don't understand why as I want to be able to change the widgets based on events in this case key presses in the textfield.
modelTextField.setText("This is new text");

console.log("This is a test2");
},


Output I get for many key presses in the Text field:

This is a test
This is a test
This is a test


I never get to see the second console.log output, which implies it is lost in the setText() call.  There is no exception thrown and the dialog continues to run okay.

I know that the widget fields are defined within createDialogArea() method and likely to have function scope, but I have tried to declare globally and still the same result.

I'd be grateful for some help on where I am going wrong - Thanks

Phil Beauvoir

August 19, 2021, 06:52:43 AM #1 Last Edit: August 19, 2021, 07:39:01 AM by Phil Beauvoir
1. modelTextField is not in scope, it needs to be declared outside of createDialogArea function
2. Setting the text on the text field inside of the text field's modify listener will result in an infinite loop.
3. Using SWT/Java components under jArchi is a side-effect of the Java->JS bridge (GraalVM). There are no guarantees that all of this stuff will work when running inside of Archi.
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Manj75

Thanks Phil - I've sorted it now by refactoring my script it was definitely down to scope - the learning continues  :)