001package jmri.jmrit.picker;
002
003import java.awt.BorderLayout;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006import javax.swing.JLabel;
007import javax.swing.JPanel;
008import javax.swing.JOptionPane;
009import javax.swing.JScrollPane;
010import javax.swing.JTabbedPane;
011import javax.swing.JTable;
012import javax.swing.JTextField;
013import javax.swing.SwingConstants;
014import javax.swing.event.ChangeEvent;
015import javax.swing.event.ChangeListener;
016import javax.swing.event.ListSelectionEvent;
017import javax.swing.event.ListSelectionListener;
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021/**
022 * Tabbed Container for holding pick list tables
023 * <p>
024 * Should perhaps be called PickTabbedPanel to distinguish from PickSinglePanel
025 *
026 * @author Pete Cressman Copyright (c) 2010
027 */
028public class PickPanel extends JPanel implements ListSelectionListener, ChangeListener {
029
030    private int ROW_HEIGHT;
031
032    PickListModel[] _models;
033    JTabbedPane _tabPane;
034
035    JPanel _addPanel;
036    JPanel _cantAddPanel;
037    JTextField _sysNametext;
038    JTextField _userNametext;
039    jmri.jmrit.picker.PickFrame _pickTables; // Opened from LogixTableAction
040
041    public PickPanel(PickListModel[] models) {
042        _tabPane = new JTabbedPane();
043        _models = new PickListModel[models.length];
044        System.arraycopy(models, 0, _models, 0, models.length);
045        for (int i = 0; i < models.length; i++) {
046            JTable table = models[i].makePickTable();
047            JPanel p = new JPanel();
048            p.setLayout(new BorderLayout(5, 5));
049            p.add(new JLabel(models[i].getName(), SwingConstants.CENTER), BorderLayout.NORTH);
050            p.add(new JScrollPane(table), BorderLayout.CENTER);
051            _tabPane.add(p, models[i].getName());
052            ROW_HEIGHT = table.getRowHeight();
053        }
054        setLayout(new BorderLayout(5, 5));
055        add(_tabPane, BorderLayout.CENTER);
056        add(makeAddToTablePanel(), BorderLayout.SOUTH);
057        _tabPane.addChangeListener(this);
058    }
059
060    private JPanel makeAddToTablePanel() {
061        _sysNametext = new JTextField();
062        _userNametext = new JTextField();
063
064        ActionListener cancelListener = new ActionListener() {
065            @Override
066            public void actionPerformed(ActionEvent a) {
067                //do nothing as Cancel button is hidden on Pick Lists
068            }
069        };
070
071        ActionListener okListener = new ActionListener() {
072            @Override
073            public void actionPerformed(ActionEvent a) {
074                addToTable();
075            }
076        };
077        _addPanel = new jmri.jmrit.beantable.AddNewDevicePanel(
078                _sysNametext, _userNametext, "addToTable", okListener, cancelListener); // No I18N
079        // hide Cancel button as not handled bij Picker Panel
080
081        _cantAddPanel = new JPanel();
082        _cantAddPanel.setLayout(new BorderLayout(5, 5));
083        _cantAddPanel.add(new JLabel(Bundle.getMessage("CantAddNew"), SwingConstants.CENTER), BorderLayout.NORTH);
084        _cantAddPanel.add(new JLabel(Bundle.getMessage("OpenToAdd"), SwingConstants.CENTER), BorderLayout.SOUTH);
085        JPanel p = new JPanel();
086        p.add(_addPanel);
087        p.add(_cantAddPanel);
088        stateChanged(null);
089        int width = Math.max(100, this.getPreferredSize().width);
090        _sysNametext.setPreferredSize(new java.awt.Dimension(width, _sysNametext.getPreferredSize().height));
091        return p;
092    }
093
094    @SuppressWarnings("unchecked") // PickList is a parameterized class, but we don't use that here
095    void addToTable() {
096        String sysname = _sysNametext.getText();
097        if (sysname != null && sysname.length() > 1) {
098            PickListModel model = _models[_tabPane.getSelectedIndex()];
099            String uname = _userNametext.getText();
100            if (uname != null && uname.trim().length() == 0) {
101                uname = null;
102            }
103            jmri.NamedBean bean = null;
104            try {
105                bean = model.addBean(sysname, uname);
106            } catch (IllegalArgumentException ex) {
107                JOptionPane.showMessageDialog(null,
108                    Bundle.getMessage("PickAddFailed", ex.getMessage()),  // NOI18N
109                    Bundle.getMessage("WarningTitle"),  // NOI18N
110                    JOptionPane.WARNING_MESSAGE);
111            }
112            if (bean != null) {
113                int setRow = model.getIndexOf(bean);
114                model.getTable().setRowSelectionInterval(setRow, setRow);
115                JPanel p = (JPanel) _tabPane.getSelectedComponent();
116                ((JScrollPane) p.getComponent(1)).getVerticalScrollBar().setValue(setRow * ROW_HEIGHT);
117                _sysNametext.setText("");
118            }
119        }
120    }
121
122    @Override
123    public void stateChanged(ChangeEvent e) {
124        PickListModel model = _models[_tabPane.getSelectedIndex()];
125        if (model.canAddBean()) {
126            _cantAddPanel.setVisible(false);
127            _addPanel.setVisible(true);
128        } else {
129            _addPanel.setVisible(false);
130            _cantAddPanel.setVisible(true);
131        }
132    }
133
134    @Override
135    public void valueChanged(ListSelectionEvent e) {
136        if (log.isDebugEnabled()) {
137            log.debug("ListSelectionEvent from {} idx= {}", e.getSource().getClass().getName(), e.getFirstIndex());
138        }
139    }
140
141    // initialize logging
142    private final static Logger log = LoggerFactory.getLogger(PickPanel.class);
143}