Search in sources :

Example 41 with ButtonRenderer

use of jmri.util.table.ButtonRenderer in project JMRI by JMRI.

the class LocoIOPanel method initComponents.

@Override
public void initComponents(LocoNetSystemConnectionMemo memo) {
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    ln = memo.getLnTrafficController();
    // creating the table (done here to ensure order OK)
    data = new LocoIOData(Integer.valueOf(addrField.getText(), 16).intValue(), Integer.valueOf(subAddrField.getText(), 16).intValue(), memo.getLnTrafficController());
    model = new LocoIOTableModel(data);
    table = new JTable(model);
    scroll = new JScrollPane(table);
    data.addPropertyChangeListener(this);
    // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541)
    // table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setShowHorizontalLines(true);
    table.setAutoCreateColumnsFromModel(true);
    TableColumnModel tcm = table.getColumnModel();
    // install a ComboBox editor on the OnMode column
    JComboBox<String> comboOnBox = new JComboBox<String>(data.getLocoIOModeList().getValidModes());
    comboOnBox.setEditable(true);
    DefaultCellEditor modeEditor = new DefaultCellEditor(comboOnBox);
    tcm.getColumn(LocoIOTableModel.MODECOLUMN).setCellEditor(modeEditor);
    // install a button renderer & editor in the Read, Write and Compare columns
    ButtonRenderer buttonRenderer = new ButtonRenderer();
    tcm.getColumn(LocoIOTableModel.READCOLUMN).setCellRenderer(buttonRenderer);
    tcm.getColumn(LocoIOTableModel.WRITECOLUMN).setCellRenderer(buttonRenderer);
    tcm.getColumn(LocoIOTableModel.CAPTURECOLUMN).setCellRenderer(buttonRenderer);
    TableCellEditor buttonEditor = new ButtonEditor(new JButton());
    tcm.getColumn(LocoIOTableModel.READCOLUMN).setCellEditor(buttonEditor);
    tcm.getColumn(LocoIOTableModel.WRITECOLUMN).setCellEditor(buttonEditor);
    tcm.getColumn(LocoIOTableModel.CAPTURECOLUMN).setCellEditor(buttonEditor);
    // ensure the table rows, columns have enough room for buttons and comboBox contents
    table.setRowHeight(new JButton("Capture").getPreferredSize().height);
    for (int col = 0; col < LocoIOTableModel.HIGHESTCOLUMN; col++) {
        table.getColumnModel().getColumn(col).setPreferredWidth(model.getPreferredWidth(col));
    }
    // A pane for SV0, SV1, SV2, the board sub address and the PIC version
    JPanel p1 = new JPanel();
    p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
    p1.add(new JLabel("LocoIO address: 0x"));
    addrField.setMaximumSize(addrField.getPreferredSize());
    subAddrField.setMaximumSize(subAddrField.getPreferredSize());
    p1.add(addrField);
    p1.add(new JLabel("/"));
    p1.add(subAddrField);
    // -------------------
    p1.add(Box.createGlue());
    probeButton = new JButton("Probe");
    probeButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent a) {
            data.setLIOVersion("<Not found>");
            LocoIO.probeLocoIOs(ln);
        }
    });
    p1.add(probeButton);
    // -------------------
    p1.add(Box.createGlue());
    readAllButton = new JButton("Read All");
    readAllButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent a) {
            data.readAll();
        }
    });
    p1.add(readAllButton);
    writeAllButton = new JButton("Write All");
    writeAllButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent a) {
            data.writeAll();
        }
    });
    p1.add(writeAllButton);
    // -------------------
    p1.add(Box.createGlue());
    addrSetButton = new JButton("Set address");
    p1.add(addrSetButton);
    addrSetButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent a) {
            addrSet();
        }
    });
    // -------------------
    p1.add(Box.createGlue());
    /*
         openButton = new JButton("Load...");
         openButton.setEnabled(false);
         p1.add(openButton);

         saveButton = new JButton("Save...");
         saveButton.setEnabled(false);
         p1.add(saveButton);
         */
    JPanel p2 = new JPanel();
    p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
    p2.add(new JLabel("Locobuffer rev: "));
    p2.add(locobuffer);
    // -------------------
    p2.add(Box.createGlue());
    p2.add(new JLabel("Status: "));
    p2.add(status);
    // -------------------
    p2.add(Box.createGlue());
    p2.add(new JLabel("LocoIO Firmware rev: "));
    p2.add(firmware);
    JPanel p3 = new JPanel();
    p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
    p3.add(p1);
    p3.add(table);
    add(p3);
    add(p2);
    // updating the Board address needs to be conveyed to the table
    ActionListener al4UnitAddress = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent a) {
            try {
                data.setUnitAddress(Integer.valueOf(addrField.getText(), 16).intValue(), Integer.valueOf(subAddrField.getText(), 16).intValue());
            } catch (NullPointerException e) {
                // NOI18N
                log.error("Caught NullPointerException", e);
            }
        }
    };
    FocusListener fl4UnitAddress = new FocusListener() {

        @Override
        public void focusGained(FocusEvent event) {
        }

        @Override
        public void focusLost(FocusEvent event) {
            try {
                data.setUnitAddress(Integer.valueOf(addrField.getText(), 16).intValue(), Integer.valueOf(subAddrField.getText(), 16).intValue());
            } catch (NullPointerException e) {
                // NOI18N
                log.error("Caught NullPointerException", e);
            }
        }
    };
    addrField.addActionListener(al4UnitAddress);
    subAddrField.addActionListener(al4UnitAddress);
    addrField.addFocusListener(fl4UnitAddress);
    subAddrField.addFocusListener(fl4UnitAddress);
    try {
        data.setUnitAddress(0x51, 0x00);
    } catch (NullPointerException e) {
        // NOI18N
        log.error("Caught NullPointerException", e);
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) ButtonEditor(jmri.util.table.ButtonEditor) JComboBox(javax.swing.JComboBox) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) JButton(javax.swing.JButton) TableColumnModel(javax.swing.table.TableColumnModel) JLabel(javax.swing.JLabel) FocusEvent(java.awt.event.FocusEvent) DefaultCellEditor(javax.swing.DefaultCellEditor) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable) TableCellEditor(javax.swing.table.TableCellEditor) FocusListener(java.awt.event.FocusListener) ButtonRenderer(jmri.util.table.ButtonRenderer)

Aggregations

ButtonEditor (jmri.util.table.ButtonEditor)41 ButtonRenderer (jmri.util.table.ButtonRenderer)41 JButton (javax.swing.JButton)40 TableCellEditor (javax.swing.table.TableCellEditor)33 TableColumnModel (javax.swing.table.TableColumnModel)23 JLabel (javax.swing.JLabel)17 JPanel (javax.swing.JPanel)15 JScrollPane (javax.swing.JScrollPane)15 JTable (javax.swing.JTable)15 ActionEvent (java.awt.event.ActionEvent)10 BoxLayout (javax.swing.BoxLayout)10 Dimension (java.awt.Dimension)9 ActionListener (java.awt.event.ActionListener)9 FlowLayout (java.awt.FlowLayout)7 XTableColumnModel (jmri.util.swing.XTableColumnModel)7 TableColumn (javax.swing.table.TableColumn)6 TableRowSorter (javax.swing.table.TableRowSorter)6 BorderLayout (java.awt.BorderLayout)5 Point (java.awt.Point)5 DefaultCellEditor (javax.swing.DefaultCellEditor)5