<commit_before>package rzehan.gui.sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import rzehan.shared.Platform;
import rzehan.shared.imageUtils.CliCommand;
import rzehan.shared.imageUtils.ImageUtil;
import rzehan.shared.imageUtils.ImageUtilRegistry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

public class Controller {

    private static final Logger LOGGER = Logger.getLogger(Controller.class.getSimpleName());

    private static final int MAX_OUTPUT_LENGTH = 100;
    private static final String MC_FILE_LINUX = "/home/martin/zakazky/NKP-validator/data/monografie_1.2/b50eb6b0-f0a4-11e3-b72e-005056827e52/mastercopy/mc_b50eb6b0-f0a4-11e3-b72e-005056827e52_0001.jp2";
    private static final String MC_FILE_WINDOWS = "C:\\Users\\Martin\\Documents\\PspValidator\\mc_b50eb6b0-f0a4-11e3-b72e-005056827e52_0001.jp2";


    @FXML
    Label osLabel;
    @FXML
    Label logLabel;

    @FXML
    Label detectJpylyzerVersionLabel;
    @FXML
    Label runJpylyzerLabel;
    @FXML
    Label detectJhoveVersionLabel;
    @FXML
    Label runJhoveLabel;

    @FXML
    Label detectImageMagickVersionLabel;
    @FXML
    Label installImageMagickLabel;
    @FXML
    Label runImageMagickLabel;
    @FXML
    Label detectKakaduVersionLabel;
    @FXML
    Label runKakaduLabel;


    private final Platform platform;

    public Controller() {
        platform = Platform.detectOs();
        LOGGER.info("platform: " + platform.toString());
    }

    public void initialize() {
        osLabel.setText(platform.toReadableString());
    }


    public void logProperties(ActionEvent actionEvent) {
        StringBuilder builder = new StringBuilder();
        Properties properties = System.getProperties();
        List<String> propertyNames = new ArrayList<String>();
        propertyNames.addAll(properties.stringPropertyNames());
        Collections.sort(propertyNames);

        for (String propertyName : propertyNames) {
            String line = String.format("%s: %s", propertyName, properties.getProperty(propertyName));
            builder.append(line).append("\n");
            //LOGGER.info();
        }
        logLabel.setText(builder.toString());
        installImageMagick(actionEvent);
    }

    public void detectUtilVersion(String utilName, Label label) {
        label.setText(String.format("checking %s ...", utilName));
        try {
            ImageUtil imageUtil = ImageUtilRegistry.getImageUtilByName().get(utilName);
            if (imageUtil == null) {
                label.setText(String.format("util '%s' not defined", utilName));
            } else {
                String version = imageUtil.runVersionDetection(platform);
                System.out.println(version);
                label.setText(version);
            }
        } catch (IOException e) {
            //program probably does not exist
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("not found");
        } catch (InterruptedException e) {
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("process interrupted");
        }
    }

    public void runUtil(String utilName, Label label, String imageFile) {
        label.setText(String.format("running %s ...", utilName));
        try {
            ImageUtil imageUtil = ImageUtilRegistry.getImageUtilByName().get(utilName);
            if (imageUtil == null) {
                label.setText(String.format("util '%s' not defined", utilName));
            } else {
                String output = imageUtil.runUtil(platform, imageFile);
                System.out.println(output);
                String partial = output.replace("\n", " ").substring(0, Math.min(output.length(), MAX_OUTPUT_LENGTH)) + " ...";
                label.setText(partial);
            }
        } catch (IOException e) {
            //program probably does not exist
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("not found");
        } catch (InterruptedException e) {
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("process interrupted");
        }
    }

    private String getMcFile() {
        switch (platform.getOperatingSystem()) {
            case LINUX:
                return MC_FILE_LINUX;
            case WINDOWS:
                return MC_FILE_WINDOWS;
            default:
                throw new IllegalStateException("Unsupported platform");
        }
    }

    public void detectJpylyzerVersion(ActionEvent actionEvent) {
        detectUtilVersion("jpylyzer", detectJpylyzerVersionLabel);
    }

    public void detectJhoveVersion(ActionEvent actionEvent) {
        detectUtilVersion("jhove", detectJhoveVersionLabel);
    }

    public void detectImageMagickVersion(ActionEvent actionEvent) {
        detectUtilVersion("imageMagick", detectImageMagickVersionLabel);
    }<commit_msg>image utils now run on worker threads<commit_after>package rzehan.gui.sample;

import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import rzehan.shared.Platform;
import rzehan.shared.imageUtils.CliCommand;
import rzehan.shared.imageUtils.ImageUtil;
import rzehan.shared.imageUtils.ImageUtilRegistry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

public class Controller {

    private static final Logger LOGGER = Logger.getLogger(Controller.class.getSimpleName());

    private static final int MAX_OUTPUT_LENGTH = 100;
    private static final String MC_FILE_LINUX = "/home/martin/zakazky/NKP-validator/data/monografie_1.2/b50eb6b0-f0a4-11e3-b72e-005056827e52/mastercopy/mc_b50eb6b0-f0a4-11e3-b72e-005056827e52_0001.jp2";
    private static final String MC_FILE_WINDOWS = "C:\\Users\\Martin\\Documents\\PspValidator\\mc_b50eb6b0-f0a4-11e3-b72e-005056827e52_0001.jp2";


    @FXML
    Label osLabel;
    @FXML
    Label logLabel;

    @FXML
    Label detectJpylyzerVersionLabel;
    @FXML
    Label runJpylyzerLabel;
    @FXML
    Label detectJhoveVersionLabel;
    @FXML
    Label runJhoveLabel;

    @FXML
    Label detectImageMagickVersionLabel;
    @FXML
    Label installImageMagickLabel;
    @FXML
    Label runImageMagickLabel;
    @FXML
    Label detectKakaduVersionLabel;
    @FXML
    Label runKakaduLabel;


    private final Platform platform;

    public Controller() {
        platform = Platform.detectOs();
        LOGGER.info("platform: " + platform.toString());
    }

    public void initialize() {
        osLabel.setText(platform.toReadableString());
    }


    public void logProperties(ActionEvent actionEvent) {
        StringBuilder builder = new StringBuilder();
        Properties properties = System.getProperties();
        List<String> propertyNames = new ArrayList<String>();
        propertyNames.addAll(properties.stringPropertyNames());
        Collections.sort(propertyNames);

        for (String propertyName : propertyNames) {
            String line = String.format("%s: %s", propertyName, properties.getProperty(propertyName));
            builder.append(line).append("\n");
            //LOGGER.info();
        }
        logLabel.setText(builder.toString());
        installImageMagick(actionEvent);
    }


    public void detectUtilVersion(String utilName, Label label) {
        //detectUtilVersionOnFxThread(utilName,label);
        detectUtilVersionOnWorkerThread(utilName, label);
    }

    public void detectUtilVersionOnFxThread(String utilName, Label label) {
        label.setText(String.format("checking %s ...", utilName));
        try {
            ImageUtil imageUtil = ImageUtilRegistry.getImageUtilByName().get(utilName);
            if (imageUtil == null) {
                label.setText(String.format("util '%s' not defined", utilName));
            } else {
                String version = imageUtil.runVersionDetection(platform);
                System.out.println(version);
                label.setText(version);
            }
        } catch (IOException e) {
            //program probably does not exist
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("not found");
        } catch (InterruptedException e) {
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("process interrupted");
        }
    }


    public void detectUtilVersionOnWorkerThread(String utilName, Label label) {
        label.setText(String.format("checking %s ...", utilName));
        Task task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                try {
                    ImageUtil imageUtil = ImageUtilRegistry.getImageUtilByName().get(utilName);
                    if (imageUtil == null) {
                        updateMessage(String.format("util '%s' not defined", utilName));
                    } else {
                        String version = imageUtil.runVersionDetection(platform);
                        System.out.println(version);
                        updateMessage(version);
                    }
                } catch (IOException e) {
                    //program probably does not exist
                    //e.printStackTrace() here throws IOEXception on Windows
                    //e.printStackTrace();
                    updateMessage("not found");
                } catch (InterruptedException e) {
                    //e.printStackTrace() here throws IOEXception on Windows
                    //e.printStackTrace();
                    updateMessage("process interrupted");
                }
                return null;
            }
        };

        new Thread(task).start();
        task.messageProperty().addListener((obs, oldMessage, newMessage) -> label.setText(newMessage));
    }


    public void runUtil(String utilName, Label label, String imageFile) {
        runUtilOnWorkerThread(utilName, label, imageFile);
        //runUtilOnFxThread(utilName, label, imageFile);
    }


    public void runUtilOnFxThread(String utilName, Label label, String imageFile) {
        label.setText(String.format("running %s ...", utilName));
        try {
            ImageUtil imageUtil = ImageUtilRegistry.getImageUtilByName().get(utilName);
            if (imageUtil == null) {
                label.setText(String.format("util '%s' not defined", utilName));
            } else {
                String output = imageUtil.runUtil(platform, imageFile);
                System.out.println(output);
                String partial = output.replace("\n", " ").substring(0, Math.min(output.length(), MAX_OUTPUT_LENGTH)) + " ...";
                label.setText(partial);
            }
        } catch (IOException e) {
            //program probably does not exist
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("not found");
        } catch (InterruptedException e) {
            //e.printStackTrace() here throws IOEXception on Windows
            //e.printStackTrace();
            label.setText("process interrupted");
        }
    }

    public void runUtilOnWorkerThread(String utilName, Label label, String imageFile) {
        label.setText(String.format("running %s ...", utilName));
        Task task = new Task<Void>() {

            @Override
            protected Void call() throws Exception {
                if (isCancelled()) {
                    updateMessage("canceled");
                    return null;
                }
                try {
                    ImageUtil imageUtil = ImageUtilRegistry.getImageUtilByName().get(utilName);
                    if (imageUtil == null) {
                        updateMessage(String.format("util '%s' not defined", utilName));
                    } else {
                        String output = imageUtil.runUtil(platform, imageFile);
                        System.out.println(output);
                        String partial = output.replace("\n", " ").substring(0, Math.min(output.length(), MAX_OUTPUT_LENGTH)) + " ...";
                        updateMessage(partial);
                    }
                } catch (IOException e) {
                    //program probably does not exist
                    //e.printStackTrace() here throws IOEXception on Windows
                    //e.printStackTrace();
                    updateMessage("not found");
                } catch (InterruptedException e) {
                    //e.printStackTrace() here throws IOEXception on Windows
                    //e.printStackTrace();
                    updateMessage("process interrupted");
                }
                return null;
            }
        };
        new Thread(task).start();
        task.messageProperty().addListener((obs, oldMessage, newMessage) -> label.setText(newMessage));
    }

    private String getMcFile() {
        switch (platform.getOperatingSystem()) {
            case LINUX:
                return MC_FILE_LINUX;
            case WINDOWS:
                return MC_FILE_WINDOWS;
            default:
                throw new IllegalStateException("Unsupported platform");
        }
    }

    public void detectJpylyzerVersion(ActionEvent actionEvent) {
        detectUtilVersion("jpylyzer", detectJpylyzerVersionLabel);
    }

    public void detectJhoveVersion(ActionEvent actionEvent) {
        detectUtilVersion("jhove", detectJhoveVersionLabel);
    }

    public void detectImageMagickVersion(ActionEvent actionEvent) {
        detectUtilVersion("imageMagick", detectImageMagickVersionLabel);
    }
{'fraction_non_alphanumeric': 0.10115463625416164, 'fraction_numerical': 0.011688035701636325, 'mean_word_length': 2.2403029607528118, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5710705', 'n_tokens_mistral': 3798, 'n_tokens_neox': 3619, 'n_words': 773}