How is the way to extends Components like Button

Hi,

i am looking for a way to add some attributes to components (in my specific case it is a long field to a button component).

I need to build a list of buttons in program-code:

for (Integer i: intvalues) {
Button button = uiComponents.create(Button.NAME);
}

Now I need a way to do something like:
button.setMyNumber(i)

So: Is there a a way to create something like:

private class MyButton extends Button {
private Long number;
public void setNumber(Long p_number) {
number = p_number;
}
public Long getNumber() {
return number;
}
}

This would result in:
for (Integer i: intvalues) {
MyButton button = ???
button.setNumber(i);
}

Is there way to do something like this ?

Thanks,

Martin

Hello Martin!

Yes, you can extend the Button component. If you are not going to use this button in the descriptor you can do the following:

  1. Extend Button interface and WebButton class:
Code snippet
public interface ExtButton extends Button {

    String NAME = "extButton";

    void setLongValue(Long value);

    Long getLongValue();
}

public class WebExtButton extends WebButton implements ExtButton {

    protected Long longValue;

    @Override
    public void setLongValue(Long value) {
        this.longValue = value;
    }

    @Override
    public Long getLongValue() {
        return longValue;
    }
}
  1. In order to use your extended component you need to create ui-component.xml in the root package in web module:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<components xmlns="http://schemas.haulmont.com/cuba/components.xsd">
    <component>
        <name>extButton</name>
        <class>com.company.extbutton.web.button.WebExtButton</class>
    </component>
</components>
  1. In the web-app.properties file define the following property:
cuba.web.componentsConfig=+com/company/extbutton/ui-component.xml

Then use ExtButton like this:

@Inject
private UiComponents uiComponents;

@Subscribe
public void onInit(InitEvent event) {
    ExtButton extButton = uiComponents.create(ExtButton.NAME);
    extButton.setLongValue(456l);
}

See full example here: extbutton.zip (80.3 KB)

P.S. For code formatting please use ```

1 Like

Hi Roman,

thanks very much for your reply. I think, this is the way for building
an own component library. But I just need a way, to put some kinds
of addon-information (things that make work easier on just a local state)
to a component.

A thing, that would help, would be an object-store like:

component.setAddonInfo(Object addoninfo);

and

component.getAddinInfo();

With a attribute like this, some things would be easier to handle.

Regards,

Martin