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

import com.install4j.api.DirectoryValidator;
import com.install4j.api.Util;

import javax.swing.*;
import java.io.File;

// Directory validator that checks whether the selected
// installation directory contains spaces. If yes, it refuses
// to install there.
//
// A directory validator can be registered on the "Advanced tasks"
// tab of the installer section in the install4j GUI.
public class NoSpacesDirectoryValidator extends DirectoryValidator {

    // has to be implemented. We return true to indicate that we want to
    // validate the user selection. Default validation messages will be
    // switched off.
    public boolean canValidate() {
        return true;
    }

    // has to be implemented. This method is called after the user selection
    // of the installation directory when the user clicks on the "Next" button
    // in the installation wizard. For directories with spaces, we return false
    // do that the wizard will not proceed to the next step.
    public boolean validate(File selectedDirectory) {
        // check for spaces in the path
        if (selectedDirectory.getPath().indexOf(' ') > -1) {
            Util.showMessage("Please select a directory without spaces in the path name.", JOptionPane.WARNING_MESSAGE);
            // Only return false after displaying an error message, the wizard will
            // not display any further error message.
            return false;
        } else {
            // no spaces, proceed
            return true;
        }
    }

    // has to be implemented. With this method you could change the
    // installation directory after the user selection. We don't do that
    // and return the selected directory.
    public File getInstallationDirectory(File selectedDirectory, String defaultDirectoryName) {
        return selectedDirectory;
    }
}
