001package jmri.jmrit.display.layoutEditor.configurexml; 002 003import java.awt.Color; 004import javax.annotation.CheckForNull; 005import javax.annotation.CheckReturnValue; 006import javax.annotation.Nonnull; 007import jmri.configurexml.AbstractXmlAdapter; 008import jmri.jmrit.display.layoutEditor.*; 009import jmri.util.ColorUtil; 010import org.jdom2.Element; 011 012/** 013 * This module handles saving and loading LayoutTrackDrawingOptions for a 014 * LayoutEditor. 015 * 016 * @author George Warner Copyright (c) 2017-2018 017 */ 018public class LayoutTrackDrawingOptionsXml extends AbstractXmlAdapter { 019 020 public LayoutTrackDrawingOptionsXml() { 021 } 022 023 /** 024 * Default implementation for storing LayoutTrackDrawingOptions contents 025 * 026 * @param o Object to store, of type LayoutTrackDrawingOptions 027 * @return Element containing the complete info 028 */ 029 @Override 030 public Element store(Object o) { 031 032 LayoutTrackDrawingOptions p = (LayoutTrackDrawingOptions) o; 033 034 Element element = new Element("layoutTrackDrawingOptions"); 035 036 // include attributes 037 element.setAttribute("name", p.getName()); 038 039 // add elements 040 element.addContent(new Element("mainBallastColor").addContent(ColorUtil.colorToHexString(p.getMainBallastColor()))); 041 element.addContent(new Element("mainBallastWidth").addContent("" + p.getMainBallastWidth())); 042 element.addContent(new Element("mainBlockLineDashPercentageX10").addContent("" + p.getMainBlockLineDashPercentageX10())); 043 element.addContent(new Element("mainBlockLineWidth").addContent("" + p.getMainBlockLineWidth())); 044 element.addContent(new Element("mainRailColor").addContent(ColorUtil.colorToHexString(p.getMainRailColor()))); 045 element.addContent(new Element("mainRailCount").addContent("" + p.getMainRailCount())); 046 element.addContent(new Element("mainRailGap").addContent("" + p.getMainRailGap())); 047 element.addContent(new Element("mainRailWidth").addContent("" + p.getMainRailWidth())); 048 element.addContent(new Element("mainTieColor").addContent(ColorUtil.colorToHexString(p.getMainTieColor()))); 049 element.addContent(new Element("mainTieGap").addContent("" + p.getMainTieGap())); 050 element.addContent(new Element("mainTieLength").addContent("" + p.getMainTieLength())); 051 element.addContent(new Element("mainTieWidth").addContent("" + p.getMainTieWidth())); 052 053 element.addContent(new Element("sideBallastColor").addContent(ColorUtil.colorToHexString(p.getSideBallastColor()))); 054 element.addContent(new Element("sideBallastWidth").addContent("" + p.getSideBallastWidth())); 055 element.addContent(new Element("sideBlockLineDashPercentageX10").addContent("" + p.getSideBlockLineDashPercentageX10())); 056 element.addContent(new Element("sideBlockLineWidth").addContent("" + p.getSideBlockLineWidth())); 057 element.addContent(new Element("sideRailColor").addContent(ColorUtil.colorToHexString(p.getSideRailColor()))); 058 element.addContent(new Element("sideRailCount").addContent("" + p.getSideRailCount())); 059 element.addContent(new Element("sideRailGap").addContent("" + p.getSideRailGap())); 060 element.addContent(new Element("sideRailWidth").addContent("" + p.getSideRailWidth())); 061 element.addContent(new Element("sideTieColor").addContent(ColorUtil.colorToHexString(p.getSideTieColor()))); 062 element.addContent(new Element("sideTieGap").addContent("" + p.getSideTieGap())); 063 element.addContent(new Element("sideTieLength").addContent("" + p.getSideTieLength())); 064 element.addContent(new Element("sideTieWidth").addContent("" + p.getSideTieWidth())); 065 066 element.setAttribute("class", getClass().getName()); 067 return element; 068 } 069 070 @Override 071 public boolean load(Element shared, Element perNode) { 072 log.error("Invalid method called"); 073 return false; 074 } 075 076 /** 077 * Load, starting with the layoutTrackDrawingOptions element, then all the 078 * other data 079 * 080 * @param element Top level Element to unpack. 081 * @param o LayoutEditor as an Object 082 */ 083 @Override 084 public void load(Element element, Object o) { 085 // create the objects 086 LayoutEditor p = (LayoutEditor) o; 087 088 // get the current LayoutTrackDrawingOptions 089 LayoutTrackDrawingOptions ltdo = p.getLayoutTrackDrawingOptions(); 090 091 // set its name 092 String name = element.getAttribute("name").getValue(); 093 ltdo.setName(name); 094 095 // get remaining option elements 096 ltdo.setMainBallastColor(getElementColor(element, "mainBallastColor", ltdo.getMainBallastColor())); 097 ltdo.setMainBallastWidth(getElementInt(element, "mainBallastWidth", ltdo.getMainBallastWidth())); 098 ltdo.setMainBlockLineDashPercentageX10(getElementInt(element, "mainBlockLineDashPercentageX10", ltdo.getMainBlockLineDashPercentageX10())); 099 ltdo.setMainBlockLineWidth(getElementInt(element, "mainBlockLineWidth", ltdo.getMainBlockLineWidth())); 100 ltdo.setMainRailColor(getElementColor(element, "mainRailColor", ltdo.getMainRailColor())); 101 ltdo.setMainRailCount(getElementInt(element, "mainRailCount", ltdo.getMainRailCount())); 102 ltdo.setMainRailGap(getElementInt(element, "mainRailGap", ltdo.getMainRailGap())); 103 ltdo.setMainRailWidth(getElementInt(element, "mainRailWidth", ltdo.getMainRailWidth())); 104 ltdo.setMainTieColor(getElementColor(element, "mainTieColor", ltdo.getMainTieColor())); 105 ltdo.setMainTieGap(getElementInt(element, "mainTieGap", ltdo.getMainTieGap())); 106 ltdo.setMainTieLength(getElementInt(element, "mainTieLength", ltdo.getMainTieLength())); 107 ltdo.setMainTieWidth(getElementInt(element, "mainTieWidth", ltdo.getMainTieWidth())); 108 109 ltdo.setSideBallastColor(getElementColor(element, "sideBallastColor", ltdo.getSideBallastColor())); 110 ltdo.setSideBallastWidth(getElementInt(element, "sideBallastWidth", ltdo.getSideBallastWidth())); 111 ltdo.setSideBlockLineDashPercentageX10(getElementInt(element, "sideBlockLineDashPercentageX10", ltdo.getSideBlockLineDashPercentageX10())); 112 ltdo.setSideBlockLineWidth(getElementInt(element, "sideBlockLineWidth", ltdo.getSideBlockLineWidth())); 113 ltdo.setSideRailColor(getElementColor(element, "sideRailColor", ltdo.getSideBallastColor())); 114 ltdo.setSideRailCount(getElementInt(element, "sideRailCount", ltdo.getSideRailCount())); 115 ltdo.setSideRailGap(getElementInt(element, "sideRailGap", ltdo.getSideRailGap())); 116 ltdo.setSideRailWidth(getElementInt(element, "sideRailWidth", ltdo.getSideRailWidth())); 117 ltdo.setSideTieColor(getElementColor(element, "sideTieColor", ltdo.getSideTieColor())); 118 ltdo.setSideTieGap(getElementInt(element, "sideTieGap", ltdo.getSideTieGap())); 119 ltdo.setSideTieLength(getElementInt(element, "sideTieLength", ltdo.getSideTieLength())); 120 ltdo.setSideTieWidth(getElementInt(element, "sideTieWidth", ltdo.getSideTieWidth())); 121 122 p.setLayoutTrackDrawingOptions(ltdo); 123 } // load 124 125 @CheckReturnValue 126 private Color getElementColor(@Nonnull Element el, @Nonnull String child, @CheckForNull Color defVal) { 127 Element c = el.getChild(child); 128 if (c != null) { 129 String val = c.getText(); 130 if ((val != null) && !val.isEmpty()) { 131 defVal = Color.decode(val); 132 } 133 } 134 return defVal; 135 } 136 137 int getElementInt(Element el, String child, int defVal) { 138 Element c = el.getChild(child); 139 if (c != null) { 140 String val = c.getText(); 141 if ((val != null) && !val.isEmpty()) { 142 defVal = Integer.parseInt(val); 143 } 144 } 145 return defVal; 146 } 147 148 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LayoutTrackDrawingOptionsXml.class); 149}