001package jmri.jmrit.logixng.tools.swing;
002
003import java.awt.*;
004import java.util.*;
005import java.util.function.Predicate;
006
007import javax.swing.*;
008import javax.swing.table.TableColumnModel;
009
010import jmri.jmrit.logixng.LogixNG;
011import jmri.util.JmriJFrame;
012
013/**
014 * Shows the inline LogixNGs.
015 *
016 * @author Daniel Bergqvist Copyright (C) 2022
017 */
018public class InlineLogixNGsFrame extends JmriJFrame {
019
020    private final JComboBox<String> _filterOnPanel = new JComboBox<>();
021    private final JComboBox<String> _filterOnIcon = new JComboBox<>();
022    private final InlineLogixNGsTableModel _inlineLogixNGsTableModel =
023            new InlineLogixNGsTableModel();
024
025    /**
026     * Construct a InlineLogixNGsFrame.
027     */
028    public InlineLogixNGsFrame() {
029        setTitle(Bundle.getMessage("TitleInlineLogixNGs"));
030        addHelpMenu("package.jmri.jmrit.logixng.InlineLogixNG", true);  // NOI18N
031    }
032
033    @Override
034    public void initComponents() {
035        super.initComponents();
036
037        Container contentPanel = getContentPane();
038//        contentPanel.setLayout(new GridLayout( 0, 1));
039        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
040
041        JPanel panel = new JPanel();
042        JPanel filterPanel = new JPanel();
043        filterPanel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("InlineLogixNGsFrame_Filter")));
044        filterPanel.setLayout(new java.awt.GridBagLayout());
045        java.awt.GridBagConstraints c = new java.awt.GridBagConstraints();
046        c.gridwidth = 1;
047        c.gridheight = 1;
048        c.gridx = 0;
049        c.gridy = 0;
050        c.anchor = java.awt.GridBagConstraints.EAST;
051        JLabel filterOnPanelLabel = new JLabel(Bundle.getMessage("InlineLogixNGsFrame_Filter_Panel"));
052        filterPanel.add(filterOnPanelLabel, c);
053        filterOnPanelLabel.setLabelFor(_filterOnPanel);
054        c.gridy = 2;
055        JLabel filterOnIconLabel = new JLabel(Bundle.getMessage("InlineLogixNGsFrame_Filter_IconType"));
056        filterPanel.add(filterOnIconLabel, c);
057        filterOnIconLabel.setLabelFor(_filterOnIcon);
058        c.gridx = 1;
059        filterPanel.add(Box.createHorizontalStrut(3), c);
060        c.gridx = 2;
061        c.gridy = 1;
062        filterPanel.add(Box.createVerticalStrut(3), c);
063        c.gridx = 2;
064        c.gridy = 0;
065        c.anchor = java.awt.GridBagConstraints.WEST;
066        c.weightx = 1.0;
067        c.fill = java.awt.GridBagConstraints.HORIZONTAL;  // text field will expand
068        filterPanel.add(_filterOnPanel, c);
069        c.gridy = 2;
070        filterPanel.add(_filterOnIcon, c);
071        panel.add(filterPanel);
072        contentPanel.add(panel);
073
074
075        JTable table = new JTable();
076        _inlineLogixNGsTableModel.init();
077        table.setModel(_inlineLogixNGsTableModel);
078        TableColumnModel columnModel = table.getColumnModel();
079//        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
080        JLabel positionColumn = new JLabel("8888888");
081        int positionColumnWidth = positionColumn.getPreferredSize().width;
082        columnModel.getColumn(InlineLogixNGsTableModel.COLUMN_POS_X)
083                .setMaxWidth(positionColumnWidth);
084        columnModel.getColumn(InlineLogixNGsTableModel.COLUMN_POS_Y)
085                .setMaxWidth(positionColumnWidth);
086        table.setDefaultRenderer(InlineLogixNGsTableModel.Menu.class,
087                new InlineLogixNGsTableModel.MenuCellRenderer());
088        table.setDefaultEditor(InlineLogixNGsTableModel.Menu.class,
089                new InlineLogixNGsTableModel.MenuCellEditor(table, _inlineLogixNGsTableModel));
090        _inlineLogixNGsTableModel.setColumnForMenu(table);
091        table.setAutoCreateRowSorter(true);
092        JScrollPane scrollpane = new JScrollPane(table);
093        scrollpane.setPreferredSize(new Dimension(600, 400));
094        contentPanel.add(scrollpane);
095
096        Set<String> panels = new HashSet<>();
097        Set<String> icons = new HashSet<>();
098        for (LogixNG logixNG : _inlineLogixNGsTableModel.getLogixNGList()) {
099            String editorName = InlineLogixNGsTableModel.getEditorName(logixNG.getInlineLogixNG());
100            String typeName = InlineLogixNGsTableModel.getTypeName(logixNG.getInlineLogixNG());
101            if (editorName != null && !editorName.isBlank()) panels.add(editorName);
102            if (typeName != null && !typeName.isBlank()) icons.add(typeName);
103        }
104
105        _filterOnPanel.addItem("");
106        for (String s : panels) _filterOnPanel.addItem(s);
107        _filterOnPanel.addActionListener((evt)->{updateFilters();});
108
109        _filterOnIcon.addItem("");
110        for (String s : icons) _filterOnIcon.addItem(s);
111        _filterOnIcon.addActionListener((evt)->{updateFilters();});
112
113        pack();
114    }
115
116    private void updateFilters() {
117        String filterOnPanelValue = _filterOnPanel.getItemAt(_filterOnPanel.getSelectedIndex());
118        String filterOnIconValue = _filterOnIcon.getItemAt(_filterOnIcon.getSelectedIndex());
119
120        Predicate<LogixNG> filter = (LogixNG logixNG) -> {
121            String editorName = logixNG.getInlineLogixNG() != null
122                    ? logixNG.getInlineLogixNG().getEditorName() : "";
123            String typeName = logixNG.getInlineLogixNG() != null
124                    ? logixNG.getInlineLogixNG().getTypeName() : "";
125            if (!filterOnPanelValue.equals("")) {
126                if (editorName == null || !filterOnPanelValue.equals(editorName)) {
127                    return false;
128                }
129            }
130            if (!filterOnIconValue.equals("")) {
131                if (typeName == null || !filterOnIconValue.equals(typeName)) {
132                    return false;
133                }
134            }
135            return true;
136        };
137
138        _inlineLogixNGsTableModel.setFilter(filter);
139    }
140
141}