// This is example code for the install4j API
// Please feel free to adapt it for your own purposes.

import com.install4j.api.CustomScreen;
import com.install4j.api.Util;

import javax.swing.*;
import java.awt.*;

// This custom screen allows the user to enter license information.
//
// For example, this license information can then be used by a
// custom action that writes it to a file.
public class LicenseKeyScreen extends CustomScreen {

    // the text fields into which the license information is entered
    private JTextField nameField;
    private JTextField companyField;
    private JTextField licenseKeyField;

    public LicenseKeyScreen() {

        // in the following code, the custom screen is populated
        // with controls

        nameField = new JTextField();
        companyField = new JTextField();
        licenseKeyField = new JTextField(20);

        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.anchor = GridBagConstraints.WEST;
        gc.insets = new Insets(3, 0, 0, 3);
        gc.gridx = 0;
        gc.gridy = 0;

        add(new JLabel("Name:"), gc);
        gc.gridy++;
        add(new JLabel("Company:"), gc);
        gc.gridy++;
        add(new JLabel(" "), gc);
        gc.gridy++;
        add(new JLabel("License key:"), gc);

        gc.gridx = 1;
        gc.gridy = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1;
        add(nameField, gc);
        gc.gridy++;
        add(companyField, gc);
        gc.gridy +=2;
        gc.fill = GridBagConstraints.NONE;
        add(licenseKeyField, gc);
    }

    // Getter for the entered name. To be used by a custom action.
    public String getName() {
        return nameField.getText().trim();
    }

    // Getter for the entered company. To be used by a custom action.
    public String getCompany() {
        return companyField.getText().trim();
    }

    // Getter for the entered license key. To be used by a custom action.
    public String getLicenseKey() {
        return licenseKeyField.getText().trim();
    }

    // has to be implemented.
    public String getTitle() {
        return "License information";
    }

    // has to be implemented.
    public String getSubTitle() {
        return "Please enter your license information below.";
    }

    // has to be implemented. The text fields fill all available
    // horizontal space, so we return true.
    public boolean isFillHorizontal() {
        return true;
    }

    // has to be implemented. We do not fill all vertical space,
    // so we return false.
    public boolean isFillVertical() {
        return false;
    }

    // invoked by the framework when the next button is clicked
    public boolean next() {
        // check if all text fields have been filled in
        // and whether the license key is correct
        return checkTextField(nameField, "name") &&
               checkTextField(companyField, "company") &&
               checkTextField(licenseKeyField, "license key") &&
               checkLicenseKey();
    }

    // helper method to check if a text fields have been filled in
    private boolean checkTextField(JTextField field, String text) {
        if (field.getText().trim().length() == 0) {
            Util.showMessage("Please enter the " + text + ".", JOptionPane.WARNING_MESSAGE);
            return false;
        } else {
            return true;
        }
    }

    // helper method to check whether the license key is correct
    private boolean checkLicenseKey() {
        // check if the license key is valid
        // add your own implementation
        return true;
    }

}
