Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

Code Block
languagexml
<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

...

Code Block
languagejava
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;
    }
}