Question

This may sound very weird, but let's start with an example:

<my:MagicWidget ui:field="someFieldName" fieldName="someFieldName"/>

It's pretty much asured that we'll always want to have the same value in ui:field and in fieldName. Clearly there is some duplucation in this code, I'd like to avoid it and make the fieldName optional.

So, this is what I have in the widget's code:

@UiConstructor
public MagicWidget(String fieldName) {
    this.fieldName = fieldName;
}

But I'd like, if possible to allow this constructor to be optional, and provide an default constructor that would "by magic" find out it's ui:field value:

@UiConstructor
public MagicWidget() {
    this.fieldName = /*some magic to get ui:field's value*/;
}

I was wondering if there is a way to get the value of "ui:field" inside my MagickWidget? (The widget extends Composite). I fear this might not be possible, because most of the time it's not so useful, but if anyone has an idea - feel free to share!

PS: I'm using GWT 2.1.0.RC1.

Was it helpful?

Solution

As you may know, the ui:field is there so you can interact with a UI Object in Java code after you've declared it with UiBinder. So, for example, if you add a MagicWidget in a UiBinder template, you can write

@UiField MagicWidget someWidget

in order to be able to interact with it programatically. Having your magic widget aware of the name of the reference that is pointing to it might not be all that helpful (or possible), as you can pass the reference to that specific MagicWidget back and forth between different parts of your application. A single MagicWidget could easily have several references with different names pointing at is simultaneously. That's why it's difficult to pick it out "by magic" at runtime. I realize this isn't much of an issue if you only want this value when the object is constructed, but keep in mind that you're not required to include a ui:field when you add a widget using UiBinder.

Why is it important that the Widget know its field name? Knowing that might make it easier to provide suggestions about other ways to accomplish what you are looking to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top