All Downloads are FREE. Search and download functionalities are using the official Maven repository.

internal.sdmxdl.desktop.util.BrowseCommand Maven / Gradle / Ivy

The newest version!
package internal.sdmxdl.desktop.util;

import ec.util.various.swing.JCommand;
import lombok.AccessLevel;
import lombok.NonNull;
import nbbrd.design.StaticFactoryMethod;

import java.awt.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.function.Function;

import static java.util.Objects.requireNonNull;

@lombok.AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class BrowseCommand extends JCommand {

    @StaticFactoryMethod
    public static  @NonNull BrowseCommand ofURI(@NonNull Function toURI) {
        return new BrowseCommand<>(toURI);
    }

    @StaticFactoryMethod
    public static  @NonNull BrowseCommand ofURL(@NonNull Function toURL) {
        return new BrowseCommand<>(toURL.andThen(BrowseCommand::toURI));
    }

    private final @NonNull Function toURI;

    @Override
    public boolean isEnabled(@NonNull C component) {
        return BROWSING_ENABLED && toURI.apply(component) != null;
    }

    @Override
    public void execute(@NonNull C component) throws Exception {
        Desktop.getDesktop().browse(requireNonNull(toURI.apply(component)));
    }

    private static final boolean BROWSING_ENABLED =
            Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE);

    private static URI toURI(URL url) {
        try {
            return url != null ? url.toURI() : null;
        } catch (URISyntaxException ignore) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy