jdplus.sdmx.desktop.plugin.web.OpenWebsiteAction Maven / Gradle / Ivy
package jdplus.sdmx.desktop.plugin.web;
import jdplus.toolkit.desktop.plugin.TsManager;
import jdplus.toolkit.desktop.plugin.actions.AbilityNodeAction;
import jdplus.toolkit.desktop.plugin.actions.Actions;
import static jdplus.toolkit.desktop.plugin.tsproviders.TsProviderNodes.SOURCE_ACTION_PATH;
import jdplus.sdmx.base.api.web.SdmxWebProvider;
import jdplus.toolkit.base.tsp.DataSource;
import ec.util.desktop.Desktop;
import ec.util.desktop.DesktopManager;
import org.openide.awt.ActionID;
import org.openide.awt.ActionRegistration;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.stream.Stream;
import javax.swing.JMenuItem;
import org.openide.awt.ActionReference;
import org.openide.util.actions.Presenter;
import sdmxdl.web.SdmxWebSource;
@ActionID(category = "Edit", id = OpenWebsiteAction.ID)
@ActionRegistration(displayName = "#CTL_OpenWebsiteAction", lazy = false)
@Messages("CTL_OpenWebsiteAction=Open web site")
@ActionReference(path = SOURCE_ACTION_PATH, position = 720, separatorBefore = 700, id = @ActionID(category = "Edit", id = OpenWebsiteAction.ID))
public final class OpenWebsiteAction extends AbilityNodeAction implements Presenter.Popup {
public static final String ID = "demetra.desktop.extra.sdmx.web.OpenWebsiteAction";
public OpenWebsiteAction() {
super(DataSource.class, true);
}
@Override
public JMenuItem getPopupPresenter() {
return Actions.hideWhenDisabled(new JMenuItem(this));
}
@Override
protected void performAction(Stream items) {
items.forEach(item -> {
try {
DesktopManager.get().browse(getWebsite(item).toURI());
} catch (IOException | URISyntaxException ex) {
Exceptions.printStackTrace(ex);
}
});
}
@Override
protected boolean enable(Stream items) {
return DesktopManager.get().isSupported(Desktop.Action.BROWSE)
&& items.anyMatch(item -> getWebsite(item) != null);
}
private URL getWebsite(DataSource dataSource) {
return TsManager.get()
.getProvider(SdmxWebProvider.class, dataSource)
.map(provider -> getWebsite(provider, dataSource))
.orElse(null);
}
private URL getWebsite(SdmxWebProvider provider, DataSource dataSource) {
SdmxWebSource source = provider.getSdmxManager().getSources().get(provider.decodeBean(dataSource).getSource());
return source != null ? source.getWebsite() : null;
}
@Override
public String getName() {
return Bundle.CTL_OpenWebsiteAction();
}
}