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