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

jdplus.toolkit.desktop.plugin.interchange.InterchangeManager Maven / Gradle / Ivy

package jdplus.toolkit.desktop.plugin.interchange;

import jdplus.main.desktop.design.GlobalService;
import jdplus.toolkit.desktop.plugin.util.CollectionSupplier;
import jdplus.toolkit.desktop.plugin.util.LazyGlobalService;
import nbbrd.design.VisibleForTesting;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.openide.util.Exceptions;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

@GlobalService
public final class InterchangeManager {

    @NonNull
    public static InterchangeManager get() {
        return LazyGlobalService.get(InterchangeManager.class, InterchangeManager::new);
    }

    private final CollectionSupplier providers;

    private InterchangeManager() {
        this(InterchangeSpiLoader::get);
    }

    @VisibleForTesting
    InterchangeManager(CollectionSupplier providers) {
        this.providers = providers;
    }

    @NonNull
    public Collection all() {
        return providers.get();
    }

    @NonNull
    public JMenuItem newImportMenu(@NonNull List importables) {
        JMenu result = new JMenu();
        result.setText("Import from");
        providers.get().forEach(o -> result.add(newImportMenu(o, importables)));
        return result;
    }

    private JMenuItem newImportMenu(InterchangeSpi o, List importables) {
        JMenuItem result = new JMenuItem(new Import(o, importables));
        result.setText(o.getDisplayName());
        result.setEnabled(o.canImport(importables));
        return result;
    }

    @NonNull
    public JMenuItem newExportMenu(@NonNull List exportables) {
        JMenu result = new JMenu();
        result.setText("Export to");
        providers.get().forEach(o -> result.add(newExportMenu(o, exportables)));
        return result;
    }

    private JMenuItem newExportMenu(InterchangeSpi o, List exportables) {
        JMenuItem result = new JMenuItem(new Export(o, exportables));
        result.setText(o.getDisplayName());
        result.setEnabled(o.canExport(exportables));
        return result;
    }

    private static final class Import extends AbstractAction {

        private final InterchangeSpi o;
        private final List importables;

        public Import(InterchangeSpi o, List importables) {
            super(o.getName());
            this.o = o;
            this.importables = importables;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                o.performImport(importables);
            } catch (IOException | IllegalArgumentException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    private static final class Export extends AbstractAction {

        private final InterchangeSpi o;
        private final List exportables;

        public Export(InterchangeSpi o, List exportables) {
            super(o.getName());
            this.o = o;
            this.exportables = exportables;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                o.performExport(exportables);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy