001package apps.configurexml;
002
003import apps.FileLocationPane;
004import java.util.List;
005import jmri.ConfigureManager;
006import jmri.profile.ProfileManager;
007import jmri.util.FileUtil;
008import org.jdom2.Element;
009
010/**
011 * Handle XML persistence of directory locations.
012 *
013 * @author Kevin Dickerson Copyright: Copyright (c) 2010
014 */
015public class FileLocationPaneXml extends jmri.configurexml.AbstractXmlAdapter {
016
017    public FileLocationPaneXml() {
018    }
019
020    /**
021     * Default implementation for storing the static contents of the Swing LAF
022     *
023     * @param o Object to store, of type FileLocationPane
024     * @return Element containing the complete info
025     */
026    @Override
027    public Element store(Object o) {
028        Element e = new Element("fileLocations");
029        /*e.setAttribute("defaultScriptLocation", FileUtil.getPythonScriptsPath());
030         e.setAttribute("defaultUserLocation", FileUtil.getUserFilesPath());
031         e.setAttribute("defaultThrottleLocation", jmri.jmrit.throttle.ThrottleFrame.getDefaultThrottleFolder());*/
032        storeLocation(e, "defaultScriptLocation", FileUtil.getScriptsPath());
033        storeUserFilesLocation(e, FileUtil.getUserFilesPath());
034        e.setAttribute("class", this.getClass().getName());
035        return e;
036    }
037
038    private void storeLocation(Element locations, String attr, String value) {
039        Element userLocation = new Element("fileLocation");
040        userLocation.setAttribute(attr, FileUtil.getPortableFilename(value));
041        locations.addContent(userLocation);
042    }
043
044    private void storeUserFilesLocation(Element locations, String value) {
045        Element userLocation = new Element("fileLocation");
046        userLocation.setAttribute("defaultUserLocation", FileUtil.getPortableFilename(value, true, false));
047        locations.addContent(userLocation);
048    }
049
050    @Override
051    public boolean load(Element shared, Element perNode) {
052        boolean result = true;
053        //Attribute scriptLocation = shared.getAttribute("defaultScriptLocation");
054        //if (scriptLocation!=null)
055        //FileUtil.setPythonScriptsPath(scriptLocation.getValue());
056        /*Attribute userLocation = shared.getAttribute("defaultUserLocation");
057         if (userLocation!=null)
058         FileUtil.setUserFilesPath(userLocation.getValue());*/
059        String value = loadUserLocations(shared, "defaultUserLocation");
060        if (value != null) {
061            FileUtil.setUserFilesPath(ProfileManager.getDefault().getActiveProfile(), value);
062        }
063        value = loadUserLocations(shared, "defaultScriptLocation");
064        if (value != null) {
065            FileUtil.setScriptsPath(ProfileManager.getDefault().getActiveProfile(), value);
066        }
067        ConfigureManager cm = jmri.InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
068        if (cm != null) {
069            cm.registerPref(new FileLocationPane());
070        }
071        return result;
072    }
073
074    private String loadUserLocations(Element messages, String attr) {
075        List<Element> messageList = messages.getChildren("fileLocation");
076        for (Element message : messageList) {
077            if (message.getAttribute(attr) != null) {
078                return FileUtil.getAbsoluteFilename(message.getAttribute(attr).getValue());
079            }
080        }
081        return null;
082
083    }
084
085    // initialize logging
086//    private final static Logger log = LoggerFactory.getLogger(FileLocationPaneXml.class);
087
088}