001package jmri.jmrix.can.cbus.swing.simulator; 002 003import java.awt.event.ActionEvent; 004import javax.swing.*; 005import javax.swing.event.ChangeEvent; 006import javax.swing.text.DefaultFormatter; 007import jmri.jmrix.can.cbus.simulator.CbusSimCanListener; 008 009// import org.slf4j.Logger; 010// import org.slf4j.LoggerFactory; 011 012/** 013 * Pane for setting listened to and sent from directions. 014 * 015 * @author Steve Young Copyright (C) 2019 016 * @since 4.15.1 017 */ 018public class DirectionPane extends JPanel { 019 020 private JCheckBox processIn; 021 private JCheckBox processOut; 022 private JCheckBox sendIn; 023 private JCheckBox sendOut; 024 025 private JSpinner delaySpinner; 026 027 private CbusSimCanListener cbl; 028 029 private JPanel processPane; 030 private JPanel sendPane; 031 private JPanel delayPane; 032 033 public DirectionPane() { 034 super(); 035 } 036 037 public DirectionPane(CbusSimCanListener cbcl) { 038 super(); 039 cbl = cbcl; 040 init(); 041 } 042 043 public void setSimCanListener( CbusSimCanListener cbcl) { 044 cbl = cbcl; 045 setWhichActive(); 046 setListeners(); 047 } 048 049 private void init() { 050 051 initPanes(); 052 initCheckBoxes(); 053 054 processPane.add(processIn); 055 processPane.add(processOut); 056 this.add(processPane); 057 058 sendPane.add(sendIn); 059 sendPane.add(sendOut); 060 this.add(sendPane); 061 062 delayPane.add(getNewJSpinner()); 063 this.add(delayPane); 064 setWhichActive(); 065 setListeners(); 066 067 } 068 069 private void initPanes() { 070 071 // Pane to hold Process 072 processPane = new JPanel(); 073 processPane.setLayout(new BoxLayout(processPane, BoxLayout.X_AXIS)); 074 processPane.setBorder(BorderFactory.createTitledBorder( 075 BorderFactory.createEtchedBorder(), (Bundle.getMessage("Listen")))); 076 // Pane to hold Send 077 sendPane = new JPanel(); 078 sendPane.setLayout(new BoxLayout(sendPane, BoxLayout.X_AXIS)); 079 sendPane.setBorder(BorderFactory.createTitledBorder( 080 BorderFactory.createEtchedBorder(), (Bundle.getMessage("Send")))); 081 // Pane to hold Send Delay 082 delayPane = new JPanel(); 083 delayPane.setLayout(new BoxLayout(delayPane, BoxLayout.X_AXIS)); 084 delayPane.setBorder(BorderFactory.createTitledBorder( 085 BorderFactory.createEtchedBorder(), (Bundle.getMessage("Delay")))); 086 087 } 088 089 private void initCheckBoxes() { 090 processIn = new JCheckBox(Bundle.getMessage("processIn")); 091 processOut = new JCheckBox(Bundle.getMessage("processOut")); 092 sendIn = new JCheckBox(Bundle.getMessage("sendIn")); 093 sendOut = new JCheckBox(Bundle.getMessage("sendOut")); 094 095 processIn.setToolTipText(Bundle.getMessage("processInTip")); 096 processOut.setToolTipText(Bundle.getMessage("processOutTip")); 097 sendIn.setToolTipText(Bundle.getMessage("sendInTip")); 098 sendOut.setToolTipText(Bundle.getMessage("sendOutTip")); 099 100 processIn.setVisible(true); 101 processOut.setVisible(true); 102 sendIn.setVisible(true); 103 sendOut.setVisible(true); 104 } 105 106 private JSpinner getNewJSpinner(){ 107 delaySpinner = new JSpinner(new SpinnerNumberModel(80,0,999999,1)); 108 JSpinner.NumberEditor editor = new JSpinner.NumberEditor(delaySpinner, "#"); 109 delaySpinner.setEditor(editor); 110 JFormattedTextField fieldEv = (JFormattedTextField) editor.getComponent(0); 111 DefaultFormatter formatterEv = (DefaultFormatter) fieldEv.getFormatter(); 112 fieldEv.setColumns(4); 113 formatterEv.setCommitsOnValidEdit(true); 114 delaySpinner.setToolTipText(Bundle.getMessage("DelayMs")); 115 return delaySpinner; 116 117 } 118 119 private void setListeners(){ 120 if (cbl == null ) { 121 return; 122 } 123 processIn.addActionListener ((ActionEvent e) -> { 124 cbl.setProcessIn(processIn.isSelected()); 125 }); 126 127 processOut.addActionListener ((ActionEvent e) -> { 128 cbl.setProcessOut(processOut.isSelected()); 129 }); 130 131 sendIn.addActionListener ((ActionEvent e) -> { 132 cbl.setSendIn(sendIn.isSelected()); 133 }); 134 135 sendOut.addActionListener ((ActionEvent e) -> { 136 cbl.setSendOut(sendOut.isSelected()); 137 }); 138 139 delaySpinner.addChangeListener((ChangeEvent e) -> { 140 cbl.setDelay((Integer) delaySpinner.getValue()); 141 }); 142 } 143 144 private void setWhichActive(){ 145 processOut.setEnabled(cbl != null); 146 processIn.setEnabled(cbl != null); 147 sendOut.setEnabled(cbl != null); 148 sendIn.setEnabled(cbl != null); 149 delaySpinner.setEnabled(cbl != null); 150 151 if (cbl == null ) { 152 return; 153 } 154 processOut.setSelected(cbl.getProcessOut()); 155 processIn.setSelected(cbl.getProcessIn()); 156 sendOut.setSelected(cbl.getSendOut()); 157 sendIn.setSelected(cbl.getSendIn()); 158 delaySpinner.setValue(cbl.getDelay()); 159 } 160 161 // private final static Logger log = LoggerFactory.getLogger(DirectionPane.class); 162 163}