001package jmri.jmrit.roster;
002
003import java.awt.Component;
004import java.io.File;
005import javax.swing.Icon;
006import jmri.util.FileUtil;
007import jmri.util.swing.WindowInterface;
008import org.jdom2.Element;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Copy a roster element, including the definition file.
014 *
015 * <hr>
016 * This file is part of JMRI.
017 * <p>
018 * JMRI is free software; you can redistribute it and/or modify it under the
019 * terms of version 2 of the GNU General Public License as published by the Free
020 * Software Foundation. See the "COPYING" file for a copy of this license.
021 * <p>
022 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
024 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
025 *
026 * @author Bob Jacobsen Copyright (C) 2001, 2002
027 * @see jmri.jmrit.XmlFile
028 */
029public class CopyRosterItemAction extends AbstractRosterItemAction {
030
031    public CopyRosterItemAction(String s, WindowInterface wi) {
032        super(s, wi);
033    }
034
035    public CopyRosterItemAction(String s, Icon i, WindowInterface wi) {
036        super(s, i, wi);
037    }
038
039    public CopyRosterItemAction(String pName, Component pWho) {
040        super(pName, pWho);
041    }
042
043    @Override
044    protected boolean selectFrom() {
045        return selectExistingFromEntry();
046    }
047
048    @Override
049    boolean selectTo() {
050        return selectNewToEntryID();
051    }
052
053    @Override
054    boolean doTransfer() {
055
056        // read the from file, change the ID, and write it out
057        log.debug("doTransfer starts");
058
059        // ensure preferences will be found
060        FileUtil.createDirectory(Roster.getDefault().getRosterFilesLocation());
061
062        // locate the file
063        //File f = new File(mFullFromFilename);
064        // read it
065        LocoFile lf = new LocoFile();  // used as a temporary
066        Element lroot;
067        try {
068            lroot = lf.rootFromName(mFullFromFilename);
069        } catch (Exception e) {
070            log.error("Exception while loading loco XML file: {}", mFullFromFilename, e);
071            return false;
072        }
073
074        // create a new entry
075        mToEntry = new RosterEntry(mFromEntry, mToID);
076
077        // set the filename from the ID
078        mToEntry.ensureFilenameExists();
079
080        // detach the content element from its existing file so
081        // it can be reused
082        lroot.detach();
083
084        // transfer the contents to a new file
085        LocoFile newLocoFile = new LocoFile();
086        File fout = new File(Roster.getDefault().getRosterFilesLocation() + mToEntry.getFileName());
087        newLocoFile.writeFile(fout, lroot, mToEntry);
088
089        return true;
090    }
091
092    // initialize logging
093    private final static Logger log = LoggerFactory.getLogger(CopyRosterItemAction.class);
094
095    // never invoked, because we overrode actionPerformed above
096    @Override
097    public jmri.util.swing.JmriPanel makePanel() {
098        throw new IllegalArgumentException("Should not be invoked");
099    }
100}