1

I want to create a custom widget that allows to specify its nested sub-elements in UiBinder. Just like DockPanel can have south, north, etc. I looked at the source code of DockPanel but haven't found a solution.

<my:LeftRightToolbar>
    <left>
        <g:Button/>
    </left>

    <right>
        <g:Button/>
    </right>        
</my:LeftRightToolbar>

UPDATE: Can I have multiple sub nodes under my custom <left> and <right>? The code doesn't compile if I add more than one widget.

Konstantin Milyutin
  • 11,946
  • 11
  • 59
  • 85

1 Answers1

2

First thing - <left> and <right> have to be in the same namespace as LeftRightToolbar, so it should be <my:left> and <my:right>.

Secondly, you need to annotate two methods in LeftRightToolbar with the @UiChild annotation:

@UiChild(tagname = "left")
void addToLeft(Widget widget) {
    left.add(widget);
}

@UiChild(tagname = "right")
void addToRight(Widget widget) {
    right.add(widget);
}

The method addToLeft will be called to add the widget specified in <my:left> tags. The <my:right> tag is handled by addToRight.

If you need to add multiple widgets to your custom tags, you should put a container in them, like FlowPanel.

Igor Klimer
  • 15,321
  • 3
  • 47
  • 57
  • Thanks! And if I don't want to have `my:left` and `my:right` childs and add widgets directly under ``. Is it possible? – Konstantin Milyutin Oct 17 '14 at 13:29
  • And the second question: if I have a method `@UiChild(tagname="data") public void addData(String key, String value) {`. Can I pass key and value from UiBinder somehow? – Konstantin Milyutin Oct 17 '14 at 13:43
  • You can't add keys and attributes to your custom tags. But you could add the needed tags on the `LeftRightToolbar` element though. You can pass arguments to your widget's constructors (using @UiConstructor or [other methods](http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html#Using_a_widget)). – Igor Klimer Oct 17 '14 at 14:17
  • Thanks! And what about my first question? – Konstantin Milyutin Oct 17 '14 at 14:19
  • In my case it looks ugly: ` ` – Konstantin Milyutin Oct 17 '14 at 14:20
  • 1
    You can put widgets directly in your `LeftRightToolbar` - it has to implement the `HasWidgets` interface. Please see http://stackoverflow.com/q/4175216/181497. – Igor Klimer Oct 17 '14 at 14:21
  • It's generally discouraged to extend your original question with further questions - in the future you should just create a new question. Answer updated. – Igor Klimer Oct 17 '14 at 14:57