<commit_before>/*
 * Copyright 2014 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.terasology.launcher.util;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public final class GuiUtils {

    private static final Logger logger = LoggerFactory.getLogger(GuiUtils.class);

    private GuiUtils() {
    }

    private static void showMessageDialog(Alert.AlertType type, String title, String message, Stage owner) {
        FutureTask<Void> dialog = new FutureTask<>(() -> {
            final Alert alert = new Alert(type);
            alert.setTitle(title);
            alert.setContentText(message);
            alert.initOwner(owner);

            alert.showAndWait();
            return null;
        });

        Platform.runLater(dialog);
        try {
            dialog.get();
        } catch (InterruptedException | ExecutionException e) {
            logger.warn("Uh oh, something went wrong with the dialog!", e);
        }
    }

    public static void showWarningMessageDialog(Stage owner, String message) {
        showMessageDialog(Alert.AlertType.WARNING, BundleUtils.getLabel("message_error_title"), message, owner);
    }

    public static void showErrorMessageDialog(Stage owner, String message) {
        showMessageDialog(Alert.AlertType.ERROR, BundleUtils.getLabel("message_error_title"), message, owner);
    }

    public static void showInfoMessageDialog(Stage owner, String message) {
        showMessageDialog(Alert.AlertType.INFORMATION, BundleUtils.getLabel("message_information_title"), message, owner);
    }

    public static File chooseDirectoryDialog(Stage owner, final File directory, final String title) {
        if (!directory.isDirectory()) {
            directory.mkdir();
        }

        final FutureTask<File> chooseDirectory = new FutureTask<>(() -> {
            final DirectoryChooser directoryChooser = new DirectoryChooser();
            directoryChooser.setInitialDirectory(directory);
            directoryChooser.setTitle(title);

            return directoryChooser.showDialog(owner);
        });

        Platform.runLater(chooseDirectory);
        File selected = null;
        try {
            selected = chooseDirectory.get();
        } catch (InterruptedException | ExecutionException e) {
            logger.warn("Uh oh, something went wrong with the dialog!", e);
        }

        // directory proposal needs to be deleted if the user chose a different one
        if (!directory.equals(selected)) {
            directory.delete();
        }
        return selected;
    }
}
<commit_msg>Run DirectoryChooser on FXApplicationThread -- fixes #339<commit_after>/*
 * Copyright 2014 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.terasology.launcher.util;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public final class GuiUtils {

    private static final Logger logger = LoggerFactory.getLogger(GuiUtils.class);

    private GuiUtils() {
    }

    private static void showMessageDialog(Alert.AlertType type, String title, String message, Stage owner) {
        FutureTask<Void> dialog = new FutureTask<>(() -> {
            final Alert alert = new Alert(type);
            alert.setTitle(title);
            alert.setContentText(message);
            alert.initOwner(owner);

            alert.showAndWait();
            return null;
        });

        Platform.runLater(dialog);
        try {
            dialog.get();
        } catch (InterruptedException | ExecutionException e) {
            logger.warn("Uh oh, something went wrong with the dialog!", e);
        }
    }

    public static void showWarningMessageDialog(Stage owner, String message) {
        showMessageDialog(Alert.AlertType.WARNING, BundleUtils.getLabel("message_error_title"), message, owner);
    }

    public static void showErrorMessageDialog(Stage owner, String message) {
        showMessageDialog(Alert.AlertType.ERROR, BundleUtils.getLabel("message_error_title"), message, owner);
    }

    public static void showInfoMessageDialog(Stage owner, String message) {
        showMessageDialog(Alert.AlertType.INFORMATION, BundleUtils.getLabel("message_information_title"), message, owner);
    }

    public static File chooseDirectoryDialog(Stage owner, final File directory, final String title) {
        if (!directory.isDirectory()) {
            directory.mkdir();
        }

        File selected = null;

        if (Platform.isFxApplicationThread()) {
            final DirectoryChooser directoryChooser = new DirectoryChooser();
            directoryChooser.setInitialDirectory(directory);
            directoryChooser.setTitle(title);

            selected = directoryChooser.showDialog(owner);
        } else {
            final FutureTask<File> chooseDirectory = new FutureTask<>(() -> {
                final DirectoryChooser directoryChooser = new DirectoryChooser();
                directoryChooser.setInitialDirectory(directory);
                directoryChooser.setTitle(title);

                return directoryChooser.showDialog(owner);
            });

            Platform.runLater(chooseDirectory);
            try {
                selected = chooseDirectory.get();
            } catch (InterruptedException | ExecutionException e) {
                logger.warn("Uh oh, something went wrong with the dialog!", e);
            }
        }

        // directory proposal needs to be deleted if the user chose a different one
        if (!directory.equals(selected)) {
            directory.delete();
        }
        return selected;
    }
}

{'fraction_non_alphanumeric': 0.08876072584048389, 'fraction_numerical': 0.00323533549022366, 'mean_word_length': 2.887370147621651, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4150495', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1741, 'n_words': 527}