Not able to creatButton with EASE

Hi all,

I was on my way to micmic Address Input.js (Implemented in Java Script) in Python.

Here is my code so far:

import json

def createView(param0):
pass
def createLabel(param0, param1):
pass
def createText(param0):
pass
def createComboViewer(param0, null, param2):
pass
def createSeparator(true, param1):
pass
def createButton(param0, saveAddress, param2):
pass

loadModule(“/System/UI Builder”);
loadModule(“/System/UI”);
loadModule(‘/System/Resources’);

createView(“Create Contact”);

createLabel(“First Name:”, “1/1 >x”);

txtFirstName = createText(“2-4/1 o!”);
createLabel(“Last Name:”, “1/2 >x”);
txtLastName = createText(“2-4/2 o!”);

createLabel(“Phone:”, “1/3 >x”);
txtPhone = createText(“2-4/3 o!”);

createLabel(“ZIP Code:”, “1/4 >x”);
txtZipCode = createText(“2/4 o”);

createLabel(“Country:”, “3/4 >x”);

cmbCountry = createComboViewer([ “Austria”, “Germany”, “India”, “USA” ])

createSeparator(True, “1-4/5 o”)

def save_address():
address = {
“lastName”: txtLastName.get_text(),
“firstName”: txtFirstName.get_text(),
“phone”: txtPhone.get_text(),
“zipCode”: txtZipCode.get_text(),
“country”: cmbCountry.get_selection().get_first_element()
}
data = json.dumps(address)

with open(f"workspace://UI Builder/AddressBook/data/{address['lastName']} {address['firstName']}.address", "w") as file:
    file.write(data)

createButton(“Save”, save_address, “4/6 >”)

I am able to see all the things in the view “Create Contact” except Save :
image

Please guide me…

PS: please provide indentations properly while trying to copy my code.

You were almost there. You need to write the code to call in a String to be interpreted by EASE on the button pressed event. Also txt* are Java Object references so you need to call the methods in camel case:

import json

def createView(param0):
    pass
def createLabel(param0, param1):
    pass
def createText(param0):
    pass
def createComboViewer(param0, null, param2):
    pass
def createSeparator(true, param1):
    pass
def createButton(param0, saveAddress, param2):
    pass

loadModule("/System/UI Builder");
loadModule("/System/UI");
loadModule('/System/Resources');

createView("Create Contact");

createLabel("First Name:", "1/1 >x");

txtFirstName = createText("2-4/1 o!");
createLabel("Last Name:", "1/2 >x");
txtLastName = createText("2-4/2 o!");

createLabel("Phone:", "1/3 >x");
txtPhone = createText("2-4/3 o!");

createLabel("ZIP Code:", "1/4 >x");
txtZipCode = createText("2/4 o");

createLabel("Country:", "3/4 >x");

cmbCountry = createComboViewer([ "Austria", "Germany", "India", "USA" ])

createSeparator(True, "1-4/5 o")

def save_address():
    address = {
        "lastName": txtLastName.getText(),
        "firstName": txtFirstName.getText(),
        "phone": txtPhone.getText(),
        "zipCode": txtZipCode.getText(),
        "country": cmbCountry.getSelection().getFirstElement()
    }
    data = json.dumps(address)
    print(data)

createButton("Save", "save_address()", "4/6 >")

And that’s a nice example. I’ll add it to the documentation. Thank you.

Note you can use three back quotes (`) before and after a block of code to keep it has is on this forum.

1 Like

Thank you @YvanLussaud, really appreciate your response, and I will sure keep you point in mind :stuck_out_tongue:

print("Hello World:P")
1 Like

Good Morning @YvanLussaud, when I observed closely I found that my program continuously run, I assume that button must be waiting for the input and hence continuously waiting for the press button event, but what if I don’t want to keep it waiting and if I press the Save button once the program should execute and free the engine/interpreter.

image

The script runs while the View is opened to react to the UI changes. So the idea is to close the view.
To do this we need two things a reference to the view:

part = createView("Create Contact");

This give us a MPart from the e4 API, to close it we need to call EPartService.hidePart(part, True)

    bundle = org.eclipse.core.runtime.Platform.getBundle("org.eclipse.e4.ui.workbench")
    bundleContext = bundle.getBundleContext()
    eclipseCtx = org.eclipse.e4.core.contexts.EclipseContextFactory.getServiceContext(bundleContext)
    partService = eclipseCtx.get("org.eclipse.e4.ui.workbench.modeling.EPartService")
    partService.hidePart(part, True)

That will close the View and stop the script. You can add this code at the end of the save_address() method.

1 Like

Interesting, I didn’t looked at it as I was not concerned with that at that particular moment but glad to see it works, could you also guide me to more information about this, like how do I get to learn about existence of such APIs…

Thank You in advance @YvanLussaud :slight_smile:

In this particular case I started with the createView() API which is exposed in the following EASE Module (see the EASE module explorer view):


Here we can see that the returned object if a MView. That’s a Java object but to get more information about its class you can run:

print(view.getClass().getCanonicalName())

And now we know it’s part of the E4 GUI. From here I searched on Eclipse forums to find how to close the MPart and we need to have access to an instance of EPartService. But in most examples it was injected using @Inject annotation. And that’s not possible from a Python script so I looked how the injection worked to find the Java code that does the same thing. Then I converted it into Python and calling Java APIs via EASE…

1 Like

Wow! great…

One more thing to be brought to your attention:

import json

def createView(param0):
    pass
def createLabel(param0, param1):
    pass
def createText(param0):
    pass
def createComboViewer(param0, null, param2):
    pass
def createSeparator(true, param1):
    pass
def createButton(param0, saveAddress, param2):
    pass
def showGrid():
    pass
def closeView(param0):
    pass

loadModule("/System/UI Builder");
loadModule("/System/UI");
loadModule('/System/Resources');

part = createView("Create Contact");
showGrid()
createLabel("First Name:", "1/1 >x");

txtFirstName = createText("2-4/1 o!");
createLabel("Last Name:", "1/2 >x");
txtLastName = createText("2-4/2 o!");

createLabel("Phone:", "1/3 >x");
txtPhone = createText("2-4/3 o!");

createLabel("ZIP Code:", "1/4 >x");
txtZipCode = createText("2/4 o");

createLabel("Country:", "3/4 >x");

cmbCountry = createComboViewer([ "Austria", "Germany", "India", "USA" ])

createSeparator(True, "1-4/5 o")

def save_address():
    address = {
        "lastName": txtLastName.getText(),
        "firstName": txtFirstName.getText(),
        "phone": txtPhone.getText(),
        "zipCode": txtZipCode.getText(),
        "country": cmbCountry.getSelection().getFirstElement()
    }
    data = json.dumps(address)
    
    print(data)
    # bundle = org.eclipse.core.runtime.Platform.getBundle("org.eclipse.e4.ui.workbench")
    # bundleContext = bundle.getBundleContext()
    # eclipseCtx = org.eclipse.e4.core.contexts.EclipseContextFactory.getServiceContext(bundleContext)
    # partService = eclipseCtx.get("org.eclipse.e4.ui.workbench.modeling.EPartService")
    # partService.hidePart(part, True)
    closeView("Create Contact")
    
# def close():
#     bundle = org.eclipse.core.runtime.Platform.getBundle("org.eclipse.e4.ui.workbench")
#     bundleContext = bundle.getBundleContext()
#     eclipseCtx = org.eclipse.e4.core.contexts.EclipseContextFactory.getServiceContext(bundleContext)
#     partService = eclipseCtx.get("org.eclipse.e4.ui.workbench.modeling.EPartService")
#     partService.hidePart(part, True)
#     # closeView("Create Contact")

createButton("Save", "save_address()", "4/6 >")
# createButton("Close", "close()", "5/6 >")

This is the bit extend version of the code where I was trying out closeView() of the UI module of EASE, turns out it works in the same way (turns off the progress bar thing on the buttom right ) as it closes the view that was created, also you may see some commented code in the block.

I create a dedicated button to close the view but uncommenting it and have both the button on and pressing one of them to close the view but don’t get rid of the progress bar…

Any thoughts about this behaviour?