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