001package jmri.jmrix.can.cbus.swing.simulator; 002 003import java.awt.event.ActionEvent; 004import java.util.ArrayList; 005import javax.swing.*; 006import javax.swing.event.ChangeEvent; 007import javax.swing.text.DefaultFormatter; 008import jmri.jmrix.can.cbus.simulator.CbusEventResponder; 009import jmri.util.swing.ComboBoxToolTipRenderer; 010 011import org.slf4j.Logger; 012import org.slf4j.LoggerFactory; 013 014/** 015 * Pane for viewing and setting simulated network objects. 016 * <p> 017 * Methods are subject to change and should not be relied on at present. 018 * 019 * @author Steve Young Copyright (C) 2018 2019 020 * @since 4.15.2 021 */ 022public class EvResponderPane extends JPanel { 023 024 private final CbusEventResponder _evr; 025 private JComboBox<String> _selectMode; 026 private int _mode; 027 private ArrayList<String> tooltips; 028 private JSpinner _spinner; 029 030 public EvResponderPane( CbusEventResponder evr) { 031 super(); 032 033 _evr = evr; 034 if ( _evr != null ){ 035 init(); 036 } 037 038 } 039 040 private void init() { 041 042 _mode = _evr.getMode(); 043 044 JLabel _nodeLabel = new JLabel("<html><h3>" + Bundle.getMessage("CbusNode") + " : </h3></html>"); 045 _nodeLabel.setToolTipText(Bundle.getMessage("simNodeSelect")); 046 047 _selectMode = new JComboBox<>(); 048 _selectMode.setEditable(false); 049 050 ComboBoxToolTipRenderer renderer = new ComboBoxToolTipRenderer(); 051 _selectMode.setRenderer(renderer); 052 053 tooltips = new ArrayList<>(); 054 String getSelected=""; 055 056 for (int i = 0; i < _evr.evModes.size(); i++) { 057 String option = _evr.evModes.get(i); 058 _selectMode.addItem(option); 059 tooltips.add(_evr.evModesTip.get(i)); 060 if ( i == _mode ){ 061 getSelected = option; 062 } 063 } 064 065 _selectMode.setSelectedItem(getSelected); 066 _selectMode.addActionListener ((ActionEvent e) -> { 067 String chosen = (String)_selectMode.getSelectedItem(); 068 069 for (int i = 0; i < _evr.evModes.size(); i++) { 070 String option = _evr.evModes.get(i); 071 if (option.equals(chosen)) { 072 log.debug("chosen {} {}",i,chosen); 073 _evr.setMode(i); 074 } 075 } 076 }); 077 renderer.setTooltips(tooltips); 078 079 _spinner = new JSpinner(new SpinnerNumberModel(_evr.getNode(), -1, 65535, 1)); 080 JSpinner.NumberEditor editor = new JSpinner.NumberEditor(_spinner, "#"); 081 _spinner.setEditor(editor); 082 JFormattedTextField field = (JFormattedTextField) editor.getComponent(0); 083 DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); 084 formatter.setCommitsOnValidEdit(true); 085 _spinner.addChangeListener((ChangeEvent e) -> { 086 int minmax = (Integer) _spinner.getValue(); 087 log.debug("value {}",minmax); 088 _evr.setNode(minmax); 089 }); 090 _spinner.setToolTipText(Bundle.getMessage("simNodeSelect")); 091 092 DirectionPane dp = new DirectionPane(_evr); 093 094 JPanel topPane = new JPanel(); 095 topPane.add(_selectMode); 096 topPane.add(_nodeLabel); 097 topPane.add(_spinner); 098 099 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 100 setBorder(BorderFactory.createEtchedBorder()); 101 102 add(topPane); 103 add(dp); 104 105 } 106 107 private final static Logger log = LoggerFactory.getLogger(EvResponderPane.class); 108 109}