package moe.tristan.easyfxml.model.awt.integrations;

import org.springframework.stereotype.Component;
import moe.tristan.easyfxml.model.exception.ExceptionHandler;
import io.vavr.control.Try;

import java.awt.Desktop;
import java.net.URI;
import java.net.URL;
import java.util.Objects;

/**
 * This class contains some utility methods to open URLs with the default web browser of the user.
 */
@Component
public class BrowserSupport {

    /**
     * Opens a given URL or a pop-up with the error if it could not do so.
     *
     * @param url The URL as a String. Must conform to {@link URL#URL(String)}.
     */
    public Try<Void> openUrl(final String url) {
        return Try.of(() -> url)
                  .mapTry(URL::new)
                  .flatMap(this::openUrl)
                  .onFailure(cause -> this.onException(cause, url));
    }

    /**
     * Opens a given URL or a pop-up with the error if it could not do so.
     *
     * @param url The URL as an {@link URL}.
     */
    public Try<Void> openUrl(final URL url) {
        return Try.of(() -> url)
                  .mapTry(URL::toURI)
                  .flatMap(this::browse)
                  .onFailure(cause -> this.onException(cause, Objects.toString(url)));
    }

    private Try<Void> browse(final URI uri) {
        return Try.run(() -> Desktop.getDesktop().browse(uri))
                  .onFailure(cause -> onException(cause, uri.toString()));
    }

    private void onException(final Throwable cause, final String url) {
        ExceptionHandler.displayExceptionPane(
            "Browser error",
            "We could not open the following url in the browser : " + url,
            cause
        );
    }
}

@@ -279,16 +279,52 @@
 Objects;
+%0Aimport java.util.function.Consumer;
 %0A%0A/**%0A *
@@ -636,16 +636,195 @@
 ring)%7D.%0A
+     *%0A     * @return a %7B@link Try%7D that can be either %7B@link Try.Success%7D (and empty) or %7B@link Try.Failure%7D and contain an%0A     * exception.%0A     * @see Try#onFailure(Consumer)%0A
      */%0A
@@ -1141,32 +1141,32 @@
 t do so.%0A     *%0A
-
      * @param ur
@@ -1194,16 +1194,195 @@
 k URL%7D.%0A
+     *%0A     * @return a %7B@link Try%7D that can be either %7B@link Try.Success%7D (and empty) or %7B@link Try.Failure%7D and contain an%0A     * exception.%0A     * @see Try#onFailure(Consumer)%0A
      */%0A

Clean up some Javadoc