001package jmri.jmrit.picker;
002
003import java.awt.*;
004import java.awt.event.*;
005import javax.swing.*;
006import javax.swing.event.*;
007import jmri.*;
008
009/**
010 * Container with a single PickList table
011 * <p>
012 * See also PickTabbedPanel for multiple panes with multiple tables
013 *
014 * @author Bob Jacobsen  Copyright (c) 2017
015 * @author Pete Cressman Copyright (c) 2010
016 */
017public class PickSinglePanel<T extends NamedBean> extends JPanel {
018
019    private int ROW_HEIGHT;
020
021    PickListModel<T> _model;
022
023    JPanel _addPanel;
024    JPanel _cantAddPanel;
025    JTextField _sysNametext;
026    JTextField _userNametext;
027    JTable _table;
028    JScrollPane _scroll;
029
030    public PickSinglePanel(PickListModel<T> model) {
031        _model = model;
032        _table = _model.makePickTable();
033        _table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
034        _table.setCellSelectionEnabled(true);
035        _table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
036            @Override
037            public void valueChanged(ListSelectionEvent e) {
038                int row = getTable().getSelectedRow();
039                int col = getTable().getSelectedColumn(); // might be -1 if just inserted
040                if (col != 1) return;
041                if (row >= 0) {
042                    String username = (String) _model.getTable().getValueAt(row, 1);
043                    if (username != null) return;
044                }
045                // have to set selection to col 0
046                _model.getTable().setColumnSelectionInterval(0,0);
047            }
048        });
049
050        JPanel p = new JPanel();
051        p.setLayout(new BorderLayout(5, 5));
052        p.add(new JLabel(_model.getName(), SwingConstants.CENTER), BorderLayout.NORTH);
053        p.add(_scroll = new JScrollPane(_table), BorderLayout.CENTER);
054        ROW_HEIGHT = _table.getRowHeight();
055
056        setLayout(new BorderLayout(5, 5));
057        add(p, BorderLayout.CENTER);
058        add(makeAddToTablePanel(), BorderLayout.SOUTH);
059
060        if (_model.canAddBean()) {
061            _cantAddPanel.setVisible(false);
062            _addPanel.setVisible(true);
063        } else {
064            _addPanel.setVisible(false);
065            _cantAddPanel.setVisible(true);
066        }
067    }
068
069    public NamedBeanHandle<T> getSelectedBeanHandle() {
070        int row = getTable().getSelectedRow();
071        int col = getTable().getSelectedColumn(); // might be -1 if just inserted
072        log.debug("PickSinglePanel: r = {}, c = {}", row, col);
073
074        // are we sure this is always col 0 for sysname and col 1 for user name?
075        String sysname = _model.getTable().getValueAt(row, 0).toString();
076        String username = (String) _model.getTable().getValueAt(row, 1);
077
078        String beanName = sysname;
079        if (col == 1 && username != null) beanName = username;
080        T bean = _model.addBean(sysname, username);
081        if (bean == null) {
082            return null;
083        }
084        return InstanceManager.getDefault(NamedBeanHandleManager.class)
085                        .getNamedBeanHandle(beanName, bean);
086    }
087
088    public JTable getTable() { return _table; }
089
090    private JPanel makeAddToTablePanel() {
091        _sysNametext = new JTextField();
092        _userNametext = new JTextField();
093
094        ActionListener cancelListener = new ActionListener() {
095            @Override
096            public void actionPerformed(ActionEvent a) {
097                //do nothing as Cancel button is hidden on Pick Lists
098            }
099        };
100
101        ActionListener okListener = new ActionListener() {
102            @Override
103            public void actionPerformed(ActionEvent a) {
104                addToTable();
105            }
106        };
107        _addPanel = new jmri.jmrit.beantable.AddNewDevicePanel(
108                _sysNametext, _userNametext, "addToTable", okListener, cancelListener); // No I18N
109        // hide Cancel button as not handled by Picker Panel
110
111        _cantAddPanel = new JPanel();
112        _cantAddPanel.setLayout(new BorderLayout(5, 5));
113        _cantAddPanel.add(new JLabel(Bundle.getMessage("CantAddNew"), SwingConstants.CENTER), BorderLayout.NORTH);
114        _cantAddPanel.add(new JLabel(Bundle.getMessage("OpenToAdd"), SwingConstants.CENTER), BorderLayout.SOUTH);
115        JPanel p = new JPanel();
116        p.add(_addPanel);
117        p.add(_cantAddPanel);
118        int width = Math.max(100, this.getPreferredSize().width);
119        _sysNametext.setPreferredSize(new java.awt.Dimension(width, _sysNametext.getPreferredSize().height));
120        return p;
121    }
122
123    void addToTable() {
124        String sysname = _sysNametext.getText();
125        if (sysname.length() > 1) {
126            String uname = NamedBean.normalizeUserName(_userNametext.getText());
127            if (uname != null && uname.trim().length() == 0) {
128                uname = null;
129            }
130            T bean = null;
131            try {
132                bean = _model.addBean(sysname, uname);
133            } catch (IllegalArgumentException ex) {
134                JOptionPane.showMessageDialog(this.getRootPane(),
135                    Bundle.getMessage("PickAddFailed", ex.getMessage()),  // NOI18N
136                    Bundle.getMessage("WarningTitle"),  // NOI18N
137                    JOptionPane.WARNING_MESSAGE);
138            }
139            if (bean != null) {
140                int setRow = _model.getIndexOf(bean);
141                _model.getTable().setRowSelectionInterval(setRow, setRow);
142                _scroll.getVerticalScrollBar().setValue(setRow * ROW_HEIGHT);
143                _sysNametext.setText("");
144            }
145        }
146    }
147
148    // initialize logging
149    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PickSinglePanel.class);
150}