Button Input Field
Description
The button input field gives the user the option to run a custom button action, which can act as a optional action or validator.
Validating a connection to a LDAP server or a web server are common use cases.
The (Custom Actions) page may be helpful to get started on designing your custom action or validator.
We will provide an example of a custom button action here, it can be found at the end of this document.
Currently there are no default button actions available for use.
Currently all strings should be defined in the appropriate CustomLangPack file(s), a 'text' attribute may be added in the future.
Field Attribute
Attribute | Usage | Required | Default Value |
---|---|---|---|
id | Set the text you want to appear on the left column | No | "" |
Spec Attributes
Attribute | Usage | Required | Default Value |
---|---|---|---|
id | Set the text you want to appear on the button. | No | "" |
successMsg | Display the success message if the action or validator executed without issue. | No | "" |
Run Attributes
Attribute | Usage | Required |
---|---|---|
class | Path to the class to be run when the button is clicked | Yes |
Msg Attributes
Attribute | Usage | Required |
---|---|---|
name | Indicate the key to be used to obtain the given text from the your cusom action or validator. | Yes |
id | The text you want to pass to your custom button action class. | Yes |
Example definition of button field in the userInputSpec
In this example we create a button with a label 'pokedex query'.
The button will contain the text 'pokedex.button', and when the PokemonConnectionTest completes successfully, the 'pokedex.working' message will be displayed.
We have passed in the 'pokedex.error' string to our PokemonConnectionTest, so that it can refer to this string through the key 'error'.
Note that all strings attempt to be resolved from the CustomLangPack file. If the string is not present in the CustomLangPack file, and empty string will be available.
<field type="button" id="pokedex.query">
<spec id="pokedex.button" successMsg="pokedex.working">
<run class="com.mtjandra.button.PokemonConnectionTest" >
<msg id="pokedex.error" name="error"/>
</run>
</spec>
</field>
Example implementation of a custom button action
Below we see an implementation of a custom ButtonAction, it attempts to verify the connection to the web server hosted at http://www.pokeapi.co/.
It is required that all button actions extend the ButtonAction class, and that the execute methods for GUI (Prompt) and Console (Console) installation are implemented.
Notice that messages are retrieved by the 'messages' HashMap.
In the implementation below if the execution methods return true then the SuccessMsg will be displayed, otherwise the ERROR message will be shown.
package com.mtjandra.button;
import com.izforge.izpack.api.handler.Prompt;
import com.izforge.izpack.api.data.InstallData;
import com.izforge.izpack.panels.userinput.action.ButtonAction;
import com.izforge.izpack.util.Console;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class PokemonConnectionTest extends ButtonAction
{
private static final String ERROR = "error";
private static final String DATABASE = "http://www.pokeapi.co/";
public PokemonConnectionTest(InstallData installData)
{
super(installData);
}
@Override
public boolean execute()
{
boolean reachable = false;
try
{
URL url = new URL(DATABASE);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode >= 200 && responseCode <300)
{
reachable = true;
}
}
catch (MalformedURLException e)
{
// Could not generate an URL
}
catch (IOException e)
{
//Could not ping address
}
return reachable;
}
@Override
public boolean execute(Console console)
{
if(!execute())
{
console.println(messages.get(ERROR));
return false;
}
return true;
}
@Override
public boolean execute(Prompt prompt)
{
if(!execute())
{
prompt.warn(messages.get(ERROR));
return false;
}
return true;
}
}