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