001package jmri.jmrit.symbolicprog;
002
003import java.awt.Color;
004import java.beans.PropertyChangeListener;
005import java.beans.PropertyChangeSupport;
006
007/**
008 * Define common base class methods for CvValue and VariableValue classes
009 * <p>
010 * The ToRead parameter (boolean, unbound) is used to remember whether this
011 * object has been read during a "read all" operation. This allows removal of
012 * duplicate operations.
013 * <p>
014 * The ToWrite parameter (boolean, unbound) is used to remember whether this
015 * object has been written during a "write all" operation. This allows removal
016 * of duplicate operations.
017 * <p>
018 * The Available parameter (boolean, unbound) remembers whether the variable
019 * should be displayed, programmed, etc.
020 * <p>
021 * Represents a single CV value
022 *
023 * @author Bob Jacobsen Copyright (C) 2001, 2005
024 */
025public abstract class AbstractValue {
026
027    PropertyChangeSupport prop = new PropertyChangeSupport(this);
028
029    /**
030     * Method to handle color changes for states.
031     *
032     * @param c the desired colour
033     */
034    abstract void setColor(Color c);
035
036    public enum ValueState {
037        /**
038         * Defines state when nothing is known about the real value.
039         */
040        UNKNOWN(Color.red.brighter(), "Unknown"),
041
042        /**
043         * Defines state where value has been edited, no longer same as in decoder
044         * or file.
045         */
046        EDITED(Color.orange, "Edited"),
047
048        /**
049         * Defines state where value has been read from (hence same as) decoder, but
050         * perhaps not same as in file.
051         */
052        READ(null, "Read"),
053
054        /**
055         * Defines state where value has been written to (hence same as) decoder,
056         * but perhaps not same as in file.
057         */
058        STORED(null, "Stored"),
059
060        /**
061         * Defines state where value was read from a config file, but might not be
062         * the same as the decoder.
063         */
064        FROMFILE(Color.yellow, "FromFile"),
065
066        /**
067         * Defines state where value was read from a config file, and is the same as
068         * the decoder.
069         */
070        SAME(null, "Same"),
071
072        /**
073         * Defines state where value was read from a config file, and is the not the
074         * same as the decoder.
075         */
076        DIFFERENT(Color.red.brighter(), "Different");
077
078
079        private final Color _color;
080        private final String _name;
081
082        private ValueState(Color color, String name) {
083            this._color = color;
084            this._name = name;
085        }
086
087        /**
088         * Gets the color associated with this state value.
089         * @return the color assigned to this state value or null if
090         *         to use default for the component
091         */
092        public Color getColor() {
093            return _color;
094        }
095
096        /**
097         * Gets the name associated with this state value.
098         * @return the name assigned to this state value
099         */
100        public String getName() {
101            return _name;
102        }
103    }
104
105    /**
106     * Mark whether this object needs to be read.
107     *
108     * @param state true if the object needs to be read, false otherwise
109     * @see AbstractValue
110     */
111    public void setToRead(boolean state) {
112        _toRead = state;
113    }
114
115    /**
116     * Ask whether this object needs to be read.
117     *
118     * @return true if the object needs to be read, false otherwise
119     * @see AbstractValue
120     */
121    public boolean isToRead() {
122        return _toRead;
123    }
124    private boolean _toRead = false;
125
126    /**
127     * Mark whether this object needs to be written.
128     *
129     * @param state true if the object needs to be written, false otherwise
130     * @see AbstractValue
131     */
132    public void setToWrite(boolean state) {
133        _toWrite = state;
134    }
135
136    /**
137     * Ask whether this object needs to be written.
138     *
139     * @return true if the object needs to be written, false otherwise
140     * @see AbstractValue
141     */
142    public boolean isToWrite() {
143        return _toWrite;
144    }
145    private boolean _toWrite = false;
146
147    /**
148     * Sets the availability status of the object.
149     *
150     * @param avail {@code true} if the object should be made available,
151     *              {@code false} if should be made unavailable
152     */
153    public void setAvailable(boolean avail) {
154        boolean oldval = this.available;
155        available = avail;
156        if (oldval != available) {
157            prop.firePropertyChange("Available", Boolean.valueOf(oldval), Boolean.valueOf(avail));
158        }
159    }
160
161    /**
162     * Gets the current availability status of the object.
163     * <p>
164     * Code can use this to determine whether to set object visibility (or other
165     * properties).
166     *
167     * @return {@code true} if the object should be available, {@code false} if
168     *         it should be unavailable
169     */
170    public boolean getAvailable() {
171        return available;
172    }
173    private boolean available = true;
174
175    /**
176     * @param p the listener to be added to the object
177     */
178    public void addPropertyChangeListener(PropertyChangeListener p) {
179        prop.addPropertyChangeListener(p);
180    }
181
182    /**
183     * @param p the listener to be removed from the object
184     */
185    public void removePropertyChangeListener(PropertyChangeListener p) {
186        prop.removePropertyChangeListener(p);
187    }
188}