001package jmri.jmrit.beantable;
002
003import java.awt.Color;
004import java.awt.FlowLayout;
005import java.awt.event.ActionListener;
006import java.awt.event.ItemEvent;
007import java.awt.event.ItemListener;
008import java.awt.event.KeyAdapter;
009import java.awt.event.KeyEvent;
010import javax.swing.BoxLayout;
011import javax.swing.JButton;
012import javax.swing.JCheckBox;
013import javax.swing.JLabel;
014import javax.swing.JPanel;
015import javax.swing.JSpinner;
016import javax.swing.JTextField;
017
018/**
019 * JPanel to create a new JMRI device (used to add Memory, Block, OBlock (UI _tabbed)).
020 *
021 * @author Bob Jacobsen Copyright (C) 2009
022 * @author Pete Cressman Copyright (C) 2010
023 */
024public class AddNewBeanPanel extends jmri.util.swing.JmriPanel {
025
026    /**
027     * Create the panel.
028     *
029     * @param sys text field for the system name or start address
030     * @param userName text field for the optional user name
031     * @param endRange selector for range to create multiple NamedBeans
032     * @param addRange check box to allow creation of multiple NamedBeans
033     * @param autoSystem checkbox to have (simple) system name created automatically
034     * @param addButtonLabel text to display on button triggering creation of NamedBean
035     * @param okListener to attach to Create button
036     * @param cancelListener listener to attach to the cancel button
037     * @param statusBar area where status messages can be presented
038     */
039    public AddNewBeanPanel(JTextField sys, JTextField userName, JSpinner endRange, JCheckBox addRange, JCheckBox autoSystem,
040            String addButtonLabel, ActionListener okListener, ActionListener cancelListener, JLabel statusBar) {
041        sysName = sys;
042        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
043        _endRange = endRange;
044        _range = addRange;
045        _autoSys = autoSystem;
046
047        JPanel p;
048        p = new JPanel();
049        p.setLayout(new FlowLayout());
050        p.setLayout(new java.awt.GridBagLayout());
051        java.awt.GridBagConstraints c = new java.awt.GridBagConstraints();
052        c.gridwidth = 1;
053        c.gridheight = 1;
054        c.gridx = 0;
055        c.gridy = 0;
056        c.anchor = java.awt.GridBagConstraints.EAST;
057        //c.gridx = 0;
058        c.gridy = 1;
059        p.add(sysNameLabel, c);
060        sysNameLabel.setLabelFor(sys);
061        c.gridy = 2;
062        p.add(userNameLabel, c);
063        userNameLabel.setLabelFor(userName);
064        c.gridx = 2;
065        c.gridy = 1;
066        c.anchor = java.awt.GridBagConstraints.WEST;
067        c.weightx = 1.0;
068        c.fill = java.awt.GridBagConstraints.HORIZONTAL;  // text field will expand
069        c.gridy = 0;
070        p.add(autoSystem, c);
071        c.gridx = 3;
072        p.add(addRange, c);
073        c.gridx = 2;
074        c.gridy = 1;
075        p.add(sys, c);
076        sys.setToolTipText(Bundle.getMessage("SysNameToolTip", "Y"));
077        c.gridx = 3;
078        p.add(finishLabel, c);
079        c.gridx = 4;
080        p.add(endRange, c);
081        c.gridx = 2;
082        c.gridy = 2;
083        p.add(userName, c);
084        add(p);
085
086        finishLabel.setEnabled(false);
087        _endRange.setEnabled(false);
088
089        // add status bar above buttons
090        JPanel panelStatus = new JPanel();
091        panelStatus.setLayout(new FlowLayout());
092        statusBar.setFont(statusBar.getFont().deriveFont(0.9f * sysNameLabel.getFont().getSize())); // a bit smaller
093        statusBar.setForeground(Color.gray);
094        panelStatus.add(statusBar);
095        add(panelStatus);
096
097        // cancel + add buttons at bottom of window
098        JPanel panelBottom = new JPanel();
099        panelBottom.setLayout(new FlowLayout(FlowLayout.TRAILING));
100
101        panelBottom.add(cancel = new JButton(Bundle.getMessage("ButtonCancel")));
102        cancel.addActionListener(cancelListener);
103
104        panelBottom.add(ok = new JButton(Bundle.getMessage(addButtonLabel)));
105        ok.addActionListener(okListener);
106
107        add(panelBottom);
108
109        addRange.addItemListener(
110                new ItemListener() {
111                    @Override
112                    public void itemStateChanged(ItemEvent e) {
113                        rangeState();
114                    }
115                });
116
117        sysName.addKeyListener(new KeyAdapter() {
118            @Override
119            public void keyReleased(KeyEvent a) {
120                if (sysName.getText().length() > 0) {
121                    ok.setEnabled(true);
122                    ok.setToolTipText(null);
123                } else {
124                    ok.setEnabled(false);
125                }
126            }
127        });
128
129        autoSystem.addItemListener(
130                new ItemListener() {
131                    @Override
132                    public void itemStateChanged(ItemEvent e) {
133                        autoSystemName();
134                    }
135                });
136    }
137
138    private void autoSystemName() {
139        if (_autoSys.isSelected()) {
140            ok.setEnabled(true);
141            sysName.setEnabled(false);
142            sysNameLabel.setEnabled(false);
143        } else {
144            ok.setEnabled(sysName.getText().length() > 0);
145            sysNameLabel.setEnabled(true);
146            sysName.setEnabled(true);
147            sysNameLabel.setEnabled(true);
148        }
149    }
150
151    private void rangeState() {
152        if (_range.isSelected()) {
153            finishLabel.setEnabled(true);
154            _endRange.setEnabled(true);
155        } else {
156            finishLabel.setEnabled(false);
157            _endRange.setEnabled(false);
158        }
159    }
160
161    JButton cancel;
162    protected JButton ok; // access from Table Action to add as default button to frame
163    JTextField sysName;
164    JLabel sysNameLabel = new JLabel(Bundle.getMessage("LabelSystemName"));
165    JLabel userNameLabel = new JLabel(Bundle.getMessage("LabelUserName"));
166
167    JSpinner _endRange;
168    JCheckBox _range;
169    JCheckBox _autoSys;
170    JLabel finishLabel = new JLabel(Bundle.getMessage("LabelNumberToAdd"));
171
172}