Extending a UI component and passing data to connector

I want to extend a TextField using AbstractExtension and pass a type value to the AbstractExtensionConnector or retrieve it from the AbstarctExtension so I can handle the input differently based on the type.

I have

public class MyField extends AbstarctExtension {
private String type;

public MyField(String type) {
super();
}

public static void extend(TextField field, String type) {
new MyField(type).extend((AbstractClientConnector(field);
}
}

@Connect(MyField.class)
public class MyFieldConnector extends AbstractExtensionConnector implements KeyPressHandler {

public void onKeyPress(KeyPressEvent event) {
// how do I look at type in MyField?
}
}

Hi,

You’ll want to use the shared state mechanism to pass the data to the client. It’s
documented in the book
for components; in the case of an extension you’ll simply need to extend the root SharedState class instead of ComponentState (there’s no separate ExtensionState class.) Something like this:


// This should be somewhere visible to both MyField and MyFieldConnector
public class MyFieldState extends SharedState {
  public String type;
}

public class MyField extends AbstractExtension {

  public MyField(String type) {
   getState().type = type;
  }

  @Override
  public MyComponentState getState() {
    // Vaadin uses a bit of magic to automatically create
    // an instance of the correct type for you
    return (MyComponentState) super.getState();
  }

 public static void extend(TextField field, String type) {
   new MyField(type).extend(field);
 }
}

@Connect(MyField.class)
public class MyFieldConnector extends AbstractExtensionConnector implements KeyPressHandler {

  public void onKeyPress(KeyPressEvent event) {
      doSomethingWith(getState().type);
  }

  @Override
  public MyComponentState getState() {
    return (MyComponentState) super.getState();
  }
}

Just what I was looking for. Thanks for the example!