Piotr Sołtysiak
Piotr Sołtysiak

Reputation: 1006

GWT restore visibility restrictions from uiBinder

Let's say I have some components with visible="false" property in my ui.xml file, and in my java code I showed some of those components. Is there any simple way/method to restore visibility restrictions stored in uiBinder, so I get the basic components configuration?

Upvotes: 1

Views: 1061

Answers (2)

You have just to call setVisible(true) to yours widget you want to have :

Fichier UiBinder

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
    xmlns:g='urn:import:com.google.gwt.user.client.ui'>

  <g:HTMLPanel>
    Hello, <g:TextBox ui:field='textBox' visible='false'/>.
  </g:HTMLPanel>

</ui:UiBinder>

Fichier Java

public class HelloWidgetWorld extends Composite {

  interface MyUiBinder extends UiBinder<Widget, HelloWidgetWorld> {}
  private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

  @UiField TextBox textBox;

  public HelloWidgetWorld() {
    // sets listBox
    initWidget(uiBinder.createAndBindUi(this));
  }

  public void restoreVisible() {
     textBox.setVisible(true);
  }
}

Upvotes: 0

Igor Klimer
Igor Klimer

Reputation: 15331

No, UiBinder doesn't work that way. The ui.xml file is used at compile time to generate Java code for you. So your visible="false" will probably result in generating a call to UiObject.setVisible, etc. This code is run when you call initWidget(uiBinder.createAndBindUi(this));. If you want to "restore" your widget to this initial state set by UiBinder I'm afraid the only easy way is to instantiate the widget again - but I don't recommend this as it's usually a costly operation. It's better to just write a method that restores the fields to a predefined state - you could use this method to set the initial state too, instead of defining it (and duplicating, in a sense) in the UiBinder template. I'm assuming you have all the necessary fields accessible in your class (since you mentioned changing their visibility via Java code in your question), so you won't have to add a bunch of fields just to call setVisibile on them.

Upvotes: 2

Related Questions