001package jmri.jmrix.can.cbus.swing;
002
003import java.awt.Color;
004
005import javax.swing.BorderFactory;
006import javax.swing.BoxLayout;
007import javax.swing.WindowConstants;
008
009import jmri.jmrix.AbstractMessage;
010import jmri.jmrix.can.cbus.CbusConstants;
011import jmri.jmrix.can.cbus.CbusEventHighlighter;
012import jmri.jmrix.can.cbus.swing.console.CbusConsolePane;
013import jmri.jmrix.can.cbus.swing.configtool.ConfigToolPane;
014import jmri.util.JmriJFrame;
015import jmri.util.ThreadingUtil;
016
017// import org.slf4j.Logger;
018// import org.slf4j.LoggerFactory;
019
020/**
021 * Frame to control an instance of CBUS highlighter to highlight events.
022 *
023 * @author Andrew Crosland Copyright (C) 2008
024 */
025public class CbusEventHighlightFrame extends JmriJFrame {
026
027    protected static final int HIGHLIGHTERS = 4;
028    public static final Color[] highlightColors = {
029        new Color(110, 235, 131), // green ish as will have black text on top
030        new Color(68, 235, 255), // cyan ish
031        new Color(228, 255, 26), // yellow ish
032        new Color(255, 132, 84) // orange ish
033        };
034    protected CbusEventHighlightPanel[] highlightPanes = new CbusEventHighlightPanel[HIGHLIGHTERS];
035
036    // member to hold reference to my HIGHLIGHTERS
037    protected CbusEventHighlighter[] _highlight = new CbusEventHighlighter[HIGHLIGHTERS];
038    protected boolean[] _highlightActive = new boolean[HIGHLIGHTERS];
039    private CbusConsolePane _console;
040    private ConfigToolPane _evCap;
041
042    /**
043     * Create a new instance of CbusFilterFrame.
044     * @param console main Console Window, can be null
045     * @param evCap main Event Capture Window, can be null
046     */
047    public CbusEventHighlightFrame(CbusConsolePane console, ConfigToolPane evCap) {
048        super();
049        for (int i = 0; i < HIGHLIGHTERS; i++) {
050            _highlight[i] = new CbusEventHighlighter();
051            _highlightActive[i] = false;
052        }
053        _console = console;
054        _evCap = evCap;
055        this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
056    }
057
058    protected CbusEventHighlightFrame() {
059        super();
060    }
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public String getTitle() {
067        if ( _console != null) {
068            return _console.getTitle() + " " + Bundle.getMessage("EventHighlightTitle");
069        }
070        else if ( _evCap != null) {
071            return _evCap.getTitle() + " " + Bundle.getMessage("EventHighlightTitle");
072        }
073        return(Bundle.getMessage("EventHighlightTitle"));
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public void dispose() {
081        super.dispose();
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public void initComponents() {
089        setTitle(getTitle());
090        // Panels will be added downwards
091        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
092
093        // add items to GUI
094        for (int i = 0; i < HIGHLIGHTERS; i++) {
095            // Pane to hold a highlighter
096            highlightPanes[i] = new CbusEventHighlightPanel(this, i);
097            highlightPanes[i].setBorder(BorderFactory.createTitledBorder(
098                    BorderFactory.createLineBorder(highlightColors[i],4), Bundle.getMessage("EventHighlightTitleX", (i + 1))));
099            highlightPanes[i].initComponents(i);
100            getContentPane().add(highlightPanes[i]);
101        }
102        // prevent button areas from expanding
103        ThreadingUtil.runOnGUI( () -> pack());
104    }
105
106    /**
107     * Enable Highlighter.
108     * @param index highlighter index.
109     * @param nn node number.
110     * @param nnEn node number enabled.
111     * @param ev event number.
112     * @param evEn event number enabled.
113     * @param ty event type.
114     * @param dr event direction.
115     */
116    public void enable(int index, int nn, boolean nnEn, int ev, boolean evEn, int ty, int dr) {
117        
118        _highlight[index].setNn(nn);
119        _highlight[index].setNnEnable(nnEn);
120        _highlight[index].setEv(ev);
121        _highlight[index].setEvEnable(evEn);
122        _highlight[index].setType(ty);
123        _highlight[index].setDir(dr);
124        _highlightActive[index] = true;
125        if ( _console != null) { 
126            updateConsole(index);
127        }
128    }
129    
130    private void updateConsole(int index) {
131    
132        // log.debug("Cbus Console highlight applied");
133        StringBuilder sb = new StringBuilder(80);
134        if ( _highlight[index].getNnEnable() ) {
135            sb.append(Bundle.getMessage("CbusNode")).append(_highlight[index].getNn()).append(" ");
136        }
137        if (_highlight[index].getEvEnable()) {
138            sb.append(Bundle.getMessage("CbusEvent")).append(_highlight[index].getEv()).append(" ");
139        }
140        
141        appendType(sb,index);
142        appendDirection(sb,index);
143        
144        sb.append("\n");
145        _console.nextLine(sb.toString(), sb.toString(), index);
146    }
147    
148    private void appendType( StringBuilder sb, int index ){
149        switch (_highlight[index].getType()) {
150            case CbusConstants.EVENT_ON:
151                sb.append(Bundle.getMessage("CbusEventOn"));
152                break;
153            case CbusConstants.EVENT_OFF:
154                sb.append(Bundle.getMessage("CbusEventOff"));
155                break;
156            default:
157                sb.append(Bundle.getMessage("CbusEventOnOrOff"));
158                break;
159        }
160    }
161    
162    private void appendDirection( StringBuilder sb, int index ){
163        switch (_highlight[index].getDir()) {
164            case CbusConstants.EVENT_DIR_IN:
165                sb.append(Bundle.getMessage("InEventsTooltip"));
166                break;
167            case CbusConstants.EVENT_DIR_OUT:
168                sb.append(Bundle.getMessage("OutEventsTooltip"));
169                break;        
170            default:
171                sb.append(Bundle.getMessage("InOrOutEventsToolTip"));
172                break;
173        }
174    }
175
176    /**
177     * Disable a Highlighter by Index Number.
178     * @param index Highlighter Index number
179     */
180    public void disable(int index) {
181        _highlightActive[index] = false;
182        if ( _console != null) { 
183            _console.nextLine( Bundle.getMessage("HighlightDisabled") + " \n", Bundle.getMessage("HighlightDisabled") + " \n",  index);
184        }
185    }
186
187    /**
188     * Get whether to Highlight a particular CAN Frame.
189     * @param m CanMessage or CanReply
190     * @return -1 to NOT Highlight, else Highlighter Number.
191     */
192    public int highlight(AbstractMessage m) {
193        for (int i = 0; i < HIGHLIGHTERS; i++) {
194            if (_highlightActive[i] && _highlight[i].highlight(m)) {
195                return i;
196            }
197        }
198        return -1;
199    }
200
201    /**
202     * Get Colour for a particular highlighter.
203     * @param i Highlight index
204     * @return Highlight Colour
205     */
206    public Color getColor(int i) {
207        return highlightColors[i];
208    }
209
210    // private final static Logger log = LoggerFactory.getLogger(CbusEventHighlightFrame.class);
211}