EventFunction for multiple InputFields

Hello everyone,

I’m relatively new to the URCaps universe and currently writing on a URCap for my first project.
Now the problem is that i need an Installation node with multiple InputFields and all of them should be stored in the DataModel.
But how?
Usign the code from the sample Programs brings me to the point where i got multiple declarations for the same functions with a scheme like that:

@Input(id = <INPUT_NAME>)
public void onMessageChange(InputEvent event) {
if ( event.getEventType() == InputEvent.EventType.ON_CHANGE) {
saveInput(<INPUT_SAVE_NAME>, <INPUT_NAME>.getText());
}
}

And right that brings me to the point where Eclipse as well as Maven tell me some sweet errors.
Maybe one of you could give an example how to do this right?

best regards Mathias

Say you have three inputs; 2 text boxes and 1 button.
In HTML, you would have something like:

<p> Tell me IP address: </p> <input id="myIpInput" type="text">
<p> Tell me IP port: </p> <input id="myPortInput" type="number">
<p> Click to send test connection: </p> <input id="myTestConnectionButton" type="button">

Now these three inputs are referenced in Java by their “id” attribute.
So in Java we can use them as:

// Register the IP address input
@Input(id="myIpInput")
InputTextField MyIPaddress;

// Register the port input
@Input(id="myPortInput")
InputTextField MyPort;

// Register the button to test connection input
@Input(id="myTestConnectionButton")
InputButton TestConnectionButton;

Now, we have registered objects for refrencing the inputs.
Hence, we can now interact in terms of getting and setting values.
E.g. String thisIP = MyIPaddress.getText(); or for setting the button text TestConnectionButton.setText("Click Me");

In order to register a callback function that executes, when the user interacts with out inputs, we need to provide a public method, that is annotated with the correct “Input” attribute and accepts the correct “event” as an argument.

// Callback when user enters IP address
@Input(id="myIpInput")
public void someNameThatSuggestsIpAddressChange(InputEvent event){
   // Here, we check that the event was actually a change, otherwise do nothing
   if(event.getEventType() == InputEvent.EventType.ON_CHANGE){
      String thisIP = MyIPaddress.getText();
      // Do something with the newly read input value, e.g. storing to DataModel
   }
}

Hence, the linking of the @Input-annotation, the correct id for this annotation, a public method and that it accepts the event type that occured (e.g. InputEvent, SelectEvent or TouchEvent) as argument will - combined - result in your callback being called correctly. The actual name of your function, i.e. “onMessageChange” or “someNameThatSuggestsIpAddressChange” have no impact.

So for the remaining two inputs, we could do:

// Callback when user enters IP address
@Input(id="myPortInput")
public void someChangedInPortSetting(InputEvent event){
   // Here, we check that the event was actually a change, otherwise do nothing
   if(event.getEventType() == InputEvent.EventType.ON_CHANGE){
      String port = MyPort.getText();
      // Do something with the newly read input value, e.g. storing to DataModel
   }
}

// Callback when user clicks the button
@Input(id="myTestConnectionButton")
public void executeConnectionTestAction(InputEvent event){
   // Here, we decided that it should happen when the button is PRESSED, hence ON_PRESSED
   // We could also select to fire the event, when the user released the button - ON_RELEASED
   if(event.getEventType() == InputEvent.EventType.ON_PRESSED){
      // Call the functional code for that connection test here... 
      foo(bar);
   }
}
2 Likes

Thanks for this prompt answer.
Didn’t realized that the event functions name is unimportent to the program (thought it had to be called onMessageChange())…
that was the point, thanks for youre help!

:slight_smile:

1 Like