001package jmri.jmrix.tams.swing.locodatabase;
002
003import java.awt.FlowLayout;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006
007import javax.swing.BoxLayout;
008import javax.swing.JButton;
009import javax.swing.JComboBox;
010import javax.swing.JLabel;
011import javax.swing.JPanel;
012import javax.swing.JScrollPane;
013import javax.swing.JTable;
014import javax.swing.JTextField;
015import javax.swing.table.TableRowSorter;
016
017import jmri.jmrix.tams.TamsMessage;
018import jmri.jmrix.tams.TamsSystemConnectionMemo;
019import jmri.util.swing.JmriJOptionPane;
020
021/**
022 * Frame providing access to the loco database on the Tams MC
023 *
024 * @author Kevin Dickerson Copyright (C) 2012
025 */
026public class LocoDataPane extends jmri.jmrix.tams.swing.TamsPanel {
027
028    LocoDataModel locoModel;
029    JTable locoTable;
030    JScrollPane locoScroll;
031
032    String[] speed = {"126", "14", "27", "28"};
033    String[] fmt = {"DCC", "MM1", "MM2"}; // NOI18N
034
035    JTextField addr = new JTextField(5);
036    JTextField name = new JTextField(10);
037    JComboBox<String> speedBox = new JComboBox<String>(speed);
038    JComboBox<String> formatBox = new JComboBox<String>(fmt);
039    JButton addButton = new JButton(Bundle.getMessage("AddLoco"));
040
041    public LocoDataPane() {
042        super();
043    }
044
045    @Override
046    public void initComponents(jmri.jmrix.tams.TamsSystemConnectionMemo memo) {
047        super.initComponents(memo);
048
049        locoModel = new LocoDataModel(128, 16, memo);
050        locoTable = new JTable(locoModel);
051        locoTable.setRowSorter(new TableRowSorter<>(locoModel));
052        locoScroll = new JScrollPane(locoTable);
053
054        locoModel.configureTable(locoTable);
055
056        // general GUI config
057        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
058
059        // install items in GUI
060        JPanel pane1 = new JPanel();
061        pane1.setLayout(new FlowLayout());
062
063        pane1.add(new JLabel(Bundle.getMessage("Address"))); // TODO reuse props key in NBB/JmrixBundle?
064        pane1.add(addr);
065        pane1.add(new JLabel(Bundle.getMessage("Name"))); // TODO reuse props key in NBB/JmrixBundle?
066        pane1.add(name);
067        pane1.add(new JLabel(Bundle.getMessage("Steps")));
068        pane1.add(speedBox);
069        pane1.add(new JLabel(Bundle.getMessage("Format")));
070        pane1.add(formatBox);
071        pane1.add(addButton);
072
073        // add listener object so checkboxes function
074        addButton.addActionListener(new ActionListener() {
075            @Override
076            public void actionPerformed(ActionEvent e) {
077                addLoco();
078            }
079        });
080
081        add(pane1);
082        add(locoScroll);
083
084        if (pane1.getMaximumSize().height > 0 && pane1.getMaximumSize().width > 0) {
085            pane1.setMaximumSize(pane1.getPreferredSize());
086        }
087    }
088
089    void addLoco() {
090        if (addr.getText() == null || addr.getText().equals("")) {
091            log.error("Require an address to be entered");
092            JmriJOptionPane.showMessageDialog(addr, Bundle.getMessage("ErrorNullAddress"), Bundle.getMessage("ErrorTitle"),
093                    JmriJOptionPane.ERROR_MESSAGE);
094            return;
095
096        }
097        try {
098            Integer.valueOf(addr.getText());
099        } catch (NumberFormatException nx) {
100            log.error("Unable to convert {} to a number", addr.getText());
101            JmriJOptionPane.showMessageDialog(addr, Bundle.getMessage("ErrorNotNumber"), Bundle.getMessage("ErrorTitle"),
102                    JmriJOptionPane.ERROR_MESSAGE);
103            return;
104        }
105        StringBuilder sb = new StringBuilder();
106        sb.append("xLOCADD ");
107        sb.append(addr.getText());
108        sb.append(",");
109        sb.append(speedBox.getSelectedItem());
110        sb.append(",");
111        sb.append(formatBox.getSelectedItem());
112        sb.append(",'");
113        sb.append(name.getText());
114        sb.append("'");
115
116        TamsMessage m = new TamsMessage(sb.toString());
117        locoModel.addLoco(m);
118    }
119
120    @Override
121    public String getHelpTarget() {
122        return "package.jmri.jmrix.tams.swing.locodatabase.LocoDataFrame";
123    }
124
125    @Override
126    public String getTitle() {
127        return Bundle.getMessage("Title");
128    }
129
130    @Override
131    public void dispose() {
132        locoModel.dispose();
133        locoModel = null;
134        locoTable = null;
135        locoScroll = null;
136        super.dispose();
137    }
138
139    /**
140     * Nested class to create one of these using old-style defaults
141     */
142    static public class Default extends jmri.jmrix.tams.swing.TamsNamedPaneAction {
143
144        public Default() {
145            super(Bundle.getMessage("Title"),
146                    new jmri.util.swing.sdi.JmriJFrameInterface(),
147                    LocoDataPane.class.getName(),
148                    jmri.InstanceManager.getDefault(TamsSystemConnectionMemo.class));
149        }
150    }
151
152    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LocoDataPane.class);
153
154}