001package jmri.jmrit.logixng.implementation.configurexml;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import jmri.jmrit.logixng.MaleSocket;
007import jmri.jmrit.logixng.implementation.ClipboardMany;
008
009import org.jdom2.Element;
010
011/**
012 * Handle XML configuration for ActionLightXml objects.
013 *
014 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
015 * @author Daniel Bergqvist Copyright (C) 2019
016 */
017public class ClipboardManyXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
018
019    /**
020     * Default implementation for storing the contents of a Many
021     *
022     * @param o Object to store, of type Many
023     * @return Element containing the complete info
024     */
025    @Override
026    public Element store(Object o) {
027        ClipboardMany p = (ClipboardMany) o;
028
029        Element element = new Element("Many");
030        element.setAttribute("class", this.getClass().getName());
031
032        for (int i=0; i < p.getChildCount(); i++) {
033            Element e2 = new Element("Socket");
034            e2.addContent(new Element("socketName").addContent(p.getChild(i).getName()));
035            MaleSocket socket = p.getChild(i).getConnectedSocket();
036            
037            String socketSystemName;
038            String socketManager;
039            if (socket != null) {
040                socketSystemName = socket.getSystemName();
041                socketManager = socket.getManager().getClass().getName();
042                e2.addContent(new Element("systemName").addContent(socketSystemName));
043                e2.addContent(new Element("manager").addContent(socketManager));
044            }
045            element.addContent(e2);
046        }
047
048        return element;
049    }
050    
051    @Override
052    public boolean load(Element shared, Element perNode) {
053        throw new UnsupportedOperationException("Not supported");
054    }
055    
056    public ClipboardMany loadItem(Element shared) {
057        
058        List<ClipboardMany.ItemData> systemNamesAndClasses = new ArrayList<>();
059        
060        Element actionElement = shared.getChild("Many");
061        for (Element socketElement : actionElement.getChildren()) {
062            String socketName = socketElement.getChild("socketName").getTextTrim();
063            Element systemNameElement = socketElement.getChild("systemName");
064            String systemName = null;
065            if (systemNameElement != null) {
066                systemName = systemNameElement.getTextTrim();
067            }
068            Element managerElement = socketElement.getChild("manager");
069            String manager = null;
070            if (managerElement != null) {
071                manager = managerElement.getTextTrim();
072            }
073            systemNamesAndClasses.add(new ClipboardMany.ItemData(socketName, systemName, manager));
074        }
075        
076        ClipboardMany h = new ClipboardMany("", null, systemNamesAndClasses);
077        
078        loadCommon(h, shared);
079        
080//        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
081        
082//        log.warn("Register action: " + h.getSystemName() + ", " + h.getLongDescription());
083        return h;
084    }
085    
086    
087//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DigitalManyXml.class);
088    
089}