I have been playing around with creation of custom GUI:s with the builtin SWT library with some success, but I'm looking for a way to add a custom menu for accessing some scripts. Is there any such feature planned?
Regards,
Mattias
Continuing my thread.
I've had some more progress, been able to display buttons etc, but I'm afraid that I have hit a roadblock. To do something when a button is pressed SWT uses a concept with listeners, which uses Java method overloading, but I think that the Java adapter/Rhino is having some problems with that kind if thing.
Any advice?
Part of my code:
var testButton = new Button(shell, SWT.PUSH); testButton.setText("Test"); var listener = new JavaAdapter(SelectionAdapter, { widgetSelected: function(event) { input = text.getText(); shell.close(); } }); testButton.addSelectionListener(listener);
Hi Mattias,
Very interesting ideas, unfortunately beyond my technical JS/Rhino knowledge. Normally Jan may answer such questions, but he is currently not in the office. Therefore you might have to wait until early March, unfortunately.
Regards Nils
Mattias, I am back and will try to continue here. So you want to attach a Java Script function as the listener to the button, right?
Could you please post more of the SWT code so I can copy paste it and just play with it to find a solution?
I just need the shell initialization etc. and may all the imports/load statements
Happy Scripting
Jan
Hi Jan and Mattias,
I have stumbled upon the same idea but unfortunately I haven't been able to implement a simple interface ( button listener) interface for the button.
I have found documentation for Rhino but tried the documented ways simply did not work.
I went a bit further trying to map some internal javascript classes package and expose the internal JavaAdapter class. OI somehow managed to create an instance of the Interface with a public method however it the listener or the method was not triggered when it should be ...
I will collect the links and post them in here.
I was even considering using external java class file with the implementation of the Interface that would be eventually loaded though bind or class-loader or at least create a class that will accept a Runnable object and will implement and interface ,....
Here is a snippet of the dialog where I wanted to create User/Password dialog
//$EXPERIMENTAL$ $DEBUG$ load(".lib/ui.js");/*SWT already in ui.js org.eclipse.swt.SWT;*/var SwtGridLayout = bind("org.eclipse.swt","org.eclipse.swt.layout.GridLayout",false);var SwtGridData = bind("org.eclipse.swt","org.eclipse.swt.layout.GridData",false);var SwtDisplay = bind ("org.eclipse.swt","org.eclipse.swt.widgets.Display",false);var SwtShell = bind ("org.eclipse.swt","org.eclipse.swt.widgets.Shell",false);var SwtText = bind ("org.eclipse.swt","org.eclipse.swt.widgets.Text",false);var SwtButton = bind ("org.eclipse.swt","org.eclipse.swt.widgets.Button",false);var SwtLabel = bind ("org.eclipse.swt","org.eclipse.swt.widgets.Label",false);var SwtListener = bind ("org.eclipse.swt","org.eclipse.swt.widgets.Listener",false);/** * creates dialof with USername label + input text, Password (label+input text) + button => not able to crate an isntance of Interface Listener !!! just close the window to porceeed* @param {String} Optional initUser : String with inittalvlaue in the userInputtext field* @param {String} Optional initPass : String with inittalvlaue in the userInputtext field*/function userPasswordTextDialog(initUser,initPass) { var display = new SwtDisplay(); var shell = new SwtShell(display); var labelUser = undefined; var labelPass = undefined; var bttnOK = undefined; var textOut = undefined; var gridLayout = new SwtGridLayout(3, false); shell.setLayout(gridLayout); //first row username labelUser= new SwtLabel(shell, SWT.NULL); labelUser.setText("User: "); gridData = new SwtGridData(SwtGridData.FILL_HORIZONTAL); gridData.grabExcessHorizontalSpace = true; var textFieldUser = new SwtText(shell, SWT.SINGLE | SWT.BORDER); textFieldUser.setLayoutData(gridData); textFieldUser.setText(initUser||""); //dummy label new SwtLabel(shell,SWT.NULL); //2nd row // Create a password text field new SwtLabel(shell,SWT.NULL).setText("Pass:"); gridData = new SwtGridData(SwtGridData.FILL_HORIZONTAL); gridData.grabExcessHorizontalSpace = true; var textField = new SwtText(shell, SWT.PASSWORD | SWT.BORDER); textField.setText(initPass||""); textField.setLayoutData(gridData); bttnOK = new SwtButton(shell, SWT.PUSH).setText("DONE"); // button need a listener it is a interfacae aand need method for new SWTListener(). //TBD not usre how to create and implenetation uusing bind/JavaAdapater shell.setSize(500,200); shell.pack(); shell.open(); textField.setFocus(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { textOut = textFieldUser.getText() +":"+textField.getText(); display.sleep(); } } display.dispose(); return [textOut.split(":")[0],textOut.split(":")[1]]; }
Now When we have Jan jumping in I believe we can crack this nut
Happy Scripting,
Marian
Hi ,
forgot to reference the links I found.
so here they go
https://p-bakker.github.io/rhino/tutorials/scripting_java/
https://stackoverflow.com/questions/15491642/mozilla-rhino-1-7r4-extending-abstract-class-js
https://www.javadoc.io/doc/org.mozilla/rhino/1.7.10/org/mozilla/javascript/InterfaceAdapter.html
https://javadoc.io/doc/org.mozilla/rhino/latest/org/mozilla/javascript/JavaAdapter.html#createAdapterCode-org.mozilla.javascript.ObjToIntMap-java.lang.String-java.lang.Class-java.lang.Class:A-java.lang.String-
Thanks for the flowers Marian, I may have a look.
I deep in my head have a memeory that I once solved that riddle, but when and how?
Lets see...
Well, I tried it quickly and succeeded, thanks for the example code Marian, that helped a lot to quickly get started.
First of all: this kind of code is dangerous, Marian:
bttnOK = new SwtButton(shell, SWT.PUSH).setText("DONE");
It stores the result of the last operation (here setText) in the variable and since this is a void function, btnnOK is undefined afterwards.
Make sure you do:
bttnOK = new SwtButton(shell, SWT.PUSH); bttnOK.setText("DONE");
And then, there is one rule: if the Java interface has just one method, you may do
bttnOK.addXXX(function foo() { ...
foo mathcing the function in the interface. Unfortunately, SWT wants a SelectionListener which has multiple methods. But no problem, just do it like this and Rhino is able to map a JS object and multiple of its methods to a Java interface:
bttnOK = new SwtButton(shell, SWT.PUSH); bttnOK.setText("DONE"); var listener = { widgetSelected : function(selectionEvent) { alert("Clicked"); }, widgetDefaultSelected : function(selectionEvent) { alert("Clicked"); } }; bttnOK.addSelectionListener(listener);
Voila:
Happy UI Scripting