001package jmri.jmrit.roster;
002
003import java.awt.Component;
004import java.awt.event.ActionEvent;
005import javax.swing.Icon;
006import javax.swing.JFileChooser;
007import javax.swing.filechooser.FileNameExtensionFilter;
008
009import jmri.util.swing.JmriJOptionPane;
010import jmri.util.swing.WindowInterface;
011
012/**
013 * Offer an easy mechanism to save a roster group's contentsfrom one instance
014 * of DecoderPro to another. The result is a zip format file, containing all of the roster
015 * entries plus the overall roster.xml index file.
016 *
017 * @author Bob Jacobsen
018 *
019 */
020public class GroupBackupExportAction
021        extends FullBackupExportAction {
022
023    // parent component for GUI
024    public GroupBackupExportAction(String s, WindowInterface wi) {
025        super(s, wi);
026    }
027
028    public GroupBackupExportAction(String s, Icon i, WindowInterface wi) {
029        super(s, i, wi);
030    }
031
032    /**
033     * @param s      Name of this action, e.g. in menus
034     * @param parent Component that action is associated with, used to ensure
035     *               proper position in of dialog boxes
036     */
037    public GroupBackupExportAction(String s, Component parent) {
038        super(s, parent);
039    }
040
041    @Override
042    public void actionPerformed(ActionEvent e) {
043
044        // Always ask for group.  (See jmri.jmrit.roster.swing.DeleteRosterGroupAction
045        // for an example of using a pre-selected group instead)
046        String group = (String) JmriJOptionPane.showInputDialog(_parent,
047                Bundle.getMessage("ExportRosterGroupDialog"),
048                Bundle.getMessage("ExportRosterGroupTitle", ""),
049                JmriJOptionPane.INFORMATION_MESSAGE,
050                null,
051                Roster.getDefault().getRosterGroupList().toArray(),
052                null);
053 
054        String roster_filename_extension = "roster";
055
056        JFileChooser chooser = new jmri.util.swing.JmriJFileChooser();
057        FileNameExtensionFilter filter = new FileNameExtensionFilter(
058                "JMRI full roster files", roster_filename_extension);
059        chooser.setFileFilter(filter);
060
061        int returnVal = chooser.showSaveDialog(_parent);
062        if (returnVal != JFileChooser.APPROVE_OPTION) {
063            return;
064        }
065
066        filename = chooser.getSelectedFile().getAbsolutePath();
067
068        if (!filename.endsWith("."+roster_filename_extension)) {
069            filename = filename.concat("."+roster_filename_extension);
070        }
071
072        var list = Roster.getDefault().getEntriesInGroup(group);
073        new Thread(() -> {run(list);}).start();
074
075    }
076
077    // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GroupBackupExportAction.class);
078}