001package jmri.jmrit.roster;
002
003import java.awt.Component;
004import jmri.util.FileUtil;
005import org.jdom2.Element;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Export a roster element as a new definition file.
011 * <p>
012 * This creates the new file containing the entry, but does <b>not</b> add it to
013 * the local {@link Roster} of locomotives. This is intended for making a
014 * transportable copy of entry, which can be imported via
015 * {@link ImportRosterItemAction} on another system.
016 *
017 *
018 * <hr>
019 * This file is part of JMRI.
020 * <p>
021 * JMRI is free software; you can redistribute it and/or modify it under the
022 * terms of version 2 of the GNU General Public License as published by the Free
023 * Software Foundation. See the "COPYING" file for a copy of this license.
024 * <p>
025 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
026 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
027 * A PARTICULAR PURPOSE. See the GNU Gene ral Public License for more details.
028 *
029 * @author Bob Jacobsen Copyright (C) 2001, 2002
030 * @see jmri.jmrit.roster.ImportRosterItemAction
031 * @see jmri.jmrit.XmlFile
032 */
033public class ExportRosterItemAction extends AbstractRosterItemAction {
034
035    public ExportRosterItemAction(String pName, Component pWho) {
036        super(pName, pWho);
037    }
038
039    @Override
040    protected boolean selectFrom() {
041        return selectExistingFromEntry();
042    }
043
044    @Override
045    boolean selectTo() {
046        return selectNewToFile();
047    }
048
049    @Override
050    boolean doTransfer() {
051
052        // read the file for the "from" entry and write it out
053        // ensure preferences will be found for read
054        FileUtil.createDirectory(Roster.getDefault().getRosterFilesLocation());
055
056        // locate the file
057        //File f = new File(mFullFromFilename);
058        // read it
059        LocoFile lf = new LocoFile();  // used as a temporary
060        Element lroot;
061        try {
062            lroot = lf.rootFromName(mFullFromFilename).clone();
063        } catch (Exception e) {
064            log.error("Exception while loading loco XML file: {}", mFullFromFilename, e);
065            return false;
066        }
067
068        // create a new entry
069        mToEntry = new RosterEntry(mFromEntry, mFromID);
070
071        // transfer the contents to the new file
072        LocoFile newLocoFile = new LocoFile();
073        // File fout = new File(mFullToFilename);
074        mToEntry.setFileName(mToFilename);
075        mToEntry.setId(mFromEntry.getId());
076        newLocoFile.writeFile(mToFile, lroot, mToEntry);
077
078        return true;
079    }
080
081    @Override
082    void updateRoster() {
083        // exported entry is NOT added to Roster
084    }
085
086    // initialize logging
087    private final static Logger log = LoggerFactory.getLogger(ExportRosterItemAction.class);
088
089}