001package jmri.jmrix.lenz.liusbserver.configurexml; 002 003import jmri.jmrix.configurexml.AbstractNetworkConnectionConfigXml; 004import jmri.jmrix.lenz.liusbserver.ConnectionConfig; 005import jmri.jmrix.lenz.liusbserver.LIUSBServerAdapter; 006import org.jdom2.Element; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010import javax.annotation.Nonnull; 011 012/** 013 * Handle XML persistance of layout connections by persistening the LIUSB Server 014 * (and connections). Note this is named as the XML version of a 015 * ConnectionConfig object, but it's actually persisting the LIUSB Server. 016 * <p> 017 * NOTE: The LIUSB Server currently has no options, so this class does not store 018 * any. 019 * <p> 020 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class 021 * is the one actually registered. Reads are brought here directly via the class 022 * attribute in the XML. 023 * 024 * @author Paul Bender Copyright (C) 2009 025 */ 026public class ConnectionConfigXml extends AbstractNetworkConnectionConfigXml { 027 028 public ConnectionConfigXml() { 029 super(); 030 } 031 032 /** 033 * An LIUSBServer connection needs no extra information, so we reimplement 034 * the superclass method to just write the necessary parts. 035 * 036 * @return Formatted element containing no attributes except the class name 037 */ 038 @Override 039 public Element store(Object o) { 040 getInstance(o); 041 Element e = new Element("connection"); 042 storeCommon(e, adapter); 043 e.setAttribute("class", this.getClass().getName()); 044 return e; 045 } 046 047 @Override 048 public boolean load(@Nonnull Element shared, Element perNode) { 049 boolean result = true; 050 getInstance(); 051 052 loadCommon(shared,perNode,adapter); 053 054 // register, so can be picked up 055 register(); 056 057 if(adapter.getDisabled()){ 058 unpackElement(shared,perNode); 059 return result; 060 } 061 return result; 062 } 063 064 @Override 065 protected void getInstance() { 066 if (adapter == null) { 067 adapter = new LIUSBServerAdapter(); 068 try { 069 adapter.connect(); 070 adapter.configure(); 071 } catch(Exception e){ 072 log.error("Error connecting or configuring port."); 073 } 074 } 075 } 076 077 @Override 078 protected void getInstance(Object object) { 079 adapter = ((ConnectionConfig) object).getAdapter(); 080 } 081 082 @Override 083 protected void register() { 084 this.register(new ConnectionConfig(adapter)); 085 } 086 087 private static final Logger log = LoggerFactory.getLogger(ConnectionConfigXml.class); 088 089}