001package jmri.jmrix.can.cbus;
002
003import java.awt.Color;
004import jmri.jmrix.AbstractMessage;
005import jmri.jmrix.can.CanMessage;
006import jmri.jmrix.can.CanReply;
007
008// import org.slf4j.Logger;
009// import org.slf4j.LoggerFactory;
010
011/**
012 * Class to implement highlighting of CBUS events.
013 *
014 * @author Andrew Crosland Copyright (C) 2008
015 */
016public class CbusEventHighlighter {
017    
018    private boolean _nnEnabled;
019    private boolean _evEnabled;
020    private int _nn;
021    private int _ev;
022    private int _type;
023    private int _dir;
024    private Color _color;
025    
026    /**
027     * Creates a new instance of CbusEventHighlighter
028     */
029    public CbusEventHighlighter() {
030    }
031    
032    /**
033     * Highlight a CAN Frame, based on previous settings.
034     *
035     * @param m CanMessage or CanReply to highlight.
036     * @return true if event matches
037     */
038    public boolean highlight(AbstractMessage m){
039        if (m instanceof CanMessage){
040            return !((doNotHighlight(m)) 
041                || ((_dir != CbusConstants.EVENT_DIR_EITHER)
042                && (_dir != CbusConstants.EVENT_DIR_OUT)));
043        }
044        else if (m instanceof CanReply){
045            return !((doNotHighlight(m)) 
046                || ((_dir != CbusConstants.EVENT_DIR_EITHER)
047                && (_dir != CbusConstants.EVENT_DIR_IN)));
048        }
049        else {
050            return false;
051        }
052    }
053
054    /**
055     * 
056     * @param m CanFrame to test against
057     * @return False if not an Event, or Node, or Event or On / Off does not match
058     */
059    private boolean doNotHighlight(AbstractMessage m) {
060        return ((!CbusMessage.isEvent(m)) 
061            || (_nnEnabled && (CbusMessage.getNodeNumber( m) != _nn))
062            || (_evEnabled && (CbusMessage.getEvent(m) != _ev))
063            || ((_type != CbusConstants.EVENT_EITHER)
064                && (_type != CbusMessage.getEventType(m))));
065    }
066    
067    /**
068     * Set whether NN (Node Number) will be included in highlight.
069     * @param b True to highlight a Node Number
070     */
071    public void setNnEnable(boolean b) {
072        _nnEnabled = b;
073    }
074    
075    public boolean getNnEnable(){
076        return _nnEnabled;
077    }
078
079    /**
080     * Set whether Ev (event number) will be included in highlight.
081     * @param b True to highlight an Event Number
082     */
083    public void setEvEnable(boolean b) {
084        _evEnabled = b;
085    }
086    
087    public boolean getEvEnable() {
088        return _evEnabled;
089    }
090
091    /**
092     * Set a Node Number to highlight.
093     * @param n Node Number
094     */
095    public void setNn(int n) {
096        _nn = n;
097    }
098    
099    public int getNn() {
100        return _nn;
101    }
102
103    /**
104     * Set an Event Number to highlight.
105     * @param n Event Number
106     */
107    public void setEv(int n) {
108        _ev = n;
109    }
110    
111    public int getEv() {
112        return _ev;
113    }
114
115    /**
116     * Set value of type to match.Type is the ON, OFF, etc. value in the CBUS
117     * frame.
118     * CbusConstants.EVENT_EITHER matches either ON or OFF.
119     * @param n See {@link CbusConstants} for values
120     */
121    public void setType(int n) {
122        _type = n;
123    }
124    
125    public int getType() {
126        return _type;
127    }
128
129    /**
130     * Set value of direction to match.
131     * @param n EVENT_DIR_UNSET EVENT_DIR_IN, EVENT_DIR_OUT, EVENT_EITHER_DIR EVENT_DIR_EITHER
132     */
133    public void setDir(int n) {
134        _dir = n;
135    }
136    
137    public int getDir() {
138        return _dir;
139    }
140
141    /**
142     * Set value of Colour
143     * @param c Colour to use
144     */
145    public void setColor(Color c) {
146        _color = c;
147    }
148
149    /**
150     * Get value of Colour to highlight.
151     * @return Colour to use
152     */
153    public Color getColor() {
154        return _color;
155    }
156
157    // private final static Logger log = LoggerFactory.getLogger(CbusEventHighlighter.class);
158}