001package jmri.jmrix.grapevine.nodetable; 002 003import java.awt.Container; 004import java.awt.event.ActionEvent; 005import java.awt.event.ActionListener; 006import javax.swing.BoxLayout; 007import javax.swing.JButton; 008import javax.swing.JLabel; 009import javax.swing.JPanel; 010import javax.swing.JSpinner; 011import javax.swing.SpinnerNumberModel; 012import javax.swing.event.ChangeEvent; 013import javax.swing.event.ChangeListener; 014import jmri.jmrix.grapevine.SerialMessage; 015import jmri.jmrix.grapevine.GrapevineSystemConnectionMemo; 016import org.slf4j.Logger; 017import org.slf4j.LoggerFactory; 018 019/** 020 * Frame lets user renumber a Grapevine node. 021 * 022 * @author Bob Jacobsen Copyright (C) 2008 023 */ 024public class RenumberFrame extends jmri.util.JmriJFrame { 025 026 private GrapevineSystemConnectionMemo memo = null; 027 028 /** 029 * Create new RenumberFrame instance. 030 * 031 * @param _memo the {@link jmri.jmrix.grapevine.GrapevineSystemConnectionMemo} for this frame 032 */ 033 public RenumberFrame(GrapevineSystemConnectionMemo _memo) { 034 super(); 035 memo = _memo; 036 } 037 038 JSpinner fromSpinner; 039 JSpinner toSpinner; 040 JButton renumberButton; 041 042 /** 043 * Initialize the window. 044 */ 045 @Override 046 public void initComponents() { 047 setTitle(Bundle.getMessage("WindowTitleRenumber")); 048 049 Container contentPane = getContentPane(); 050 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 051 052 JPanel p = new JPanel(); 053 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); 054 contentPane.add(p); 055 056 p.add(new JLabel(Bundle.getMessage("LabelFrom"))); 057 fromSpinner = new JSpinner(new SpinnerNumberModel(1,1,127,1)); 058 p.add(fromSpinner); 059 fromSpinner.addChangeListener(new ChangeListener() { 060 @Override 061 public void stateChanged(ChangeEvent e) { 062 numberSet(); 063 } 064 }); 065 066 p.add(new JLabel(Bundle.getMessage("LabelTo"))); 067 toSpinner = new JSpinner(new SpinnerNumberModel(1,1,127,1)); 068 p.add(toSpinner); 069 toSpinner.addChangeListener(new ChangeListener() { 070 @Override 071 public void stateChanged(ChangeEvent e) { 072 numberSet(); 073 } 074 }); 075 076 p = new JPanel(); 077 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); 078 contentPane.add(p); 079 080 renumberButton = new JButton(Bundle.getMessage("ButtonExec")); 081 p.add(renumberButton); 082 renumberButton.addActionListener(new ActionListener() { 083 @Override 084 public void actionPerformed(ActionEvent event) { 085 execute(); 086 } 087 }); 088 renumberButton.setEnabled(false); // start as disabled, with old and new both set to 1 089 090 // add help menu to window 091 addHelpMenu("package.jmri.jmrix.grapevine.nodetable.RenumberFrame", true); 092 093 // pack for display 094 pack(); 095 } 096 097 /** 098 * Send the message to change the address. 099 */ 100 void execute() { 101 // get addresses 102 int f = (Integer) fromSpinner.getValue(); 103 int t = (Integer) toSpinner.getValue(); 104 if (f >= t) { 105 return; // no use if old == new 106 } 107 // format the message 108 SerialMessage m = new SerialMessage(); 109 m.setElement(0, 0x80 + (f & 0x7F)); 110 m.setElement(1, (t & 0x7F)); 111 m.setElement(2, 0x80 + (f & 0x7F)); 112 m.setElement(3, 0x60); 113 m.setParity(); 114 memo.getTrafficController().sendSerialMessage(m, null); 115 log.debug("RenumberFrame sent {}", 0x60); 116 } 117 118 /** 119 * Check values set for old and new numbers. 120 */ 121 void numberSet() { 122 int f = (Integer) fromSpinner.getValue(); 123 int t = (Integer) toSpinner.getValue(); 124 if (f == t) { 125 renumberButton.setEnabled(false); 126 } else { 127 renumberButton.setEnabled(true); 128 } 129 } 130 131 private final static Logger log = LoggerFactory.getLogger(RenumberFrame.class); 132 133}