001package jmri.jmrit.operations.locations.divisions;
002
003import org.jdom2.Attribute;
004import org.jdom2.Element;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008import jmri.InstanceManager;
009import jmri.beans.Identifiable;
010import jmri.beans.PropertyChangeSupport;
011import jmri.jmrit.operations.locations.LocationManagerXml;
012
013
014/**
015 * Represents a railroad division
016 *
017 * @author Daniel Boudreau Copyright (C) 2021
018 */
019public class Division extends PropertyChangeSupport implements Identifiable {
020
021    public static final String NONE = "";
022
023    protected String _id = NONE;
024    protected String _name = NONE;
025    protected String _comment = NONE;
026
027    public static final String NAME_CHANGED_PROPERTY = "divisionName"; // NOI18N
028
029    public Division(String id, String name) {
030        log.debug("New division ({}) id: {}", name, id);
031        _name = name;
032        _id = id;
033    }
034
035    @Override
036    public String getId() {
037        return _id;
038    }
039
040    /**
041     * Sets the division's name.
042     * 
043     * @param name The string name for this division.
044     *
045     */
046    public void setName(String name) {
047        String old = _name;
048        _name = name;
049        if (!old.equals(name)) {
050            setDirtyAndFirePropertyChange(NAME_CHANGED_PROPERTY, old, name);
051        }
052    }
053
054    // for combo boxes
055    @Override
056    public String toString() {
057        return _name;
058    }
059
060    public String getName() {
061        return _name;
062    }
063
064    public void setComment(String comment) {
065        String old = _comment;
066        _comment = comment;
067        if (!old.equals(comment)) {
068            setDirtyAndFirePropertyChange("divisionComment", old, comment); // NOI18N
069        }
070    }
071
072    public String getComment() {
073        return _comment;
074    }
075
076    /**
077     * Construct this Entry from XML. This member has to remain synchronized with
078     * the detailed DTD in operations-locations.dtd
079     *
080     * @param e Consist XML element
081     */
082    public Division(Element e) {
083        Attribute a;
084        if ((a = e.getAttribute(Xml.ID)) != null) {
085            _id = a.getValue();
086        } else {
087            log.warn("no id attribute in location element when reading operations");
088        }
089        if ((a = e.getAttribute(Xml.NAME)) != null) {
090            _name = a.getValue();
091        }
092        if ((a = e.getAttribute(Xml.COMMENT)) != null) {
093            _comment = a.getValue();
094        }
095    }
096
097    /**
098     * Create an XML element to represent this Entry. This member has to remain
099     * synchronized with the detailed DTD in operations-locations.dtd.
100     *
101     * @return Contents in a JDOM Element
102     */
103    public Element store() {
104        Element e = new Element(Xml.DIVISION);
105        e.setAttribute(Xml.ID, getId());
106        e.setAttribute(Xml.NAME, getName());
107        e.setAttribute(Xml.COMMENT, getComment());
108        return e;
109    }
110
111    protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) {
112        InstanceManager.getDefault(LocationManagerXml.class).setDirty(true);
113        firePropertyChange(p, old, n);
114    }
115
116    private final static Logger log = LoggerFactory.getLogger(Division.class);
117}