001package jmri.jmrix.rps.swing;
002
003import java.io.File;
004import javax.annotation.concurrent.GuardedBy;
005import javax.swing.JButton;
006import javax.swing.JFileChooser;
007
008import jmri.jmrix.rps.Engine;
009import jmri.jmrix.rps.PositionFile;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Panel for load/store of RPS setup.
015 *
016 * @author Bob Jacobsen Copyright (C) 2008
017 * @author i18n Egbert Broerse 2021
018 */
019public class LoadStorePanel extends javax.swing.JPanel {
020
021    public LoadStorePanel() {
022        super();
023
024        // file load, store
025        JButton b1;
026        b1 = new JButton(Bundle.getMessage("ButtonSetDefaults"));
027        b1.setToolTipText(Bundle.getMessage("HintSetDefaults"));
028        b1.addActionListener(event -> storeDefault());
029        add(b1);
030        b1 = new JButton(Bundle.getMessage("ButtonStore_"));
031        b1.setToolTipText(Bundle.getMessage("HintStore"));
032        b1.addActionListener(event -> store());
033        add(b1);
034        b1 = new JButton(Bundle.getMessage("ButtonLoad_"));
035        b1.setToolTipText(Bundle.getMessage("HintLoad"));
036        b1.addActionListener(event -> load());
037        add(b1);
038    }
039
040    @GuardedBy("this")
041    JFileChooser fci = jmri.jmrit.XmlFile.userFileChooser();
042
043    public synchronized void load() {
044        try {
045            // request the filename from an open dialog
046            fci.rescanCurrentDirectory();
047            int retVal = fci.showOpenDialog(this);
048            // handle selection or cancel
049            if (retVal == JFileChooser.APPROVE_OPTION) {
050                File file = fci.getSelectedFile();
051                log.debug("located file {} for load", file);
052                // handle the file
053                Engine.instance().loadAlignment(file);
054            } else {
055                log.debug("load cancelled in open dialog");
056            }
057        } catch (Exception e) {
058            log.error("exception during load: ", e);
059        }
060    }
061
062    public synchronized void store() {
063        try {
064            // request the filename from an open dialog
065            fci.rescanCurrentDirectory();
066            int retVal = fci.showSaveDialog(this);
067            // handle selection or cancel
068            if (retVal == JFileChooser.APPROVE_OPTION) {
069                File file = fci.getSelectedFile();
070                log.debug("located file {} for store", file);
071                // handle the file
072                Engine.instance().storeAlignment(file);
073            } else {
074                log.debug("load cancelled in open dialog");
075            }
076        } catch (Exception e) {
077            log.error("exception during store: ", e);
078        }
079    }
080
081    public void storeDefault() {
082        try {
083            File file = new File(PositionFile.defaultFilename());
084            log.debug("located file {} for store", file);
085            // handle the file
086            Engine.instance().storeAlignment(file);
087        } catch (Exception e) {
088            log.error("exception during storeDefault: ", e);
089        }
090    }
091
092    private final static Logger log = LoggerFactory.getLogger(LoadStorePanel.class);
093
094}